| Author |
Topic |
|
sphericalx
Starting Member
10 Posts |
Posted - 2008-12-11 : 03:06:56
|
| This are the tables examples.ServicesAndProductsTableproductID, productDescription, type, productCategory1000, product1, product, skincare1001, product2, service, facial1002, product3, product, skincare1003, product4, service, manicureReceiptSummaryTableReceiptID, clientID, totalAmt, employeeName11000, 100, $10, James11001, 110, $5, James11002, 150, $15, SandyReceiptRowsTableReceiptID, productID, type, amt11000, 1000, product, $511000, 1003, service, $511001, 1002, product, $511002, 1003, service, $511002, 1001, service, $10==============================================Expected output:employee ID, employee Name, productTotal, facialTotal, manicureTotal1, James, $10, $0, $56, Sandy, $0, $10, $5is there a query which can do this?productTotal is calculated from the ReceiptSummaryTable where type = productAs for facialTotal and manicureTotal will be slightly complicated.facialTotal is calculated from ReceiptSummaryTable where type = product AND category = facial in ServicesAndProductsTable.likewise for manicureTotal. |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2008-12-11 : 04:39:33
|
try thisSET NOCOUNT ONDECLARE @ServicesAndProductsTable TABLE (productID INT, productDescription VARCHAR(1000), type VARCHAR(1000), productCategory VARCHAR(1000)) INSERT INTO @ServicesAndProductsTable SELECT 1000, 'product1', 'product', 'skincare'UNION ALL SELECT 1001, 'product2', 'service', 'facial'UNION ALL SELECT 1002, 'product3', 'product', 'skincare'UNION ALL SELECT 1003, 'product4', 'service', 'manicure'DECLARE @ReceiptSummaryTable TABLE (ReceiptID INT, clientID INT, totalAmt INT, employeeName VARCHAR(1000))INSERT INTO @ReceiptSummaryTable SELECT 11000, 100, 10, 'James'UNION ALL SELECT 11001, 110, 5, 'James'UNION ALL SELECT 11002, 150, 15, 'Sandy'DECLARE @ReceiptRowsTable TABLE (ReceiptID INT, productID INT, type VARCHAR(1000), amt INT)INSERT INTO @ReceiptRowsTable SELECT 11000, 1000, 'product', $5UNION ALL SELECT 11000, 1003, 'service', $5UNION ALL SELECT 11001, 1002, 'product', $5UNION ALL SELECT 11002, 1003, 'service', $5UNION ALL SELECT 11002, 1001, 'service', $10SELECT employeeName , ISNULL([skincare], 0) AS 'productTotal' , ISNULL([facial], 0) AS 'facialTotal' , ISNULL([manicure], 0) AS 'manicureTotal'FROM ( SELECT RST.employeeName , RRT.amt , SPT.productCategory FROM @ReceiptSummaryTable RST INNER JOIN @ReceiptRowsTable RRT ON RRT.ReceiptID = RST.ReceiptID INNER JOIN @ServicesAndProductsTable SPT ON SPT.productID = RRT.productID AND SPT.type = RRT.type ) PPIVOT ( SUM(amt) FOR productCategory IN ([manicure], [skincare], [facial]) ) AS PvtORDER BY employeeName "There is only one difference between a dream and an aim.A dream requires soundless sleep to see,whereas an aim requires sleepless efforts to achieve..!!" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-11 : 10:27:30
|
alsoSELECT RST.employeeName,SUM(CASE WHEN SPT.productCategory='skincare' THEN RRT.amt ELSE 0 END) AS productTotal, SUM(CASE WHEN SPT.productCategory='facial' THEN RRT.amt ELSE 0 END) AS facialTotal,SUM(CASE WHEN SPT.productCategory='manicure' THEN RRT.amt ELSE 0 END) AS manicureTotalFROM @ReceiptSummaryTable RSTINNER JOIN @ReceiptRowsTable RRT ON RRT.ReceiptID = RST.ReceiptIDINNER JOIN @ServicesAndProductsTable SPT ON SPT.productID = RRT.productID AND SPT.type = RRT.typeGROUP BY RST.employeeName |
 |
|
|
sphericalx
Starting Member
10 Posts |
Posted - 2008-12-11 : 23:45:49
|
| thnx.. its working perfectly great..thnx guys for all ur help.. ^^ |
 |
|
|
|
|
|