1

I have the following statetment:

 Select a.Data AS Data_A,b.Data AS Data_B,
(Select Round(Sum(C.Value)) From Values c  
  Where 
  (Year(c.TimeStamp) = x) and 
  (Month(c.TimeStamp) = y)
) AS MonthYear

From Table1 a  Left Join Table2 b on (a.ID = b.ID)

Ist there any way to add a column to the select clause based on my desired range of date? (where Year(c.TimeStamp) < 2019 and Month(c.TimeStamp) <= 12)

Example1 - i want to see the data for 1 month:

Select a.Data AS Data_A,b.Data AS Data_B,
(Select Round(Sum(C.Value)) From Values c  
  Where 
  (Year(c.TimeStamp) = 2018) and 
  (Month(c.TimeStamp) = 1)
) AS Jan2018

From Table1 a  Left Join Table2 b on (a.ID = b.ID)

Example2: i want to see the data for 3 months

Select a.Data AS Data_A,b.Data AS Data_B,
(Select Round(Sum(C.Value)) From Values c  
  Where 
  (Year(c.TimeStamp) = 2018) and 
  (Month(c.TimeStamp) = 1)
) AS Jan2018,
(Select Round(Sum(C.Value)) From Values c  
  Where 
  (Year(c.TimeStamp) = 2018) and 
  (Month(c.TimeStamp) = 2)
) AS Feb2018,
(Select Round(Sum(C.Value)) From Values c  
  Where 
  (Year(c.TimeStamp) = 2018) and 
  (Month(c.TimeStamp) = 3)
) AS Mar2018

From Table1 a  Left Join Table2 b on (a.ID = b.ID)

I hope my question is understandable. :)

Thank you for reading.

  • Something tells me 2018 is not a valid Month() – Michael Kutz Feb 19 at 11:48
  • @MichaelKutz - so true :) Thank you. It's edited. – Huppel Feb 19 at 11:51
0

You must construct different queries to get 1 column of output versus 3 columns. That is best done in your application language. If you are trying to do it entirely in SQL, it would require a messy Stored Procedure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.