Solved

MS SQL 2008 Select

Posted on 2015-01-11
5
64 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 45

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

762 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

18 Experts available now in Live!

Get 1:1 Help Now