ورود

View Full Version : راهنمایی از select چندتایی



asp2.net
چهارشنبه 04 اردیبهشت 1392, 12:20 عصر
با سلام من چندتا select بصورت زیر دارم آیا میشه بجای دستور چند select از یک دستور استفاده کنم که نتیجه را به من بده

select count(s1) from Tbl1 where s1=0
select count(s2) from Tbl1 where s2=0
select count(s3) from Tbl1 where s3=0
select count(s4) from Tbl1 where s4=0
select count(s5) from Tbl1 where s5=0
select count(s6) from Tbl1 where s6=0
select count(s7) from Tbl1 where s7=0
select count(s8) from Tbl1 where s8=0
select count(s9) from Tbl1 where s9=0
select count(s10) from Tbl1 where s10=0

ممنون میشم راهنمایی کنید

mehdi.fni
چهارشنبه 04 اردیبهشت 1392, 12:42 عصر
اینو امتهان کن

select count(s2) from Tbl1 where s2=0
union all select count(s3) from Tbl1 where s3=0
union all select count(s4) from Tbl1 where s4=0
union all select count(s5) from Tbl1 where s5=0
union all select count(s6) from Tbl1 where s6=0
union all select count(s7) from Tbl1 where s7=0
union all select count(s8) from Tbl1 where s8=0
union all select count(s9) from Tbl1 where s9=0
union all select count(s10) from Tbl1 where s10=0

cherchil_hra
چهارشنبه 04 اردیبهشت 1392, 15:19 عصر
از این روش هم می تونی استفاده کنی :

SELECT myColumn,sum(case when myval=0 THEN 1 ELSE 0 END)as Result
FROM ( SELECT * FROM test ) t
UNPIVOT(MyVal FOR MyColumn IN (s1, s2, s3, s4, s5,s6,s7,s8,s9,s10)) AS unpvt
GROUP BY myColumn

موفق باشید!