Author |
Topic |
KabirPatel
Yak Posting Veteran
54 Posts |
Posted - 2006-11-14 : 15:02:59
|
I have data in the following table:FactoryID FactoryJob-------------------------------------Factory1 No1Factory1 No2Factory2 No1Factory3 No3Factory3 No4Factory3 No5and I would like the data out in the format of:FactoryID FactoryJob1 FactoryJob2 FactoryJob3--------------------------------------------------------------Factory1 No1 No2 NULLFactory2 No1 NULL NULLFactory3 No3 No4 No5Is there an easy way to do this withouth using a cursor?Cheers,Kabir |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-14 : 15:18:52
|
[code]-- prepare test datadeclare @test table (FactoryID varchar(50), FactoryJob varchar(10))insert @testselect 'Factory1', 'No1' union allselect 'Factory1', 'No2' union allselect 'Factory2', 'No1' union allselect 'Factory3', 'No3' union allselect 'Factory3', 'No4' union allselect 'Factory3', 'No5'-- do the workselect factoryid, max(case when seq = 1 then factoryjob end) AS FactoryJob1, max(case when seq = 2 then factoryjob end) AS FactoryJob2, max(case when seq = 3 then factoryjob end) AS FactoryJob3from ( select t.factoryid, t.factoryjob, (select count(*) from @test y where y.factoryid = t.factoryid and cast(substring(y.factoryjob, 3, 8000) as int) <= cast(substring(t.factoryjob, 3, 8000) as int)) AS Seq from @test t ) dgroup by factoryid[/code]Peter LarssonHelsingborg, Sweden |
 |
|
KabirPatel
Yak Posting Veteran
54 Posts |
Posted - 2006-11-15 : 08:23:18
|
Thanks for that.My only problem is that the FactoryJob value is usually not a number (I just used simple examples previously).So my dataset would more likely look like this:FactoryID FactoryJob-------------------------------------Factory1 ABCFactory1 CDQFactory2 ABCFactory3 QWEFactory3 ERTFactory3 TYUIs there an easy way to get this data out in the format of:FactoryID FactoryJob1 FactoryJob2 FactoryJob3--------------------------------------------------------------Factory1 ABC CDQ NULLFactory2 ABC NULL NULLFactory3 QWE ERT TYUCheers,Kabir |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-15 : 09:16:29
|
How hard can it be?-- do the workselect factoryid, max(case when seq = 1 then factoryjob end) AS FactoryJob1, max(case when seq = 2 then factoryjob end) AS FactoryJob2, max(case when seq = 3 then factoryjob end) AS FactoryJob3from ( select t.factoryid, t.factoryjob, (select count(*) from @test y where y.factoryid = t.factoryid and y.factoryjob <= t.factoryjob) AS Seq from @test t ) dgroup by factoryid Peter LarssonHelsingborg, Sweden |
 |
|
KabirPatel
Yak Posting Veteran
54 Posts |
Posted - 2006-11-16 : 05:01:51
|
Thanks for that.Is it possible to extend the example further so that it caters for the following scenario:FactoryID FactoryJob FactorySubJob-------------------------------------Factory1 ABC FRTFactory1 CDQ NULLFactory2 ABC NULLFactory3 QWE DDDFactory3 ERT WWWFactory3 TYU NULLand gives the following outputFactoryID FactoryJob1 FactoryJob2 FactoryJob3 FactoryJob4 FactoryJob5--------------------------------------------------------------Factory1 ABC CDQ FRT NULL NULLFactory2 ABC NULL NULL NULL NULLFactory3 QWE ERT TYU DDD WWWAs you can probably tell I am not very good at SQL (I am a VB programmer - so I always use cursors..). As such your help is much appreciated.Cheers,Kabir |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-16 : 05:05:40
|
Put in some effort yourself, god damn-it! How hard can it be?How do you think you will survive professionally if you don't take the time to study and understand the suggested code we provide?-- do the workselect factoryid, max(case when seq = 1 then factoryjob end) AS FactoryJob1, max(case when seq = 2 then factoryjob end) AS FactoryJob2, max(case when seq = 3 then factoryjob end) AS FactoryJob3, max(case when seq = 4 then factoryjob end) AS FactoryJob4, max(case when seq = 5 then factoryjob end) AS FactoryJob5, ...from ( select t.factoryid, t.factoryjob, (select count(*) from @test y where y.factoryid = t.factoryid and y.factoryjob <= t.factoryjob) AS Seq from @test t union all select t.factoryid, t.factorysubjob, (select count(*) from @test y where y.factoryid = t.factoryid and y.factorysubjob <= t.factorysubjob) + (select count(*) from @test y where y.factoryid = t.factoryid) from @test t ) dgroup by factoryid If you truly want this to be done dynamically, learn about PIVOT and CROSS TAB reports in the excellent aid BOOKS ONLINE. And GOOGLE.BTW, your table design is horrible. You should consider make a change to normalized data.Peter LarssonHelsingborg, Sweden |
 |
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2006-11-16 : 08:12:54
|
Patronizing Saint of Lost Yaks Jay White |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
KabirPatel
Yak Posting Veteran
54 Posts |
Posted - 2006-11-17 : 05:30:18
|
Thanks for that.I am studying, but its taking me time to get my head around SQL....Cheers,KabirPS Do you recommend any good books - I have been told Celko's books are good. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-11-17 : 05:57:48
|
http://sqlteam.com/store.aspMadhivananFailing to plan is Planning to fail |
 |
|
|