2012-10-24 88 views
0

使用Teradata,我有一個列中有6個選項的數據,我想要的是在一個單獨列中的六個選項中的每個選項與它有多少條目的計數, 6個單獨select statments和一個在底部選擇加入它一起,反正是有得到它在一個聲明中Sorting Columns

輸出看起來是這樣的:

AgentName|emp_number|Manager_Empl_No| Hardware/Fault| Customer Experience| Prefer another Provider| Coverage/Serviceability| Costs Service| No Longer Required |Customer Usage 
name 1| xxxx| xxxx |15| 27| 10| 3| 7| 22| 6 
Name 2| xxxx| xxxx |19 |6 |29 |22 |10 |42 |4 

和我的代碼....

--- Hardware fault --- 
create volatile table hardware as 
(
select 
CSR_FirstName||' '||CSR_LastName as AgentName 
,emp_number 
,Manager_empl_no 
,count(service) as "Hardware/Fault" 
from ipshare.ORR_retention_database a 
full join RMOIDS 
on emp_number = Employee_No 
where date_created between (sel start_date from startend_date) and (sel end_date from startend_date) 
and referred_product = 'Mobile' 
and decline_reason1 = 'Hardware/Fault' 
group by (AgentName,emp_number,Manager_empl_no,decline_reason1) 
qualify RANK() over (partition by AgentName order by AgentName DESC)=1 
) 
with data on commit preserve rows; 
sel * from hardware 


--- Customer Experience --- 
create volatile table custex as 
(
select 
CSR_FirstName||' '||CSR_LastName as AgentName 
,emp_number 
,Manager_empl_no 
,count(service) as "Customer Experience" 
from ipshare.ORR_retention_database a 
full join RMOIDS 
on emp_number = Employee_No 
where date_created between (sel start_date from startend_date) and (sel end_date from startend_date) 
and referred_product = 'Mobile' 
and decline_reason1 = 'Customer Experience' 
group by (AgentName,emp_number,Manager_empl_no,decline_reason1) 
qualify RANK() over (partition by AgentName order by AgentName DESC)=1 
) 
with data on commit preserve rows; 

---Prefer another Provider 
create volatile table pap as 
(
select 
CSR_FirstName||' '||CSR_LastName as AgentName 
,emp_number 
,Manager_empl_no 
,count(service) as "Prefer another Provider" 
from ipshare.ORR_retention_database a 
full join RMOIDS 
on emp_number = Employee_No 
where date_created between (sel start_date from startend_date) and (sel end_date from startend_date) 
and referred_product = 'Mobile' 
and decline_reason1 = 'Prefer another Provider' 
group by (AgentName,emp_number,Manager_empl_no,decline_reason1) 
qualify RANK() over (partition by AgentName order by AgentName DESC)=1 
) 
with data on commit preserve rows; 

----Coverage/Serviceability 
create volatile table coverage as 
(
select 
CSR_FirstName||' '||CSR_LastName as AgentName 
,emp_number 
,Manager_empl_no 
,count(service) as "Coverage/Serviceability" 
from ipshare.ORR_retention_database a 
full join RMOIDS 
on emp_number = Employee_No 
where date_created between (sel start_date from startend_date) and (sel end_date from startend_date) 
and referred_product = 'Mobile' 
and decline_reason1 = 'Coverage/Serviceability' 
group by (AgentName,emp_number,Manager_empl_no,decline_reason1) 
qualify RANK() over (partition by AgentName order by AgentName DESC)=1 
) 
with data on commit preserve rows; 

---Cost 

create volatile table cost as 
(
select 
CSR_FirstName||' '||CSR_LastName as AgentName 
,emp_number 
,Manager_empl_no 
,count(service) as "Costs" 
from ipshare.ORR_retention_database a 
full join RMOIDS 
on emp_number = Employee_No 
where date_created between (sel start_date from startend_date) and (sel end_date from startend_date) 
and referred_product = 'Mobile' 
and decline_reason1 = 'Costs' 
group by (AgentName,emp_number,Manager_empl_no,decline_reason1) 
qualify RANK() over (partition by AgentName order by AgentName DESC)=1 
) 
with data on commit preserve rows; 

--Service no longer required 
create volatile table snlr as 
(
select 
CSR_FirstName||' '||CSR_LastName as AgentName 
,emp_number 
,Manager_empl_no 
,count(service) as "Service No Longer Required" 
from ipshare.ORR_retention_database a 
full join RMOIDS 
on emp_number = Employee_No 
where date_created between (sel start_date from startend_date) and (sel end_date from startend_date) 
and referred_product = 'Mobile' 
and decline_reason1 = 'Service No Longer Required' 
group by (AgentName,emp_number,Manager_empl_no,decline_reason1) 
qualify RANK() over (partition by AgentName order by AgentName DESC)=1 
) 
with data on commit preserve rows; 

--Customer Usage 
create volatile table usage as 
(
select 
CSR_FirstName||' '||CSR_LastName as AgentName 
,emp_number 
,Manager_empl_no 
,count(service) as "Customer Usage" 
from ipshare.ORR_retention_database a 
full join RMOIDS 
on emp_number = Employee_No 
where date_created between (sel start_date from startend_date) and (sel end_date from startend_date) 
and referred_product = 'Mobile' 
and decline_reason1 = 'Customer Usage' 
group by (AgentName,emp_number,Manager_empl_no,decline_reason1) 
qualify RANK() over (partition by AgentName order by AgentName DESC)=1 
) 
with data on commit preserve rows; 

sel a.* 
,b."Customer Experience" 
,c."Prefer another Provider" 
,d."Coverage/Serviceability" 
,e."Costs" 
,f."Service No Longer Required" 
,g."Customer Usage" 
from hardware a 
inner join custex b 
on a.emp_number = b.emp_number 
inner join pap c 
on a.emp_number = c.emp_number 
inner join coverage d 
on a.emp_number = d.emp_number 
inner join cost e 
on a.emp_number = e.emp_number 
inner join snlr f 
on a.emp_number = f.emp_number 
inner join usage g 
on a.emp_number = g.emp_number 

回答

0

我還沒有驗證你的SQL的其餘部分,但是我相信你可以通過在聚合函數中使用CASE語句來消除多個易失性表並通過源表傳遞。

SELECT CSR_FirstName||' '||CSR_LastName as AgentName 
    , emp_number 
    , Manager_empl_no 
    , COUNT(CASE WHEN Decline_Reason1 = 'Customer Experience' 
        THEN Service 
        ELSE NULL 
      END) AS CustomerExperinceCount_ 
    , COUNT(CASE WHEN Decline_Reason1 = /* {Next Reason} */ 
        THEN Service 
        ELSE NULL 
      END) AS NextReasonCount_ 
    FROM ipshare.ORR_retention_database a 
    FULL JOIN 
     RMOIDS 
    ON emp_number = Employee_No 
WHERE date_created between (sel start_date from startend_date) 
         and (sel end_date from startend_date) 
    AND referred_product = 'Mobile' 
GROUP BY (AgentName,emp_number,Manager_empl_no,decline_reason1) 
QUALIFY RANK() over (partition by AgentName order by AgentName DESC)=1