SQL noob here. I'm using MS Access to query a table of streetlight data to exclude lights of a certain size and only where the fixture type is a variant of "Cobra". Is anyone kind enough to give me some guidance on what I might be doing wrong?
I can produce the lights I need to be excluded with:
SELECT *
FROM Lighting
WHERE Lighting.TYPE_FIXTURE LIKE '*cobra*'
AND Lighting.SIZ < '16'
Simple enough. I thought this would be easy to tweak a little bit to get it to exclude these instead of produce them.
SELECT *
FROM Lighting
WHERE Lighting.TYPE_FIXTURE NOT IN ('*Cobra*')
AND Lighting.SIZ NOT IN ('10', '15')
This code excludes the lights I need to be excluded, but also every other type of light that is 100 or 150w. For the life of me, I can't figure out how to exclude ONLY Cobra lights 150w or less.
Through extensive internet research, I tried using an IIF expression to produce a temporary column that would have a value of 0 or 1, depending on if it needed to be excluded or not,
SELECT *
,IIf(Lighting.TYPE_FIXTURE LIKE "*cobra*" AND Lighting.SIZ < 15, 0, 1) AS IncludeFlag
INTO #MyTempTable
FROM Lighting
SELECT *
FROM #MyTempTable
WHERE IncludeFlag = 1
but my only result is an error message that I don't know how to resolve. I've spent all day on this one query and I'm starting to pull my hair out!!
Try this...
SELECT *
FROM Lighting
WHERE (Lighting.TYPE_FIXTURE NOT LIKE '*cobra*'
AND Lighting.SIZ < '16')
Converting from a TRUE and TRUE to a NOT(TRUE and TRUE) involves distributing the NOT inside the brackets and switch the AND to an OR.
The following should give you the results you want:
SELECT *
FROM Lighting
WHERE Lighting.TYPE_FIXTURE Not Like '*cobra*' OR Lighting.SIZ>='16';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.