| Author |
Topic |
|
callinnn
Starting Member
7 Posts |
Posted - 2007-05-16 : 09:20:12
|
| I have in my table something like thisCol1 Col26 O6 O6 C6 C6 C5 O5 Oi want the value as 6 iff all the correspondingrecords in col2 are Csimilarly, since for 5 there are no C it shouldnot pick record 5Please suggest me a query for this |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2007-05-16 : 09:51:23
|
| Wow, what do you want? I see the table and the sample data, but not expected outcome.[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
yumyum113
Starting Member
31 Posts |
Posted - 2007-05-16 : 09:56:42
|
| something like this?declare @temptable table( col1 int, col2 varchar(1))insert into @temptableselect 6,'O' union allselect 6,'O' union allselect 6,'C' union allselect 6,'C' union allselect 6,'C' union allselect 5,'O' union allselect 5,'O' union allselect 4,'C' union allselect 4,'C'select t1.col1from @temptable t1left join (select col1,count(col1) 'col1_count' from @temptable group by col1) t2 on t1.col1 = t2.col1where t1.col2 = 'C'group by t1.col1,t1.col2,t2.col1_counthaving count(t1.col2) = t2.col1_count |
 |
|
|
callinnn
Starting Member
7 Posts |
Posted - 2007-05-16 : 09:57:03
|
| I have in my table something like thisCol1 Col26 O6 O6 C6 C6 C5 O5 O3 C3 Ci want the value as 6 iff all the correspondingrecords in col2 are Csimilarly, since for 5 there are no C it shouldnot pick record 5Please suggest me a query for this*i want to select col1 where all col2='C' *It should give me an output as*Col1 3 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-16 : 10:20:30
|
[code]select c.col1from( select col1, cnt = count(*) from tbl where col2 = 'C' group by col1) cinner join( select col1, cnt = count(*) from tbl group by col1) aon c.col1 = a.col1where c.cnt = a.cnt[/code] KH |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-16 : 10:30:41
|
| [code]declare @t table( a int, b varchar(1))insert @tselect 6, 'O' union allselect 6, 'O' union allselect 6, 'C' union allselect 6, 'C' union allselect 6, 'C' union allselect 5, 'O' union allselect 5, 'O' union allselect 3, 'C' union allselect 3, 'C'select afrom @tgroup by ahaving min(b) = max(b) and min(b) = 'C'[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-16 : 11:42:30
|
| If only O and C are available as option value,select afrom @tgroup by ahaving max(b) = 'C'orselect afrom @tgroup by ahaving min(b) = 'C' and min(b) = 'C'Peter LarssonHelsingborg, Sweden |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-16 : 11:45:04
|
quote: Originally posted by Peso If only O and C are available as option value,select afrom @tgroup by ahaving max(b) = 'C'orselect afrom @tgroup by ahaving min(b) = 'C' and minmax(b) = 'C'Peter LarssonHelsingborg, Sweden
Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-16 : 11:51:03
|
| Thanks.Peter LarssonHelsingborg, Sweden |
 |
|
|
callinnn
Starting Member
7 Posts |
Posted - 2007-05-17 : 09:57:41
|
| Thank you for giving me such a nice detailed solutionI m very thankful to u since i got it in half an hour of my posting and since it was really urgentI don't have words to really than but it is as many timesas this number9999999999999999999999999999999999999999999999999999999+1 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-05-17 : 10:42:34
|
<<I don't have words to really than but it is as many timesas this number9999999999999999999999999999999999999999999999999999999+1>>Better say "Infinity" MadhivananFailing to plan is Planning to fail |
 |
|
|
|