2011-01-31 240 views
0

查詢1:SQL嵌套查詢

select 
PremiumYTDCurrent=Sum((AASI.Inv_Premium)*R.[Percent]), 
R.STAFF, L.Description, L.LINE_OF_BUSINESS 
from AAS_Invoice AASI,Invoice I,Revenue_Tracking R, Policy P, Line_Of_Business L 
where I.Invoice_No=convert(Char,Convert(int,AASI.Inv_Entry_Num)) 
and I.Invoice=R.Invoice 
and I.POLICY=P.POLICY 
and L.LINE_OF_BUSINESS=P.LINE_OF_BUSINESS 
and AASI.Inv_Acctcur>='201001' 
and AASI.Inv_Acctcur<='201004' 
and R.Organization=200 
and R.Activity_type='Broker' 
and R.STAFF=7600 
and R.[Percent]>0 
group by R.STAFF, L.Description, L.LINE_OF_BUSINESS 

問題2:

select 
PremiumYTDPrevious=Sum((AASI.Inv_Premium)*R.[Percent]), 
R.STAFF, L.Description, L.LINE_OF_BUSINESS 
from AAS_Invoice AASI,Invoice I,Revenue_Tracking R, Policy P, Line_Of_Business L 
where I.Invoice_No=convert(Char,Convert(int,AASI.Inv_Entry_Num)) 
and I.Invoice=R.Invoice 
and I.POLICY=P.POLICY 
and L.LINE_OF_BUSINESS=P.LINE_OF_BUSINESS 
and AASI.Inv_Acctcur>='200901' 
and AASI.Inv_Acctcur<='200904' 
and R.Organization=200 
and R.Activity_type='Broker' 
and R.STAFF=7600 
and R.[Percent]>0 
group by R.STAFF, L.Description, L.LINE_OF_BUSINESS 

這些2個查詢是除日期週期是相同的。我想要的是有一個查詢,但添加一個字段(PremiumYTDPrevious)。可以做到嗎?

感謝,

回答

1

是的,你可以做這樣的事情:

select 
PremiumYTDPrevious=Sum(CASE WHEN AASI.Inv_Acctcur>='200901' 
    AND AASI.Inv_Acctcur<= '200904' 
THEN (AASI.Inv_Premium)*R.[Percent] 
ELSE 0 END), 
PremiumYTDCurrent=Sum(CASE WHEN AASI.Inv_Acctcur>='201001' 
    AND AASI.Inv_Acctcur<= '201004' 
THEN (AASI.Inv_Premium)*R.[Percent] 
ELSE 0 END), 
[... the rest of your query ...] 
+0

這一個工程好:)謝謝! – 2011-01-31 14:39:20

1
select 
'CurrentRange' as 'theRange', 
PremiumYTDCurrent=Sum((AASI.Inv_Premium)*R.[Percent]), 
R.STAFF, L.Description, L.LINE_OF_BUSINESS 
from AAS_Invoice AASI,Invoice I,Revenue_Tracking R, Policy P, Line_Of_Business L 
where I.Invoice_No=convert(Char,Convert(int,AASI.Inv_Entry_Num)) 
and I.Invoice=R.Invoice 
and I.POLICY=P.POLICY 
and L.LINE_OF_BUSINESS=P.LINE_OF_BUSINESS 
and AASI.Inv_Acctcur>='201001' 
and AASI.Inv_Acctcur<='201004' 
and R.Organization=200 
and R.Activity_type='Broker' 
and R.STAFF=7600 
and R.[Percent]>0 
group by R.STAFF, L.Description, L.LINE_OF_BUSINESS 
UNION 
(
'PreviousRange' as 'theRange', 
PremiumYTDPrevious=Sum((AASI.Inv_Premium)*R.[Percent]), 
R.STAFF, L.Description, L.LINE_OF_BUSINESS 
from AAS_Invoice AASI,Invoice I,Revenue_Tracking R, Policy P, Line_Of_Business L 
where I.Invoice_No=convert(Char,Convert(int,AASI.Inv_Entry_Num)) 
and I.Invoice=R.Invoice 
and I.POLICY=P.POLICY 
and L.LINE_OF_BUSINESS=P.LINE_OF_BUSINESS 
and AASI.Inv_Acctcur>='200901' 
and AASI.Inv_Acctcur<='200904' 
and R.Organization=200 
and R.Activity_type='Broker' 
and R.STAFF=7600 
and R.[Percent]>0 
group by R.STAFF, L.Description, L.LINE_OF_BUSINESS 
) 

更新...去除

1
select 
PremiumYTDPrevious=(SELECT Sum(Inv_Premium*R.[Percent]) 
        FROM AAS_Invoice 
        WHERE I.Invoice = convert(Char,Convert(int,AASI.Inv_Entry_Num)) 
         and AASI.Inv_Acctcur>='200901' 
         and AASI.Inv_Acctcur<='200904'), 
PremiumYTDCurrent =(SELECT Sum(Inv_Premium*R.[Percent]) 
         FROM AAS_Invoice 
        WHERE I.Invoice = convert(Char,Convert(int,AASI.Inv_Entry_Num)) 
         and AASI.Inv_Acctcur>='201001' 
         and AASI.Inv_Acctcur<='201004'), 
R.STAFF, L.Description, L.LINE_OF_BUSINESS 
from Invoice I,Revenue_Tracking R, Policy P, Line_Of_Business L 
where I.Invoice_No=convert(Char,Convert(int,AASI.Inv_Entry_Num)) 
and I.Invoice=R.Invoice 
and I.POLICY=P.POLICY 
and L.LINE_OF_BUSINESS=P.LINE_OF_BUSINESS 
and R.Organization=200 
and R.Activity_type='Broker' 
and R.STAFF=7600 
and R.[Percent]>0