PDA

View Full Version : جلوگیری از نمایش سطرهایی که در شرط نیستند.



masysh
چهارشنبه 13 اردیبهشت 1385, 16:42 عصر
جلوگیری از نمایش سطرهایی که مقادیر مربوط به ماهها خالی می باشند.
با این کوئری در فیلد نام و کد تمام کد ونامهایی که در جدول موجود باشد را نمایش می دهد .و عدم وجود مقدار برای ستون های دیگر را نادیده می گیرد.یعنی اگر مقادیر تمام ستونهای دیگر خالی باشد و مقدار دهی نشده باشند یا در شرط صدق نکنند باز نام را نمایش می دهد.
اگر بخواهم فقط نامهایی را نمایش دهد که حداقل یکی از ماههای آن دارای مقدار باشد چه باید بکنم؟
این کد را برای تمام 12 ماه نوشته ام.



select distinct personnel_code,personnel_name,
(select case when sum(personnel_prize)>=@fb then sum(personnel_prize)+@f
else sum(personnel_prize) end as f
from table1 as tfarvardin
where (P_date between @f1 and @f2 ) and (personnel_code=table1.personnel_code)
having (@f=@f11) and (@fb=@fb11)) as 'farvardin'
(select case when sum(personnel_prize)>=@ob then sum(personnel_prize)+@o
else sum(personnel_prize) end as o
from table1 as tordibehesht
where (P_date between @o1 and @o2) and (personnel_code=table1.personnel_code)
having (@o=@o11) and (@ob=@ob11)) as 'ordibehesht'
from table1
where sex=@sex
order by personnel_code

AminSobati
چهارشنبه 13 اردیبهشت 1385, 17:24 عصر
دوست عزیزم،
با توجه به اینکه خروجی این Query رو ندیدیم، درک صورت مساله کمی دشواره. اگر یک Screen Shot از نتیجه اجرای اون بگذارید (در صورت امکان) کمک بزرگی میکنه

masysh
چهارشنبه 13 اردیبهشت 1385, 19:30 عصر
سطرهای با کد پرسنلی 1 و 4 و 5 نباید نمایش داده شوند چون در هیچ کدام از ماه هایشان مقادیری ندارند.

AminSobati
چهارشنبه 13 اردیبهشت 1385, 19:54 عصر
قسمت where رو به این شکل بنویسید:


where sex=@sex and coalesce(farvardin,ordibehesht) is not null

masysh
پنج شنبه 14 اردیبهشت 1385, 17:35 عصر
جواب نداد.
لطفا بیشتر راهنمایی کنید.

Server: Msg 207, Level 16, State 3, Line 16
Invalid column name 'farvardin'.
Server: Msg 207, Level 16, State 1, Line 16
Invalid column name 'farvardin'.
Server: Msg 207, Level 16, State 1, Line 16
Invalid column name 'ordibehesht'.

AminSobati
جمعه 15 اردیبهشت 1385, 18:37 عصر
select * from
(select distinct personnel_code,personnel_name,
(select case when sum(personnel_prize)>=fb then sum(personnel_prize)+f
else sum(personnel_prize) end as f
from table1 as tfarvardin
where (P_date between @f1 and @f2 ) and (personnel_code=table1.personnel_code)
having (@f=@f11) and (@fb=@fb11)) as 'farvardin',
(select case when sum(personnel_prize)>=@ob then sum(personnel_prize)+@o
else sum(personnel_prize) end as o
from table1 as tordibehesht
where (P_date between @o1 and @o2) and (personnel_code=table1.personnel_code)
having (@o=@o11) and (@ob=@ob11)) as 'ordibehesht'
from table1
) tmp
where sex=@sex and coalesce(farvardin,ordibehesht) is not null
order by personnel_code