Please start any new threads on our new
site at http://forums.sqlteam.com. 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.

Yes and that is TOTALSUM for all 3 mounth (Jan+feb+Mar). I want im my resault COLUMNS like SumJun, SumFeb, SumMar, TOTALSUM. I suppose need to join tables copule times to get that columns...

select p.PREJEMNIK, month(DATUM),
SUM (case when month(DATUM) = 1 and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaJanuar
SUM (case when month(DATUM) = 2 and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaFebruary
SUM (case when month(DATUM) = 3 and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaMarch
SUM (case when month(DATUM) in (1, 2, 3) and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaTotal
from
PROMET p left join SUBJEKT s
on s.NAZIV=p.PREJEMNIK
where p.POSLDOG='3000'
group by p.PREJEMNIK, month(DATUM)
order by p.PREJEMNIK

select p.PREJEMNIK, sum (p.ZAPLACILO) as SumaJanuar from (PROMET p left join SUBJEKT s on s.NAZIV=p.PREJEMNIK where p.POSLDOG='3000' and (month(DATUM) in (1) and year(DATUM) = 2013) group by p.PREJEMNIK order by p.PREJEMNIK) and (select p.PREJEMNIK, sum (p.ZAPLACILO) as SumaFebruar from PROMET p left join SUBJEKT s on s.NAZIV=p.PREJEMNIK where p.POSLDOG='3000' and (month(DATUM) in (2) and year(DATUM) = 2013) group by p.PREJEMNIK order by p.PREJEMNIK)

I am trying somethink like this...

and error is

Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'where'. Msg 156, Level 15, State 1, Line 16 Incorrect syntax near the keyword 'order'.

quote: You may want to use case statement like this:

select p.PREJEMNIK, month(DATUM), SUM (case when month(DATUM) = 1 and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaJanuar SUM (case when month(DATUM) = 2 and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaFebruary SUM (case when month(DATUM) = 3 and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaMarch SUM (case when month(DATUM) in (1, 2, 3) and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaTotal from PROMET p left join SUBJEKT s on s.NAZIV=p.PREJEMNIK where p.POSLDOG='3000' group by p.PREJEMNIK, month(DATUM) order by p.PREJEMNIK

nikoz, Did you try the query I posted earlier? (Shown above)

select p.PREJEMNIK,
SUM (case when month(DATUM) = 1 and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaJanuar
SUM (case when month(DATUM) = 2 and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaFebruary
SUM (case when month(DATUM) = 3 and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaMarch
SUM (case when month(DATUM) in (1, 2, 3) and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaTotal
from
PROMET p
join SUBJEKT s
on s.NAZIV=p.PREJEMNIK
where p.POSLDOG='3000'
group by p.PREJEMNIK
order by p.PREJEMNIK

select p.PREJEMNIK,
SUM (case when month(DATUM) = 1 and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaJanuar
SUM (case when month(DATUM) = 2 and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaFebruary
SUM (case when month(DATUM) = 3 and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaMarch
SUM (case when month(DATUM) in (1, 2, 3) and year(DATUM) = 2013 THEN p.ZAPLACILO END) as SumaTotal
from
PROMET p
join SUBJEKT s
on s.NAZIV=p.PREJEMNIK
where p.POSLDOG='3000'
group by p.PREJEMNIK
order by p.PREJEMNIK

The more efficient way to write the query is this:

select p.PREJEMNIK,
SUM (case when DATUM >= '20130101' and DATUM < '20130201' THEN p.ZAPLACILO ELSE 0 END) as SumaJanuar,
SUM (case when DATUM >= '20130201' and DATUM < '20130301' THEN p.ZAPLACILO ELSE 0 END) as SumaFebruary,
SUM (case when DATUM >= '20130301' and DATUM < '20130401' THEN p.ZAPLACILO ELSE 0 END) as SumaMarch,
SUM (case when DATUM >= '20130101' and DATUM < '20130401' THEN p.ZAPLACILO ELSE 0 END) as SumaTotal
from
PROMET p
join SUBJEKT s
on s.NAZIV=p.PREJEMNIK
where DATUM >= '20130101' and DATUM < '20130401' AND p.POSLDOG='3000'
group by p.PREJEMNIK
order by p.PREJEMNIK