Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to print a record against a value that doesn't exist?

$
0
0

I am trying an INNER JOIN on two columns of date type.

I need to compare the two columns and find out if one of the column doesn't have a particular date. 

In case, there is a mismatch - I want to print the date that is missing and print 0 in the corresponding row element.

for example:-

original set

2013-07-17 00:00:00.00076505
2013-07-16 00:00:00.00038617
2013-07-15 00:00:00.00037117
2013-07-14 00:00:00.000309320
2013-07-12 00:00:00.000387583

intended result set

2013-07-17 00:00:00.00076505
2013-07-16 00:00:00.00038617
2013-07-15 00:00:00.00037117
2013-07-14 00:00:00.000309320
2013-07-13 00:00:00.000       0             -- insert this record
2013-07-12 00:00:00.000387583


How best can the following query be modified?

SELECT DISTINCT ex, 

                countnum 
FROM   (SELECT Dateadd(dd, 0, Datediff(dd, 0, extract_date)) AS ex, 
               Count(*)                                      AS countnum 
        FROM   ods..o_cmhistory_idx(nolock) 
        WHERE  extract_date >= Dateadd(dd, -20, Getdate()) 
        GROUP  BY extract_date) AS tr1 
       INNER JOIN (SELECT TOP 20 Dateadd(dd, 0, Datediff(dd, 0, date)) AS 
                                 display_date 
                   FROM   warehouse..dim_date 
                   WHERE  date <= Cast(Getdate() AS DATE) 
                   ORDER  BY date DESC) AS tr2 
               ON tr1.ex = tr2.display_date 
ORDER  BY ex DESC 

Is there a way, the above query can be simplified to get the desired result set?



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>