Help with SQL Statement

MCTampa

Board Regular
Joined
Apr 14, 2016
Messages
97
I have the following SQL statement, which works correctly

SQL:
SELECT
OA.RAREA,
OA.RCODE,
OA.RGNAME,
OA.RRATCD,
OA.Size,
OA.StartWeek,
OA.EndWeek

FROM [Automatics with Ranges] AS OA

WHERE (((Exists (SELECT*
FROM [SSG Exclusion List] ab
Where ab.RESX = OA.[RCODE]
AND OA.Size = ab.Size
AND OA.[StartWeek] <= ab.SW
AND OA.[EndWeek]>= ab.EW
))<>False));

However all I want to do is include the fields ab.SW and ab.EW in my result, but I'm unsure how or where to add them.
I've tried simply adding a Group By line, but it does not accept anything from ab.
The board helped me create the query and I've modified it a few times, but I have not had a necessity for adding these elements as of yet.
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
You mean you get more fields than you want but you only want ab.SW and ab.EW?
Then don't select all fields - SELECT *
but "I'm unsure how or where to add them" implies you're not seeing them at all. Confusing.
 
Upvote 0
I am NOT getting ab.SW and ab.EW by simply adding them to the select statement.
I believe it is because I'm using a subquery so I'm not sure how to ensure that they are added.
 
Upvote 0
I am NOT getting ab.SW and ab.EW by simply adding them to the select statement.
I believe it is because I'm using a subquery so I'm not sure how to ensure that they are added.
I think you just want to do a query instead of a subquery.
Then those fields should be available to you.

If you have problems getting it together, please post a small sample of the data from each table, so we can see what it looks like, and explain exactly what it is that you are trying to do.
 
Upvote 0
You won't be able to grab anything from ab because it's in a subquery. The below uses a LEFT JOIN to return the same records but will let you select fields from either table involved:

SQL:
SELECT
    ab.SW,
    ab.EW
FROM
    [Automatics with Ranges] AS OA
LEFT JOIN
    [SSG Exclusion List] ab
ON
    OA.[RCODE] = ab.RESX AND
    OA.Size = ab.Size AND
    OA.[StartWeek] <= ab.SW AND
    OA.[EndWeek] >= ab.EW
WHERE
    ab.RESX IS NOT NULL;
 
Upvote 0
^^ Correct but would suggest Inner Join instead of Left Join (inner join can be considerably faster so only use left join when you really must).
 
Upvote 0
^^ Correct but would suggest Inner Join instead of Left Join (inner join can be considerably faster so only use left join when you really must).
Is there risk of duplication, though?

I could swear I've run into that before...
 
Upvote 0
Hmmm. Inner joins and left joins aren't usually culprits in duplication concerns. In either case you'd have the same set of "matched records" and the only difference would be the left join to include "unmatched records"
 
Upvote 0
My problems must have been because I don't really know what I'm doing... :oops:
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,577
Members
449,039
Latest member
Arbind kumar

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