dbTalk Databases Forums  

How can I combine different rows?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss How can I combine different rows? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
hlajeunessse@gmail.com
 
Posts: n/a

Default How can I combine different rows? - 07-03-2007 , 03:53 PM






Hi!

I have a table looking like

(username) (account number) (start date) (end date) (product)

wich I can have up to 4 lines for the same client.

I wist to transfert those lines into a new table looking like

(username) (account number) (start date 1) (end date 1) (product 1)
(start date 2) (end date 2) ... (product 4)

How (in SQL) I could do it?


Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: How can I combine different rows? - 07-03-2007 , 04:16 PM






(hlajeunessse (AT) gmail (DOT) com) writes:
Quote:
I have a table looking like

(username) (account number) (start date) (end date) (product)

wich I can have up to 4 lines for the same client.

I wist to transfert those lines into a new table looking like

(username) (account number) (start date 1) (end date 1) (product 1)
(start date 2) (end date 2) ... (product 4)

How (in SQL) I could do it?
SELECT username, accountnumber,
MIN(CASE rn WHEN 1 THEN startdate END),
MIN(CASE rn WHEN 1 THEN enddate END),
MIN(CASE rn WHEN 1 THEN product END),
MIN(CASE rn WHEN 2 THEN startdate END),
MIN(CASE rn WHEN 2 THEN enddate END),
MIN(CASE rn WHEN 2 THEN product END),
...
FROM (SELECT username, accountnumber, startdate, enddate, product,
rn = row_number()
OVER(PARTITION BY username, accountnumer
ORDER BY startdate, product) AS x
GROUP BY username, account

The trick is that by using MIN and GROUP by we get all on the same row,
else we would have four rows with NULL values in the column the row
does not apply to. Actually, it does not matter if you use MIN or MAX.

If you are on SQL 2000, you cannot use the row_number function to number
the rows. You can use:

rn = (SELECT COUNT(*)
FROM tbl b
WHERE a.username = b.username
AND a.accountnumber = b.accountnumber
AND (a.startdate < b.startdate OR
a.startdate = b.startdate AND a.product <= b.product)

But it will be very slow at large volumes.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #3  
Old   
--CELKO--
 
Posts: n/a

Default Re: How can I combine different rows? - 07-05-2007 , 08:14 PM



Quote:
I have a table looking like ..
Please post DDL instead of your personal narrative. If you had done
the talbe properly, i mgiht look like this:

CREATE TABLE AccountHistory
(acct_nbr INTEGER NOT NULL,
product_nbr INTEGER NOT NULL,
product_cnt INTEGER DEFAULT 1 NOT NULL
CHECK(product_cnt BETWEEEN ! AND 4),
PRIMARY KEY (acct_nbr, product_nbr, product_cnt),
user_name VARCHAR(25) NOT NULL,
start_date DATETIME NOT NULL,
end_date DATETIME NOT NULL,
CHECK (start_date < end_date));

I left out the REFERENCES clause you would need and some other
things.

Quote:
which I can have up to 4 lines [sic] for the same client.
Lines appear on a paper form or an input screen; a table has rows.
You need a constraint to enforce this rule.

Quote:
I wish to transfer those lines into a new table looking like ..
You also failed to give any rules for sorting the repeating groups.
But th real question is why are you doing this at all?? That would
violate First Normal Form (1NF). This is not a good way to write
SQL.




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.