SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Multiple Rows in One Resultset


Multiple Rows in One Resultset

Author
Message
clay.calvin
clay.calvin
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 15
Hi All,

Let's say I have a table like the following:

row1 row2 row3 row4
0 6 0 0
0 0 30 0
4 0 0 0
0 0 0 18

but I want to return a resultset like:

row1 row2 row3 row4
4 6 30 18

Does anyone have any ideas as to how I would do that with T-SQL?


Thanks,
C
laurie-789651
laurie-789651
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2394 Visits: 1272
-- Test data:

declare @a as table
(
row1 int,
row2 int,
row3 int,
row4 int
);

insert @a values (0,6,0,0);
insert @a values (0,0,30,0);
insert @a values (4,0,0,0);
insert @a values (0,0,0,18);

--select * from @a;

-- Example solution:

select
max(case when row1 <> 0 then row1 end) as Row1,
max(case when row2 <> 0 then row2 end) as Row2,
max(case when row3 <> 0 then row3 end) as Row3,
max(case when row4 <> 0 then row4 end) as Row4
from @a
clay.calvin
clay.calvin
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 15
Laurie,

Thanks so much. I think that is really gonna help me.


C
~
laurie-789651
laurie-789651
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2394 Visits: 1272
Noooo problem!
T_Dot_Geek
T_Dot_Geek
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 Visits: 99
Here is my two cents.
You don't need a Case Statement. Just simple One Line Query would work fine.

declare @MyTable as table
(
Column1 int,
Column2 int,
Column3 int,
column4 int
);

insert @MyTable values (0,6,0,0);
insert @MyTable values (0,0,30,0);
insert @MyTable values (4,0,0,0);
insert @MyTable values (0,0,0,18);

Select Col1 = Max(Column1), Col2 = Max(Column2), Col3 = Max(Column3), Col4 = Max(Column4) FROM @MyTable
clay.calvin
clay.calvin
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 15
T,

Thanks for your reply.

C
~
gorthog
gorthog
SSC-Addicted
SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)SSC-Addicted (484 reputation)

Group: General Forum Members
Points: 484 Visits: 168
clay.calvin,

The SUM() function is another option that may be applicable. Compare the MAX() function to the SUM() function (note I have changed the VALUES in the fourth insert to demonstrate the difference):


DECLARE @MyTable AS TABLE
(
Column1 INT,
Column2 INT,
Column3 INT,
column4 INT
);

INSERT @MyTable VALUES (0,6,0,0);
INSERT @MyTable VALUES (0,0,30,0);
INSERT @MyTable VALUES (4,0,0,0);
INSERT @MyTable VALUES (1,2,3,18);

SELECT Col1 = MAX(Column1), Col2 = MAX(Column2), Col3 = MAX(Column3), Col4 = MAX(Column4)
FROM @MyTable

SELECT Col1 = SUM(Column1), Col2 = SUM(Column2), Col3 = SUM(Column3), Col4 = SUM(Column4)
FROM @MyTable


output from first statement:

Col1 Col2 Col3 Col4
----------- ----------- ----------- -----------
4 6 30 18


output from second statement:

Col1 Col2 Col3 Col4
----------- ----------- ----------- -----------
5 8 33 18



The aggregate function you will choose depends on what you want to accomplish.

-gjr
clay.calvin
clay.calvin
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 15
Thanks everyone,

Laurie's suggestion worked perfectly. I'm on to the next project. I did not mean to offend anyone or make things hard for people to help me. I'll remember the tip for the next post.


C
~
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