Say I have a query that returns none grouped data, as the first query below does, and I wish to group the data by summing and counting some fields and grouping on the remaining fields, as shown in the second query.
Why in this case do the count fields return 2 instead of the expected 1?
EDIT:
I have just realized its counting distinct rows so in the RequestsOnSite it has 114065 and 0 resulting in a count of 2. Is there a way to discount 0 valued cells?
Query:
SELECT 'Month' AS [Period],
ISNULL(Zone.ZoneID,'') AS ZoneID,
ISNULL(Zone.ZoneName,'') AS ZoneName,
ISNULL(Region.RegionCode,'') AS RegionCode,
ISNULL(Region.RegionName,'') AS RegionName,
Branch.BranchID,
ISNULL(Branch.BranchName,'') AS BranchName,
SR01.ServicingRep,
ISNULL(LCRep.RepName,'') AS RepName,
CASE WHEN ProductivityRequests.RequestType <> 'MN' AND (ProductivityRequests.OnOffSite = 'ON' OR ProductivityRequests.OnOffSiteSvc = 'ON') THEN ProductivityRequests.ReportKey ELSE 0 END AS RequestsOnSite,
CASE WHEN ProductivityHours.RequestType <> 'MN' AND (ProductivityHours.OnOffSite = 'ON' OR ProductivityHours.OnOffSiteSvc = 'ON') THEN ProductivityHours.TotalOnSiteTime ELSE 0 END AS HoursOnSite,
CASE WHEN ProductivityRequests.RequestType <> 'MN' AND (ProductivityRequests.OnOffSite = 'OF' OR ProductivityRequests.OnOffSiteSvc = 'OF') THEN ProductivityRequests.ReportKey ELSE 0 END AS RequestsOffSite,
CASE WHEN ProductivityHours.RequestType <> 'MN' AND (ProductivityHours.OnOffSite = 'OF' OR ProductivityHours.OnOffSiteSvc = 'OF') THEN ProductivityHours.TotalOffSiteTime ELSE 0 END AS HoursOffSite,
CASE WHEN (ProductivityRequests.RequestType = 'NR') THEN ProductivityRequests.ReportKey ELSE 0 END AS RequestsNonReport,
CASE WHEN (ProductivityHours.RequestType = 'NR') THEN ProductivityHours.TotalNonReportTime ELSE 0 END AS HoursNonReport,
ProductivityRequests.OnOffSite,
ProductivityRequests.OnOffSiteSvc
FROM SR01
INNER JOIN ProductivityHours ON ProductivityHours.ReportKey = SR01.ReportKey
INNER JOIN ProductivityRequests ON ProductivityRequests.ReportKey = SR01.ReportKey
LEFT JOIN LCRep ON SR01.ServicingRep = LCRep.RepID
LEFT JOIN Branch ON SR01.ServicingBranch = Branch.BranchID
LEFT JOIN Region ON Region.RegionCode = Branch.Region
LEFT JOIN Zone ON Zone.ZoneAbbrev = Region.Zone
WHERE ISNULL(SR01.ServicingBranch,'-') <> '-'
AND SR01.[Status]='X'
AND SR01.DateComplete BETWEEN DATEADD(month, DATEDIFF(month, 0, #DateTo), 0) AND #DateTo
AND Zone.ZoneID IN (#Zone)
AND Region.RegionCode IN (#Region)
AND Branch.BranchID IN (#Branch)
AND LCRep.RepID IN(#RepID)
AND (#EquipmentBreakdown = 0 OR (#EquipmentBreakdown = 1 AND #PropertyCasualty = 1) OR (LCRep.EB = #EquipmentBreakdown))
AND (#PropertyCasualty = 0 OR (#EquipmentBreakdown = 1 AND #PropertyCasualty = 1) OR (LCRep.PC = #PropertyCasualty))
NOTE: OnOffSite and OnOffSiteSvc are shown here just for clarity and will not appear in the final query.
Resultset
Aggregate query
SELECT 'Month' AS [Period],
ISNULL(Zone.ZoneID,'') AS ZoneID,
ISNULL(Zone.ZoneName,'') AS ZoneName,
ISNULL(Region.RegionCode,'') AS RegionCode,
ISNULL(Region.RegionName,'') AS RegionName,
Branch.BranchID,
ISNULL(Branch.BranchName,'') AS BranchName,
SR01.ServicingRep,
ISNULL(LCRep.RepName,'') AS RepName,
COUNT(DISTINCT CASE WHEN ProductivityRequests.RequestType <> 'MN' AND (ProductivityRequests.OnOffSite = 'ON' OR ProductivityRequests.OnOffSiteSvc = 'ON') THEN ProductivityRequests.ReportKey ELSE 0 END) AS RequestsOnSite,
SUM(DISTINCT CASE WHEN ProductivityHours.RequestType <> 'MN' AND (ProductivityHours.OnOffSite = 'ON' OR ProductivityHours.OnOffSiteSvc = 'ON') THEN ProductivityHours.TotalOnSiteTime ELSE 0 END) AS HoursOnSite,
COUNT(DISTINCT CASE WHEN ProductivityRequests.RequestType <> 'MN' AND (ProductivityRequests.OnOffSite = 'OF' OR ProductivityRequests.OnOffSiteSvc = 'OF') THEN ProductivityRequests.ReportKey ELSE 0 END) AS RequestsOffSite,
SUM(DISTINCT CASE WHEN ProductivityHours.RequestType <> 'MN' AND (ProductivityHours.OnOffSite = 'OF' OR ProductivityHours.OnOffSiteSvc = 'OF') THEN ProductivityHours.TotalOffSiteTime ELSE 0 END) AS HoursOffSite,
COUNT(DISTINCT CASE WHEN (ProductivityRequests.RequestType = 'NR') THEN ProductivityRequests.ReportKey ELSE 0 END) AS RequestsNonReport,
SUM(DISTINCT CASE WHEN (ProductivityHours.RequestType = 'NR') THEN ProductivityHours.TotalNonReportTime ELSE 0 END) AS HoursNonReport
FROM SR01
INNER JOIN ProductivityHours ON ProductivityHours.ReportKey = SR01.ReportKey
INNER JOIN ProductivityRequests ON ProductivityRequests.ReportKey = SR01.ReportKey
LEFT JOIN LCRep ON SR01.ServicingRep = LCRep.RepID
LEFT JOIN Branch ON SR01.ServicingBranch = Branch.BranchID
LEFT JOIN Region ON Region.RegionCode = Branch.Region
LEFT JOIN Zone ON Zone.ZoneAbbrev = Region.Zone
WHERE ISNULL(SR01.ServicingBranch,'-') <> '-'
AND SR01.[Status]='X'
AND SR01.DateComplete BETWEEN DATEADD(month, DATEDIFF(month, 0, #DateTo), 0) AND #DateTo
AND Zone.ZoneID IN (#Zone)
AND Region.RegionCode IN (#Region)
AND Branch.BranchID IN (#Branch)
AND LCRep.RepID IN(#RepID)
AND (#EquipmentBreakdown = 0 OR (#EquipmentBreakdown = 1 AND #PropertyCasualty = 1) OR (LCRep.EB = #EquipmentBreakdown))
AND (#PropertyCasualty = 0 OR (#EquipmentBreakdown = 1 AND #PropertyCasualty = 1) OR (LCRep.PC = #PropertyCasualty))
GROUP BY Zone.ZoneID, Zone.ZoneName, Region.RegionCode, Region.RegionName, Branch.BranchID, BranchName, SR01.ServicingRep, LCRep.RepName
Resultset:
Note that the RequestsOnSite, RequestsOffSite and RequestsNonReport all contain 2 rather than the expected 1.
OK, the answer was to use the NullIF operator as discussed in this Stack Overflow question
The resultant query would look like this
SELECT 'Month' AS [Period],
ISNULL(Zone.ZoneID,'') AS ZoneID,
ISNULL(Zone.ZoneName,'') AS ZoneName,
ISNULL(Region.RegionCode,'') AS RegionCode,
ISNULL(Region.RegionName,'') AS RegionName,
Branch.BranchID,
ISNULL(Branch.BranchName,'') AS BranchName,
SR01.ServicingRep,
ISNULL(LCRep.RepName,'') AS RepName,
COUNT(DISTINCT CASE WHEN ProductivityRequests.RequestType <> 'MN' AND (ProductivityRequests.OnOffSite = 'ON' OR ProductivityRequests.OnOffSiteSvc = 'ON') THEN NULLIF(ProductivityRequests.ReportKey, 0) ELSE NULL END) AS RequestsOnSite,
SUM(DISTINCT CASE WHEN ProductivityHours.RequestType <> 'MN' AND (ProductivityHours.OnOffSite = 'ON' OR ProductivityHours.OnOffSiteSvc = 'ON') THEN ProductivityHours.TotalOnSiteTime ELSE 0 END) AS HoursOnSite,
COUNT(DISTINCT CASE WHEN ProductivityRequests.RequestType <> 'MN' AND (ProductivityRequests.OnOffSite = 'OF' OR ProductivityRequests.OnOffSiteSvc = 'OF') THEN NULLIF(ProductivityRequests.ReportKey, 0) ELSE NULL END) AS RequestsOffSite,
SUM(DISTINCT CASE WHEN ProductivityHours.RequestType <> 'MN' AND (ProductivityHours.OnOffSite = 'OF' OR ProductivityHours.OnOffSiteSvc = 'OF') THEN ProductivityHours.TotalOffSiteTime ELSE 0 END) AS HoursOffSite,
COUNT(DISTINCT CASE WHEN (ProductivityRequests.RequestType = 'NR') THEN NULLIF(ProductivityRequests.ReportKey, 0) ELSE NULL END) AS RequestsNonReport,
SUM(DISTINCT CASE WHEN (ProductivityHours.RequestType = 'NR') THEN ProductivityHours.TotalNonReportTime ELSE 0 END) AS HoursNonReport
FROM SR01
INNER JOIN ProductivityHours ON ProductivityHours.ReportKey = SR01.ReportKey
INNER JOIN ProductivityRequests ON ProductivityRequests.ReportKey = SR01.ReportKey
LEFT JOIN LCRep ON SR01.ServicingRep = LCRep.RepID
LEFT JOIN Branch ON SR01.ServicingBranch = Branch.BranchID
LEFT JOIN Region ON Region.RegionCode = Branch.Region
LEFT JOIN Zone ON Zone.ZoneAbbrev = Region.Zone
WHERE ISNULL(SR01.ServicingBranch,'-') <> '-'
AND SR01.[Status]='X'
AND SR01.DateComplete BETWEEN DATEADD(month, DATEDIFF(month, 0, #DateTo), 0) AND #DateTo
AND Zone.ZoneID IN (#Zone)
AND Region.RegionCode IN (#Region)
AND Branch.BranchID IN (#Branch)
AND LCRep.RepID IN(#RepID)
AND (#EquipmentBreakdown = 0 OR (#EquipmentBreakdown = 1 AND #PropertyCasualty = 1) OR (LCRep.EB = #EquipmentBreakdown))
AND (#PropertyCasualty = 0 OR (#EquipmentBreakdown = 1 AND #PropertyCasualty = 1) OR (LCRep.PC = #PropertyCasualty))
GROUP BY Zone.ZoneID, Zone.ZoneName, Region.RegionCode, Region.RegionName, Branch.BranchID, BranchName, SR01.ServicingRep, LCRep.RepName
Related
SELECT bio.nama_lengkap AS nama,bio.jenis_pegawai,bio.progdi,bio.jabatan as jabatan1,bio.cabang,bio.tahun,
GROUP_CONCAT(CASE WHEN tb_penilaian.id_user = tb_user.id_user THEN rata_rata END) AS nilai,
round (avg(rata_rata),2) as rata
FROM tb_biodata as bio
INNER JOIN tb_penilaian ON tb_biodata.id_biodata = tb_penilaian.id_biodata
INNER JOIN tb_user ON tb_penilaian.id_user = tb_user.id_user
WHERE tb_biodata.jenis_pegawai = 'Tenaga Kependidikan' and tb_penilaian.tahun='2020' and tb_biodata.cabang = 'Majapahit 605' GROUP BY nama'
SELECT ... ,
SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN tb_penilaian.id_user = tb_user.id_user THEN rata_rata END), ',', 1) AS nilai1,
SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN tb_penilaian.id_user = tb_user.id_user THEN rata_rata END), ',', 2), ',', -1) AS nilai2,
SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN tb_penilaian.id_user = tb_user.id_user THEN rata_rata END), ',', -1) AS nilai3,
...
Alternatively:
SELECT bio.nama_lengkap AS nama,
bio.jenis_pegawai,
bio.progdi,
bio.jabatan as jabatan1,
bio.cabang,
bio.tahun,
CASE WHEN tb_penilaian.id_user = tb_user.id_user and numners.num = 1
THEN ANY_VALUE(rata_rata)
END AS nilai1,
CASE WHEN tb_penilaian.id_user = tb_user.id_user and numners.num = 2
THEN ANY_VALUE(rata_rata)
END AS nilai2,
CASE WHEN tb_penilaian.id_user = tb_user.id_user and numners.num = 3
THEN ANY_VALUE(rata_rata)
END AS nilai3,
round (avg(rata_rata),2) as rata
FROM tb_biodata as bio
INNER JOIN tb_penilaian ON tb_biodata.id_biodata = tb_penilaian.id_biodata
INNER JOIN tb_user ON tb_penilaian.id_user = tb_user.id_user
INNER JOIN (SELECT 1 num UNION SELECT 2 UNION SELECT 3) numbers
WHERE tb_biodata.jenis_pegawai = 'Tenaga Kependidikan'
and tb_penilaian.tahun='2020'
and tb_biodata.cabang = 'Majapahit 605'
GROUP BY nama'
I need to group the sum by different columns:
SUM1 group by Banco_Recarga
SUM2 group by BancoRetiro
SELECT Banco_Recarga, BancoRetiro, bancos_recargas.banco,
SUM(CASE WHEN Tipo='1' AND (Status = '1' OR Status = '2') THEN '1' ELSE 0 END) AS SUM1,
SUM(CASE WHEN Tipo='2' AND (Status = '1' OR Status = '2') THEN '1' ELSE 0 END) AS SUM2
FROM transaccionesrr
INNER JOIN bancos_recargas ON (transaccionesrr.Banco_Recarga = bancos_recargas.id)
WHERE Fecha_Recarga BETWEEN '2017-11-01' AND '2017-11-10' GROUP BY Banco_Recarga ORDER BY SUM1;
I have been looking for hours but i can't find a solution
Try this:
SELECT
CASE
WHEN Tipo = '1' AND (Status = '1' OR Status = '2') THEN Banco_Recarga
WHEN Tipo = '2' AND (Status = '1' OR Status = '2') THEN BancoRetiro
ELSE 'Unexpected'
END AS banco
, SUM(CASE WHEN Tipo = '1' AND (Status = '1' OR Status = '2') THEN '1' ELSE 0 END) AS sum1
, SUM(CASE WHEN Tipo = '2' AND (Status = '1' OR Status = '2') THEN '1' ELSE 0 END) AS sum2
FROM transaccionesrr
INNER JOIN bancos_recargas ON (transaccionesrr.Banco_Recarga = bancos_recargas.id)
WHERE Fecha_Recarga BETWEEN '2017-11-01' AND '2017-11-10'
GROUP BY
CASE
WHEN Tipo = '1' AND (Status = '1' OR Status = '2') THEN Banco_Recarga
WHEN Tipo = '2' AND (Status = '1' OR Status = '2') THEN BancoRetiro
ELSE 'Unexpected'
END
ORDER BY sum1, sum2
You may need to add conditions into the where clause.
I want to ask about mysql with rollup only get rollup value, this is my code
SELECT tbldept.CodeDept AS Dept,
SUM(CASE WHEN tblemp.Status = '1' THEN 1 ELSE 0 END) AS headcount,
SUM(CASE WHEN tblemp.Status = '1' THEN 1 ELSE 0 END) * 144 AS RegHrsEst,
CONCAT(ROUND(20), '%') AS TargetOT,
round(SUM(CASE WHEN tblemp.Status = '1' THEN 1 ELSE 0 END) * 144 * 0.2) AS OTHrsBdgt
FROM tbldept
LEFT JOIN tblemp
ON tbldept.CodeDept = tblemp.CodeDept AND
tblemp.class = "DIR"
WHERE tbldept.CodeDept != 'FIN' AND
tbldept.CodeDept != 'HRT' AND
tbldept.CodeDept != 'ISD'
GROUP BY tbldept.CodeDept with ROLLUP;
result of rollup query:
how can i only get the rollup values? Thanks before
If you just want the row of rollup, simply use having:
SELECT tbldept.CodeDept AS Dept,
SUM(CASE WHEN tblemp.Status = '1' THEN 1 ELSE 0 END) AS headcount,
SUM(CASE WHEN tblemp.Status = '1' THEN 1 ELSE 0 END) * 144 AS RegHrsEst,
CONCAT(ROUND(20), '%') AS TargetOT,
round(SUM(CASE WHEN tblemp.Status = '1' THEN 1 ELSE 0 END) * 144 * 0.2) AS OTHrsBdgt
FROM tbldept
LEFT JOIN tblemp
ON tbldept.CodeDept = tblemp.CodeDept AND
tblemp.class = "DIR"
WHERE tbldept.CodeDept != 'FIN' AND
tbldept.CodeDept != 'HRT' AND
tbldept.CodeDept != 'ISD'
GROUP BY tbldept.CodeDept with ROLLUP
HAVING Dept IS NULL;
Edit:
having should work, check if there are some other reasons exist or not.
Or just wrap your query with a subquery:
SELECT *
FROM (
SELECT tbldept.CodeDept AS Dept,
SUM(CASE WHEN tblemp.Status = '1' THEN 1 ELSE 0 END) AS headcount,
SUM(CASE WHEN tblemp.Status = '1' THEN 1 ELSE 0 END) * 144 AS RegHrsEst,
CONCAT(ROUND(20), '%') AS TargetOT,
round(SUM(CASE WHEN tblemp.Status = '1' THEN 1 ELSE 0 END) * 144 * 0.2) AS OTHrsBdgt
FROM tbldept
LEFT JOIN tblemp
ON tbldept.CodeDept = tblemp.CodeDept AND
tblemp.class = "DIR"
WHERE tbldept.CodeDept != 'FIN' AND
tbldept.CodeDept != 'HRT' AND
tbldept.CodeDept != 'ISD'
GROUP BY tbldept.CodeDept with ROLLUP
) t
WHERE Dept IS NULL;
I have two tables and i wanted data should come in specific format.
My query
SELECT
DISTINCT ur.suid,
(CASE WHEN ur.sceneID = '1' THEN ur.data ELSE 0 END) AS Scenario1data,
(CASE WHEN ur.sceneID = '1' THEN ss.score ELSE 0 END) AS Scenario1score,
(CASE WHEN ur.sceneID = '2' THEN ur.data ELSE 0 END) AS Scenario2,
(CASE WHEN ur.sceneID = '3' THEN ur.data ELSE 0 END) AS Scenario3,
(CASE WHEN ur.sceneID = '4' THEN ur.data ELSE 0 END) AS Scenario4
FROM tbluserresults AS ur LEFT JOIN tbluserscenescores AS ss ON ss.suid = ur.suid;
Result i wanted
suid Scenario1data Scenario1score Scenario2 Scenario3 Scenario4
1 'abc show1' 334 abc show2 abc show3 abc show 4
2 0 0 0 0.
Check my SQLFIDDLE
There are probably other better ways to do this, but I believe the following does what you want
SELECT results.suid as suid,
(CASE WHEN results.Scenario1data IS NOT NULL THEN results.Scenario1data ELSE 0 END) AS Scenario1data,
(CASE WHEN results.Scenario1score IS NOT NULL THEN results.Scenario1score ELSE 0 END) AS Scenario1score,
(CASE WHEN results.Scenario2 IS NOT NULL THEN results.Scenario2 ELSE 0 END) AS Scenario2,
(CASE WHEN results.Scenario3 IS NOT NULL THEN results.Scenario3 ELSE 0 END) AS Scenario3,
(CASE WHEN results.Scenario4 IS NOT NULL THEN results.Scenario4 ELSE 0 END) AS Scenario4
FROM
(SELECT DISTINCT(suid), #suid:= suid,
(SELECT ur.data FROM tbluserresults ur WHERE ur.suid = #suid AND ur.sceneID = '1') AS Scenario1data,
(SELECT ss.score FROM tbluserscenescores ss WHERE ss.suid = #suid AND ss.sceneID = '1') AS Scenario1score,
(SELECT ur.data FROM tbluserresults ur WHERE ur.suid = #suid AND ur.sceneID = '2') AS Scenario2,
(SELECT ur.data FROM tbluserresults ur WHERE ur.suid = #suid AND ur.sceneID = '3') AS Scenario3,
(SELECT ur.data FROM tbluserresults ur WHERE ur.suid = #suid AND ur.sceneID = '4') AS Scenario4
FROM tbluserscenescores) AS results
Hi friends can any one please help me to optimize this query ,it takes more than 30 sec.
any suggestion is warmly welcome.
Query :
SELECT
CONCAT(CCD.CONTACT_FIRST_NAME, ' ', CCD.CONTACT_LAST_NAME) AS NAME,
A.EMAIL_IDS,
CCD.UNSUBSCRIBE,
IFNULL(CSL.LOG_DATE, '') AS LOG_DATE,
CSL.IP_ADDRESS,
IF(CSL.BROWSER IS NULL, '', CSL.BROWSER) AS BROWSER,
(CASE
WHEN (UNSUBSCRIBE = 0 OR UNSUBSCRIBE IS NULL) THEN 'Opted In'
ELSE (CASE
WHEN (UNSUBSCRIBE = 1) THEN 'Opted Out'
ELSE (CASE
WHEN
((UNSUBSCRIBE = 2 OR UNSUBSCRIBE = 3)
AND CCD.CONTACT_ID NOT IN (SELECT
CONTACT_ID
FROM
CM_SUBSCRIPTION_MAIL_DATA
WHERE
IS_MAIL_SENT = 'Y'))
THEN
'Opt-In Request not Sent'
ELSE 'Opt-In Pending'
END)
END)
END) AS CURR_SUB,
(CASE
WHEN (SUBSCRIBE_FROM = 0) THEN 'Opted In'
ELSE (CASE
WHEN (SUBSCRIBE_FROM = 1) THEN 'Opted Out'
ELSE (CASE
WHEN (SUBSCRIBE_FROM = 2 OR SUBSCRIBE_FROM = 3) THEN 'Opt-In Pending'
ELSE ''
END)
END)
END) AS SUB_FROM,
SUBSCRIBE_FROM,
(CASE
WHEN (SUBSCRIBE_TO = 0) THEN 'Opted In'
ELSE (CASE
WHEN (SUBSCRIBE_TO = 1) THEN 'Opted Out'
ELSE (CASE
WHEN (SUBSCRIBE_TO = 2 OR SUBSCRIBE_TO = 3) THEN 'Opt-In Pending'
ELSE ''
END)
END)
END) AS SUB_TO,
SUBSCRIBE_TO
FROM
CM_CONTACT_DETAILS CCD
LEFT JOIN
ADDRESS A ON CCD.CONTACT_ID = A.FOREIGN_ID
LEFT JOIN
CM_SUBSCRIPTION_LOGS CSL ON CSL.CONTACT_ID = CCD.CONTACT_ID
WHERE
1 = 1
GROUP BY CSL.LOG_DATE , CCD.CONTACT_ID
ORDER BY NAME DESC , LOG_DATE DESC
LIMIT 0 , 20
Your case/when can be simplified, but I believe the killer of your query was the correlated NOT IN SELECT clause for your Opt-in request not sent.
I've change your query to do a left-join to your mailing table based on the contact AND the mailed status of "Y". So this simplifies your case to only have to check for IS NULL of the mailing table contact ID.
You can't do too much on performance since your Group by is by columns from different tables that won't take advantage of any index optimization, then all that ordered by the name makes it more of an issue... but the correlated subquery per the field being executed every time to a left-join SHOULD help.
SELECT
CONCAT(CCD.CONTACT_FIRST_NAME, ' ', CCD.CONTACT_LAST_NAME) AS NAME,
A.EMAIL_IDS,
CCD.UNSUBSCRIBE,
IFNULL(CSL.LOG_DATE, '') AS LOG_DATE,
CSL.IP_ADDRESS,
IF(CSL.BROWSER IS NULL, '', CSL.BROWSER) AS BROWSER,
CASE WHEN UNSUBSCRIBE = 0 OR UNSUBSCRIBE IS NULL THEN 'Opted In'
WHEN UNSUBSCRIBE = 1 THEN 'Opted Out'
WHEN UNSUBSCRIBE IN (2,3) AND SMD.CONTACT_ID IS NULL THEN 'Opt-In Request not Sent'
ELSE 'Opt-In Pending' END AS CURR_SUB,
CASE WHEN SUBSCRIBE_FROM = 0 THEN 'Opted In'
WHEN SUBSCRIBE_FROM = 1 THEN 'Opted Out'
WHEN SUBSCRIBE_FROM = 2 OR SUBSCRIBE_FROM = 3 THEN 'Opt-In Pending'
ELSE '' END AS SUB_FROM,
SUBSCRIBE_FROM,
CASE WHEN SUBSCRIBE_TO = 0 THEN 'Opted In'
WHEN SUBSCRIBE_TO = 1 THEN 'Opted Out'
WHEN SUBSCRIBE_TO = 2 OR SUBSCRIBE_TO = 3 THEN 'Opt-In Pending'
ELSE '' END AS SUB_TO,
SUBSCRIBE_TO
FROM
CM_CONTACT_DETAILS CCD
LEFT JOIN ADDRESS A
ON CCD.CONTACT_ID = A.FOREIGN_ID
LEFT JOIN CM_SUBSCRIPTION_LOGS CSL
ON CSL.CONTACT_ID = CCD.CONTACT_ID
LEFT JOIN CM_SUBSCRIPTION_MAIL_DATA SMD
ON CCD.CONTACT_ID = SMD.CONTACT_ID
AND IS_MAIL_SENT = 'Y'
WHERE
1 = 1
GROUP BY
CSL.LOG_DATE,
CCD.CONTACT_ID
ORDER BY
NAME DESC,
LOG_DATE DESC
LIMIT
0, 20