Author |
Topic |
sql_dan
Starting Member
43 Posts |
Posted - 2014-08-08 : 05:46:14
|
Ok firstly my apologies for this, I have searched for the answer but can't see anything relevant.I have a query with the following data set:Person Seen 1 Seen 2 Order 72 Red Square 1 72 Red Square 2 140 Blue Circle 1 631 Green Rect 1 631 Green Flat 2 631 Green Round 3 631 Green Sphere 4 2753 Pink Circle 1 2753 Orange Flat 2 2753 Orange Bleep 3 2753 Orange Circle 4 What i'm trying to achieve is that if in the Seen 2 column any of the following are seen: - Beep - RoundThen mark all of the records for the person with a 1 so it looks like this:Person Seen 1 Seen 2 Order COUNTIF72 Red Square 1 072 Red Square 2 0140 Blue Circle 1 1631 Green Rect 1 0631 Green Flat 2 0631 Green Round 3 0631 Green Sphere 4 02753 Pink Circle 1 12753 Orange Flat 2 12753 Orange Bleep 3 12753 Orange Circle 4 1Any help or guidance would be brilliant. I just cant get my head around it!Thanks in advanceDan__________________________________________________If you cant sleep at night, its not the coffee its the bunk! |
|
ahmeds08
Aged Yak Warrior
737 Posts |
Posted - 2014-08-08 : 06:13:35
|
why is the countif value 1 for person 140?seen 2 is not either Beep or RoundJaveed Ahmed |
 |
|
sql_dan
Starting Member
43 Posts |
Posted - 2014-08-08 : 07:30:39
|
Hi Javeed,That is what I am trying to code. If any of the Seen 2 codes I am looking for (Beep, Round) happened for a person, mark all records for that person with a 1, not just the instances of where they happened.Sorry for any confusion.quote: Originally posted by ahmeds08 why is the countif value 1 for person 140?seen 2 is not either Beep or RoundJaveed Ahmed
__________________________________________________If you cant sleep at night, its not the coffee its the bunk! |
 |
|
MichaelJSQL
Constraint Violating Yak Guru
252 Posts |
Posted - 2014-08-08 : 07:52:20
|
You really haven't cleared things up. Neither person you have mentioned have either of those two codes. The closest is 2753 who has bLeep not beep.140 Blue Circle 1 12753 Pink Circle 1 12753 Orange Flat 2 12753 Orange Bleep 3 12753 Orange Circle 4 1 |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-08-08 : 08:15:02
|
MAX(CASE WHEN [Seen 2] IN ('Beep', 'Round') THEN 1 ELSE 0 END) OVER (PARTITION BY Person) Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
 |
|
sql_dan
Starting Member
43 Posts |
Posted - 2014-08-08 : 08:32:27
|
YES! Thank you SwePeseExactly what I was after!quote: Originally posted by SwePeso MAX(CASE WHEN [Seen 2] IN ('Beep', 'Round') THEN 1 ELSE 0 END) OVER (PARTITION BY Person) Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
__________________________________________________If you cant sleep at night, its not the coffee its the bunk! |
 |
|
MichaelJSQL
Constraint Violating Yak Guru
252 Posts |
Posted - 2014-08-08 : 08:58:11
|
yeah - but you did not get the result set you were asking for: 140 is still 0 : I think your result set was wrong in your example.Person Seen1 Seen2 SomeOrder (No column name)72 Red Square 1 072 Red Square 2 0140 Blue Circle 1 0631 Green Rect 1 1631 Green Flat 2 1631 Green Round 3 1631 Green Sphere 4 12753 Pink Circle 1 02753 Orange Flat 2 02753 Orange Bleep 3 02753 Orange Circle 4 0 |
 |
|
sql_dan
Starting Member
43 Posts |
Posted - 2014-08-08 : 09:10:12
|
Hi MichaelJSQLIndeed the example was wrong - the code is working exactly as intended now!Thanks very much for your help.Sorry for any confusion!__________________________________________________If you cant sleep at night, its not the coffee its the bunk! |
 |
|
|