Author |
Topic |
konark
Yak Posting Veteran
60 Posts |
Posted - 2010-11-08 : 17:10:50
|
Create table dbo.Sales( sales_id int , sku varchar(10) , item varchar(10) , creation_date datetime , update_date datetime) Insert into dbo.Sales select 1, 'ABC123', 'IceCream', GETDATE()-5, GETDATE()-5 unionselect 2, 'ABC123', 'Chocolate', GETDATE()-5, GETDATE()-5 unionselect 3, 'ABC123', 'Chocolate', GETDATE()-4, GETDATE()-5 unionselect 4, 'ABC123', 'Candy', GETDATE()-4, GETDATE()-4 unionselect 5, 'ABC123', 'Candy', GETDATE()-4, GETDATE()-4 unionselect 6, 'ABC123', 'Candy', GETDATE()-2, GETDATE()-2 unionselect 7, 'ABC123', 'IceCream', GETDATE()-1, GETDATE()-1 unionselect 7, 'ABC123', 'Chocolate', GETDATE()-1, GETDATE()-1 unionselect 7, 'ABC123', 'Candy', GETDATE()-1, GETDATE()-1 Need to go back dbo.sales for last 5 days and fine the Creation_date day when we didnt recive any entry into the table.In the above example. we didnt have any entry on getdate()-3 .Can you please advise.Chandragupta MouryaTakhyashila |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2010-11-08 : 17:54:06
|
1. define missing day, a business day, weekend included etc2. define your week (Mon-fri? mon-sun? etc) business days3. define your first of business week (Sunday or Monday)4. you might need a calendar table (http://codeinet.blogspot.com/2006/08/auxiliary-calendar-table-for-sql.html)If you don't have the passion to help people, you have no passion |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-11-08 : 18:46:00
|
As Yosiaaz suggests you probably want to use some sort of Date table. But, here is one way using an in-line tally table that might get you going:;WITH Tens (N) AS (SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9), Thousands(N) AS (SELECT t1.N FROM Tens t1 CROSS JOIN Tens t2 CROSS JOIN Tens t3), Millions (N) AS (SELECT t1.N FROM Thousands t1 CROSS JOIN Thousands t2), Tally (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) FROM Millions)SELECT *FROM ( SELECT DATEADD(DAY, N * -1, CAST(CURRENT_TIMESTAMP AS DATE)) AS StartDate ,DATEADD(DAY, (N * -1) + 1, CAST(CURRENT_TIMESTAMP AS DATE)) AS EndDate FROM Tally WHERE N BETWEEN 1 AND 5 ) AS TLEFT OUTER JOIN dbo.Sales ON Sales.creation_date >= StartDate AND Sales.creation_date < EndDateWHERE Sales.sales_id IS NULL |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-09 : 04:03:08
|
We have had this two days ago like this:declare @Sales table ( sales_id int , sku varchar(10) , item varchar(10) , creation_date datetime , update_date datetime) Insert into @Sales select 1, 'ABC123', 'IceCream', GETDATE()-5, GETDATE()-5 unionselect 2, 'ABC123', 'Chocolate', GETDATE()-5, GETDATE()-5 unionselect 3, 'ABC123', 'Chocolate', GETDATE()-4, GETDATE()-5 unionselect 4, 'ABC123', 'Candy', GETDATE()-4, GETDATE()-4 unionselect 5, 'ABC123', 'Candy', GETDATE()-4, GETDATE()-4 unionselect 6, 'ABC123', 'Candy', GETDATE()-2, GETDATE()-2 unionselect 7, 'ABC123', 'IceCream', GETDATE()-1, GETDATE()-1 unionselect 7, 'ABC123', 'Chocolate', GETDATE()-1, GETDATE()-1 unionselect 7, 'ABC123', 'Candy', GETDATE()-1, GETDATE()-1 --select * from @SalesDeclare @startdate datetimeDeclare @enddate datetimeSet @startdate=dateadd(day,datediff(day,0,getdate()-5),0) -- getdate()-5 with time part 00:00:00.000Set @enddate=dateadd(day,datediff(day,0,getdate()),0) -- getdate() with time part 00:00:00.000select dateadd(day,number,@startdate) as missingDate from master.dbo.spt_values where master.dbo.spt_values.type='p' and dateadd(day,number,@startdate)<=@enddateand dateadd(day,number,@startdate) not in (select dateadd(day,datediff(day,0,creation_date),0) from @Sales) No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|