Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Qeury for data in date range; have result include missing dates in range.

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-02-19 : 23:57:06
bryan writes "First of all- you guys are the greatest. I do a little programming/sql queries/web stuff on the side and your site provides an invaluable source of information. thank you.

on to my question- perhaps the subject I entered above is not quite clear. I'm trying to get my SQL query to add some missing data to a particular query.

Say I have a simple table w/two fields, Date and CarsSold. A record only exists if one or more cars were sold on a particular day. I want to query the database for a range of dates and then plot them. I want my plot to show every day, even days where no cars were sold. To make this easy on me, I need to get back every day in my range (say Feb 3rd through Feb 7th), even if no cars were sold (and therefore no entry in the table).

Is there any way to query the table so it comes back with a record for every day in the range, even if a particular day does not exist in the database (assign a '0' to the accompaning 'CarsSold' field)?

e.g. query for date and CarsSold from 2/3/01 through 2/7/01 inclusive.

data in table:
Date   CarsSold
2/1/01 9
2/4/01 4
2/6/01 1


Query would return:
2/3/01  0
2/4/01 4
2/5/01 0
2/6/01 1
2/7/01 0


Thanks again, Bryan"
   

- Advertisement -