Solved

MS SQL 2008 Select

Posted on 2015-01-11
5
65 Views
Last Modified: 2015-05-12
Hello Everybode,

i have the following MS SQL Statement and it works fine:

Select ID , SUM(costs) AS costs1 ,

Sum ( Case when (type = 1 or type = 4) then Amount else 0 End) as costs2

from costs

Group by ID

Open in new window


Now i want to use this:

Select ID , SUM(costs) AS costs1 ,Date_of_Payment AS MyDate

Sum ( Case when (type = 1 or type = 4 AND Year (Date_of_Payment) = 2013 then Amount else 0 End) as costs2

from costs

Group by ID

Open in new window


I get a error Maessage. False Syntay in the near from '='.

Do you have any Ideas?

Thank you very much, Kind regards
Bounty
0
Comment
Question by:bounty457
5 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 40543189
missing parenthesiss?

Select ID , SUM(costs) AS costs1 ,Date_of_Payment AS MyDate,
Sum Case when (type = 1 or type = 4 AND Year (Date_of_Payment) = 2013) then Amount else 0 End as costs2
from costs
Group by ID

Open in new window

0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40543493

1.

Missing a right parentheses (comment above), guessing after type=4 but you'll have to verify

2.

The GROUP BY clause is missing Date_of_Payment, as it's in the select and not aggregated.
SELECT
   ID, 
   SUM(costs) AS costs1,
   Date_of_Payment AS MyDate,
  Sum (CASE WHEN (type = 1 or type = 4) AND Year (Date_of_Payment) = 2013 THEN Amount ELSE 0 END) as costs2
FROM costs
GROUP BY ID, Date_of_Payment

Open in new window


< edited to add comma that Vitor spotted >
0
 
LVL 11

Expert Comment

by:Goodangel Matope
ID: 40543707
You need to group by all fields not being summed

so your group by clause should include payment date
0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 40543901
I think no one saw the missing comma before the SUM.
Jim Horn has the more complete solution so I'll take his query and add the missing comma:
SELECT
   ID , 
   SUM(costs) AS costs1 ,
   Date_of_Payment AS MyDate,
  Sum (CASE WHEN (type = 1 or type = 4) AND Year (Date_of_Payment) = 2013 THEN Amount ELSE 0 END) as costs2
FROM costs
GROUP BY ID, Date_of_Payment

Open in new window


NO POINTS PLEASE
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 40548162

1.

@vitor, I had the comma.

2.

@Jim,  Love the numbers. How do you know he wants to group by date_of_payment? He may need to aggregate that with Min or something.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now