﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server Newbies  / Multiple Rows in One Resultset / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 01:57:49 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Multiple Rows in One Resultset</title><link>http://www.sqlservercentral.com/Forums/Topic1377949-1292-1.aspx</link><description>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~</description><pubDate>Thu, 01 Nov 2012 20:08:06 GMT</pubDate><dc:creator>clay.calvin</dc:creator></item><item><title>RE: Multiple Rows in One Resultset</title><link>http://www.sqlservercentral.com/Forums/Topic1377949-1292-1.aspx</link><description>Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. If you know how, follow ISO-11179 data element naming conventions and formatting rules. Code should be in Standard SQL as much as possible and not local dialect. This is minimal polite behavior on SQL forums. It would also help if you knew what normalization was because this looks like a repeating group mess. You seem to have an integrity rule that there is one and only one non-zero value per row and per column. But you have no constraint to enforce; in fact, you have no data integrity at all. First let me kludge up the mess and add those constraints. CREATE TABLE Foobar(row1 INTEGER DEFAULT 0 NOT NULL  CHECK (row1 &amp;gt;= 0),  row2 INTEGER DEFAULT 0 NOT NULL  CHECK (row2 &amp;gt;= 0),  row3 INTEGER DEFAULT 0 NOT NULL  CHECK (row3 &amp;gt;= 0),  row4 INTEGER DEFAULT 0 NOT NULL  CHECK (row4 &amp;gt;= 0),  PRIMARY KEY (row1, row2, row3, row4), CHECK (SIGN(row1) + SIGN(row2) + SIGN(row3) + SIGN(row4) = 1));INSERT INTO Foobar VALUES (0, 6, 0, 0),        (0, 0, 30, 0),        (4, 0, 0, 0),        (0, 0, 0, 18);Do you see why 80-95% of the work in SQL is in the DDL, not the DML? All that work makes this super simple DML possible: SELECT MAX(row1), MAX(row2), MAX(row3), MAX(row4)  FROM Foobar;Now the right way to do a “fake array” with values on the main diagonal is:CREATE TABLE Foobar(i INTEGER NOT NULL UNIQUE, j INTEGER NOT NULL UNIQUE, foo_value INTEGER NOT NULL  CHECK (foo_value &amp;gt; 0));Generate the non-diagonal zeroes in a VIEW or the query. Google up an old article of mine on arrays for more details. </description><pubDate>Thu, 01 Nov 2012 19:42:36 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Multiple Rows in One Resultset</title><link>http://www.sqlservercentral.com/Forums/Topic1377949-1292-1.aspx</link><description>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):[code="sql"]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 @MyTableSELECT Col1 = SUM(Column1), Col2 = SUM(Column2), Col3 = SUM(Column3), Col4 = SUM(Column4) FROM @MyTableoutput from first statement:       Col1        Col2        Col3        Col4----------- ----------- ----------- -----------          4           6          30          18output from second statement:       Col1        Col2        Col3        Col4----------- ----------- ----------- -----------          5           8          33          18[/code]The aggregate function you will choose depends on what you want to accomplish.-gjr</description><pubDate>Thu, 01 Nov 2012 15:01:54 GMT</pubDate><dc:creator>gorthog</dc:creator></item><item><title>RE: Multiple Rows in One Resultset</title><link>http://www.sqlservercentral.com/Forums/Topic1377949-1292-1.aspx</link><description>T,Thanks for your reply.C~</description><pubDate>Tue, 30 Oct 2012 18:16:37 GMT</pubDate><dc:creator>clay.calvin</dc:creator></item><item><title>RE: Multiple Rows in One Resultset</title><link>http://www.sqlservercentral.com/Forums/Topic1377949-1292-1.aspx</link><description>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</description><pubDate>Mon, 29 Oct 2012 14:53:58 GMT</pubDate><dc:creator>T_Dot_Geek</dc:creator></item><item><title>RE: Multiple Rows in One Resultset</title><link>http://www.sqlservercentral.com/Forums/Topic1377949-1292-1.aspx</link><description>Noooo problem!</description><pubDate>Sat, 27 Oct 2012 09:44:28 GMT</pubDate><dc:creator>laurie-789651</dc:creator></item><item><title>RE: Multiple Rows in One Resultset</title><link>http://www.sqlservercentral.com/Forums/Topic1377949-1292-1.aspx</link><description>Laurie,Thanks so much.  I think that is really gonna help me.C~</description><pubDate>Sat, 27 Oct 2012 09:12:27 GMT</pubDate><dc:creator>clay.calvin</dc:creator></item><item><title>RE: Multiple Rows in One Resultset</title><link>http://www.sqlservercentral.com/Forums/Topic1377949-1292-1.aspx</link><description>-- 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 &amp;lt;&amp;gt; 0 then row1 end) as Row1,	max(case when row2 &amp;lt;&amp;gt; 0 then row2 end) as Row2,	max(case when row3 &amp;lt;&amp;gt; 0 then row3 end) as Row3,	max(case when row4 &amp;lt;&amp;gt; 0 then row4 end) as Row4from @a</description><pubDate>Sat, 27 Oct 2012 07:52:22 GMT</pubDate><dc:creator>laurie-789651</dc:creator></item><item><title>Multiple Rows in One Resultset</title><link>http://www.sqlservercentral.com/Forums/Topic1377949-1292-1.aspx</link><description>Hi All,Let's say I have a table like the following:row1	row2	row3	row40	6	0	00	0	30	04	0	0	00	0	0	18but I want to return a resultset like:row1	row2	row3	row44	6	30	18Does anyone have any ideas as to how I would do that with T-SQL?Thanks,C</description><pubDate>Sat, 27 Oct 2012 07:49:21 GMT</pubDate><dc:creator>clay.calvin</dc:creator></item></channel></rss>