Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Kapital123
Starting Member
31 Posts |
Posted - 2014-03-01 : 07:02:28
|
Hello all,I have a rather interesting problem that I cannot figure out. Its properly best I give an example of what I am after:Table 1 (5 columns)Year Interval Prod_Line_1 Prod_Line_2 Result2012 1 A F F2012 1 B G B 2012 1 C H H2012 1 D I I2012 1 E J E 2012 2 A G G2012 2 B E E2012 2 I H ITable to ConstructYear Interval Prod_Line_1 Prod_Line_2 Result NEW_COLUMN2012 1 A F F .2012 1 B G B .2012 1 C H H .2012 1 D I I .2012 1 E J E .2012 2 A G G .2012 2 B E E .2012 2 I A I IOkay so let me explain. The first table above is what I currently have. The most important information here is stored in the 'Result' column where 'Interval' = 1. What I would like to construct is the second table above which creates a new column whereby if the 'Result' column for 'Interval' = 1 exists in either Prod_Line_A or Prod_Line_B then to populate the 'new column' with the respective letter. So for example,see 'I'. If it doesn't exist, then just leave it empty e.g. see the row for interval '2' where A and G are not existent for the previous interval in the 'Result' column. There is one rule however, we can see that 'B' and 'I' exist in the 'Result' column for 'Interval' 1, however because they are lined up against each other for 'Interval' 2 I would like the new column to still generate a null. There needs to be mutual exclusivity. Now this sounds harder than it is, but the idea is very simple. Its essentially like taking the previous week's result and comparing to the current week results. The only catch is that the products from the previous week have to exist in the current week, those that don't exist are irrelevant.If anyone on here has the solution, I'd love to hear your thoughts. I'm sure there are some SQL tricks that I am unaware of that can solve this. |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-03-01 : 08:00:17
|
i have read through your thread more than 10 times and still no clue what are you trying to achieve quote: if the 'Result' column for 'Interval' = 1 exists in either Prod_Line_A or Prod_Line_B then to populate the 'new column' with the respective letter.
What is the respective letter ? What i see you have here is dot ?2012 1 A F F .2012 1 B G B .2012 1 C H H .2012 1 D I I .2012 1 E J E . quote: So for example,see 'I'. If it doesn't exist, then just leave it empty
Can you highlight when row you are referring to ?i can see a "2012 1 D I I ." but the new column is dot not emptyquote: e.g. see the row for interval '2' where A and G are not existent for the previous interval in the 'Result' column
what row are you referring to ? What previous interval ?quote: There is one rule however, we can see that 'B' and 'I' exist in the 'Result' column for 'Interval' 1, however because they are lined up against each other for 'Interval' 2 I would like the new column to still generate a null
whate do you mean by "lined up" KH[spoiler]Time is always against us[/spoiler] |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
Kapital123
Starting Member
31 Posts |
Posted - 2014-03-01 : 19:22:38
|
My apologies guys. I will try to use a different analogy to tell the story. Lets pretend we're looking at soccer matches where we record the round played, the home team, the away team and the loser of that match. So we will have 5 columns in the table:Col1 - YearCol2 - RoundCol3 - Home TeamCol4 - Away TeamCol5 - LoserThe idea: I want to place a bet on all the teams in round 2 that were losers in round 1. But sometimes the losers in round 1 won't play in round 2 because they have a bye. Also sometimes we may find that two losers from round 1 will play each other in round 2, I want to avoid that scenario also as it makes no sense to place a bet on both teams if they play each other.So I want to somehow match up the round 2 matches with the losers from round 1. For example, in round 1 there was a match where Blues Vs Blacks and the Blues lost. In round 2, the Blues play the Reds. I want a new column alongside that tells me that Blues were the loser in round 1.I hope that makes sense |
|
|
Kapital123
Starting Member
31 Posts |
Posted - 2014-03-01 : 19:48:13
|
In terms of the approach, perhaps some of you SQL guru's know a better approach. Sticking with the same sport analogy I just want to marry up the losers of the previous round to the games that their playing in round 2.If I had to provide sample data it would be something like this:Year Round Home_Team Away_Team Loser2012 1 Reds Storm Storm2012 1 Yellows Ravens Yellows2012 1 Blues Blacks Blues2012 1 Greens Lions Lions2012 2 Reds Blues Reds2012 2 Yellows Storm StormThe new table I'd imagine would look like:Year Round Home_Team Away_Team Loser Loser_Previous_Round2012 1 Reds Storm Storm .2012 1 Yellows Ravens Yellows .2012 1 Blues Blacks Blues .2012 1 Greens Lions Lions .2012 2 Reds Blues Reds Blues2012 2 Yellows Storm Storm . NOTE: in the final row above, both the Yellows and Storm were losers in round 1 so I just leave the value null otherwise it wouldn't make sense.Any questions please let me know. I keen to resolve this.Table Two above has a new columnCol6 - Loser_Previous_Round (this is the one we hope to create) |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-03-02 : 03:19:36
|
[code]select *from yourtable t outer apply ( select Loser_Previous_Round = max(x.Loser) from yourtable x where x.[Round] < t.[Round] and ( x.Loser = t.Home_Team or x.Loser = t.Away_Team ) group by x.[Round] having count(*) = 1 ) lpr[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-03-02 : 10:01:47
|
[code]SELECT t.[Year],t.[Round],t.[Home_Team],t.[Away_Team],t.[Loser],t1.Loser AS Loser_Previous_RoundFROM(SELECT t.*,COUNT(1) OVER (PARTITION BY t.[Year],t.[Round],t.[Home_Team],t.[Away_Team],t.[Loser]) AS CntFROM YourTable tLEFT JOIN YourTable t1On (t1.Loser = t.Home_Team OR t1.Loser = Away_Team)AND t.[Year] = t1.[Year]AND t.[Round] = t1.[Round] + 1)tWHERE cnt = 1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|