PDA

View Full Version : رفع اشکال در ساب کوئری



masysh
شنبه 09 اردیبهشت 1385, 19:15 عصر
لطفا در رفع اشکال کوئری زیر کمک نمایید.

select personnel_name,personnel_code
,(select sum(personnl_prize)
from table1
where p_date between 'date1' and 'date2'
group by personnel_code) as month1
,(select sum(personnl_prize)
from table1
where p_date between 'date3' and 'date4'
group by personnel_code) as month2
from table1

Inspiration
یک شنبه 10 اردیبهشت 1385, 06:28 صبح
این Query دستوری اشکالی ندارد. ساختار جداول و صورت مسئلتون چیه؟؟

Kamyar.Kimiyabeigi
یک شنبه 10 اردیبهشت 1385, 07:47 صبح
در GROUP BY باید کلیه فیلدهای SELECT شرکت کنند

AminSobati
یک شنبه 10 اردیبهشت 1385, 12:47 عصر
فیلدهای Select در Group By شرکت دارند ولی من هم معتقدم در ساختار باید اشکالی باشه. اگر پیغام خطا رو پست کنین کمک میکنه...

masysh
یک شنبه 10 اردیبهشت 1385, 18:00 عصر
با تشکر از لطف دوستان
پیغام خطا به این شکل است:


Server: Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

JavanSoft
یک شنبه 10 اردیبهشت 1385, 18:05 عصر
این SubQuery بیش از 1 جواب را برمی گرداند بنابر این با استفاده از توابع Max,Min,... و یا Exist جواب را بصورتی دراورید که به ازای هر رکورد اصلی یک جواب در ساب باشد

masysh
دوشنبه 11 اردیبهشت 1385, 06:08 صبح
با تشکر
ممکنه بیشتر در این مورد کمک کنید.

AminSobati
دوشنبه 11 اردیبهشت 1385, 13:19 عصر
select personnel_name,personnel_code
,(select sum(personnl_prize)
from table1 t2
where (p_date between 'date1' and 'date2') and
t2.personnel_code=table1.personnel_code) as month1
,(select sum(personnl_prize)
from table1 t3
where (p_date between 'date3' and 'date4') and
t3.personnel_code=table1.personnel_code) as month2
from table1

masysh
دوشنبه 11 اردیبهشت 1385, 16:04 عصر
با تشکر فراوان از لطف استاد گرامی و دوستان
مشکل حل شد.


select distinct(personnel_code),personnel_name
,(select sum(personnel_prize)
from table1 t2
where (p_date between 'date1' and 'date2') and
t2.personnel_code=table1.personnel_code having sum(personnel_prize)>'amount1'
) as month1
,(select sum(personnel_prize)
from table1 t3
where (p_date between 'date3' and 'date4') and
t3.personnel_code=table1.personnel_code
having sum(personnel_prize)>'amount2') as month2
from table1
order by personnel_code