Forum Discussion
Martin_Kevin
Aug 10, 2024Copper Contributor
Help in grouping function desired output
Every RuleID has different Ruleperiod, RuleDate and Rulestatus. I want the following desired output as per below calculation. I want to display those row only which has Rulestatus-'Active'. Every gr...
rodgerkong
Aug 11, 2024Iron Contributor
1. Find 'Active' row as dataset CA;
2. Find ’Max‘ row as dataset CM;
3. Join CA and CM, calculate columns you need.
SELECT CA.RuleID, CA.ActivePeriod Ruleperiod,
CONVERT(DATE, CA.ActiveDate) RuleDate_of_Active,
CONVERT(DATE, CM.MaxDate) RuleDate_Max,
CM.MaxPeriod - CA.ActivePeriod Ruleperiod_Left,
DATEDIFF(month, CA.ActiveDate, CM.MaxDate) RuleDate_Left_Round_Month
FROM
(
SELECT RuleID, MAX(Ruleperiod) ActivePeriod, MAX(RuleDate) ActiveDate
FROM #Classic
WHERE Rulestatus = 'Active'
GROUP BY RuleID
) CA
INNER JOIN
(
SELECT RuleID, MAX(Ruleperiod) MaxPeriod, MAX(RuleDate) MaxDate
FROM #Classic
GROUP BY RuleID
) CM
ON
CA.RuleID=CM.RuleIDMartin_Kevin
Aug 22, 2024Copper Contributor
@ rodgerkong: Thanks so much.