Not Sure Which Join To Use

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:

ProductStart Week (SW)Start Year (SY)End Week (EW)End Year (EY)Searches
Product XYZ23202332202310
Product ABC1420242620245
Product DEF4720239202415

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.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I am not sure about what you want.
You say you want to sum the searches of a "given" week. I don't see a condition where you compare the year/week with such a week.
I don't know what the use of the week table in this query is. The years/weeks are in the searches table, so you can select just there.
 
Upvote 0
I rely mostly on query design to figure things out so I'm not sure about this: Seems to me you want either a HAVING or WHERE clause. The criteria would be like
WHERE SY In(2023, 2024) AND EY In(2023, 2024)
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top