Sunday, February 19, 2012

hellp with the sql is needed

hello i am a beginner:

on the .mdf sql.s 2005Ex

i need to present:

the 12 month as field name

whan the first filed is the customer name

and the rest is the sum(sale)

so the result will be like this

name 1 2 3 4 5 6 7 8 9 10 11 12

name1 $1 $2 0$ 8$ $1 $2 0$ 8$ $1 $2 0$ 8$

name2 $1 $2 0$ 8$ $1 $23 0$ 8$ $1 $23 0$ 8$

name3 $1 $2 0$ 8$ $1 $2 0$ 8$ $13 $2 0$ 8$

name4 $1 $2 0$ 83$ $1 $2 0$ 8$ $1 $2 0$ 8$

well so far i reached this - wich do the sum but on 12 row per name: well .. so if you could help me ?

SELECT
CASE WHEN MONTHs = 1 THEN MONEY ELSE 0 END AS '1',
CASE WHEN MONTHs = 2 THEN MONEY ELSE 0 END AS '2',
CASE WHEN MONTHs = 3 THEN MONEY ELSE 0 END AS '3',
CASE WHEN MONTHs = 4 THEN MONEY ELSE 0 END AS '4',
CASE WHEN MONTHs = 5 THEN MONEY ELSE 0 END AS '5',
CASE WHEN MONTHs = 6 THEN MONEY ELSE 0 END AS '6',
CASE WHEN MONTHs = 7 THEN MONEY ELSE 0 END AS '7',
CASE WHEN MONTHs = 8 THEN MONEY ELSE 0 END AS '8',
CASE WHEN MONTHs = 9 THEN MONEY ELSE 0 END AS '9',
CASE WHEN MONTHs = 10 THEN MONEY ELSE 0 END AS '10'

FROM

(SELECT DISTINCT MONTH(tblActoin.MyDate) AS MONTHs, SUM(tblActoin.amount) AS MONEY, tblIdentity.name
FROM tblActoin INNER JOIN
tblIdentity ON tblActoin.IdentityID = tblIdentity.id
GROUP BY tblActoin.MyDate, tblIdentity.name) AS derivedtbl_1

thanks

try this

SELECT Names,
sum(CASE WHEN MONTHs = 1 THEN MONEY ELSE 0 END) AS '1',
sum(CASE WHEN MONTHs = 2 THEN MONEY ELSE 0 END) AS '2',
sum(CASE WHEN MONTHs = 3 THEN MONEY ELSE 0 END) AS '3',
sum(CASE WHEN MONTHs = 4 THEN MONEY ELSE 0 END) AS '4',
sum(CASE WHEN MONTHs = 5 THEN MONEY ELSE 0 END) AS '5',
sum(CASE WHEN MONTHs = 6 THEN MONEY ELSE 0 END) AS '6',
sum(CASE WHEN MONTHs = 7 THEN MONEY ELSE 0 END) AS '7',
sum(CASE WHEN MONTHs = 8 THEN MONEY ELSE 0 END) AS '8',
sum(CASE WHEN MONTHs = 9 THEN MONEY ELSE 0 END) AS '9',
sum(CASE WHEN MONTHs = 10 THEN MONEY ELSE 0 END) AS '10,
sum(CASE WHEN MONTHs = 11 THEN MONEY ELSE 0 END) AS '11',
sum(CASE WHEN MONTHs = 12 THEN MONEY ELSE 0 END) AS '12'

FROM

(SELECT DISTINCT MONTH(tblActoin.MyDate) AS MONTHs, SUM(tblActoin.amount) AS MONEY, tblIdentity.name as Names
FROM tblActoin INNER JOIN
tblIdentity ON tblActoin.IdentityID = tblIdentity.id
GROUP BY tblActoin.MyDate, tblIdentity.name) AS derivedtbl_1)

group by Names

|||Yes!!! Thank you!!!

No comments:

Post a Comment