SQL Clone
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
Right there with Babe
Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)

Group: General Forum Members
Points: 720 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
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64718 Visits: 17979
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 Modens 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
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43186 Visits: 19859
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
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64718 Visits: 17979
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 Modens 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
Right there with Babe
Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)

Group: General Forum Members
Points: 720 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
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6142 Visits: 5288
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
Right there with Babe
Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)Right there with Babe (720 reputation)

Group: General Forum Members
Points: 720 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
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5145 Visits: 3232
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
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42807 Visits: 20015
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
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5145 Visits: 3232
+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