Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Madam/Sir,
Good day,

I have table with 11 column and I want to change column name of 10 of them to yesterday date - before yesterday date ..... etc

the table structure is :


-------------------------------------------------------------------------------------
name | column 1 | | column 2 | | column 3 | | column 4 | | column 5 | | column 6 |
-------------------------------------------------------------------------------------
var1 | 159 | | 200 | | 300 | | 179 | | 410 | | 999 |


P.S the table structure after excute query below :

SQL
SELECT DISTINCT 
   case Name when '123' then 'var1'   when   else name end as name,
        sum(CASE WHEN  cast (dbo.fa_ConvertToDateTime(TIMESTAMP_S) as date) =  cast (DATEADD(dd, -1, GETDATE()) as date) THEN cast (ta.VALUE as float) ELSE 0 END) as getdate ,   
        sum(CASE WHEN  cast (dbo.fa_ConvertToDateTime(TIMESTAMP_S) as date) =  cast (DATEADD(dd, -2, GETDATE()) as date) THEN cast (ta.VALUE as float) ELSE 0 END),
		sum(CASE WHEN  cast (dbo.fa_ConvertToDateTime(TIMESTAMP_S) as date) =  cast (DATEADD(dd, -3, GETDATE()) as date) THEN cast (ta.VALUE as float) ELSE 0 END)   ,
		sum(CASE WHEN  cast (dbo.fa_ConvertToDateTime(TIMESTAMP_S) as date) =  cast (DATEADD(dd, -4, GETDATE()) as date) THEN cast (Ta.VALUE as float) ELSE 0 END) ,
		sum(CASE WHEN  cast (dbo.fa_ConvertToDateTime(TIMESTAMP_S) as date) =  cast (DATEADD(dd, -5, GETDATE()) as date) THEN cast (Ta.VALUE as float) ELSE 0 END)  ,
		sum(CASE WHEN  cast (dbo.fa_ConvertToDateTime(TIMESTAMP_S) as date) =  cast (DATEADD(dd, -6, GETDATE()) as date) THEN cast (Ta.VALUE as float) ELSE 0 END)   ,
		sum(CASE WHEN  cast (dbo.fa_ConvertToDateTime(TIMESTAMP_S) as date) =  cast (DATEADD(dd, -7, GETDATE()) as date) THEN cast (Ta.VALUE as float) ELSE 0 END)   ,
		sum(CASE WHEN  cast (dbo.fa_ConvertToDateTime(TIMESTAMP_S) as date) =  cast (DATEADD(dd, -8, GETDATE()) as date) THEN cast (Ta.VALUE as float) ELSE 0 END)   ,
		sum(CASE WHEN  cast (dbo.fa_ConvertToDateTime(TIMESTAMP_S) as date) =  cast (DATEADD(dd, -9, GETDATE()) as date) THEN cast (Ta.VALUE as float) ELSE 0 END)   ,
		sum(CASE WHEN  cast (dbo.fa_ConvertToDateTime(TIMESTAMP_S) as date) =  cast (DATEADD(dd, -10, GETDATE()) as date) THEN cast (Ta.VALUE as float) ELSE 0 END)   

        FROM tabel name 
GROUP BY  name


What I have tried:

the result now
-------------------------------------------------------------------------------------
name | column 1 | | column 2 | | column 3 | | column 4 | | column 5 | | column 6 |
-------------------------------------------------------------------------------------
var1 | 159 | | 200 | | 300 | | 179 | | 410 | | 999 |


and what we expect is :
------------------------------------------------------------------------------------------------------
name | 20/6/2017 | | 19/6/2017 | | 18/6/2017 | | 17/6/2017 | | 16/6/2017 | | 15/6/2017 |
------------------------------------------------------------------------------------------------------
var1 | 159 | | 200 | | 300 | | 179 | | 410 | | 999 |



I tried to create varible
SQL
declare @1  date = DATEADD(dd, -1, GETDATE())


and make the result of this varible as a column name

SQL
sum(CASE WHEN  cast (dbo.fa_ConvertToDateTime(TIMESTAMP_S) as date) =  cast (DATEADD(dd, -2, GETDATE()) as date) THEN cast (ta.VALUE as float) ELSE 0 END) as (Select @1),


I belive its wrong and I couldn't find the way to do it.

thanks in advanse.

have a good day,
Posted
Updated 20-Jun-17 22:59pm
Comments
Andy Lanng 20-Jun-17 8:48am    
I think your best option is to use pivot :
Using PIVOT and UNPIVOT[^]
Jerji Rani 20-Jun-17 9:05am    
Thank you for your help , would you please show me an example like my code?

thank you.
Jerji Rani 20-Jun-17 9:09am    
I am writing this code in SQL and move it to dashboard software to draw it and only by using this way ( more than one column ) I can draw the chart.
thank you.
Kornfeld Eliyahu Peter 20-Jun-17 9:03am    
1. After AS you can use only literals...
2. Left SQL out of it... As it is for the UI, do it in the UI
Jerji Rani 20-Jun-17 9:07am    
thank you for your support,
Acutely i am writing this code in SQL and move it to dashboard software to draw it in charts.is it possible in my case to use UI ?
thank you for your support,

1 solution

I solve it guys thank you for your help.

have a good day.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 21-Jun-17 5:04am    
And where is the solution?
Jerji Rani 21-Jun-17 8:45am    
I changed the structure of output now its like :
2017-06-21 sum(xxx) sum(yyy) ....
2017-06-20 sum(xxx) sum(yyy) ....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900