| Author |
Topic |
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2011-09-15 : 05:49:05
|
| Hi All i have data like this..ID Name1 Price1 Name2 Price2 Name3 Price31 ABC 10.00 DEF 20.00 GHI 30.00I need output like..ID Name Price1 ABC 10.001 DEF 20.001 GHI 30.00iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-15 : 05:53:18
|
| [code]select ID,Name1,Price1 from tableunion allselect ID,Name2,Price2 from tableunion allselect ID,Name3,Price3 from table[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2011-09-15 : 06:00:44
|
| We cant use union all in it.Can i do this using UNPIVOT or something using crosstab queriesiF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2011-09-15 : 07:26:51
|
| IS there any way..??iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-15 : 07:31:14
|
quote: Originally posted by ashishashish We cant use union all in it.Can i do this using UNPIVOT or something using crosstab queriesiF theRe iS a wAy iN tHen theRe iS a wAy oUt..
why so?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2011-09-15 : 07:32:42
|
| because this is a bad practice i think.iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-15 : 07:36:55
|
here's another waySELECT m.ID,m.Val AS Name,n.Val AS PriceFROM(SELECT ID,Val FROM (SELECT ID,Name1,Name2,Name3 FROM table)t1UNPIVOT(Val FOR Name IN ([Name1],[Name2],[Name3]))u1)mINNER JOIN (SELECT ID,Val FROM (SELECT ID,Price1,Price2,Price3 FROM table)t2UNPIVOT(Val FOR Price IN ([Price1],[Price2],[Price3]))u2)nON n.ID = m.ID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2011-09-15 : 07:44:35
|
| Its not giving the right results. As if we join on ID than all names with all prices get included in the query result the partial result set from your query is...1 ABC 10.001 DEF 10.001 GHI 10.001 ABC 20.001 DEF 20.001 GHI 20.001 ABC 30.001 DEF 30.001 GHI 30.00iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-15 : 07:46:51
|
| do you have multiple records for same ID value in your original table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2011-09-15 : 07:49:36
|
| No i dont have multiple records in my original table but when we unpivot this than we will get multiple records and on these multiple records we are joining i think.iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-15 : 07:52:19
|
| [code]SELECT m.ID,m.Val AS Name,n.Val AS PriceFROM(SELECT ROW_NUMBER() OVER (PRTITION BY ID ORDER BY ID) AS Seq,ID,Val FROM (SELECT ID,Name1,Name2,Name3 FROM table)t1UNPIVOT(Val FOR Name IN ([Name1],[Name2],[Name3]))u1)mINNER JOIN (SELECT ROW_NUMBER() OVER (PRTITION BY ID ORDER BY ID) AS Seq,ID,Val FROM (SELECT ID,Price1,Price2,Price3 FROM table)t2UNPIVOT(Val FOR Price IN ([Price1],[Price2],[Price3]))u2)nON n.ID = m.IDAND n.Seq=m.Seq[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2011-09-15 : 07:59:24
|
| Thanks... it helped a lot.Thanks for your support and patience.iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2011-09-15 : 10:21:12
|
quote: Originally posted by ashishashish because this is a bad practice i think.
...and that's the point where I would have stopped trying to help.________________________________________________If it is not practically useful, then it is practically useless.________________________________________________ |
 |
|
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2011-09-15 : 13:21:24
|
| SELECT m.ID,m.Val AS Name,n.Val AS PriceFROM(SELECT ROW_NUMBER() OVER (PRTITION BY ID ORDER BY ID) AS Seq,ID,Val FROM (SELECT ID,Name1,Name2,Name3 FROM table)t1UNPIVOT(Val FOR Name IN ([Name1],[Name2],[Name3]))u1)mINNER JOIN (SELECT ROW_NUMBER() OVER (PRTITION BY ID ORDER BY ID) AS Seq,ID,Val FROM (SELECT ID,Price1,Price2,Price3 FROM table)t2UNPIVOT(Val FOR Price IN ([Price1],[Price2],[Price3]))u2)nON n.ID = m.IDAND n.Seq=m.SeqUsing cross join for better performance. |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2011-09-16 : 01:35:28
|
quote: Originally posted by blindman
quote: Originally posted by ashishashish because this is a bad practice i think.
...and that's the point where I would have stopped trying to help.________________________________________________If it is not practically useful, then it is practically useless.________________________________________________
Sir,I think you are there to help others and everybody knows that using UNION ALL method is not preferable, if you think that u should stop helping others others because somebody wants or like to have some other approach to solve the problem than it is definitely your choice you can stop sharing or helping peoples anytime you want.iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2011-09-16 : 11:43:37
|
quote: Originally posted by ashishashish...everybody knows that using UNION ALL method is not preferable
Sir, you are completely mistaken in this. Which was the point of my post.________________________________________________If it is not practically useful, then it is practically useless.________________________________________________ |
 |
|
|
|