Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get 2 column result from 1 field


Get 2 column result from 1 field

Author
Message
davdam8
davdam8
SSC-Enthusiastic
SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)

Group: General Forum Members
Points: 180 Visits: 668
Hi geniuses

Let say I got a table:

Years   |   Value
2000 | 2
2001 | 4
2002 | 5
2003 | 3
2004 | 2



I need to get the sum of the Value for all the years in 1 column and the sum(Value) to 2002 in another column.
in order to give:


Sumof.All | Sumto.2002
16 | 11



Thanks in advance!
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16550 Visits: 17004
Seems like a semi strange requirement but certainly not to bad. Notice how I posted consumable ddl and sample data? This is something you should do in the future.

Here are a couple of different ways...I am sure there are plenty of others.


create table #Something
(
Years int,
SomeValue int
)

insert #Something(Years, SomeValue)
Values
(2000, 2)
,(2001, 4)
,(2002, 5)
,(2003, 3)
,(2004, 2)

select SUM(SomeValue) as SumOfAll, (select SUM(SomeValue) from #Something where Years <= 2002) as SumTo2002
from #Something

select SUM(SomeValue) as SumOfAll, x.Sumto2002
from #Something
cross apply (select SUM(SomeValue) as Sumto2002 from #Something where Years <= 2002)x
group by x.Sumto2002




_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8506 Visits: 18115
How about using a CASE statement to filter the years needed?


DECLARE @Something table
(
Years int,
SomeValue int
)

insert @Something(Years, SomeValue)
Values
(2000, 2)
,(2001, 4)
,(2002, 5)
,(2003, 3)
,(2004, 2)

SELECT SUM( SomeValue) SumOfAll, SUM( CASE WHEN Years <= 2002 THEN SomeValue END) Sumto2002
FROM @Something




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16550 Visits: 17004
Luis Cazares (6/13/2013)
How about using a CASE statement to filter the year needed?


DECLARE @Something table
(
Years int,
SomeValue int
)

insert @Something(Years, SomeValue)
Values
(2000, 2)
,(2001, 4)
,(2002, 5)
,(2003, 3)
,(2004, 2)

SELECT SUM( SomeValue) SumOfAll, SUM( CASE WHEN Years <= 2002 THEN SomeValue END) Sumto2002
FROM @Something



There you go again Luis...taking a perfectly slow version and making it faster. :-D

Not really sure why my brain was stuck doing it the hard way on this one. Thanks!!

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
davdam8
davdam8
SSC-Enthusiastic
SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)

Group: General Forum Members
Points: 180 Visits: 668
There an issue:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.



How do I work around this?

Thanks!
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3001 Visits: 4996
Can you post the query you are using. That might give us some idea.


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
davdam8
davdam8
SSC-Enthusiastic
SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)

Group: General Forum Members
Points: 180 Visits: 668

SELECT
ID,
CASE WHEN (SUM([VALPAYREAL]) IS NULL) THEN 0 ELSE SUM([VALPAYREAL]) END AS PAYREAL,
CASE WHEN (SUM([VALPAYPLAN]) IS NULL) THEN 0 ELSE SUM([VALPAYPLAN]) END AS PAYPLAN,
(SELECT SUM([VALPAYPLAN])
FROM ABC
WHERE DATE <= Getdate() AND ([TYPE] = 'PAYMENT') GROUP BY ID )
AS PAYPLANTODATE,
CASE WHEN (SUM([VALPAYPROCESS])IS NULL) THEN 0 ELSE SUM([VALPAYPROCESS]) END AS PAYPROCESS
FROM ABC
WHERE ([TYPE] = 'PAYMENT')
GROUP BY ID


Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
This is the problem


(SELECT SUM([VALPAYPLAN])
FROM ABC
WHERE DATE <= Getdate() AND ([TYPE] = 'PAYMENT') GROUP BY ID )



You need to add an additional filter to this sub query


SELECT
ID
, CASE
WHEN (SUM([VALPAYREAL]) IS NULL) THEN 0
ELSE SUM([VALPAYREAL])
END AS PAYREAL
, CASE
WHEN (SUM([VALPAYPLAN]) IS NULL) THEN 0
ELSE SUM([VALPAYPLAN])
END AS PAYPLAN
, (SELECT SUM([VALPAYPLAN])
FROM ABC
WHERE
DATE <= Getdate()
AND ([TYPE] = 'PAYMENT')
AND ID = OUTERABC.ID
GROUP BY
ID ) AS PAYPLANTODATE
, CASE
WHEN (SUM([VALPAYPROCESS])IS NULL) THEN 0
ELSE SUM([VALPAYPROCESS])
END AS PAYPROCESS
FROM
ABC AS OUTERABC
WHERE
([TYPE] = 'PAYMENT')
GROUP BY ID



Edit : Code reformated, as Chris made me feel guilty about not doing it in the frist place :-D

_________________________________________________________________________
SSC Guide to Posting and Best Practices
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8959 Visits: 19017
Hands up if you find this easier to scan:

SELECT 
o.ID,
PAYREAL = ISNULL(SUM(o.VALPAYREAL),0),
PAYPLAN = ISNULL(SUM(o.VALPAYPLAN),0),
x.PAYPLANTODATE,
PAYPROCESS = ISNULL(SUM(o.VALPAYPROCESS),0)
FROM ABC o
CROSS APPLY (
SELECT
PAYPLANTODATE = SUM(i.VALPAYPLAN)
FROM ABC i
WHERE i.[DATE] <= GETDATE()
AND i.[TYPE] = 'PAYMENT'
AND i.ID = o.ID
GROUP BY i.ID),
) x
WHERE o.[TYPE] = 'PAYMENT'
GROUP BY o.ID



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
+1. Definately easier to read Chris, Formatting FTW.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search