There are two problems with the query below...
1. I'm trying to populate the field 'Territory_Code' with '900' if the Conn.Rep_Code exists in Table CCR, and populate it with the Conn.Territory_Code if it doesn't. I've got the first part working (populating certain records with '900') but you're obviously not allowed to use the word NOT after the 'WHEN' clause, so all the other records are being populated with NULL. Any suggestions as to how I can get this working ?
The second problem...This query uses GROUPING and I do NOT want to group on Rep_Code, but I need to test for Rep_Code in the above test, and it requires me to put it in the list.
DO I need to use a subquery ? I'm having REAL trouble getting subqueries to work, I'm struggling to understand them. I've read Transact SQL help and seen some examples but they tend to keep it simple and don't mix subselects and grouping. Any way around this ?...otherwise I'm up shit creek without a paddle.
SELECT Conn.Store, CASE NConn.Rep_Code WHEN (SELECT
CCR.Rep_Code FROM CallCentreReps CCR WHERE CAST
(NConn.Rep_Code AS VarChar) = CAST(CCR.Rep_Code AS VarChar))
THEN '900' WHEN NOT (SELECT Rep_Code FROM CallCentreReps
CCR WHERE NOT CAST(NConn.Rep_Code AS VarChar) = CAST
(CCR.Rep_Code AS VarChar)) THEN Conn.Territory_Code END AS
Territory_Code,
Conn.Profit, Conn.Profit_Target, Conn.LBU, Conn.LBSO,
Conn.FRU, Conn.FRN, Conn.FRunner_Target,
Conn.FR_Perf, Conn.PSU, Conn.PSN, Conn.PSaver_Target,
Conn.PS_Perf, Conn.UGPlanInc, Conn.OtherNew, Conn.TotalNew,
Conn.TotalUG, Conn.Handsets,
IsNull(Conn.Profit,0) + Sum(IsNull(NConn.Profit,0)) AS
Total_Profit,
CASE Conn.Profit_Target WHEN 0 THEN 0 ELSE (IsNull
(Conn.Profit,0) + Sum(IsNull(NConn.Profit,0))) /
Conn.Profit_Target END AS Profit_Perf
FROM ConnSummMTD Conn LEFT JOIN VW_Rep_Non_Conn_Profit
NConn ON Conn.Territory_Code = NConn.Territory_Code
WHERE NConn.so_invoice_date >= '01-Sep-2001' AND
NConn.so_invoice_date <= '17-Sep-2001'
GROUP BY
Store,NConn.Territory_Code,Rep_Code,Conn.Profit,Profit_Targe
t,LBU,LBSO,FRU,FRN,FRunner_Target,FR_Perf,PSU,PSN,PSaver_Tar
get,PS_Perf,UGPlanInc,OtherNEW,TotalNew,TotalUG,Handsets
1. I'm trying to populate the field 'Territory_Code' with '900' if the Conn.Rep_Code exists in Table CCR, and populate it with the Conn.Territory_Code if it doesn't. I've got the first part working (populating certain records with '900') but you're obviously not allowed to use the word NOT after the 'WHEN' clause, so all the other records are being populated with NULL. Any suggestions as to how I can get this working ?
The second problem...This query uses GROUPING and I do NOT want to group on Rep_Code, but I need to test for Rep_Code in the above test, and it requires me to put it in the list.
DO I need to use a subquery ? I'm having REAL trouble getting subqueries to work, I'm struggling to understand them. I've read Transact SQL help and seen some examples but they tend to keep it simple and don't mix subselects and grouping. Any way around this ?...otherwise I'm up shit creek without a paddle.
SELECT Conn.Store, CASE NConn.Rep_Code WHEN (SELECT
CCR.Rep_Code FROM CallCentreReps CCR WHERE CAST
(NConn.Rep_Code AS VarChar) = CAST(CCR.Rep_Code AS VarChar))
THEN '900' WHEN NOT (SELECT Rep_Code FROM CallCentreReps
CCR WHERE NOT CAST(NConn.Rep_Code AS VarChar) = CAST
(CCR.Rep_Code AS VarChar)) THEN Conn.Territory_Code END AS
Territory_Code,
Conn.Profit, Conn.Profit_Target, Conn.LBU, Conn.LBSO,
Conn.FRU, Conn.FRN, Conn.FRunner_Target,
Conn.FR_Perf, Conn.PSU, Conn.PSN, Conn.PSaver_Target,
Conn.PS_Perf, Conn.UGPlanInc, Conn.OtherNew, Conn.TotalNew,
Conn.TotalUG, Conn.Handsets,
IsNull(Conn.Profit,0) + Sum(IsNull(NConn.Profit,0)) AS
Total_Profit,
CASE Conn.Profit_Target WHEN 0 THEN 0 ELSE (IsNull
(Conn.Profit,0) + Sum(IsNull(NConn.Profit,0))) /
Conn.Profit_Target END AS Profit_Perf
FROM ConnSummMTD Conn LEFT JOIN VW_Rep_Non_Conn_Profit
NConn ON Conn.Territory_Code = NConn.Territory_Code
WHERE NConn.so_invoice_date >= '01-Sep-2001' AND
NConn.so_invoice_date <= '17-Sep-2001'
GROUP BY
Store,NConn.Territory_Code,Rep_Code,Conn.Profit,Profit_Targe
t,LBU,LBSO,FRU,FRN,FRunner_Target,FR_Perf,PSU,PSN,PSaver_Tar
get,PS_Perf,UGPlanInc,OtherNEW,TotalNew,TotalUG,Handsets