Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 10th, 2004, 03:30 AM
Authorized User
 
Join Date: Jun 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to Update row to differenct column

Hi,

I have a table as this

whs item_no cust_no seq_no value
11 A001 a001 1 1000
11 a001 a001 2 2000
11 a001 a001 3 3000

i need to update value to this table as this

whs item_no cust_no value1 value2 value3
11 a001 a001 1000 2000 3000


what are SQL statement should take ?



Thanks :)

Jane
 
Old December 10th, 2004, 07:51 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

HI,

If you know that the Max Seq_no is 3 then here is the query for you

---------------------------------------
SELECT whs,item_no,cust_no,
    Value1= ISNULL((SELECT Value FROM TBL WHERE seq_no = 1 AND whs=a.whs and item_no=a.item_no and cust_no=a.cust_no),0),

    Value2= ISNULL((SELECT Value FROM TBL WHERE seq_no = 2 AND whs=a.whs and item_no=a.item_no and cust_no=a.cust_no),0),

    Value3= ISNULL((SELECT Value FROM TBL WHERE seq_no = 3 AND whs=a.whs and item_no=a.item_no and cust_no=a.cust_no),0),

FROM TBL a
GROUP BY col1,col2
-----------------------------------

If you don't know the max of seq_no, you may want to declare a variable and store the MAX(SEQ_NO) with grouping on whs,item_no,cust_no. Then u can use a WHILE LOOP to generate the above query. Same Logic will be working. You can Generate teh SQL Statement dynamically storing into a Varchar variable and execute one by one using UNION ALL .

Hope you got my point.

Gud luck




B. Anant
 
Old December 10th, 2004, 07:53 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

HI again,

Please remove the '," from last colum...here it is---------
---------------------------------

SELECT whs,item_no,cust_no,
    Value1= ISNULL((SELECT Value FROM TBL WHERE seq_no = 1 AND whs=a.whs and item_no=a.item_no and cust_no=a.cust_no),0),

    Value2= ISNULL((SELECT Value FROM TBL WHERE seq_no = 2 AND whs=a.whs and item_no=a.item_no and cust_no=a.cust_no),0),

    Value3= ISNULL((SELECT Value FROM TBL WHERE seq_no = 3 AND whs=a.whs and item_no=a.item_no and cust_no=a.cust_no),0)

FROM TBL a
GROUP BY col1,col2
-------------------------------------

B. Anant
 
Old December 10th, 2004, 07:55 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

Hey,

Sorry, again I forgot something. This one is correct
--------------------------------------

SELECT whs,item_no,cust_no,
    Value1= ISNULL((SELECT Value FROM TBL WHERE seq_no = 1 AND whs=a.whs and item_no=a.item_no and cust_no=a.cust_no),0),

    Value2= ISNULL((SELECT Value FROM TBL WHERE seq_no = 2 AND whs=a.whs and item_no=a.item_no and cust_no=a.cust_no),0),

    Value3= ISNULL((SELECT Value FROM TBL WHERE seq_no = 3 AND whs=a.whs and item_no=a.item_no and cust_no=a.cust_no),0),

FROM TBL a
GROUP BY whs,item_no,cust_no

B. Anant
 
Old December 12th, 2004, 08:55 PM
Authorized User
 
Join Date: Jun 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
in fact i have two table


table A is as below:


whs item_no cust_no seq_no value
11 A001 a001 1 1000
11 a001 a001 2 2000
11 a001 a001 3 3000


then Table B is the value from table A update to Table B';
whs item_no cust_no value1 value2 value3
11 a001 a001 1000 2000 3000


is using the same SQL command as you recommended?

pls advised


Thanks


Jane





Similar Threads
Thread Thread Starter Forum Replies Last Post
find the FIRST USED row/column? crmpicco Excel VBA 3 July 23rd, 2013 12:52 PM
Column to Row [email protected] Excel VBA 2 March 23rd, 2007 04:16 AM
Column to Row [email protected] SQL Server DTS 0 March 1st, 2007 10:57 AM
How to get the last row and last column value ramk_1978 SQL Server 2000 1 April 4th, 2005 06:34 PM
Read a column and Search Row by Row in another col AyatKh Excel VBA 2 January 26th, 2005 12:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.