Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Sum in Query

Author  Topic 

Sep410
Posting Yak Master

117 Posts

Posted - 2008-09-19 : 12:47:57
Hi,

Here is the query that I have :
SELECT DISTINCT 
TOP (100) PERCENT dbo.vIntervals.WorDescription, ISNULL(SUM(dbo.vIntervals.d14), N'') AS d14, ISNULL(SUM(dbo.vIntervals.d29), N'') AS d29,
ISNULL(SUM(dbo.vIntervals.d44), N'') AS d44, ISNULL(SUM(dbo.vIntervals.d59), N'') AS d59, ISNULL(SUM(dbo.vIntervals.d74), N'') AS d74,
ISNULL(SUM(dbo.vIntervals.d89), N'') AS d89, ISNULL(SUM(dbo.vIntervals.d90), N'') AS d90
FROM dbo.vIntervals RIGHT OUTER JOIN
dbo.vIntervals AS vIntervals_1 ON dbo.vIntervals.CaseWorkerID = vIntervals_1.CaseWorkerID AND
dbo.vIntervals.WorDescription = vIntervals_1.WorDescription
WHERE (vIntervals_1.OpenDate BETWEEN CONVERT(DATETIME, '2008-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2009-01-01 00:00:00', 102))
GROUP BY dbo.vIntervals.WorDescription, ISNULL(dbo.vIntervals.d14, N''), ISNULL(dbo.vIntervals.d29, N''), ISNULL(dbo.vIntervals.d44, N''),
ISNULL(dbo.vIntervals.d59, N''), ISNULL(dbo.vIntervals.d74, N''), ISNULL(dbo.vIntervals.d89, N''), ISNULL(dbo.vIntervals.d90, N'')
ORDER BY dbo.vIntervals.WorDescription

And here is it outcome:
Alison 0 0 0 0 0 0 2
Alison 0 0 0 0 1 0 0
John 0 0 0 4 0 0 0
John 0 0 1 1 0 0 0
John 0 0 2 0 0 0 0
John 0 0 10 0 0 0 0
John 0 1 0 0 0 1 0
Mark 0 16 0 0 0 0 0
Mark 1 0 0 0 0 0 1
Mark 1 0 0 1 0 0 0

I need to have

Alison 0 0 0 0 1 0 2

John 0 1 13 4 5 0 1

Mark 2 16 0 1 0 0 1

What should I do?


Sep

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-19 : 12:56:33
i think you need only dbo.vIntervals.WorDescription in GROUP BY. why are you including other fields on group by and again applying sum() over them. remove them from group by and try.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-09-19 : 12:56:37
quote:
Originally posted by Sep410

Hi,

Here is the query that I have :


Select dbo.vIntervals.WorDescription,sum(d14) as d14,Sum(d29)as d29,Sum(d44) as d44,sum(d59)as d59,sum(d74)as d74,sum(d89) as d89,sum(d90) as d90
from


(SELECT DISTINCT
TOP (100) PERCENT dbo.vIntervals.WorDescription, ISNULL(SUM(dbo.vIntervals.d14), N'') AS d14, ISNULL(SUM(dbo.vIntervals.d29), N'') AS d29,
ISNULL(SUM(dbo.vIntervals.d44), N'') AS d44, ISNULL(SUM(dbo.vIntervals.d59), N'') AS d59, ISNULL(SUM(dbo.vIntervals.d74), N'') AS d74,
ISNULL(SUM(dbo.vIntervals.d89), N'') AS d89, ISNULL(SUM(dbo.vIntervals.d90), N'') AS d90
FROM dbo.vIntervals RIGHT OUTER JOIN
dbo.vIntervals AS vIntervals_1 ON dbo.vIntervals.CaseWorkerID = vIntervals_1.CaseWorkerID AND
dbo.vIntervals.WorDescription = vIntervals_1.WorDescription
WHERE (vIntervals_1.OpenDate BETWEEN CONVERT(DATETIME, '2008-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2009-01-01 00:00:00', 102))
GROUP BY dbo.vIntervals.WorDescription, ISNULL(dbo.vIntervals.d14, N''), ISNULL(dbo.vIntervals.d29, N''), ISNULL(dbo.vIntervals.d44, N''),
ISNULL(dbo.vIntervals.d59, N''), ISNULL(dbo.vIntervals.d74, N''), ISNULL(dbo.vIntervals.d89, N''), ISNULL(dbo.vIntervals.d90, N'')
ORDER BY dbo.vIntervals.WorDescription
)t
group by dbo.vIntervals.WorDescription


And here is it outcome:
Alison 0 0 0 0 0 0 2
Alison 0 0 0 0 1 0 0
John 0 0 0 4 0 0 0
John 0 0 1 1 0 0 0
John 0 0 2 0 0 0 0
John 0 0 10 0 0 0 0
John 0 1 0 0 0 1 0
Mark 0 16 0 0 0 0 0
Mark 1 0 0 0 0 0 1
Mark 1 0 0 1 0 0 0

I need to have

Alison 0 0 0 0 1 0 2

John 0 1 13 4 5 0 1

Mark 2 16 0 1 0 0 1

What should I do?


Sep

Go to Top of Page

Sep410
Posting Yak Master

117 Posts

Posted - 2008-09-19 : 13:01:33
quote:
Originally posted by visakh16

i think you need only dbo.vIntervals.WorDescription in GROUP BY. why are you including other fields on group by and again applying sum() over them. remove them from group by and try.


As you see in mark's data I need to have sum for all columns of information.

Sep
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-19 : 13:04:19
quote:
Originally posted by Sep410

quote:
Originally posted by visakh16

i think you need only dbo.vIntervals.WorDescription in GROUP BY. why are you including other fields on group by and again applying sum() over them. remove them from group by and try.


As you see in mark's data I need to have sum for all columns of information.

Sep


ok i agree. in which case why should you include them in group by?
Go to Top of Page

Sep410
Posting Yak Master

117 Posts

Posted - 2008-09-19 : 13:06:41
Thanks visakh16,You were right. It is working well When I removed them from group by.

Sep
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-19 : 13:12:15
quote:
Originally posted by Sep410

Thanks visakh16,You were right. It is working well When I removed them from group by.

Sep


Cheers
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2008-09-19 : 13:12:37
Please provide some sample data, but from what I can tell all you need is this.


SELECT
vi.WorDescription
,Sum(isnull(vi.d14,0)) as d14
,Sum(isnull(vi.d29,0)) as d29
,Sum(isnull(vi.d44,0)) as d44
,Sum(isnull(vi.d59,0)) as d59
,Sum(isnull(vi.d74,0)) as d74
,Sum(isnull(vi.d89,0)) as d89
,Sum(isnull(vi.d90,0)) as d90
FROM
dbo.vIntervals vi
WHERE
vi.OpenDate BETWEEN '2008-01-01' AND '2009-01-01'
GROUP BY
vi.WorDescription
ORDER BY vi.WorDescription


Unless you need all the names, which then it's


SELECT
aa.WorDescription
,Sum(isnull(vi.d14,0)) as d14
,Sum(isnull(vi.d29,0)) as d29
,Sum(isnull(vi.d44,0)) as d44
,Sum(isnull(vi.d59,0)) as d59
,Sum(isnull(vi.d74,0)) as d74
,Sum(isnull(vi.d89,0)) as d89
,Sum(isnull(vi.d90,0)) as d90
FROM
(
Select distinct a.WorDescription
from
dbo.vIntervals a
) aa
Left Join
dbo.vIntervals vi
on aa.WorDescription = vi.WorDescription
WHERE
vi.OpenDate BETWEEN '2008-01-01' AND '2009-01-01'
GROUP BY
vi.WorDescription
ORDER BY vi.WorDescription


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page
   

- Advertisement -