Related
More brain freeze moments from me. I'm sure this will be an easy one.
I have two tables. One is a list of part usage by week. This is called TransactionsPerWeek and looks like this:
ItemPK xWeek xYear TotalQty
1234 2 2019 65
1234 4 2019 15
1234 5 2019 50
I also have a DateList table that has week numbers and years in it
xWeek xYear
1 2019
2 2019
3 2019
etc.
When I right join the two together on week and year I get
ItemPK xWeek xYear TotalQty
NULL 1 2019 0
1234 2 2019 65
NULL 3 2019 0
1234 4 2019 15
1234 5 2019 50
What I need is to have the ItemPK on every line, even if the TotalQty is 0. So in effect, I need:
ItemPK xWeek xYear TotalQty
1234 1 2019 0
1234 2 2019 65
1234 3 2019 0
1234 4 2019 15
1234 5 2019 50
This is my code...
SELECT itemfk,
dates.year,
dates.week,
isnull(transactionsperweek.TotalQty,0) as TotalQty
from (
SELECT iit.ItemFK,
year(iit.transactiondate) xYear,
datepart(wk,iit.transactiondate) xWeek,
abs(sum(iit.quantity)) TotalQty
from iteminventorytransaction iit
INNER JOIN ItemInventoryTransactionType iitt on ItemInventoryTransactionTypePK = iit.ItemInventoryTransactionTypeFK
where iit.itemfk = 5311
and iit.ItemInventoryTransactionTypeFK in (10,8)
and iit.TransactionDate BETWEEN
-- 1 year up to the sunday of last week
DateAdd(wk,-51,DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + ##DATEFIRST - 2) % 7,GETDATE()))
AND DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + ##DATEFIRST - 2) % 7,GETDATE())
AND Quantity < 0
group by iit.itemfk,
year(iit.transactiondate),
datepart(wk,iit.transactiondate)
) transactionsPerWeek
RIGHT JOIN (
select year,
week
from DatesList
where date > DateAdd(wk,-51,DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + ##DATEFIRST - 2) % 7,GETDATE()))
AND date < DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + ##DATEFIRST - 2) % 7,GETDATE())
group by year,
week
) Dates ON dates.week = transactionsPerWeek.xWeek
AND dates.year = transactionsPerWeek.xYear
where week not in (52,53)
Hope this is clear enough. Thanks in advance.
You can use recursive cte :
with cte as (
select 1 as id, max(xWeek) as maxwk
from TransactionsPerWeek
union all
select id + 1, maxwk
from cte c
where c.id < maxwk
)
select coalesce(wk.ItemPK, wk1.ItemPK) as ItemPK, c.id as xWeek, wk.xYear, wk.TotalQty
from cte c left join
TransactionsPerWeek wk
on wk.xWeek = c.id outer apply
( select top (1) wk1.ItemPK
from TransactionsPerWeek wk1
where wk1.xWeek >= c.id and wk1.xWeek is not null
order by wk1.xWeek
) wk1;
Ok, so I did what #larnu suggested and cross joined the item with the dates, then left joined it to the transactionsperweek table and it worked. Thank you.
This is my code now;
SELECT itempk, week, year
, ISNULL(transactionsPerWeek.TotalQty,0) as TotalQty
from item
CROSS JOIN
(
select year, week from DatesList where date >
DateAdd(wk,-51,DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + ##DATEFIRST - 2) % 7,GETDATE()))
AND date <
DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + ##DATEFIRST - 2) % 7,GETDATE())
group by year, week
) dates
LEFT JOIN
(
SELECT iit.ItemFK, year(iit.transactiondate) xYear, datepart(wk,iit.transactiondate) xWeek, abs(sum(iit.quantity)) TotalQty from iteminventorytransaction iit
INNER JOIN ItemInventoryTransactionType iitt on ItemInventoryTransactionTypePK = iit.ItemInventoryTransactionTypeFK
where iit.itemfk = 5311 and iit.ItemInventoryTransactionTypeFK in (10,8)
and iit.TransactionDate BETWEEN
-- 1 year up to the sunday of last week
DateAdd(wk,-51,DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + ##DATEFIRST - 2) % 7,GETDATE()))
AND
DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + ##DATEFIRST - 2) % 7,GETDATE())
AND Quantity < 0
group by iit.itemfk, year(iit.transactiondate), datepart(wk,iit.transactiondate)
) transactionsPerWeek
ON itempk = transactionsperweek.ItemFK and transactionsPerWeek.xYear = dates.year and transactionsPerWeek.xWeek = dates.week
where itempk = 5311
Use a cross join to generate the rows and a left join to bring in the results you already have.
Your question explicitly states that you have two tables. Hence, I don't know what your SQL code is doing, because it is not referencing those tables. So, based on the description:
select i.ItemPK, d.xWeek, d.xYear,
coalesce(TotalQty, 0) as TotalQty
from (select distinct itemPK from TransactionsPerWeek
) i cross join
DateList d left join
TransactionsPerWeek t
on t.itemPK = i.itemPK and
t.xWeek = d.xWeek and
t.xYear = d.xYear;
Of course if the "tables" are really subqueries, then I would recommend using CTEs and still this basic query structure.
Say I have a year and a quarter and I want to build a table lists a specific number of year and quarter combinations ending with the given year and quarter.
E.g.
**Input:**
Year = 2018
Quarter = 3
NumRows = 4
**Output:**
Year.....|Quarter
2018.....|3
2018.....|2
2018.....|1
2017.....|4
You can use recursive generation.
First you need to build a DATETIME object from the year and quarter.
DECLARE #Quarter INT = 3;
DECLARE #Year INT = 2018;
DECLARE #NumRows INT = 4;
DECLARE #initial_date DATETIME = DATEADD(quarter, #Quarter-1, DATEADD(year, #Year-1900, 0));
Then you can recursively generate the year & quarter combinations like this.
;WITH quarters AS (
SELECT #initial_date AS [qdate]
UNION ALL
SELECT DATEADD(quarter, -1, [qdate]) AS [qdate]
FROM quarters
WHERE [qdate] > DATEADD(quarter, -1*(#NumRows-1), #initial_date)
)
SELECT DATEPART(year, qdate) as year, DATEPART(quarter, qdate) as quarter FROM quarters
this uses a recursive cte
for
declare #year int = 2018,
#quarter int = 3,
#numrows int = 6
; with rcte as
(
select n = 1,
yr = #year,
qtr = #quarter
union all
select n = n + 1,
yr = yr - (((qtr - 1 + 4 - 1) % 4 + 1) / 4),
qtr = (qtr - 1 + 4 - 1) % 4 + 1
from rcte
where n < #numrows
)
select *
from rcte
order by n
/* result:
n yr qtr
1 2018 3
2 2018 2
3 2018 1
4 2017 4
5 2017 3
6 2017 2
*/
You can try to use cte recursive with some calculation
DECLARE #Year INT= 2018
DECLARE #Quarter INT = 3
DECLARE #NumRows INT= 10
;WITH CTE AS (
SELECT (#Year - #NumRows/4) yr,
4 - (#NumRows % 4) Quarter,
#NumRows NumRows
UNION ALL
SELECT yr + Quarter / 4,
CASE WHEN (Quarter + 1) % 4 = 0 THEN 4
ELSE (Quarter + 1) % 4
END,
NumRows- 1
FROM CTE
WHERE NumRows > 1
)
select yr,Quarter
from cte
sqlfiddle
We are running reports for a seasonal business, with expected lulls during the summer months. For some metrics, we'd essentially like to pretend that those months don't even exist.
Thus consider the default behavior of:
SELECT DATEDIFF(MONTH, '2015-05-01', '2015-06-01') -- answer = 1
SELECT DATEDIFF(MONTH, '2015-05-01', '2015-07-01') -- 2
SELECT DATEDIFF(MONTH, '2015-05-01', '2015-08-01') -- 3
SELECT DATEDIFF(MONTH, '2015-05-01', '2015-09-01') -- 4
We want to ignore June and July, so we would like those answers to look like this:
SELECT DATEDIFF(MONTH, '2015-05-01', '2015-06-01') -- answer = 1
SELECT DATEDIFF(MONTH, '2015-05-01', '2015-07-01') -- 1
SELECT DATEDIFF(MONTH, '2015-05-01', '2015-08-01') -- 1
SELECT DATEDIFF(MONTH, '2015-05-01', '2015-09-01') -- 2
What is the easiest way to accomplish this? I'd like a pure SQL solution, rather than something using TSQL, but writing a custom function such as NOSUMMER_DATEDIFF could also work.
Also, keep in mind the reports will span multiple years, so the solution should be able to handle that.
If you are only interested month differences, then I would suggest a trick here. Count the number of months since some date 0, but ignore the summer months. For example:
'2015-05-01' --> 2015 * 10 + 5 = 20155
'2015-06-01' --> 2015 * 10 + 6 = 20156
'2015-07-01' --> 2015 * 10 + 6 = 20156
'2015-08-01' --> 2015 * 10 + 6 = 20156
'2015-09-01' --> 2015 * 10 + 7 = 20157
This is a fairly easy calculation:
select (case when month(date2) <= 6 then year(date2) * 10 + month(date2)
when month(date2) in (7, 8) then year(date2) * 10 + 6
else year(date2) * 10 + (month(date2) - 2)
end)
For the difference:
select ((case when month(date2) <= 6 then year(date2) * 10 + month(date2)
when month(date2) in (7, 8) then year(date2) * 10 + 6
else year(date2) * 10 + (month(date2) - 2)
end) -
(case when month(date1) <= 6 then year(date1) * 10 + month(date1)
when month(date1) in (7, 8) then year(date1) * 10 + 6
else year(date1) * 10 + (month(date1) - 2)
end)
)
To able to achieve that, you have to "split" dates ranges to an "array" of dates for every single range of dates. CTE might be helpful in this case.
See:
--your table which holds dates ranges
DECLARE #dates TABLE(id INT IDENTITY(1,1), dFrom DATE, dTo DATE)
INSERT INTO #dates (dFrom, dTo)
VALUES('2015-05-01', '2015-06-01'),
('2015-05-01', '2015-07-01'),
('2015-05-01', '2015-08-01'),
('2015-05-01', '2015-09-01')
--summer month table
DECLARE #summermonths TABLE(summMonth INT)
INSERT INTO #summermonths(summMonth)
VALUES(6), (7)
--here Common Table Expressions is in action to "split" dates ranges to an array of dates for every single date range
;WITH CTE AS
(
SELECT id, DATEADD(MM, 0, dFrom) AS ndFrom, dTo, CASE WHEN MONTH(DATEADD(MM, 0, dFrom)) = 6 OR MONTH(DATEADD(MM, 0, dFrom)) = 7 THEN 0 ELSE 1 END AS COfMonth
FROM #dates
WHERE DATEADD(MM, 1, dFrom)<=dTo
UNION ALL
SELECT id, DATEADD(MM, 1, ndFrom) AS ndFrom, dTo, CASE WHEN MONTH(DATEADD(MM, 1, ndFrom)) = 6 OR MONTH(DATEADD(MM, 1, ndFrom)) = 7 THEN 0 ELSE 1 END AS COfMonth
FROM CTE
WHERE DATEADD(MM, 1, ndFrom)<=dTo
)
SELECT t1.id, t2.dFrom, t2.dTo, SUM(t1.COfMonth) AS MyDateDiff
FROM CTE AS t1 INNER JOIN #dates AS t2 ON t1.id = t2.id
GROUP BY t1.id, t2.dFrom , t2.dTo
Result:
id dFrom dTo MyDateDiff
1 2015-05-01 2015-06-01 1
2 2015-05-01 2015-07-01 1
3 2015-05-01 2015-08-01 2
4 2015-05-01 2015-09-01 3 --not 2, because of 5, 8, 9
Got it?
Note: a solution might be differ in case of dFrom and dTo is not the first date of month.
I am very new in SQL (started just 3 days back) and I got an issue and would hope someone is able to assist me.
I have a query that creates date loops to return me the week number based on the passed in dates. The idea here is to pass loop through the days, months and years and return me a week value. But because of these, I have been getting multiple entries of the same result.
For eg. I am getting:
period_wk_key period_yr_key period_week period_week_day period_week_full_desc
--------------- -------------- ------------ -------------- -----------------------
200001 2000 1 6 2000 WEEK 1
200001 2000 1 6 2000 WEEK 1
200001 2000 1 6 2000 WEEK 1
. . . . .
200002 2000 2 6 2000 WEEK 2
200002 2000 2 6 2000 WEEK 2
200002 2000 2 6 2000 WEEK 2
. . . . .
200003 2000 3 6 2000 WEEK 3
each period_wk_key is returning me 7 similar rows which is not what I wanted.
The ideal situation should be:
period_wk_key period_yr_key period_week period_week_day period_week_full_desc
--------------- -------------- ------------ -------------- -----------------------
200001 2000 1 6 2000 WEEK 1
200002 2000 2 6 2000 WEEK 2
200003 2000 3 6 2000 WEEK 3
200004 2000 4 6 2000 WEEK 4
I need to know a way to limit the number of output from a loop so that I can have distinct records instead of multiple similar rows. I have attached a code below. It maybe crap to many of you but this is just my 3rd day beginning scripting. Much thanks for any help rendered.
DECLARE #iStartYear INT
SET #iStartYear = 2000
DECLARE #iEndYear INT
SET #iEndYear = 2030
DECLARE #iMth INT
SET #iMth = 1
DECLARE #iDay INT
SET #iDay = 1
DECLARE #iWeek INT
`DECLARE #StartDate DATETIME
WHILE (#iStartYear <= #iEndYear)
BEGIN
WHILE (#iMth <= 12)
BEGIN
WHILE (#iDay <= DATEDIFF(DAY,DATEADD(DAY, 0, DATEADD(m, ((#iStartYear - 1900) * 12) + #iMth - 1, 0)),DATEADD(DAY, 0, DATEADD(m, ((#iStartYear - 1900) * 12) + #iMth, 0)))))
BEGIN `
SET #iWeek = (DATEPART(dy, CONVERT(DATETIME, CONVERT(VARCHAR(4), #iStartYear) + '/' + CONVERT(VARCHAR(2), #iMth) + '/' + CONVERT(VARCHAR(2), #iDay))) - 1) / 7 + 1;
INSERT INTO dim_period_week (period_wk_key, period_yr_key, period_week, period_week_full_desc,start_date, end_date, period_week_day)
VALUES (
(SELECT CASE WHEN #iWeek < 10 THEN
CAST((#iStartYear) AS VARCHAR) + '0' + CAST((#iWeek) AS VARCHAR)
ELSE
CAST((#iStartYear) AS VARCHAR)+ CAST((#iWeek) AS VARCHAR)
END),
#iStartYear,
(SELECT (DATEPART(dy, CONVERT(DATETIME, CONVERT(VARCHAR(4), #iStartYear) + '/' + CONVERT(VARCHAR(2), #iMth) + '/' + CONVERT(VARCHAR(2), #iDay))) - 1) / 7 + 1),
(SELECT CAST((#iStartYear) AS VARCHAR) + ' ' + 'WEEK' + ' ' + CAST((#iWeek) AS VARCHAR)),
(SELECT DATEADD(wk,#iWeek-1, DATEADD(yy,#iStartYear-1900,0))), --START DATE
(SELECT DATEADD(wk,#iWeek, DATEADD(yy,#iStartYear-1900,0)) -1), --END DATE
(SELECT DATEDIFF(DAY, DATEADD(wk,#iWeek-1, DATEADD(yy,#iStartYear-1900,0)), DATEADD(wk,#iWeek, DATEADD(yy,#iStartYear-1900,0)) -1))
)
`SET #iDay = #iDay + 1
END
SET #iDay = 1
SET #iMth = #iMth + 1
END
SET #iMth = 1
SET #iStartYear = #iStartYear + 1
END
`
You are looping every day, but never storing at the day level, so you are doing 7 times as many INSERTs as needed. Add the following just above your INSERT statement
IF DATEPART(weekday, CAST(CAST(#iStartYear * 10000 + #iMth * 100 + #iDay AS VARCHAR(8)) AS datetime)) = 1
INSERT INTO dim_period_week ...
Below is a set based approach using a numbers table.
IF OBJECT_ID('tempdb.dbo.#Numbers') IS NOT NULL DROP TABLE #Numbers
GO
DECLARE #startDate DATETIME = '20000101'
SELECT
(a.Number * 256) + b.Number AS Number
INTO #Numbers
FROM
(SELECT number FROM master..spt_values WHERE type = 'P' AND number <= 255) a (Number),
(SELECT number FROM master..spt_values WHERE type = 'P' AND number <= 255) b (Number)
WHERE
(a.Number * 256) + b.Number BETWEEN 0 AND 1617
SELECT
WeekKey = CAST(YEAR(DATEADD(day, N.Number * 7, #startDate)) * 100 + DATEPART(week, DATEADD(day, N.Number * 7, #startDate)) AS VARCHAR(10)),
TheYear = YEAR(DATEADD(day, N.Number * 7, #startDate)),
TheWeek = DATEPART(week, DATEADD(day, N.Number * 7, #startDate)),
TheWeekDay= DATEPART(weekday, DATEADD(day, N.Number * 7, #startDate)),
WeekDetail= DATENAME(year, DATEADD(day, N.Number * 7, #startDate)) + ' WEEK ' + DATENAME(week, DATEADD(day, N.Number * 7, #startDate)),
StartDate = DATEADD(day, N.Number * 7, #startDate),
EndDate = DATEADD(day, 6, DATEADD(day, N.Number * 7, #startDate))
FROM #Numbers N
ORDER BY 1
Here is my query:
DECLARE #MM INT -- Current month
DECLARE #DD INT -- Current date
SET #MM = 1 -- For testing, set it to January
SET #DD = 1 -- For testing, set it to 01
SELECT xxxID, xxxFK, StartMonth, StartDate, StopMonth, StopDate, NULL AS OKorNOT
FROM xxxTable
ORDER BY xxxFK
And here is the data:
xxxID xxxFK StartMonth StartDate StopMonth StopDate OKorNOT
---------------- ----------- ----------- ----------- ----------- ----------- -----------
8 2287 11 15 1 2 NULL
4 2290 2 1 2 21 NULL
2 2306 9 15 10 31 NULL
3 2306 1 3 1 20 NULL
9 2661 11 15 1 3 NULL
10 2661 5 5 5 31 NULL
5 3778 6 2 9 5 NULL
6 3778 1 1 3 31 NULL
7 3778 5 10 5 31 NULL
1 3778 12 10 12 31 NULL
I need to populate OKorNot column with 1/0 depending on whether the given month-date lies between StartMonth-StartDate and StopMonth-StopDate. This is SQL Server 2000 by the way.
EDIT
The thing here to note that is that there are no years stored in the data and the months-dates may start in, say Nov-15 and end in Jan-15 so on Dec-31 and Jan-1 this case should return true.
Using integer operations only and an imaginary 384-days calendar
Since your dates are combinations of month and day, I tried to create an integer for every such combination, an integer that is unique and also preserves order. To have calculations as simple as possible, we invent a new calendar where all months have exactly 32 days and we act as if our dates are from this calendar. Then to get how many days have past since 1st of January, we have the formula:
DaysPast = 32 * month + day
(OK, it should be 32 * (month-1) + (day-1) but this way it's simpler and we only want to compare dates relatively to one another, not to January 1st. And the result is still unique for every date).
Therefore, we first calculate the DaysPast for our check date:
SET #CHECK = 32 * #MM + #DD
Then, we calculate the DaysPast for all dates (both start and stop ones) in our table:
( SELECT *
, (32 * StartMonth + StartDate) AS Start
, (32 * StopMonth + StopDate ) AS Stop
FROM xxxTable
) AS temp
Then, we have two cases.
First case, when Start = (8-Feb) and Stop = (23-Nov).
Then, the first condition #CHECK BETWEEN Start AND Stop will be true and the dates between Start and Stop will be OK.
The second condition will be False, so no more dates will be OK.
Second case, when Start = (23-Nov) and Stop = (8-Feb). :
Then, the first condition #CHECK BETWEEN Start AND Stop will be false because Start is bigger than Stop so no dates can match this condition.
The second condition Stop < Start will be true, so we also test if
#CHECK is NOT BETWEEN (9-Feb) AND (22-Nov)
to match the dates that are before (9-Feb) or after (22-Nov).
DECLARE #CHECK INT
SET #CHECK = 32 * #MM + #DD
SELECT *
, CASE WHEN
#CHECK BETWEEN Start AND Stop
OR ( Stop < Start
AND #CHECK NOT BETWEEN Stop+1 AND Start-1
)
THEN 1
ELSE 0
END
AS OKorNOT
FROM
( SELECT *
, (32 * StartMonth + StartDate) AS Start
, (32 * StopMonth + StopDate ) AS Stop
FROM xxxTable
) AS temp
ORDER BY xxxFK
It would be easier if you'd stored dates as, well, dates...
Anyway, something like this. I haven't tested. And you need to deal with year boundary which I've done
SELECT
xxxID, xxxFK, StartMonth, StartDate, StopMonth, StopDate,
CASE
WHEN
FullStart <= FullStop AND
DATEADD(month, #MM-1, DATEADD(day, #DD-1, 0)) BETWEEN FullStart AND FullStop
THEN 1
WHEN
FullStart > FullStop AND
DATEADD(month, #MM-1, DATEADD(day, #DD-1, 0)) BETWEEN
FullStart AND DATEADD(year, 1, FullStop)
THEN 1
ELSE 0
END AS OKOrNot
FROM
(
SELECT
xxxID, xxxFK, StartMonth, StartDate, StopMonth, StopDate,
DATEADD(month, StartMonth-1, DATEADD(day, StartDate-1, 0)) AS FullStart,
DATEADD(month, StopMonth-1, DATEADD(day, StopDate-1, 0)) AS FullStop
FROM xxxTable
) foo
ORDER BY xxxFK
edit: added "-1" to all values: if we're already Jan don't add another month...
SELECT *
FROM xxxTable
WHERE (StartMonth < StopMonth OR StartMonth = StopMonth AND StartDate<=StopDate)
AND (#MM > StartMonth OR #MM = StartMonth AND #DD >= StartDate)
AND (#MM < StopMonth OR #MM = StopMonth AND #DD <= StopDate)
OR (StartMonth > StopMonth OR StartMonth = StopMonth AND StartDate>StopDate)
AND ((#MM > StartMonth OR #MM = StartMonth AND #DD >= StartDate)
OR (#MM < StopMonth OR #MM = StopMonth AND #DD <= StopDate))