SQL to identify missing week - sql

I have a database table with the following structure -
Week_End Sales
2009-11-01 43223.43
2009-11-08 4324.23
2009-11-15 64343.23
...
Week_End is a datetime column, and the date increments by 7 days with each new entry.
What I want is a SQL statement that will identify if there is a week missing in the sequence. So, if the table contained the following data -
Week_End Sales
2009-11-01 43223.43
2009-11-08 4324.23
2009-11-22 64343.73
...
The query would return 2009-11-15.
Is this possible? I am using SQL Server 2008, btw.

You've already accepted an answer so I guess you don't need this, but I was almost finished with it anyway and it has one advantage that the selected solution doesn't have: it doesn't require updating every year. Here it is:
SELECT T1.*
FROM Table1 T1
LEFT JOIN Table1 T2
ON T2.Week_End = DATEADD(week, 1, T1.Week_End)
WHERE T2.Week_End IS NULL
AND T1.Week_End <> (SELECT MAX(Week_End) FROM Table1)
It is based on Andemar's solution, but handles the changing year too, and doesn't require the existence of the Sales column.

Join the table on itself to search for consecutive rows:
select a.*
from YourTable a
left join YourTable b
on datepart(wk,b.Week_End) = datepart(wk,a.Week_End) + 1
-- No next week
where b.sales is null
-- Not the last week
and datepart(wk,a.Week_End) <> (
select datepart(wk,max(Week_End)) from YourTable
)
This should return any weeks without a next week.

Assuming your "week_end" dates are always going to be the Sundays of the week, you could try a CTE - a common table expression that lists out all the Sundays for 2009, and then do an outer join against your table.
All those rows missing from your table will have a NULL value for their "week_end" in the select:
;WITH Sundays2009 AS
(
SELECT CAST('20090104' AS DATETIME) AS Sunday
UNION ALL
SELECT
DATEADD(DAY, 7, cte.Sunday)
FROM
Sundays2009 cte
WHERE
DATEADD(DAY, 7, cte.Sunday) < '20100101'
)
SELECT
sun.Sunday 'Missing week end date'
FROM
Sundays2009 sun
LEFT OUTER JOIN
dbo.YourTable tbl ON sun.Sunday = tbl.week_end
WHERE
tbl.week_end IS NULL

I know this has already been answered, but can I suggest something really simple?
/* First make a list of weeks using a table of numbers (mine is dbo.nums(num), starting with 1) */
WITH AllWeeks AS (
SELECT DATEADD(week,num-1,w.FirstWeek) AS eachWeek
FROM
dbo.nums
JOIN
(SELECT MIN(week_end) AS FirstWeek, MAX(week_end) as LastWeek FROM yourTable) w
ON num <= DATEDIFF(week,FirstWeek,LastWeek)
)
/* Now just look for ones that don't exist in your table */
SELECT w.eachWeek AS MissingWeek
FROM AllWeeks w
WHERE NOT EXISTS (SELECT * FROM yourTable t WHERE t.week_end = w.eachWeek)
;
If you know the range you want to look over, you don't need to use the MIN/MAX subquery in the CTE.

Related

Detect if a month is missing and insert them automatically with a select statement (MSSQL)

I am trying to write a select statement which detects if a month is not existent and automatically inserts that month with a value 0. It should insert all missing months from the first entry to the last entry.
Example:
My table looks like this:
After the statement it should look like this:
You need a recursive CTE to get all the years in the table (and the missing ones if any) and another one to get all the month numbers 1-12.
A CROSS join of these CTEs will be joined with a LEFT join to the table and finally filtered so that rows prior to the first year/month and later of the last year/month are left out:
WITH
limits AS (
SELECT MIN(year) min_year, -- min year in the table
MAX(year) max_year, -- max year in the table
MIN(DATEFROMPARTS(year, monthnum, 1)) min_date, -- min date in the table
MAX(DATEFROMPARTS(year, monthnum, 1)) max_date -- max date in the table
FROM tablename
),
years(year) AS ( -- recursive CTE to get all the years of the table (and the missing ones if any)
SELECT min_year FROM limits
UNION ALL
SELECT year + 1
FROM years
WHERE year < (SELECT max_year FROM limits)
),
months(monthnum) AS ( -- recursive CTE to get all the month numbers 1-12
SELECT 1
UNION ALL
SELECT monthnum + 1
FROM months
WHERE monthnum < 12
)
SELECT y.year, m.monthnum,
DATENAME(MONTH, DATEFROMPARTS(y.year, m.monthnum, 1)) month,
COALESCE(value, 0) value
FROM months m CROSS JOIN years y
LEFT JOIN tablename t
ON t.year = y.year AND t.monthnum = m.monthnum
WHERE DATEFROMPARTS(y.year, m.monthnum, 1)
BETWEEN (SELECT min_date FROM limits) AND (SELECT max_date FROM limits)
ORDER BY y.year, m.monthnum
See the demo.
You should not be storing date components in two separate columns; instead, you should have just one column, with a proper date-like datatype.
One approach is to use a recursive query to generate all starts of month between the earliest and latest date in the table, then brin the table with a left join.
In SQL Server:
with cte as (
select min(datefromparts(year, monthnum, 1)) as dt,
max(datefromparts(year, monthnum, 1)) as dt_max
from mytable
union all
select dateadd(month, 1, dt)
from cte
where dt < dt_max
)
select c.dt, coalesce(t.value, 0) as value
from cte c
left join mytable t on datefromparts(t.year, t.month, 1) = c.dt
If your data spreads over more that 100 months, you need to add option(maxrecursion 0) at the end of the query.
You can extract the date components in the final select if you like:
select
year(c.dt) as yr,
month(c.dt) as monthnum,
datename(month, c.dt) as monthname,
coalesce(t.value, 0) as value
from ...

How can I get the count to display zero for months that have no records

I am pulling transactions that happen on an attribute (attribute ID 4205 in table 1235) by the date that a change happened to the attribute (found in the History table) and counting up the number of changes that occurred by month. So far I have
SELECT TOP(100) PERCENT MONTH(H.transactiondate) AS Month, COUNT(*) AS Count
FROM hsi.rmObjectInstance1235 AS O LEFT OUTER JOIN
hsi.rmObjectHistory AS H ON H.objectID = O.objectID
WHERE H.attributeid = 4205) AND Year(H.transaction date) = '2020'
GROUP BY MONTH(H.transactiondate)
And I get
Month Count
---------------
1 9
2 4
3 11
4 14
5 1
I need to display a zero for months June - December instead of excluding those months.
One option uses a recursive query to generate the dates, and then brings the original query with a left join:
with all_dates as (
select cast('2020-01-01' as date) dt
union all
select dateadd(month, 1, dt) from all_dates where dt < '2020-12-01'
)
select
month(d.dt) as month,
count(h.objectid) as cnt
from all_dates d
left join hsi.rmobjecthistory as h
on h.attributeid = 4205
and h.transaction_date >= d.dt
and h.transaction_date < dateadd(month, 1, d.dt)
and exists (select 1 from hsi.rmObjectInstance1235 o where o.objectID = h.objectID)
group by month(d.dt)
I am quite unclear about the intent of the table hsi.rmObjectInstance1235 in the query, as none of its column are used in the select and group by clauses; it it is meant to filter hsi.rmobjecthistory by objectID, then you can rewrite this as an exists condition, as shown in the above solution. Possibly, you might as well be able to just remove that part of the query.
Also, note that
top without order by does not really make sense
top (100) percent is a no op
As a consequence, I removed that row-limiting clause.

SQL Inner Join returns duplicates

I have the following 2 tables:
tab1 with 37146 rows
week_ref with 730 rows
All I want to do is join those tables on year and week so that the first week day and last week day will display next to the columns of the first table.
Below is my query:
SELECT tab1.year
,tab1.week
,tab1.col3
,tab1.col4
,tab1.col5
,tab1.col6
,tab1.total
,tab1.col7
,week_ref.first_week_day
,week_ref.last_week_day
FROM dtsetname.tab1
JOIN spyros.week_ref ON (week_ref.year = tab1.year AND week_ref.week = tab1.week)
The return of the query returns the 2 extra columns but the rows are 255535. So it is full of duplicates. I used to get how join works, but I guess not anymore xd... Any help on this? The correct output table should only give me 37146 rows since I only just want to add 2 extra columns.
Thanks
Below is for BigQuery Standard SQL
Before JOIN'ing you just need to dedup data in week_ref table as in below example
#standardSQL
SELECT tab1.year
,tab1.week
,tab1.col3
,tab1.col4
,tab1.col5
,tab1.col6
,tab1.total
,tab1.col7
,week_ref.first_week_day
,week_ref.last_week_day
FROM dtsetname.tab1 tab1
JOIN (SELECT DISTINCT year, week, first_week_day, last_week_day FROM spyros.week_ref) week_ref
ON (week_ref.year = tab1.year AND week_ref.week = tab1.week)
The problem is that your week_ref table has a row for each day rather than per week.
You can select just one day. If you have a weekday number or name (which I'm guessing that you do), that can be used:
FROM dtsetname.tab1 JOIN
spyros.week_ref wr
ON wr.year = tab1.year AND
wr.week = tab1.week AND
wr.dayname = 'Monday'
If such a column is not available, then you can either extract() the information or aggregate:
FROM dtsetname.tab1 JOIN
(SELECT ANY_VALUE(wr).*
FROM spyros.week_ref wr
GROUP BY wr.year, wr.week
) wr
ON wr.year = tab1.year AND
wr.week = tab1.week
first, I hope that year+week & year+day are primary keys in corresponding tables, otherwise the problem is there.
If so, here is another hint to check:
I notice that you join them by year and week, however, in the first table I see many 52 in a week column and in the second one 0 as a value.
There are only 52 weeks in year, plus a day, so is it possible you need to join by
week_ref.year = tab1.year AND week_ref.week = tab1.week+1
I think the solutions mentioned by others should work if you are looking to join to your reference table to get week start/end dates.
However, if you think your tab1 table has definite values in the week and year columns (and if I understand your data correctly) you can avoid the join altogether to get your desired results:
select
year
,week
,col3
,col4
,col5
,col6
,total
,col7
,date_sub(weekdate, interval IF(EXTRACT(DAYOFWEEK FROM weekdate) = 1, 6, EXTRACT(DAYOFWEEK FROM weekdate) - 1) day) as first_week_day
,date_add(date_sub(weekdate, interval IF(EXTRACT(DAYOFWEEK FROM weekdate) = 1, 6, EXTRACT(DAYOFWEEK FROM weekdate) - 1) day), interval 6 day) as last_week_day
from (
select
tab1.year
,tab1.week
,tab1.col3
,tab1.col4
,tab1.col5
,tab1.col6
,tab1.total
,tab1.col7
date_add(date(cast(tab1.year as int64), 1, 1), interval cast(tab1.week as int64) week) as weekdate
from `mydataset.tab1` as tab1
)
Hope it helps :)

How to Find Week Number in look up Table

I have a Two Tables
One is Lookup Table Week for WeekNumber and Weekdate
And Another is Register
What I want to Return is current WeekLabel from the Week table where Entrytime is falling between two dates
For example :
Entrytime 2016-08-15 09:02:15.0000000
is between Weekstartdate value 2016-08-15 00:00:00.000 and 2016-08-22 00:00:00.000
so I want to return WeekLabel 1 and WeekStartdate 2016-08-15 from week table and so on..
Using cross apply() to get the latest WeekStartDate that occurs before or on EntryTime.
select r.*
, x.WeekLabel
from register r
cross apply (
select top 1 w.WeekLabel
from [Week] w
where w.weekstartdate =< r.entrytime
order by w.weekstartdate desc
) as x
switch to outer apply() for left join functionality, e.g. return null if there is no corresponding WeekLabel.
You can join the tables based on the entrytime being greater than the weekstartdate; this will multiply each entry for all the preceding weeks, so you can get the latest one with a max on WeekLabel (assuming they're constantly increasing).
select t2.ID, t2.CODE, max(t1.WeekLabel)
from Week t1
join Register t2
on t2.Entrytime > t1.Weekstartdate
group by t2.ID, t2.CODE
Another option is a simple JOIN
Select R.*
,W.WeekLabel
,W.WeekStartdate
From Register R
Join [Week] W
on R.EntryTime >= W.WeekStartDate
and R.EntryTime < DateAdd(DAY,7,W.WeekStartDate)
Another option is to join to the week reference table using the datediff function:
SELECT * from
register r
join [Week] w on
DATEDIFF(day, r.entryTime, w.weekStartDate) between 0 and 7

Join two tables with not exactly matching dates in sql

I have a table with dates that are about 1 month before the other table.
For example,
one table reports 1st quarter end on March 31st
and the other reports 1st quarter end on February 28th (or 29th)
but it would be perfectly fine to join them together by the date regardless that the two dates arent exactly the same.
Any suggestions, please.
Thanks
You can join on DateDiff(dd, Date1, Date2) < x
Or to get more exact
select endOfMonth.*, begOfMonth.*
from endOfMonth join begOfMonth
on DATEADD (dd , 1 , endOfMonth.date ) = begOfMonth.Date
Your ON clause could look at year and quarter for a match:
ON TABLE1.YEAR([1st quarter end ]) = TABLE2.YEAR([1st quarter end ])
AND TABLE1.QUARTER([1st quarter end ]) = TABLE2.QUARTER([1st quarter end ])
select val1 From Table1 T1 inner Join Table2 t2 on MONTH(T1.date1) = MONTH(t2.date1)
And YEAR(T1.date1) = YEAR(t2.date1)
One approach would be to use the DATEPART() function that returns the quarter for any given date. Then you would be able to join on the returned quarter.
Sample SQL:
SELECT *
FROM
(
SELECT DATEPART(QUARTER,date_column) AS t1_quarter
FROM table1
UNION ALL
SELECT DATEPART(QUARTER,date_column) AS t2_quarter
FROM table2
) AS temp
WHERE temp.t1_quarter = temp.t2_quarter;
Put any other fields as you require (ID fields most probably) in the internal SELECTS.
If I rigthly understood you and you have the same number of column in those tables then you should use UNION in your SQL-query. See more information about UNION here: http://en.wikipedia.org/wiki/Set_operations_%28SQL%29.