Ok , Here is the sticky situation :-
consider table FLOWERS.
Code:
[b]
FLWRID FLWRLGCYID FLWRTYP FLWRNM LNGTH WDTH SLSIND[/b]
1 101 Stemmed Rose 10" 2" 1
2 102 Stemmed Rose 20" 3"
3 103 Stemmed Rose 1" 4"
4 104 Stemmed Rose 13" 5"
5 105 Delicate Lily 14" 2"
6 106 Petal Rose 9.5" 3"
7 107 Delicate Lily 1" 4"
8 108 Hardy Tulip 2" 3" 1
9 109 Delicate Orchid 3" 2"
10 110 Delicate Tulip 4" 1"
11 111 Petal Rose 6" 3" 1
12 112 Fragrant Rose 4.5" 4"
13 113 Petal Rose 6.2" 3"
14 114 Fragrant Rose 4.3" 4"
15 115 Fragrant Rose 4.1" 4"
Now the requirement is that i should compare the flowers with the sales ind = 1 (STEP A), and see if there are other records
which have the same flower name , but with a different flower type (STEP B). Among these records , i should pick only one
for each flower type and display all details (STEP C).
to illustrate one example:-
Code:
Step A --
[b]
FLWRID FLWRLGCYID FLWRTYP FLWRNM LNGTH WDTH SLSIND[/b]
1 101 Stemmed Rose 10" 2" 1
Step B --
[b]
FLWRID FLWRLGCYID FLWRTYP FLWRNM LNGTH WDTH SLSIND[/b]
6 106 Petal Rose 9.5" 3"
11 111 Petal Rose 6" 3" 1
12 112 Fragrant Rose 4.5" 4"
13 113 Petal Rose 6.2" 3"
14 114 Fragrant Rose 4.3" 4"
15 115 Fragrant Rose 4.1" 4"
Step C --
[b]
FLWRID FLWRLGCYID FLWRTYP FLWRNM LNGTH WDTH SLSIND[/b]
6 106 Petal Rose 9.5" 3"
12 112 Fragrant Rose 4.5" 4"
While the business asks me to pick any random row, i intend to take the one with the min flowerid , so that there is some logic in selection
I would have to write several subqueries , atelast one each for Step A & Step B and 2 for Step C ( one for selecting Min flower id , and one for getting ALL the details for that Min flower id)
I was wondering if anyone had a simpler way to achieve this.
This is a one time cleanup operation , and i can go ahead with the slow solution of subqueries, or even write a procedure...but where there is a chance of learning and improvement in SQL , i want to use it to the max and learn.
Let me know your thoughts.
Thanks for reading!
Regards,
S. Jayaram Uparna .