Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Debug SQL syntax
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

United Kingdom
28 Posts

Posted - 06/14/2013 :  10:03:45  Show Profile  Reply with Quote
I'm receiving the following error from the preceeding code:

Select Supplier,
SUM(Select InvoiceValue From SalesHistory Where ImportDate = '01/01/2013') As January,
SUM(Select InvoiceValue from SalesHistory Where ImportDate = '02/01/2013') As February
From SalesHistory
Group By Supplier


Incorrect syntax near the keyboard 'Select'
Incorrect syntax near ')'
Incorrect syntax near the keyboard 'From'

ps. I'm aware that the crosstab wont be summing over supplier yet, that stage I plan to implement next

Flowing Fount of Yak Knowledge

1054 Posts

Posted - 06/14/2013 :  10:19:21  Show Profile  Reply with Quote
Sum cannot be used over a select statment (the way you used it)

You may rewrite the query as below

,Sum(Case When ImportDate='2013-01-01' Then InvoiceValue END) as Jan
,Sum(Case When ImportDate='2013-02-01' Then InvoiceValue END) as Feb
FROM SalesHistory
Group by Supplier


Edited by - MIK_2008 on 06/14/2013 10:22:09
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 06/14/2013 :  10:43:18  Show Profile  Reply with Quote

Select Supplier,
From (SELECT Supplier,DATENAME(mm,ImportDate) AS DateMonth,InvoiceValue FROM SalesHistory)t
Pivot(SUM(InvoiceValue) FOR DateMonth IN ([January],[February]))p

SQL Server MVP
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000