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 |
|
rum23
Yak Posting Veteran
77 Posts |
Posted - 2009-05-29 : 09:09:43
|
| SELECT Sum(TotalPaidAmount) FROM(SELECT 'FY' + y1 + '/' + y2 AS FiscalYear,QuarterNumber,(SELECT CASE WHEN Month(PaidDate)>9 THEN (Year(PaidDate) + 1) ELSE Year(PaidDate) END) as PaidYear,Month(PaidDate) as PaidMonth,SUM(PaidAmount) AS TotalPaidAmount,PaidDate FROM (SELECT RIGHT(DATEPART(YEAR, DATEADD(MONTH, -9, PaidDate)), 2) AS y1,RIGHT(DATEPART(YEAR, DATEADD(MONTH, +3, PaidDate)), 2) AS y2,'Q'+ Convert(varchar(10), (DATEPART(QUARTER, DATEADD(MONTH, 3, PaidDate)))) AS QuarterNumber,PaidAmount,PaidDate FROM Invoice WHERE Invoice.InvoiceStatusId =2 AND Invoice.InvoiceId IN (SELECT DISTINCT I.InvoiceId FROM Invoice I INNER JOIN InvoiceType IT ON I.InvoiceTypeId = IT.InvoiceTypeId INNER JOIN InvoiceProject IP ON I.InvoiceId = IP.InvoiceIdINNER JOIN Phase PP ON IP.tPhaseId = PP.PhaseIdINNER JOIN Project P ON P.ProjectId = PP.ProjectIdINNER JOIN tblMaster MR ON MR.MasterId = P.ProjectIdWHERE P.ProjectId='123-456-987')) AS d GROUP BY y1,y2,QuarterNumber,PaidDate)WHERE PaidYear=2008 AND QuarterNumber='Q3'I get a syntax error "Incorrect syntax near the keyword 'WHERE'." and the line shown is bold above is highlighted for this error. Please help! |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2009-05-29 : 09:18:19
|
Missing an alias...SELECT Sum(TotalPaidAmount) FROM(SELECT 'FY' + y1 + '/' + y2 AS FiscalYear,QuarterNumber,(SELECT CASE WHEN Month(PaidDate)>9 THEN (Year(PaidDate) + 1) ELSE Year(PaidDate) END) as PaidYear,Month(PaidDate) as PaidMonth,SUM(PaidAmount) AS TotalPaidAmount,PaidDate FROM (SELECT RIGHT(DATEPART(YEAR, DATEADD(MONTH, -9, PaidDate)), 2) AS y1,RIGHT(DATEPART(YEAR, DATEADD(MONTH, +3, PaidDate)), 2) AS y2,'Q'+ Convert(varchar(10), (DATEPART(QUARTER, DATEADD(MONTH, 3, PaidDate)))) AS QuarterNumber,PaidAmount,PaidDate FROM Invoice WHERE Invoice.InvoiceStatusId =2 AND Invoice.InvoiceId IN (SELECT DISTINCT I.InvoiceId FROM Invoice I INNER JOIN InvoiceType IT ON I.InvoiceTypeId = IT.InvoiceTypeId INNER JOIN InvoiceProject IP ON I.InvoiceId = IP.InvoiceIdINNER JOIN Phase PP ON IP.tPhaseId = PP.PhaseIdINNER JOIN Project P ON P.ProjectId = PP.ProjectIdINNER JOIN tblMaster MR ON MR.MasterId = P.ProjectIdWHERE P.ProjectId='123-456-987')) AS d GROUP BY y1,y2,QuarterNumber,PaidDate) SomeTable WHERE PaidYear=2008 AND QuarterNumber='Q3' Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
rum23
Yak Posting Veteran
77 Posts |
Posted - 2009-05-29 : 09:37:55
|
| I can't try Dinakar's solution because I'm trying to compute on the data generated on fly. The generated data is not saved in any table.I want to stay way from creating temporary tables, if possible.Please help. Thanks! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-05-29 : 09:52:56
|
| You dont need to create new table. How are you forming the query?MadhivananFailing to plan is Planning to fail |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-05-29 : 10:11:35
|
quote: Originally posted by rum23 I can't try Dinakar's solution because I'm trying to compute on the data generated on fly. The generated data is not saved in any table.I want to stay way from creating temporary tables, if possible.Please help. Thanks!
The SomeTable is not a real table. It is only an alias.This is called derived table. It is only a record set with a name. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
rum23
Yak Posting Veteran
77 Posts |
Posted - 2009-05-29 : 11:48:50
|
| ok, Thanks! dinakar solution worked great! Thanks a lot |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2009-05-29 : 12:33:29
|
| You have already used an alias in your query... the portion "as d" before the GROUP BY is an alias to the resultset from the SELECT.Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
|
|
|
|
|