Himu\’s Attempt at Blogging

Tidbits from my thoughts

Posts Tagged ‘date range

SQL – Dynamic Date Range, Number Range in Oracle, SQL Server, and DB2/400

with 9 comments

Often, we need to make coverage reports that span over a period of time and where all points in the period do not contain data.

Assume a shop has 50 outlets and we want to find the number of sales in each branch over the past ten days. Business is bad in some areas and so some outlets had no sale at all. A query like the following will give only those outlets that had sales but not a list of all outlets:

SELECT outletid, saledate, count(*)
FROM sales
GROUP BY outletid, saledate
ORDER BY outletid, saledate

Read the rest of this entry »


Written by mhimu

May 7, 2009 at 11:03 am