SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

stoolpidgeon
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

Error:

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

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1052 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

SELECT
Supplier
,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

Cheers
MIK

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

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

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

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


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.06 seconds. Powered By: Snitz Forums 2000