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 |
|
mindtrap
Starting Member
10 Posts |
Posted - 2011-07-05 : 06:25:45
|
Try to find which vendor from each state have largest invoiceWITH [Summary] AS(SELECT VendorState, VendorName, SUM(InvoiceTotal) AS TotalInvoiceFROM Vendors JOIN InvoicesON Vendors.VendorID = Invoices.VendorIDGROUP BY Vendors.VendorState, Vendors.VendorName);WITH [Total] AS(SELECT VendorState, MAX(TotalInvoice) AS TotalInvoiceFROM Vendors JOIN InvoicesON Vendors.VendorID = Invoices.VendorIDGROUP BY VendorState)SELECT [Summary].VendorState,Summary.VendorName,[Total].TotalInvoice FROM [Summary] JOIN [Total]ON[Summary].VendorState = [Total].VendorStateAND [Summary].TotalInvoice = [Total].TotalInvoiceORDER BY Summary.VendorState But the code above constantl giving errorMsg 156, Level 15, State 1, Line 8Incorrect syntax near the keyword 'WITH'.Msg 319, Level 15, State 1, Line 8Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.All help will be appreciated and thanks in advance |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2011-07-05 : 06:31:22
|
See below: quote: Originally posted by mindtrap Try to find which vendor from each state have largest invoice;WITH [Summary] AS(SELECT VendorState, VendorName, SUM(InvoiceTotal) AS TotalInvoiceFROM Vendors JOIN InvoicesON Vendors.VendorID = Invoices.VendorIDGROUP BY Vendors.VendorState, Vendors.VendorName);,WITH [Total] AS(SELECT VendorState, MAX(TotalInvoice) AS TotalInvoiceFROM Vendors JOIN InvoicesON Vendors.VendorID = Invoices.VendorIDGROUP BY VendorState)SELECT [Summary].VendorState,Summary.VendorName,[Total].TotalInvoice FROM [Summary] JOIN [Total]ON[Summary].VendorState = [Total].VendorStateAND [Summary].TotalInvoice = [Total].TotalInvoiceORDER BY Summary.VendorState But the code above constantl giving errorMsg 156, Level 15, State 1, Line 8Incorrect syntax near the keyword 'WITH'.Msg 319, Level 15, State 1, Line 8Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.All help will be appreciated and thanks in advance
Corey I Has Returned!! |
 |
|
|
mindtrap
Starting Member
10 Posts |
Posted - 2011-07-05 : 06:39:01
|
Sir really thanks for your help I was about to lose the plot :);WITH [Summary] AS(SELECT VendorState, VendorName, SUM(InvoiceTotal) AS TotalInvoiceFROM Vendors JOIN InvoicesON Vendors.VendorID = Invoices.VendorIDGROUP BY Vendors.VendorState, Vendors.VendorName),[Total] AS(SELECT VendorState, MAX(Summary.TotalInvoice) AS TotalInvoiceFROM SummaryGROUP BY VendorState)SELECT [Summary].VendorState,Summary.VendorName,[Total].TotalInvoice FROM [Summary] JOIN [Total]ON[Summary].VendorState = [Total].VendorStateAND [Summary].TotalInvoice = [Total].TotalInvoiceORDER BY Summary.VendorState |
 |
|
|
|
|
|
|
|