开发者

problem in getting count

Here is my table complete schema , i want to get the each category sale count ,

How to get that each category sale count ,

CREATE TABLE IF NOT EXISTS `tblbasket` (
    `BID` int(20) NOT NULL
    AUTO_INCREMENT,
    `BasketSessionID` varchar(100) NOT NULL DEFAULT '0',
    `ProductCode` varchar(50) NOT NULL,
    `Quantity` int(20) NOT NULL,
    `AfterDiscount` double NOT NULL DEFAULT '0',
    `ProductCost` double NOT NULL,
    `SaleMode` varchar(10) NOT NULL DEFAULT 'p' COMMENT 'p Price f Free',
    `BillType` varchar(5) NOT NULL DEFAULT 's' COMMENT 's sale r Returns',
    `EntryUser` int(20) NOT NULL,
    `EntryTimestamp` int(50) NOT NULL,
    `EntryDate` datetime NOT NULL,
    `UpdatedUser` int(20) NOT NULL,
    `UpdatedDate` datetime NOT NU开发者_开发知识库LL,
    `Status` int(3) NOT NULL DEFAULT '1',
    PRIMARY KEY (`BID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

CREATE TABLE IF NOT EXISTS `tblcategory` (
    `CatID` int(11) NOT NULL AUTO_INCREMENT,
    `CatName` varchar(20) NOT NULL,
    `Discount` int(11) NOT NULL,
    `EntryUser` int(10) NOT NULL,
    `EntryDate` datetime NOT NULL,
    `UpdateUser` int(20) NOT NULL,
    `UpdatedDate` datetime NOT NULL,
    `Status` int(3) NOT NULL DEFAULT '1',
    PRIMARY KEY (`CatID`),
    KEY `CatName` (`CatName`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

CREATE TABLE IF NOT EXISTS `tblsale` ( `SaleID` int(20) NOT NULL AUTO_INCREMENT,
    `BillNo` varchar(30) NOT NULL DEFAULT '0',
    `BasketSessionID` varchar(200) NOT NULL,
    `AfterDiscount` double NOT NULL,
    `ProductCost` double NOT NULL, `VAT` int(20) NOT NULL DEFAULT '0',
    `BillType` varchar(5) NOT NULL DEFAULT 's' COMMENT 'same like tblbasket table',
    `EntryUser` int(20) NOT NULL,
    `EntryDate` datetime NOT NULL,
    `UpdatedUser` int(20) NOT NULL,
    `UpdateDate` datetime NOT NULL,
    `OfficeNotesTS` text NOT NULL,
    `Status` int(5) NOT NULL DEFAULT '1',
    PRIMARY KEY (`SaleID`),
    UNIQUE KEY `BillNo` (`BillNo`),
    UNIQUE KEY `BasketSessionID` (`BasketSessionID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

CREATE TABLE IF NOT EXISTS `tblstock` (
    `StockID` int(20) NOT NULL AUTO_INCREMENT,
    `ProductCode` varchar(50) NOT NULL,        
    `ManufacturerID` int(20) NOT NULL,
    `CategoryID` int(20) NOT NULL,
    `ProductTitle` varchar(150) NOT NULL,
    `Timestamp` int(50) NOT NULL,
    `EntryUser` int(20) NOT NULL,
    `EntryDate` datetime NOT NULL,
    `UpdateUser` int(20) NOT NULL,
    `UpdatedDate` datetime NOT NULL,
    `Status` int(3) NOT NULL DEFAULT '1',
    PRIMARY KEY (`StockID`),
    KEY `ManufacturerID` (`ManufacturerID`),
    KEY `CategoryID` (`CategoryID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2175 ;

CREATE TABLE IF NOT EXISTS `tblstockhistory` (
`STID` int(20) NOT NULL AUTO_INCREMENT,
    `StockID` int(20) NOT NULL,  
    `SupplierID` int(20) NOT NULL,
    `InvoiceNO` varchar(20) NOT NULL,
    `InvoiceDate` datetime NOT NULL,
    `InvoiceAmount` float NOT NULL DEFAULT '0',
    `Quantity` int(20) NOT NULL,
    `Cost` float NOT NULL,
    `MRP` float NOT NULL,
    `Discount` int(20) NOT NULL DEFAULT '0',
    `VAT` float NOT NULL,
    `EntryUser` int(10) NOT NULL,
    `EntryDate` datetime NOT NULL,
    `UpdateUser` int(20) NOT NULL,
    `UpdatedDate` datetime NOT NULL,
    `Status` int(10) NOT NULL DEFAULT '1',
    PRIMARY KEY (`STID`),  
    KEY `StockID` (`StockID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2190 ;

ALTER TABLE `tblstock`
    ADD CONSTRAINT `tblStock_ibfk_1` 
    FOREIGN KEY (`ManufacturerID`)
    REFERENCES `tblmanufacturer` (`ManufacturerID`);

ALTER TABLE `tblstockhistory`
    ADD CONSTRAINT `tblStockHistory_ibfk_1`
    FOREIGN KEY (`StockID`) 
    REFERENCES `tblstock` (`StockID`);


try something like that (its hard to understand without foreign key, and little explain ):

SELECT st.CategoryID,SUM(b.ProductCost*b.Quantity) FROM tblsale s
LEFT JOIN tblbasket b ON b.BasketSessionID = s.BasketSessionID
LEFT JOIN tblstock st ON st.ProductCode = b.ProductCode GROUP BY st.CategoryID
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜