SQL Server - Help with a SQL query. - Asked By Aryan Bhatt on 11-Feb-14 11:02 PM



Help with SQL query.

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.

select t1.col1, t2.col2, SUM(col3)
from my_table1 inner join my_table2 on  t1.id = t2.id
where [Date] between '2026-01-01' and '2026-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 :

select t2.col4 from my_table1 inner join my_table2 on  t1.id = t2.id
where [Date] = '2026-02-11'

Is there any way both of these could be achieved using a single query?

Any help would be much appreciated.


Robbe Morris replied to Aryan Bhatt on 12-Feb-14 08:32 AM
If your query were to be combined into one result set, would the results really be what you'd want?

case #1 would yield you a row with col4 value being null

col1Value, col2Value, col3Value, null

case #2 would yield you a second row with the exact reverse

null, null, null, col4value

Do you really want multiple disparate rows?  I would think that would throw off your reports.