Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Spica66
Starting Member
23 Posts |
Posted - 2012-08-06 : 11:11:02
|
I have the following query in a stored procedure:ALTER PROCEDURE [dbo].[frc_Top25byFRCcode]@startDate AS DATETIME = null,@endDate AS DATETIME = null,@NFClaimStatus int = nullAS DECLARE @SD DATETIMEDECLARE @ED DATETIMEDECLARE @NFCS INT SET @SD = @startDateSET @ED = @endDateSET @NFCS = @NFClaimStatusBEGIN SELECT w.NFWARRANTYPARTID + ' - ' + p.NFWARRANTYPARTDESCRIPTION AS WCCDESCRIPTION, ((SELECT ROUND(ISNULL(SUM(PRICE*QTY),0),2) FROM NFWARRANTYCLAIMSITEMS i WHERE i.CLAIMID = w.CLAIMID AND i.CORRECTIONCODE = w.CORRECTIONCODE)+ (SELECT ROUND(ISNULL(SUM(AMOUNT),0),2) FROM NFWARRANTYMISCCOSTS m WHERE m.CLAIMID = w.CLAIMID AND m.CORRECTIONCODE = w.CORRECTIONCODE)) + (W.HOURS*W.PRICE) as AMOUNT FROM dbo.NFWARRANTYCLAIMS AS cINNER JOIN ( SELECT NFCLAIMID, MAX(TRANSDATE) AS TRANSDATE FROM NFWARRANTYCLAIMHISTORY WHERE NFCLAIMSTATUS = @NFCS AND (transDate BETWEEN @SD AND @ED) GROUP BY NFCLAIMID ) AS h ON h.NFCLAIMID = c.CLAIMIDINNER JOIN NFWARRANTYTABLE AS t ON t.WARRANTYNUMBER = c.WARRANTYNUMBERINNER JOIN ( SELECT * FROM NFWARRANTYCORRECTIONTABLE WHERE NFCORRECTIONSTATUS <> 7 ) AS w ON w.CLAIMID = h.NFCLAIMID INNER JOIN NFWARRANTYPARTS AS p ON p.NFWARRANTYPARTID = w.NFWARRANTYPARTID ORDER BY w.WCCDESCRIPTIONENDIt generates the table shown below:WCC Description Amount 02.09.07 - Tires 170.00 02.09.07 - Tires 350.0002.09.07 - Tires 285.00 02.09.07 - Tires 190.0003.19.08 - Paint 130.0003.19.08 - Paint 280.0003.19.08 - Paint 285.00 06.01.05 - Sink 150.0006.01.05 - Sink 250.0006.01.05 - Sink 85.00 06.01.05 - Sink 150.00 I am trying to get the SQL statement to SUM the amounts for the grouped WCC Description and sort based on the totals for the group, highest total first:02.09.07 - Tires 805.0003.19.08 - Paint 695.00 06.01.05 - Sink 635.00I thought I could SUM(AMOUNT) and GROUP BY(WCCDESCRIPTION), but I can't figure out where in my code to do those two things. No matter what combination I try, it gives me a Syntax Error.ALTER PROCEDURE [dbo].[frc_Top25byFRCcode]@startDate AS DATETIME = null,@endDate AS DATETIME = null,@NFClaimStatus int = nullAS DECLARE @SD DATETIMEDECLARE @ED DATETIMEDECLARE @NFCS INT SET @SD = @startDateSET @ED = @endDateSET @NFCS = @NFClaimStatusBEGIN SELECT w.NFWARRANTYPARTID + ' - ' + p.NFWARRANTYPARTDESCRIPTION AS WCCDESCRIPTION, Select SUM(Amount) From (((SELECT ROUND(ISNULL(SUM(PRICE*QTY),0),2) FROM NFWARRANTYCLAIMSITEMS i WHERE i.CLAIMID = w.CLAIMID AND i.CORRECTIONCODE = w.CORRECTIONCODE)+ (SELECT ROUND(ISNULL(SUM(AMOUNT),0),2) FROM NFWARRANTYMISCCOSTS m WHERE m.CLAIMID = w.CLAIMID AND m.CORRECTIONCODE = w.CORRECTIONCODE)) + (W.HOURS*W.PRICE) as AMOUNT) x FROM dbo.NFWARRANTYCLAIMS AS cINNER JOIN ( SELECT NFCLAIMID, MAX(TRANSDATE) AS TRANSDATE FROM NFWARRANTYCLAIMHISTORY WHERE NFCLAIMSTATUS = @NFCS AND (transDate BETWEEN @SD AND @ED) GROUP BY NFCLAIMID ) AS h ON h.NFCLAIMID = c.CLAIMIDINNER JOIN NFWARRANTYTABLE AS t ON t.WARRANTYNUMBER = c.WARRANTYNUMBERINNER JOIN ( SELECT * FROM NFWARRANTYCORRECTIONTABLE WHERE NFCORRECTIONSTATUS <> 7 ) AS w ON w.CLAIMID = h.NFCLAIMID INNER JOIN NFWARRANTYPARTS AS p ON p.NFWARRANTYPARTID = w.NFWARRANTYPARTID GROUP BY w.WCCDESCRIPTIONEND |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-06 : 11:19:40
|
[code]ALTER PROCEDURE [dbo].[frc_Top25byFRCcode]@startDate AS DATETIME = null,@endDate AS DATETIME = null,@NFClaimStatus int = nullAS DECLARE @SD DATETIMEDECLARE @ED DATETIMEDECLARE @NFCS INTSET @SD = @startDateSET @ED = @endDateSET @NFCS = @NFClaimStatusBEGINSELECT w.NFWARRANTYPARTID + ' - ' + p.NFWARRANTYPARTDESCRIPTION AS WCCDESCRIPTION,SUM(ROUND(ISNULL(i.PRICE*i.QTY,0),2)+ROUND(ISNULL(AMOUNT,0),2)+ (W.HOURS*W.PRICE)) as AMOUNT FROM dbo.NFWARRANTYCLAIMS AS cINNER JOIN (SELECT NFCLAIMID, MAX(TRANSDATE) AS TRANSDATE FROM NFWARRANTYCLAIMHISTORYWHERE NFCLAIMSTATUS = @NFCS AND (transDate BETWEEN @SD AND @ED)GROUP BY NFCLAIMID) AS h ON h.NFCLAIMID = c.CLAIMIDINNER JOIN NFWARRANTYTABLE AS t ON t.WARRANTYNUMBER = c.WARRANTYNUMBERINNER JOIN (SELECT * FROM NFWARRANTYCORRECTIONTABLE WHERE NFCORRECTIONSTATUS <> 7 )AS w ON w.CLAIMID = h.NFCLAIMID INNER JOIN NFWARRANTYPARTS AS p ON p.NFWARRANTYPARTID = w.NFWARRANTYPARTID INNER JOIN NFWARRANTYCLAIMSITEMS iON i.CLAIMID = w.CLAIMID AND i.CORRECTIONCODE = w.CORRECTIONCODEINNER JOIN NFWARRANTYMISCCOSTS mON m.CLAIMID = w.CLAIMID AND m.CORRECTIONCODE = w.CORRECTIONCODEGROUP BY w.NFWARRANTYPARTID + ' - ' + p.NFWARRANTYPARTDESCRIPTIONORDER BY AMOUNT DESCEND[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Spica66
Starting Member
23 Posts |
Posted - 2012-08-06 : 11:32:18
|
Wow!! Thank you for that quick response. I was just coming back to add that I was also hoping to get the count of the rows that made up the group, and you had already posted. Any thoughts on getting the counts as well? |
 |
|
Spica66
Starting Member
23 Posts |
Posted - 2012-08-06 : 11:59:53
|
I am afraid that your proposed code did not work. There are $28,000 worth of Paint Repairs, but your query returned $344,000. The amount calculation is as follows: ((SELECT ROUND(ISNULL(SUM(PRICE*QTY),0),2)FROM NFWARRANTYCLAIMSITEMS iWHERE i.CLAIMID = w.CLAIMID AND i.CORRECTIONCODE = w.CORRECTIONCODE)+(SELECT ROUND(ISNULL(SUM(AMOUNT),0),2)FROM NFWARRANTYMISCCOSTS mWHERE m.CLAIMID = w.CLAIMID AND m.CORRECTIONCODE = w.CORRECTIONCODE))+ (W.HOURS*W.PRICE) as AMOUNT Some of that code disappeared from your response. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-06 : 12:24:10
|
quote: Originally posted by Spica66 I am afraid that your proposed code did not work. There are $28,000 worth of Paint Repairs, but your query returned $344,000. The amount calculation is as follows: ((SELECT ROUND(ISNULL(SUM(PRICE*QTY),0),2)FROM NFWARRANTYCLAIMSITEMS iWHERE i.CLAIMID = w.CLAIMID AND i.CORRECTIONCODE = w.CORRECTIONCODE)+(SELECT ROUND(ISNULL(SUM(AMOUNT),0),2)FROM NFWARRANTYMISCCOSTS mWHERE m.CLAIMID = w.CLAIMID AND m.CORRECTIONCODE = w.CORRECTIONCODE))+ (W.HOURS*W.PRICE) as AMOUNT Some of that code disappeared from your response.
nopei've replaced subqueries with joins. see the full suggestion and last two joins------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-06 : 12:25:17
|
quote: Originally posted by Spica66 I am afraid that your proposed code did not work. There are $28,000 worth of Paint Repairs, but your query returned $344,000. The amount calculation is as follows: ((SELECT ROUND(ISNULL(SUM(PRICE*QTY),0),2)FROM NFWARRANTYCLAIMSITEMS iWHERE i.CLAIMID = w.CLAIMID AND i.CORRECTIONCODE = w.CORRECTIONCODE)+(SELECT ROUND(ISNULL(SUM(AMOUNT),0),2)FROM NFWARRANTYMISCCOSTS mWHERE m.CLAIMID = w.CLAIMID AND m.CORRECTIONCODE = w.CORRECTIONCODE))+ (W.HOURS*W.PRICE) as AMOUNT Some of that code disappeared from your response.
also reg your total issue i cant suggest anything unless i see how data is. so if you can post the data from tables then i may be able to help------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|