Although the following query runs, no data is being returned. The query splits columns into JudgmentMonth, Consumer and Commercial. My knowledge of SQL Server is not great but I'm guessing no date parameters are given hence no data is being returned. I'm not sure where date is to be manually entered after >= and <=
WITH dset AS
(
SELECT
COUNT(category) AS Volumes,
MONTH(creation_date) AS JudgmentMonth,
transaction_type,
REPLACE(record_type, 3, 2) AS RecordType
FROM
table
WHERE
transaction_type = 'jg'
AND category = 'CCJ'
AND CAST(creation_date AS DATE) >= CONVERT(VARCHAR(12), GETDATE(), 101)
AND CAST(creation_date AS DATE) <= CONVERT(VARCHAR(12), GETDATE(), 101)
GROUP BY
MONTH(creation_date), transaction_type, REPLACE(record_type, 3, 2)
)
SELECT
x.JudgmentMonth,
MAX(CASE x.RecordType WHEN '1' THEN x.Volumes END) CONSUMER,
MAX(CASE x.RecordType WHEN '2' THEN x.Volumes END) COMMERCIAL
FROM
(SELECT
r.JudgmentMonth, r.RecordType, r.Volumes
FROM
dset r) x
GROUP BY
x.JudgmentMonth