Вложенные запросы SQL - PullRequest
0 голосов
/ 31 января 2011

Запрос 1:

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). Возможно ли это сделать?

Спасибо
Ile

Ответы [ 3 ]

1 голос
/ 31 января 2011

Да, вы можете сделать что-то вроде этого:

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 ...]
1 голос
/ 31 января 2011
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
1 голос
/ 31 января 2011
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
)

Обновление ... удален

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...