MCTampa
Board Regular
- Joined
- Apr 14, 2016
- Messages
- 97
I have a dataset with website searches by product, week range, start year and end year.
For example, I have:
I'm trying to sum the total number of searches which cover a given week.
I made a simple two column table of Year and Week, going from 1 to 52 in 2023 and 1 to 52 in 2024.
Using the code below, I'm able to capture all of the searches which start and end in 2023 and all of the searches which start and end in 2024, but NOT the searches which start in 2023 and end in 2024.
Should I be using an Union? And Outer Join? I've tried a few to no avail.
Thank you for your help.
For example, I have:
Product | Start Week (SW) | Start Year (SY) | End Week (EW) | End Year (EY) | Searches |
Product XYZ | 23 | 2023 | 32 | 2023 | 10 |
Product ABC | 14 | 2024 | 26 | 2024 | 5 |
Product DEF | 47 | 2023 | 9 | 2024 | 15 |
I'm trying to sum the total number of searches which cover a given week.
I made a simple two column table of Year and Week, going from 1 to 52 in 2023 and 1 to 52 in 2024.
Using the code below, I'm able to capture all of the searches which start and end in 2023 and all of the searches which start and end in 2024, but NOT the searches which start in 2023 and end in 2024.
Should I be using an Union? And Outer Join? I've tried a few to no avail.
SQL:
SELECT
W.Week,
W.Year,
S.Trans,
S.SW,
S.SY,
S.EW,
S.EY,
S.RGNAME,
S.RAREA,
Sum(S.Views) AS Views
into Data
FROM Weeks AS W left join Search3 AS S ON
(W.Year >= S.SY)
AND (W.Week >= S.SW)
AND (W.Week <= S.EW)
AND (W.Year <= S.EY)
GROUP BY W.Week, W.Year, S.Trans, S.SW, S.SY, S.EW, S.EY, S.RGNAME, S.RAREA;
Thank you for your help.