Sql Query To Return Nil For Dates Not Present In The Table
I have a table 'my_table'. It has the following data : ID --- Date 1 --- 01/30/2012 2 --- 01/30/2012 3 --- 05/30/2012 I can write a SQL query to re
Solution 1:
The way I do it is to have a static table with list of all the dates. In your case that's 30th of each month (what about February?). Lets call this table REF_DATE. It has a single column DT that holds the date.
Assuming that my_table only contains 0 or at most 1 distinct date (30th) in each month, what you need to do is:
select DT,count(ID) from
REF_DT REFleftouterjoin my_table MT
on REF.DT=my_table.DATE
groupby REF.DT;
Solution 2:
I came up with somewhat hackish way through rails
classMonth<Date # for getting months in rangedefsuccself >> 1endend
range = Month.new(2010,1,1)..Month.new(2013,1,1) # range of date to query
months=Hash.new
(range).each do|month|
months.merge!({month.to_s => 0}) # get all months as per range requirement of projectend
db_months = MyTable.find_all_by_date(range).group_by{ |u| u.date.beginning_of_month }.map{|m,v| [m.to_s , v.size]} #get all records grouped by months
all_months = months.merge(Hash[db_months]) # merge all missing monthsReplace the range with the dates you want also the format of the date as per your requirement.
Post a Comment for "Sql Query To Return Nil For Dates Not Present In The Table"