SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Applying constraints to WHERE statement


Applying constraints to WHERE statement

Author
Message
Zephyris
Zephyris
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 18
Hi,

I have to write code to produce data along the lines of SIZE, COLOUR and PATTERN. I'm coming unstuck because I have to apply specific constraints on PATTERN given a specific SIZE and COLOUR. For example, SIZE=10; COLOUR=R; PATTERNID must not be in (001,009): SIZE=10; COLOUR=G; PATTERNID must not be in (001,002,008) and so on.

I'm not new to programming, but am to SQL and I can't seem to get the syntax right. Any help would be much appreciated! :-)
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14339 Visits: 15978
Here's one way, but I'm not sure that it's the best. Create a table called ForbiddenPatterns with columns Size, Colour, Pattern. Insert one row for each forbidden combination. Write a function that takes Colour, Size and Pattern as parameters and returns Forbidden if the combination supplied appears in ForbiddenPatterns. Add a check constraint to your original table that checks that the result of the function is not Forbidden.

John

Edit - corrected typo
Zephyris
Zephyris
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 18
Hi John,

Yes - that sounds as though it would work :-D I'm on another task now for the rest of the day, but I'll be sure to give an update when I can. Many thanks!!
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16464 Visits: 19556
CROSS APPLY is an excellent tool for building complex WHERE clauses and would also help you to figure out if it's worthwhile building up an exclusion table as suggested. Once you've constructed your exclusions list in the CROSS APPLY block, you can drop it down into the WHERE clause.
SELECT x.Exclusion, m.* 
FROM MyTable m
CROSS APPLY (
SELECT Exclusion = CASE
WHEN m.SIZE = 10 AND m.COLOUR = 'R' AND m.PATTERNID IN (001,009) THEN 1
WHEN m.SIZE = 10 AND m.COLOUR = 'G' AND m.PATTERNID IN (001,002,008) THEN 2
ELSE NULL END
) x
WHERE x.Exclusion IS NULL



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Zephyris
Zephyris
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 18
Thanks Chris! That looks like it would be useful for other tasks that I have to perform as well :-)
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16464 Visits: 19556
Anytime.
Third row up in my sig block has links to a couple of outstanding articles on APPLY. Well worth a read.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Zephyris
Zephyris
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 18
Update: using adaptations of the code/ideas you both supplied, I have been able to correct a troublesome report at work. My bosses were very pleased! ;-) Thanks again!
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14339 Visits: 15978
Happy to help, and I'm glad it turned out so well. Please will you share your solution, in case anyone else has a similar issue and wonders how you resolved it?

Thanks
John
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search