 |
|
 |
|
Next: subtracting 2 dates to calculate age; format as y..
|
| Author |
Message |
External

Since: Feb 07, 2008 Posts: 39
|
(Msg. 1) Posted: Wed Oct 22, 2008 4:47 pm
Post subject: How to combine two rows into one row with same ID? Archived from groups: microsoft>public>sqlserver>programming (more info?)
|
|
|
Hi
I have a data that needs to be arranged from two rows into one row as follow:
From two rows into -->
-----------------------------------------
AcctNo : Value 1 : Date 1
-----------------------------------------
1234 : 10,000 : 1/1/08
-----------------------------------------
1234 : 25,000 : 10/1/08
-----------------------------------------
One row -->
-------------------------------------------------------------------------
AcctNo : Value 1 : Date 1 : Value 2 : Date 2
-------------------------------------------------------------------------
1234 : 10,000 : 1/1/08 : 25,000 : 10/1/08
-------------------------------------------------------------------------
I would appreciate for any feedback.
Thanks. >> Stay informed about: How to combine two rows into one row with same ID? |
|
| Back to top |
|
 |  |
External

Since: Aug 20, 2008 Posts: 672
|
(Msg. 2) Posted: Wed Oct 22, 2008 9:32 pm
Post subject: Re: How to combine two rows into one row with same ID? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
You can rank by date and then pivot based on the rank. Here is example
(SQL Server 2005/2008):
CREATE TABLE AccountTransactions (
acc_nbr INT,
value DECIMAL(10, 2),
transaction_date DATETIME,
PRIMARY KEY (acc_nbr, transaction_date));
INSERT INTO AccountTransactions VALUES (1234, 10000, '20080101');
INSERT INTO AccountTransactions VALUES (1234, 25000, '20081001');
SELECT acc_nbr,
MAX(CASE WHEN rk = 1 THEN value END) AS value1,
MAX(CASE WHEN rk = 1 THEN transaction_date END) AS date1,
MAX(CASE WHEN rk = 2 THEN value END) AS value2,
MAX(CASE WHEN rk = 2 THEN transaction_date END) AS date2
FROM (SELECT acc_nbr, value, transaction_date,
ROW_NUMBER() OVER(PARTITION BY acc_nbr
ORDER BY transaction_date) AS rk
FROM AccountTransactions) AS A
GROUP BY acc_nbr;
--
Plamen Ratchev
http://www.SQLStudio.com >> Stay informed about: How to combine two rows into one row with same ID? |
|
| Back to top |
|
 |  |
External

Since: Feb 07, 2008 Posts: 39
|
(Msg. 3) Posted: Thu Oct 23, 2008 9:42 am
Post subject: Re: How to combine two rows into one row with same ID? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Hi Plamen,
Thank you very much for the feedback.
I am afraid that my sp is SQL 2000 version.
Do you have suggestion how the SQL 2000 version's work?
I would appreciate if you let me know.
Thanks.
Justin
"Plamen Ratchev" wrote:
> You can rank by date and then pivot based on the rank. Here is example
> (SQL Server 2005/2008):
>
> CREATE TABLE AccountTransactions (
> acc_nbr INT,
> value DECIMAL(10, 2),
> transaction_date DATETIME,
> PRIMARY KEY (acc_nbr, transaction_date));
>
> INSERT INTO AccountTransactions VALUES (1234, 10000, '20080101');
> INSERT INTO AccountTransactions VALUES (1234, 25000, '20081001');
>
> SELECT acc_nbr,
> MAX(CASE WHEN rk = 1 THEN value END) AS value1,
> MAX(CASE WHEN rk = 1 THEN transaction_date END) AS date1,
> MAX(CASE WHEN rk = 2 THEN value END) AS value2,
> MAX(CASE WHEN rk = 2 THEN transaction_date END) AS date2
> FROM (SELECT acc_nbr, value, transaction_date,
> ROW_NUMBER() OVER(PARTITION BY acc_nbr
> ORDER BY transaction_date) AS rk
> FROM AccountTransactions) AS A
> GROUP BY acc_nbr;
>
>
> --
> Plamen Ratchev
> http://www.SQLStudio.com
> >> Stay informed about: How to combine two rows into one row with same ID? |
|
| Back to top |
|
 |  |
External

Since: Aug 20, 2008 Posts: 672
|
(Msg. 4) Posted: Thu Oct 23, 2008 12:52 pm
Post subject: Re: How to combine two rows into one row with same ID? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Here is how you can simulate the same on SQL Server 2000. But note
performance will not be great with large data set.
SELECT acc_nbr,
MAX(CASE WHEN rk = 1 THEN value END) AS value1,
MAX(CASE WHEN rk = 1 THEN transaction_date END) AS date1,
MAX(CASE WHEN rk = 2 THEN value END) AS value2,
MAX(CASE WHEN rk = 2 THEN transaction_date END) AS date2
FROM (SELECT acc_nbr, value, transaction_date,
(SELECT COUNT(*)
FROM AccountTransactions AS B
WHERE B.acc_nbr = A.acc_nbr
AND B.transaction_date <= A.transaction_date) AS rk
FROM AccountTransactions AS A) AS A
GROUP BY acc_nbr;
--
Plamen Ratchev
http://www.SQLStudio.com >> Stay informed about: How to combine two rows into one row with same ID? |
|
| Back to top |
|
 |  |
External

Since: Feb 07, 2008 Posts: 39
|
(Msg. 5) Posted: Thu Oct 23, 2008 12:52 pm
Post subject: Re: How to combine two rows into one row with same ID? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Thank you so much!
It was great success.
Appreciated for your help.
"Plamen Ratchev" wrote:
> Here is how you can simulate the same on SQL Server 2000. But note
> performance will not be great with large data set.
>
> SELECT acc_nbr,
> MAX(CASE WHEN rk = 1 THEN value END) AS value1,
> MAX(CASE WHEN rk = 1 THEN transaction_date END) AS date1,
> MAX(CASE WHEN rk = 2 THEN value END) AS value2,
> MAX(CASE WHEN rk = 2 THEN transaction_date END) AS date2
> FROM (SELECT acc_nbr, value, transaction_date,
> (SELECT COUNT(*)
> FROM AccountTransactions AS B
> WHERE B.acc_nbr = A.acc_nbr
> AND B.transaction_date <= A.transaction_date) AS rk
> FROM AccountTransactions AS A) AS A
> GROUP BY acc_nbr;
>
> --
> Plamen Ratchev
> http://www.SQLStudio.com
> >> Stay informed about: How to combine two rows into one row with same ID? |
|
| Back to top |
|
 |  |
External

Since: Jan 10, 2008 Posts: 463
|
(Msg. 6) Posted: Thu Oct 23, 2008 1:03 pm
Post subject: Re: How to combine two rows into one row with same ID? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
One way (assuming that each acc_nbr has exactly two rows)
SELECT a1.acc_nbr,
a1.value AS value1,
a1.transaction_date AS value2,
a2.value AS value2,
a2.transaction_date AS value2
FROM AccountTransactions a1
INNER JOIN AccountTransactions a2 ON a1.acc_nbr = a2.acc_nbr
AND a1.transaction_date < a2.transaction_date;
Tom
"Justin Doh" wrote in message
> Hi Plamen,
>
> Thank you very much for the feedback.
> I am afraid that my sp is SQL 2000 version.
> Do you have suggestion how the SQL 2000 version's work?
> I would appreciate if you let me know.
>
> Thanks.
> Justin
>
> "Plamen Ratchev" wrote:
>
>> You can rank by date and then pivot based on the rank. Here is example
>> (SQL Server 2005/2008):
>>
>> CREATE TABLE AccountTransactions (
>> acc_nbr INT,
>> value DECIMAL(10, 2),
>> transaction_date DATETIME,
>> PRIMARY KEY (acc_nbr, transaction_date));
>>
>> INSERT INTO AccountTransactions VALUES (1234, 10000, '20080101');
>> INSERT INTO AccountTransactions VALUES (1234, 25000, '20081001');
>>
>> SELECT acc_nbr,
>> MAX(CASE WHEN rk = 1 THEN value END) AS value1,
>> MAX(CASE WHEN rk = 1 THEN transaction_date END) AS date1,
>> MAX(CASE WHEN rk = 2 THEN value END) AS value2,
>> MAX(CASE WHEN rk = 2 THEN transaction_date END) AS date2
>> FROM (SELECT acc_nbr, value, transaction_date,
>> ROW_NUMBER() OVER(PARTITION BY acc_nbr
>> ORDER BY transaction_date) AS rk
>> FROM AccountTransactions) AS A
>> GROUP BY acc_nbr;
>>
>>
>> --
>> Plamen Ratchev
>> http://www.SQLStudio.com
>> >> Stay informed about: How to combine two rows into one row with same ID? |
|
| Back to top |
|
 |  |
External

Since: Jan 31, 2011 Posts: 1
|
(Msg. 7) Posted: Mon Jan 31, 2011 11:25 am
Post subject: Re: Here is how you can simulate the same on SQL Server 2000. [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Similar to the above. But what if you don't know how many rows you will have from one table. I have 2 tables - and sometimes one of the tables has 2 rows and sometimes it has 5. But I still want to combine it all on 1 row. How could I modify the above query if I needed an unknow number of rows to go on the same row (up to 5 but could be none also)? Any help would be great.
> On Wednesday, October 22, 2008 7:47 PM JustinDo wrote:
> Hi
> I have a data that needs to be arranged from two rows into one row as follow:
>
> From two rows into -->
> -----------------------------------------
> AcctNo : Value 1 : Date 1
> -----------------------------------------
> 1234 : 10,000 : 1/1/08
> -----------------------------------------
> 1234 : 25,000 : 10/1/08
> -----------------------------------------
>
> One row -->
> -------------------------------------------------------------------------
> AcctNo : Value 1 : Date 1 : Value 2 : Date 2
> -------------------------------------------------------------------------
> 1234 : 10,000 : 1/1/08 : 25,000 : 10/1/08
> -------------------------------------------------------------------------
>
> I would appreciate for any feedback.
>
> Thanks.
>> On Wednesday, October 22, 2008 9:32 PM Plamen Ratchev wrote:
>> You can rank by date and then pivot based on the rank. Here is example
>> (SQL Server 2005/2008):
>>
>> CREATE TABLE AccountTransactions (
>> acc_nbr INT,
>> value DECIMAL(10, 2),
>> transaction_date DATETIME,
>> PRIMARY KEY (acc_nbr, transaction_date));
>>
>> INSERT INTO AccountTransactions VALUES (1234, 10000, '20080101');
>> INSERT INTO AccountTransactions VALUES (1234, 25000, '20081001');
>>
>> SELECT acc_nbr,
>> MAX(CASE WHEN rk = 1 THEN value END) AS value1,
>> MAX(CASE WHEN rk = 1 THEN transaction_date END) AS date1,
>> MAX(CASE WHEN rk = 2 THEN value END) AS value2,
>> MAX(CASE WHEN rk = 2 THEN transaction_date END) AS date2
>> FROM (SELECT acc_nbr, value, transaction_date,
>> ROW_NUMBER() OVER(PARTITION BY acc_nbr
>> ORDER BY transaction_date) AS rk
>> FROM AccountTransactions) AS A
>> GROUP BY acc_nbr;
>>
>>
>> --
>> Plamen Ratchev
>> http://www.SQLStudio.com
>>> On Thursday, October 23, 2008 12:42 PM JustinDo wrote:
>>> Hi Plamen,
>>>
>>> Thank you very much for the feedback.
>>> I am afraid that my sp is SQL 2000 version.
>>> Do you have suggestion how the SQL 2000 version's work?
>>> I would appreciate if you let me know.
>>>
>>> Thanks.
>>> Justin
>>>
>>> "Plamen Ratchev" wrote:
>>>> On Thursday, October 23, 2008 12:52 PM Plamen Ratchev wrote:
>>>> Here is how you can simulate the same on SQL Server 2000. But note
>>>> performance will not be great with large data set.
>>>>
>>>> SELECT acc_nbr,
>>>> MAX(CASE WHEN rk = 1 THEN value END) AS value1,
>>>> MAX(CASE WHEN rk = 1 THEN transaction_date END) AS date1,
>>>> MAX(CASE WHEN rk = 2 THEN value END) AS value2,
>>>> MAX(CASE WHEN rk = 2 THEN transaction_date END) AS date2
>>>> FROM (SELECT acc_nbr, value, transaction_date,
>>>> (SELECT COUNT(*)
>>>> FROM AccountTransactions AS B
>>>> WHERE B.acc_nbr = A.acc_nbr
>>>> AND B.transaction_date <= A.transaction_date) AS rk
>>>> FROM AccountTransactions AS A) AS A
>>>> GROUP BY acc_nbr;
>>>>
>>>> --
>>>> Plamen Ratchev
>>>> http://www.SQLStudio.com
>>>>> On Thursday, October 23, 2008 1:03 PM Tom Cooper wrote:
>>>>> One way (assuming that each acc_nbr has exactly two rows)
>>>>>
>>>>> SELECT a1.acc_nbr,
>>>>> a1.value AS value1,
>>>>> a1.transaction_date AS value2,
>>>>> a2.value AS value2,
>>>>> a2.transaction_date AS value2
>>>>> FROM AccountTransactions a1
>>>>> INNER JOIN AccountTransactions a2 ON a1.acc_nbr = a2.acc_nbr
>>>>> AND a1.transaction_date < a2.transaction_date;
>>>>>
>>>>> Tom
>>>>>
>>>>> "Justin Doh" wrote in message
>>>>>
>>>>>> On Thursday, October 23, 2008 2:17 PM JustinDo wrote:
>>>>>> Thank you so much!
>>>>>> It was great success.
>>>>>>
>>>>>> Appreciated for your help.
>>>>>>
>>>>>> "Plamen Ratchev" wrote:
>>>>>>> On Tuesday, May 26, 2009 10:42 AM Mohammad Shaik wrote:
>>>>>>> How should I merge two rows with different account number.The above example is similar to mine please help me with it.
>>>>>>>> On Tuesday, May 26, 2009 10:43 AM Muzz wrote:
>>>>>>>> How should I merge two rows with different account number.The above example is similar to mine please help me with it.
>>>>>>>>> On Monday, August 10, 2009 11:31 AM yeehaw mcgraw wrote:
>>>>>>>>> You are required to be a member to post replies. After logging in or becoming a member, you will be redirected back to this page.
>>>>>>>>>> On Wednesday, August 12, 2009 3:22 AM kiran wrote:
>>>>>>>>>> You are required to be a member to post replies. After logging in or becoming a member, you will be redirected back to this page.
>>>>>>>>>>> On Wednesday, August 12, 2009 3:23 AM kiran wrote:
>>>>>>>>>>> You are required to be a member to post replies. After logging in or becoming a member, you will be redirected back to this page.
>>>>>>>>>>> Submitted via EggHeadCafe
>>>>>>>>>>> WCF Generic DataContract object Serializer
>>>>>>>>>>> http://www.eggheadcafe.com/tutorials/aspnet/59ae2b9e-a3be-4cd5-a0ef-93...abbdc3a >> Stay informed about: How to combine two rows into one row with same ID? |
|
| Back to top |
|
 |  |
| Related Topics: | Combine multiple details rows into one column on master (s.. - I find this at: http://www.sqlservercurry.com/2008/06/combine-multiple-rows-into-one-row.html ================================== -- Query to combine multiple rows into one DECLARE @str VARCHAR(100) SELECT @str = COALESCE(@str + '|', '') + [Numbers]..
Excluding rows that have no child rows in a hireachy type .. - Hi All, I have inherited a badly designed database. ANyway, I have a table that is losely based on a hirearchy type table l2,l3,l4, relate to the hirearchy. l2 is the parent, l3 is first node/child, l4 is secont node/ grand child. What I want is to..
Converting Multiple Rows Into Single CSV Rows - Hi, Hoping someone could help me convert this data... ID Size 60203 M 60203 L 60203 XXL 30020 10 30020 8 ....into this... ID Size 60203 M,L,XXL 30020 10,8 Any help would be greatly appreciated. Thx, EightBall
Combine record? - I want a string that consist full account number as follow: account_num = 300.111000.10 But table only consist of the following: -gbmcu : 300 -gbobj : 111000 -gbsub : 10 So how to combine gbmcu,gbobj and gbsub into account_num? I had tried this..
Help -- Combine these 2 SProcs.. - I need advice on how to combine these 2 seperate SProcs -- I'd like to be able to create 1 ADOrecordset from them (a VB Application). I am having difficulty determining how to: 1). Return "TotCollected = SUM(S.female)" for each unique "... |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|