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.

asked Feb 19, 2019 at 11:34
2
  • Something tells me 2018 is not a valid Month() Commented Feb 19, 2019 at 11:48
  • @MichaelKutz - so true :) Thank you. It's edited. Commented Feb 19, 2019 at 11:51

1 Answer 1

1

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.

answered Feb 19, 2019 at 17:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.