获取数据集2我正在使用以下CTE
;WITH RecordsWithNoDuplicates_CTE (RowNo,[Name]
,[DateAdded]
,Reason
) AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY name Order by DateAdded DESC) AS 'RowNo'
,Name,
DateAdded,
Reason
FROM
MyTable
)
SELECT * FROM RecordsWithNoDuplicates_CTE where rowno=1
为获取数据集3,我正在执行以下操作
;WITH RecordsWithSpecificCase_CTE (RowNo,[Name]
,[DateAdded]
,Reason
) AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY name Order by DateAdded DESC) AS 'RowNo'
,Name,
DateAdded,
Reason
FROM
MyTable
WHERE REASON='Case1'
)
SELECT * FROM RecordsWithSpecificCase_CTE where rowno=1
我需要计算特定于Datset2中所有记录的Reason = Case1的记录百分比
答案 0 :(得分:0)
只是为了扩展上面的评论......
在您的情况下,我可能会首先制作视图或功能以获取最新记录。这个 只是CTE周围的包装,并且会阻止你复制它。
CREATE VIEW LatestRecords
AS
WITH RecordsWithNoDuplicates_CTE AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY name Order by DateAdded DESC) AS RowNo,
Name,
DateAdded,
Reason
FROM
MyTable
)
SELECT * FROM RecordsWithNoDuplicates_CTE WHERE RowNo=1
此视图随后可用于后续查询和函数,例如:
SELECT
100*Count(CASE WHEN Reason='Case1' THEN 1 ELSE NULL END)/Count(*) as Percentage
FROM LatestRecords
将使用reason =“Case1”计算最新记录的出现次数,并将其除以最新记录的总数(给出百分比)。