Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Kalaiselvan
Posting Yak Master
112 Posts |
Posted - 2011-03-30 : 04:06:07
|
| I have used a select query results as Price_Act Price_FC Money_New Money_OldPrice_act 148.7774 147.1522 58721.94 59777.26Price_FC 148.7774 147.1522 58721.94 59777.26Money_new 148.7774 147.1522 58721.94 59777.26Money_old 148.7774 147.1522 58721.94 59777.26I need the above output as Act FCPrice 148.7774 147.1522Money 58721.94 59777.26Please help me in this. Very urgent...Regards,Kalaiselvan RLove Yourself First.... |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-03-30 : 04:13:53
|
| In the desired results you list 2 column headers but three columns. Which?What's the logic behind the values? What determines what goes into what column?--Gail ShawSQL Server MVP |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-30 : 04:24:53
|
- please use [code] tag to format the data- can you explain how the required output come about ?- you might want to re-post a more meaningful sample data, the sample data that you posted have only 2 distinct value. Without clear explanation, it is very hard for anyone to guess what do you want. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Kalaiselvan
Posting Yak Master
112 Posts |
Posted - 2011-03-30 : 04:25:58
|
| Groups Price_Act Price_FC Money_New Money_OldPrice_act 148.7774 147.1522 58721.94 59777.26Price_FC 148.7774 147.1522 58721.94 59777.26Money_new 148.7774 147.1522 58721.94 59777.26Money_old 148.7774 147.1522 58721.94 59777.26Groups Act FCPrice 148.7774 147.1522Money 58721.94 59777.26Now its clear.Regards,Kalaiselvan RLove Yourself First.... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-30 : 04:33:55
|
Still not very clear.In your output, you have Price 148.7774 under the Act column, i know it is coming from the Price_Act column of the source table but which row ?And how about money ? there isn't a Act & FC in the source table KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Kalaiselvan
Posting Yak Master
112 Posts |
Posted - 2011-03-30 : 04:47:57
|
| SELECT * FROM dbo.M_GroupingValues Where CountryID=1 and CategoryID in (1) Output:[ValueNAmes] [Country] [Category]Volume_actual 1 1Volume_old_forecast 1 1Price_actual 1 1Price_old_forecast 1 1Money Supply_new 1 1Money Supply_old 1 1----------------------------------------------------------------------SELECT Country,Category,Volume_actual,Volume_old_forecast,Price_actual,Price_old_forecast,[Money Supply_New],[Money Supply_old] FROM M_Working Where Country=1 and Category=1 and UploadQuarterId=2 and Quarter=3 and Year=10Output:[Country] [Category] [Volume_Actual] [Volume_Old_Forecast] 1 1 158174416 158619377[Price_Actual] [Price_Old_Forecast] [Money Supply_New] 148.7774 147.1522 58721.94[Money Supply_Old] 59777.26From the above selected Queries i need the output as below:[ValueNames] [Actual] [Forecast] Volume 158174416 158619377 Price 148.7774 147.1522 Money Supply 58721.94 59777.26Here the Valuenames column comes from 1st select query (where as Volume_Act and Volume_Forecast must come as Volume) repeats for othersAnd Actual value and Forecast value comes from 2nd Select query related Actual and Forecast amount. Money Supply_Newis actual and Money Supply_Old is forecast. Is this clear now, revert me back if any calrifications. But am in urgent to finish this.. Regards,Kalaiselvan RLove Yourself First.... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-30 : 05:00:37
|
trySELECT g.ValueNAmes, v.ACT, v.FCFROM ( SELECT DISTINCT ValueNAmes = LEFT(ValueNAmes, charindex('_', ValueNAmes) - 1), Country, Category FROM M_GroupingValues WHERE Country = 1 and Category = 1 ) g INNER JOIN ( SELECT Country, Category, Volume_actual as ACT, Volume_old_forecast as FC FROM M_Working Where UploadQuarterId=2 and Quarter=3 and Year=10 union all SELECT Country, Category, Price_actual as ACT, Price_old_forecast as FC FROM M_Working Where UploadQuarterId=2 and Quarter=3 and Year=10 union all SELECT Country, Category, [Money Supply_New],[Money Supply_old] FROM M_Working Where UploadQuarterId=2 and Quarter=3 and Year=10 ) v on g.Country = v.Country and g.Category = v.Category KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Kalaiselvan
Posting Yak Master
112 Posts |
Posted - 2011-03-30 : 05:57:38
|
| Khtan, Not bad. But wat u have replied is already done here. But the output will be repeated for all columns and rows. For your Query Output comes as :[Value Names] [Actual] [Forecast]Money Supply 158174416 158619377Price 158174416 158619377Volume 158174416 158619377Money Supply 148.7774 147.1522Price 148.7774 147.1522Volume 148.7774 147.1522Money Supply 58721.94 59777.26Price 58721.94 59777.26Volume 58721.94 59777.26But i need the Output as:[Value Names] [Actual] [Forecast]Volume 158174416 158619377Price 148.7774 147.1522Money Supply 58721.94 59777.26Regards,Kalaiselvan RLove Yourself First.... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-30 : 06:31:26
|
[code]SELECT g.ValueNAmes, v.ACT, v.FCFROM ( SELECT DISTINCT ValueNAmes = LEFT(ValueNAmes, charindex('_', ValueNAmes) - 1), Country, Category FROM M_GroupingValues WHERE Country = 1 and Category = 1 ) g INNER JOIN ( SELECT Country, Category, Volume_Actual as ACT, Volume_Old_Forecast as FC, ValueNAmes = 'Volume' FROM M_Working Where UploadQuarterId=2 and Quarter=3 and Year=10 union all SELECT Country, Category, Price_Actual as ACT, Price_Old_Forecast as FC, ValueNAmes = 'Price' FROM M_Working Where UploadQuarterId=2 and Quarter=3 and Year=10 union all SELECT Country, Category, [Money Supply_New], [Money Supply_Old], ValueNAmes = 'Money Supply' FROM M_Working Where UploadQuarterId=2 and Quarter=3 and Year=10 ) v on g.Country = v.Country and g.Category = v.Category and g.ValueNAmes = v.ValueNAmes[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|