Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a query where
a particular column value is required as of a particular date whereas some of the other column values are required as of between two dates
e.g.
SQL
select t1.col1, t2.col2, SUM(col3) 
from my_table1 inner join my_table2 on  t1.id = t2.id
where [Date] between '2014-01-01' and '2014-02-11'
group by t1.col1, t2.col2

Now, I need another column i.e. col4 which required only as of a particular date

i.e. if I were to write a separate query it would be :
SQL
select t2.col4 from my_table1 inner join my_table2 on  t1.id = t2.id
where [Date] = '2014-02-11'

Is there any way both of these could be achieved using a single query?
Any help would be much appreciated.
Posted
Updated 11-Feb-14 17:43pm
v3
Comments
chaau 12-Feb-14 0:20am    
Is this col4 required to be selected as a fourth column in your query, or in place of one of already selected columns (i.e. col2)?
aakar 12-Feb-14 1:36am    
yes col4 is required to be selected as the fourth column

you can use CTE with this to achieve it easily.
 
Share this answer
 
I'd do it like this:
select t1.col1, t2.col2, SUM(col3),
case when [Date] = '2014-02-11' then col4 else NULL end
from my_table1 inner join my_table2 on  t1.id = t2.id
where [Date] between '2014-01-01' and '2014-02-11'
group by t1.col1, t2.col2, case when [Date] = '2014-02-11' then col4 else NULL end
 
Share this answer
 

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