SQL 和 SSRS - 相同的查询返回两个不同的数据集
TSQL & SSRS - Same query returning two different data sets
解释起来有点尴尬,所以我会尽力而为。
我在 SSMS 17 中编写了一个查询。查询 运行 没问题并且 returns 从 1990-01-01 到 2018-03-04 的数据正确。这是正确的,2018-03-04 是此查询提取的最新案例。
当将这个确切的查询放入 SSRS (Visual Studio) 时,我最初放置了一个数据参数,该参数级联到案例类型和位置的 select 的另外两个选项。我将这些作为单独的数据集放入 link 参数以将它们级联。这一切都很好。直到我对报告进行最后检查时,我才意识到我无法获得 2017 年 10 月 27 日之后的任何数据。 任何数据都没有过滤器。
最后,我现在删除了所有参数,因此它只是主要数据集,并放入一个代码来提取过去 2 年的数据。这仍然在 2017-10-27 结束。有谁知道 SSMS returns 中的相同查询 运行 数据如何正确复制并粘贴到 SSRS VS 中,它突然不会超过 2017-10-27?
当时只有 1,615 行,所以它不像有大量数据,我已经达到了一些限制。
真的被这个难住了。我没有发布我的代码,因为它可以在 SSMS 中正常工作,所以我认为问题不在于此。
这是当前使用的代码。日期的长 Case When 是将 UTC 数据库时间与 BST 相对应。
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SET NOCOUNT ON;
SET ARITHABORT ON;
WITH cvr AS
(
SELECT DISTINCT
hcx.mps_Person,
cpx.mps_Name [Name],
CASE WHEN hcx.mps_SchemeCode IS NULL THEN LTRIM(RTRIM(hcx.mps_MembershipNumber))
WHEN hcx.mps_SchemeCode = '' THEN LTRIM(RTRIM(hcx.mps_MembershipNumber))
WHEN hcx.mps_SchemeCode IS NOT NULL THEN LTRIM(RTRIM(hcx.mps_SchemeCode))+'/'+LTRIM(RTRIM(hcx.mps_MembershipNumber)) ELSE NULL END AS [Membership Number],
hcx.mps_MemberCoverStatus [Cover Status],
cmd.[Country of Incident] [Case Country],
CASE WHEN hcx.mps_CoverSource = 0 THEN 'Cover Account'
WHEN hcx.mps_CoverSource = 1 THEN 'SAM Cover'
WHEN hcx.mps_CoverSource = 2 THEN 'MDU Transfer Cover' ELSE NULL END AS [Cover Source],
CASE WHEN hcx.mps_startdate > (DATEADD(HH,1,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,hcx.mps_startdate) AS CHAR) + '/03/01'),30))/7*7,'19000107')))) AND hcx.mps_startdate < (DATEADD(HH,2,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,hcx.mps_startdate) AS CHAR) + '/10/01'),30))/7*7,'19000107')))) THEN (DATEADD(HH,1,hcx.mps_startdate)) ELSE hcx.mps_startdate END AS [CM Cover Start Date],
CASE WHEN hcx.mps_enddate > (DATEADD(HH,1,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,hcx.mps_enddate) AS CHAR) + '/03/01'),30))/7*7,'19000107')))) AND hcx.mps_enddate < (DATEADD(HH,2,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,hcx.mps_enddate) AS CHAR) + '/10/01'),30))/7*7,'19000107')))) THEN (DATEADD(HH,1,hcx.mps_enddate)) ELSE hcx.mps_enddate END AS [CM Cover End Date],
cmd.[Case Number] AS [Case Number],
cmd.IncidentId,
cmd.[Medical/Dental] AS [Medical/Dental],
cmd.[Primary Case Type] AS [Incident Primary Case Type],
cmd.[Case Types] AS [Incident Case Types],
cpx.mps_dn_CaseTypesInvolved AS [Member Involved Case Types],
CASE WHEN cpx.mps_involvedfrom > (DATEADD(HH,1,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cpx.mps_involvedfrom) AS CHAR) + '/03/01'),30))/7*7,'19000107')))) AND cpx.mps_involvedfrom < (DATEADD(HH,2,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cpx.mps_involvedfrom) AS CHAR) + '/10/01'),30))/7*7,'19000107')))) THEN (DATEADD(HH,1,cpx.mps_involvedfrom)) ELSE cpx.mps_involvedfrom END AS [Involved From],
CASE WHEN cpx.mps_involvedto > (DATEADD(HH,1,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cpx.mps_involvedto) AS CHAR) + '/03/01'),30))/7*7,'19000107')))) AND cpx.mps_involvedto < (DATEADD(HH,2,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cpx.mps_involvedto) AS CHAR) + '/10/01'),30))/7*7,'19000107')))) THEN (DATEADD(HH,1,cpx.mps_involvedto)) ELSE cpx.mps_involvedto END AS [Involved To],
CASE WHEN cpx.mps_claimsmadenotificationdate > (DATEADD(HH,1,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cpx.mps_claimsmadenotificationdate) AS CHAR) + '/03/01'),30))/7*7,'19000107')))) AND cpx.mps_claimsmadenotificationdate < (DATEADD(HH,2,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cpx.mps_claimsmadenotificationdate) AS CHAR) + '/10/01'),30))/7*7,'19000107')))) THEN (DATEADD(HH,1,cpx.mps_claimsmadenotificationdate)) ELSE cpx.mps_claimsmadenotificationdate END AS [Claims Made Notification Date],
CASE WHEN cmd.[MPS Claim Date] > (DATEADD(HH,1,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cmd.[MPS Claim Date]) AS CHAR) + '/03/01'),30))/7*7,'19000107')))) AND cmd.[MPS Claim Date] < (DATEADD(HH,2,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cmd.[MPS Claim Date]) AS CHAR) + '/10/01'),30))/7*7,'19000107')))) THEN (DATEADD(HH,1,cmd.[MPS Claim Date])) ELSE cmd.[MPS Claim Date] END AS [MPS Claim Date]
,cmd.[Total Claim Payments (Sterling Equivalent in £)] AS [TotalClaimPayments]
,cmd.[Total Non-Claim Payments (Sterling Equivalent in £)] AS [TotalNonClaimPayments]
,FLOOR(cmd.[MPS Apportionment %]*100) AS [Liability]
FROM OneMPS_MSCRM.dbo.mps_historiccoveraccountExtensionBase AS hcx
INNER JOIN OneMPS_MSCRM.dbo.mps_historiccoveraccountBase AS hca ON hca.mps_historiccoveraccountId = hcx.mps_historiccoveraccountId AND hca.statuscode = 1 /*Active*/
INNER JOIN OneMPS_MSCRM.dbo.mps_casepartyExtensionBase AS cpx ON hcx.mps_Person = cpx.mps_Person
INNER JOIN OneMPS_MSCRM.dbo.mps_casepartyBase AS cpb ON cpx.mps_casepartyId = cpb.mps_casepartyId AND cpb.statuscode = 1 /*Active*/ AND cpx.mps_PrimaryRole = 0
INNER JOIN dbo.CasesMasterData AS cmd ON cmd.incidentid = cpx.mps_Case
WHERE hcx.mps_CoverBasis = 'Claims Made'
AND hcx.mps_IsSuperseded = 0 /*Not Superseded*/
AND cpx.mps_involvedto >= hcx.mps_StartDate /*Intersects with Period of Involvement*/
AND cpx.mps_involvedfrom <= hcx.mps_EndDate /*Intersects with Period of Involvement*/
AND cpx.mps_InvolvedFrom >= '1990-01-01 00:00:00.000'
)
,cts AS
(
SELECT
cvr.mps_Person,
cvr.[Cover Source],
cvr.Name,
cvr.[Membership Number],
cvr.[Cover Status],
cvr.[CM Cover Start Date],
cvr.[CM Cover End Date],
cvr.[Case Number],
cvr.IncidentId,
cvr.[Involved From],
cvr.[Involved To],
cvr.[Claims Made Notification Date],
cvr.[MPS Claim Date],
cvr.[Medical/Dental],
cvr.[Case Country],
cvr.[Incident Primary Case Type],
cvr.[Incident Case Types],
cvr.[Member Involved Case Types],
CASE WHEN LEAD(cvr.[CM Cover Start Date],1,0) OVER (PARTITION BY cvr.mps_person, cvr.[Membership Number], cvr.[Case Number] ORDER BY cvr.[CM Cover Start Date]) = cvr.[CM Cover Start Date]
AND LEAD(cvr.[CM Cover End Date],1,0) OVER (PARTITION BY cvr.mps_person, cvr.[Membership Number], cvr.[Case Number] ORDER BY cvr.[CM Cover Start Date]) = cvr.[CM Cover End Date] THEN NULL
ELSE DATEDIFF(d,cvr.[CM Cover Start Date],cvr.[CM Cover End Date]) END [CM Cover Days],
(SELECT MIN(cvr_sd.[CM Cover Start Date]) FROM cvr cvr_sd WHERE cvr_sd.mps_Person = cvr.mps_Person AND cvr_sd.[Membership Number] = cvr.[Membership Number] AND cvr_sd.[Case Number] = cvr.[Case Number]) [CM Cover Start Date (Min)],
(SELECT MAX(cvr_ed.[CM Cover End Date]) FROM cvr cvr_ed WHERE cvr_ed.mps_Person = cvr.mps_Person AND cvr_ed.[Membership Number] = cvr.[Membership Number] AND cvr_ed.[Case Number] = cvr.[Case Number]) [CM Cover End Date (Max)],
(SELECT COUNT(cvr_rw.mps_Person) FROM cvr cvr_rw WHERE cvr_rw.mps_Person = cvr.mps_Person AND cvr_rw.[Membership Number] = cvr.[Membership Number] AND cvr_rw.[Case Number] = cvr.[Case Number]) [Total Rows],
cvr.TotalClaimPayments,
cvr.TotalNonClaimPayments,
cvr.Liability
FROM cvr
)
,chk AS
(
SELECT
cts.mps_Person,
cts.[Case Number],
cts.IncidentId,
cts.[Membership Number],
SUM(cts.[CM Cover Days]) AS [CM Cover Days],
DATEDIFF(d,cts.[CM Cover Start Date (Min)],cts.[CM Cover End Date (Max)])-MAX(cts.[Total Rows]) AS [CM Cover Days (If Unbroken)]
FROM cts
GROUP BY
cts.mps_Person,
cts.[Case Number],
cts.IncidentId,
cts.[Membership Number],
cts.[CM Cover Start Date (Min)],
cts.[CM Cover End Date (Max)],
cts.[Case Country]
)
SELECT
cts.Name,
cts.[Membership Number],
cts.[Cover Status],
cts.[Cover Source],
cts.[CM Cover Start Date],
cts.[CM Cover End Date],
CASE WHEN chk.[CM Cover Days] >= DATEDIFF(d,cts.[CM Cover Start Date (Min)],cts.[CM Cover End Date (Max)])-cts.[Total Rows] THEN cts.[CM Cover Start Date (Min)] ELSE NULL END [CM Continuous Cover Start Date],
CASE WHEN chk.[CM Cover Days] >= DATEDIFF(d,cts.[CM Cover Start Date (Min)],cts.[CM Cover End Date (Max)])-cts.[Total Rows] THEN cts.[CM Cover End Date (Max)] ELSE NULL END [CM Continuous Cover End Date],
a2a.[A2A Decision],
a2a.[A2A Decision Reason],
cts.[Case Number],
cts.[Involved From] AS [Involved From Date],
cts.[Involved To] AS [Involved To Date],
cts.[Claims Made Notification Date] AS [Claims Made Notification Date],
cts.[MPS Claim Date],
cts.[Medical/Dental],
cts.[Case Country],
cts.[Incident Primary Case Type],
cts.[Incident Case Types],
cts.[Member Involved Case Types],
mbr.[# Members Involved],
SUM(ISNULL(cts.TotalClaimPayments,0)+ISNULL(cts.TotalNonClaimPayments,0)) AS [TotalSpend],
ISNULL(cts.TotalClaimPayments,0) AS [Claim Payments - Total (£)],
CAST(cts.Liability*(cts.TotalClaimPayments/100) AS NUMERIC(14,2)) AS [Claim Payments - Apportioned to Member (£)],
ISNULL(cts.TotalNonClaimPayments,0) AS [Non-Claim Payments - Total (£)],
cts.Liability AS [MPSClaimLiability]
FROM cts
INNER JOIN chk ON chk.[Case Number] = cts.[Case Number] AND chk.[Membership Number] = cts.[Membership Number] AND chk.mps_Person = cts.mps_Person
OUTER APPLY
(
SELECT TOP 1
CASE WHEN adx.mps_Decision = 0 THEN 'No'
WHEN adx.mps_Decision = 1 THEN 'Yes'
WHEN adx.mps_Decision = 2 THEN 'Pending'
WHEN adx.mps_Decision = 3 THEN 'No - Member Declined'
WHEN adx.mps_Decision = 4 THEN 'Yes - Ex Gratia'
WHEN adx.mps_Decision = 5 THEN 'No - Member Uncontactable/Not Responding'
ELSE NULL END AS [A2A Decision],
adx.mps_DecisionReason AS [A2A Decision Reason]
FROM OneMPS_MSCRM.dbo.mps_authoritytoassistdecisionBase AS adb
INNER JOIN OneMPS_MSCRM.dbo.mps_authoritytoassistdecisionExtensionBase adx ON adb.mps_authoritytoassistdecisionId = adx.mps_authoritytoassistdecisionId AND adb.statuscode IN (2) /*2=Valid*/
INNER JOIN OneMPS_MSCRM.dbo.mps_casepartyExtensionBase cpx ON cpx.mps_casepartyId = adx.mps_CaseParty
INNER JOIN OneMPS_MSCRM.dbo.mps_casepartyBase cpb ON cpx.mps_casepartyId = cpb.mps_casepartyId AND cpb.statuscode = 1 /*Active*/ AND cpx.mps_PrimaryRole = 0
WHERE cpx.mps_Case = cts.IncidentId
ORDER BY adx.mps_DecisionOn DESC
) AS a2a
OUTER APPLY
(
SELECT TOP 1 COUNT(cpx.mps_casepartyId) AS [# Members Involved]
FROM OneMPS_MSCRM.dbo.mps_casepartyExtensionBase cpx
INNER JOIN OneMPS_MSCRM.dbo.mps_casepartyBase cpb ON cpx.mps_casepartyId = cpb.mps_casepartyId AND cpb.statuscode = 1 /*Active*/ AND cpx.mps_PrimaryRole = 0
WHERE cpx.mps_Case = cts.IncidentId
) AS mbr
GROUP BY cts.Name,
cts.[Membership Number],
cts.[Cover Status],
cts.[Cover Source],
cts.[CM Cover Start Date],
cts.[CM Cover End Date],
CASE WHEN chk.[CM Cover Days] >= DATEDIFF(d,cts.[CM Cover Start Date (Min)],cts.[CM Cover End Date (Max)])-cts.[Total Rows] THEN cts.[CM Cover Start Date (Min)] ELSE NULL END,
CASE WHEN chk.[CM Cover Days] >= DATEDIFF(d,cts.[CM Cover Start Date (Min)],cts.[CM Cover End Date (Max)])-cts.[Total Rows] THEN cts.[CM Cover End Date (Max)] ELSE NULL ENd,
a2a.[A2A Decision],
a2a.[A2A Decision Reason],
cts.[Case Number],
cts.[Involved From],
cts.[Involved To] ,
cts.[Claims Made Notification Date],
cts.[MPS Claim Date],
cts.[Medical/Dental],
cts.[Case Country],
cts.[Incident Primary Case Type],
cts.[Incident Case Types],
cts.[Member Involved Case Types],
mbr.[# Members Involved],
cts.TotalClaimPayments ,
CAST(cts.Liability*(cts.TotalClaimPayments/100) AS NUMERIC(14,2)),
cts.TotalNonClaimPayments,
cts.Liability
ORDER BY cts.[Involved From] ASC
示例数据已缩短。它本质上是一个数据提取。
Name|Membership Number|CoverStatus|Cover Date |Involved From
Bob |984684638 |Active |2017-03-01 00:00:00.000|2017-10-27 00:00:00.000
Test|135486968 |Active |2017-07-01 00:00:00.000|2018-03-04 00:00:00.000
第一行将在SSRS 中正常显示,第二行将根本不可见。这将解决的主要日期是最后一个 [Involved From]。
我唯一能想到的是 BST 转换可能会以某种方式影响它,因为它是我对日期所做的唯一事情。
将此查询加载到 SP 并提供来自 SP 的源,我希望这能解决问题,因为代码很大。
You have SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
May be Same query returning two different data sets due to this isolation level session. As if any table is updated (insert or update or delete) under a transaction and same transaction is not completed that is not committed or roll backed then uncommitted values will displaly (Dirty Read) in select query of "Read Uncommitted" isolation transaction sessions.
So, SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
大家好,抱歉,我似乎想多了。感谢那些提出想法的人。
原来是Shared Data Source,我们有一个live和一个测试环境。数据源说它指向实时,但是当我检查连接字符串时(为什么我之前没有想到它)它实际上是在查找数据有限的测试服务器。
这个不能见树不见林:) 所以吸取了教训。请记住检查您的连接字符串,而不仅仅是名称。
解释起来有点尴尬,所以我会尽力而为。
我在 SSMS 17 中编写了一个查询。查询 运行 没问题并且 returns 从 1990-01-01 到 2018-03-04 的数据正确。这是正确的,2018-03-04 是此查询提取的最新案例。
当将这个确切的查询放入 SSRS (Visual Studio) 时,我最初放置了一个数据参数,该参数级联到案例类型和位置的 select 的另外两个选项。我将这些作为单独的数据集放入 link 参数以将它们级联。这一切都很好。直到我对报告进行最后检查时,我才意识到我无法获得 2017 年 10 月 27 日之后的任何数据。 任何数据都没有过滤器。
最后,我现在删除了所有参数,因此它只是主要数据集,并放入一个代码来提取过去 2 年的数据。这仍然在 2017-10-27 结束。有谁知道 SSMS returns 中的相同查询 运行 数据如何正确复制并粘贴到 SSRS VS 中,它突然不会超过 2017-10-27?
当时只有 1,615 行,所以它不像有大量数据,我已经达到了一些限制。
真的被这个难住了。我没有发布我的代码,因为它可以在 SSMS 中正常工作,所以我认为问题不在于此。
这是当前使用的代码。日期的长 Case When 是将 UTC 数据库时间与 BST 相对应。
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SET NOCOUNT ON;
SET ARITHABORT ON;
WITH cvr AS
(
SELECT DISTINCT
hcx.mps_Person,
cpx.mps_Name [Name],
CASE WHEN hcx.mps_SchemeCode IS NULL THEN LTRIM(RTRIM(hcx.mps_MembershipNumber))
WHEN hcx.mps_SchemeCode = '' THEN LTRIM(RTRIM(hcx.mps_MembershipNumber))
WHEN hcx.mps_SchemeCode IS NOT NULL THEN LTRIM(RTRIM(hcx.mps_SchemeCode))+'/'+LTRIM(RTRIM(hcx.mps_MembershipNumber)) ELSE NULL END AS [Membership Number],
hcx.mps_MemberCoverStatus [Cover Status],
cmd.[Country of Incident] [Case Country],
CASE WHEN hcx.mps_CoverSource = 0 THEN 'Cover Account'
WHEN hcx.mps_CoverSource = 1 THEN 'SAM Cover'
WHEN hcx.mps_CoverSource = 2 THEN 'MDU Transfer Cover' ELSE NULL END AS [Cover Source],
CASE WHEN hcx.mps_startdate > (DATEADD(HH,1,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,hcx.mps_startdate) AS CHAR) + '/03/01'),30))/7*7,'19000107')))) AND hcx.mps_startdate < (DATEADD(HH,2,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,hcx.mps_startdate) AS CHAR) + '/10/01'),30))/7*7,'19000107')))) THEN (DATEADD(HH,1,hcx.mps_startdate)) ELSE hcx.mps_startdate END AS [CM Cover Start Date],
CASE WHEN hcx.mps_enddate > (DATEADD(HH,1,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,hcx.mps_enddate) AS CHAR) + '/03/01'),30))/7*7,'19000107')))) AND hcx.mps_enddate < (DATEADD(HH,2,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,hcx.mps_enddate) AS CHAR) + '/10/01'),30))/7*7,'19000107')))) THEN (DATEADD(HH,1,hcx.mps_enddate)) ELSE hcx.mps_enddate END AS [CM Cover End Date],
cmd.[Case Number] AS [Case Number],
cmd.IncidentId,
cmd.[Medical/Dental] AS [Medical/Dental],
cmd.[Primary Case Type] AS [Incident Primary Case Type],
cmd.[Case Types] AS [Incident Case Types],
cpx.mps_dn_CaseTypesInvolved AS [Member Involved Case Types],
CASE WHEN cpx.mps_involvedfrom > (DATEADD(HH,1,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cpx.mps_involvedfrom) AS CHAR) + '/03/01'),30))/7*7,'19000107')))) AND cpx.mps_involvedfrom < (DATEADD(HH,2,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cpx.mps_involvedfrom) AS CHAR) + '/10/01'),30))/7*7,'19000107')))) THEN (DATEADD(HH,1,cpx.mps_involvedfrom)) ELSE cpx.mps_involvedfrom END AS [Involved From],
CASE WHEN cpx.mps_involvedto > (DATEADD(HH,1,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cpx.mps_involvedto) AS CHAR) + '/03/01'),30))/7*7,'19000107')))) AND cpx.mps_involvedto < (DATEADD(HH,2,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cpx.mps_involvedto) AS CHAR) + '/10/01'),30))/7*7,'19000107')))) THEN (DATEADD(HH,1,cpx.mps_involvedto)) ELSE cpx.mps_involvedto END AS [Involved To],
CASE WHEN cpx.mps_claimsmadenotificationdate > (DATEADD(HH,1,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cpx.mps_claimsmadenotificationdate) AS CHAR) + '/03/01'),30))/7*7,'19000107')))) AND cpx.mps_claimsmadenotificationdate < (DATEADD(HH,2,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cpx.mps_claimsmadenotificationdate) AS CHAR) + '/10/01'),30))/7*7,'19000107')))) THEN (DATEADD(HH,1,cpx.mps_claimsmadenotificationdate)) ELSE cpx.mps_claimsmadenotificationdate END AS [Claims Made Notification Date],
CASE WHEN cmd.[MPS Claim Date] > (DATEADD(HH,1,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cmd.[MPS Claim Date]) AS CHAR) + '/03/01'),30))/7*7,'19000107')))) AND cmd.[MPS Claim Date] < (DATEADD(HH,2,(DATEADD(DAY,DATEDIFF(DAY,'19000107',DATEADD(MONTH,DATEDIFF(MONTH,0,CAST(DATEPART(YEAR,cmd.[MPS Claim Date]) AS CHAR) + '/10/01'),30))/7*7,'19000107')))) THEN (DATEADD(HH,1,cmd.[MPS Claim Date])) ELSE cmd.[MPS Claim Date] END AS [MPS Claim Date]
,cmd.[Total Claim Payments (Sterling Equivalent in £)] AS [TotalClaimPayments]
,cmd.[Total Non-Claim Payments (Sterling Equivalent in £)] AS [TotalNonClaimPayments]
,FLOOR(cmd.[MPS Apportionment %]*100) AS [Liability]
FROM OneMPS_MSCRM.dbo.mps_historiccoveraccountExtensionBase AS hcx
INNER JOIN OneMPS_MSCRM.dbo.mps_historiccoveraccountBase AS hca ON hca.mps_historiccoveraccountId = hcx.mps_historiccoveraccountId AND hca.statuscode = 1 /*Active*/
INNER JOIN OneMPS_MSCRM.dbo.mps_casepartyExtensionBase AS cpx ON hcx.mps_Person = cpx.mps_Person
INNER JOIN OneMPS_MSCRM.dbo.mps_casepartyBase AS cpb ON cpx.mps_casepartyId = cpb.mps_casepartyId AND cpb.statuscode = 1 /*Active*/ AND cpx.mps_PrimaryRole = 0
INNER JOIN dbo.CasesMasterData AS cmd ON cmd.incidentid = cpx.mps_Case
WHERE hcx.mps_CoverBasis = 'Claims Made'
AND hcx.mps_IsSuperseded = 0 /*Not Superseded*/
AND cpx.mps_involvedto >= hcx.mps_StartDate /*Intersects with Period of Involvement*/
AND cpx.mps_involvedfrom <= hcx.mps_EndDate /*Intersects with Period of Involvement*/
AND cpx.mps_InvolvedFrom >= '1990-01-01 00:00:00.000'
)
,cts AS
(
SELECT
cvr.mps_Person,
cvr.[Cover Source],
cvr.Name,
cvr.[Membership Number],
cvr.[Cover Status],
cvr.[CM Cover Start Date],
cvr.[CM Cover End Date],
cvr.[Case Number],
cvr.IncidentId,
cvr.[Involved From],
cvr.[Involved To],
cvr.[Claims Made Notification Date],
cvr.[MPS Claim Date],
cvr.[Medical/Dental],
cvr.[Case Country],
cvr.[Incident Primary Case Type],
cvr.[Incident Case Types],
cvr.[Member Involved Case Types],
CASE WHEN LEAD(cvr.[CM Cover Start Date],1,0) OVER (PARTITION BY cvr.mps_person, cvr.[Membership Number], cvr.[Case Number] ORDER BY cvr.[CM Cover Start Date]) = cvr.[CM Cover Start Date]
AND LEAD(cvr.[CM Cover End Date],1,0) OVER (PARTITION BY cvr.mps_person, cvr.[Membership Number], cvr.[Case Number] ORDER BY cvr.[CM Cover Start Date]) = cvr.[CM Cover End Date] THEN NULL
ELSE DATEDIFF(d,cvr.[CM Cover Start Date],cvr.[CM Cover End Date]) END [CM Cover Days],
(SELECT MIN(cvr_sd.[CM Cover Start Date]) FROM cvr cvr_sd WHERE cvr_sd.mps_Person = cvr.mps_Person AND cvr_sd.[Membership Number] = cvr.[Membership Number] AND cvr_sd.[Case Number] = cvr.[Case Number]) [CM Cover Start Date (Min)],
(SELECT MAX(cvr_ed.[CM Cover End Date]) FROM cvr cvr_ed WHERE cvr_ed.mps_Person = cvr.mps_Person AND cvr_ed.[Membership Number] = cvr.[Membership Number] AND cvr_ed.[Case Number] = cvr.[Case Number]) [CM Cover End Date (Max)],
(SELECT COUNT(cvr_rw.mps_Person) FROM cvr cvr_rw WHERE cvr_rw.mps_Person = cvr.mps_Person AND cvr_rw.[Membership Number] = cvr.[Membership Number] AND cvr_rw.[Case Number] = cvr.[Case Number]) [Total Rows],
cvr.TotalClaimPayments,
cvr.TotalNonClaimPayments,
cvr.Liability
FROM cvr
)
,chk AS
(
SELECT
cts.mps_Person,
cts.[Case Number],
cts.IncidentId,
cts.[Membership Number],
SUM(cts.[CM Cover Days]) AS [CM Cover Days],
DATEDIFF(d,cts.[CM Cover Start Date (Min)],cts.[CM Cover End Date (Max)])-MAX(cts.[Total Rows]) AS [CM Cover Days (If Unbroken)]
FROM cts
GROUP BY
cts.mps_Person,
cts.[Case Number],
cts.IncidentId,
cts.[Membership Number],
cts.[CM Cover Start Date (Min)],
cts.[CM Cover End Date (Max)],
cts.[Case Country]
)
SELECT
cts.Name,
cts.[Membership Number],
cts.[Cover Status],
cts.[Cover Source],
cts.[CM Cover Start Date],
cts.[CM Cover End Date],
CASE WHEN chk.[CM Cover Days] >= DATEDIFF(d,cts.[CM Cover Start Date (Min)],cts.[CM Cover End Date (Max)])-cts.[Total Rows] THEN cts.[CM Cover Start Date (Min)] ELSE NULL END [CM Continuous Cover Start Date],
CASE WHEN chk.[CM Cover Days] >= DATEDIFF(d,cts.[CM Cover Start Date (Min)],cts.[CM Cover End Date (Max)])-cts.[Total Rows] THEN cts.[CM Cover End Date (Max)] ELSE NULL END [CM Continuous Cover End Date],
a2a.[A2A Decision],
a2a.[A2A Decision Reason],
cts.[Case Number],
cts.[Involved From] AS [Involved From Date],
cts.[Involved To] AS [Involved To Date],
cts.[Claims Made Notification Date] AS [Claims Made Notification Date],
cts.[MPS Claim Date],
cts.[Medical/Dental],
cts.[Case Country],
cts.[Incident Primary Case Type],
cts.[Incident Case Types],
cts.[Member Involved Case Types],
mbr.[# Members Involved],
SUM(ISNULL(cts.TotalClaimPayments,0)+ISNULL(cts.TotalNonClaimPayments,0)) AS [TotalSpend],
ISNULL(cts.TotalClaimPayments,0) AS [Claim Payments - Total (£)],
CAST(cts.Liability*(cts.TotalClaimPayments/100) AS NUMERIC(14,2)) AS [Claim Payments - Apportioned to Member (£)],
ISNULL(cts.TotalNonClaimPayments,0) AS [Non-Claim Payments - Total (£)],
cts.Liability AS [MPSClaimLiability]
FROM cts
INNER JOIN chk ON chk.[Case Number] = cts.[Case Number] AND chk.[Membership Number] = cts.[Membership Number] AND chk.mps_Person = cts.mps_Person
OUTER APPLY
(
SELECT TOP 1
CASE WHEN adx.mps_Decision = 0 THEN 'No'
WHEN adx.mps_Decision = 1 THEN 'Yes'
WHEN adx.mps_Decision = 2 THEN 'Pending'
WHEN adx.mps_Decision = 3 THEN 'No - Member Declined'
WHEN adx.mps_Decision = 4 THEN 'Yes - Ex Gratia'
WHEN adx.mps_Decision = 5 THEN 'No - Member Uncontactable/Not Responding'
ELSE NULL END AS [A2A Decision],
adx.mps_DecisionReason AS [A2A Decision Reason]
FROM OneMPS_MSCRM.dbo.mps_authoritytoassistdecisionBase AS adb
INNER JOIN OneMPS_MSCRM.dbo.mps_authoritytoassistdecisionExtensionBase adx ON adb.mps_authoritytoassistdecisionId = adx.mps_authoritytoassistdecisionId AND adb.statuscode IN (2) /*2=Valid*/
INNER JOIN OneMPS_MSCRM.dbo.mps_casepartyExtensionBase cpx ON cpx.mps_casepartyId = adx.mps_CaseParty
INNER JOIN OneMPS_MSCRM.dbo.mps_casepartyBase cpb ON cpx.mps_casepartyId = cpb.mps_casepartyId AND cpb.statuscode = 1 /*Active*/ AND cpx.mps_PrimaryRole = 0
WHERE cpx.mps_Case = cts.IncidentId
ORDER BY adx.mps_DecisionOn DESC
) AS a2a
OUTER APPLY
(
SELECT TOP 1 COUNT(cpx.mps_casepartyId) AS [# Members Involved]
FROM OneMPS_MSCRM.dbo.mps_casepartyExtensionBase cpx
INNER JOIN OneMPS_MSCRM.dbo.mps_casepartyBase cpb ON cpx.mps_casepartyId = cpb.mps_casepartyId AND cpb.statuscode = 1 /*Active*/ AND cpx.mps_PrimaryRole = 0
WHERE cpx.mps_Case = cts.IncidentId
) AS mbr
GROUP BY cts.Name,
cts.[Membership Number],
cts.[Cover Status],
cts.[Cover Source],
cts.[CM Cover Start Date],
cts.[CM Cover End Date],
CASE WHEN chk.[CM Cover Days] >= DATEDIFF(d,cts.[CM Cover Start Date (Min)],cts.[CM Cover End Date (Max)])-cts.[Total Rows] THEN cts.[CM Cover Start Date (Min)] ELSE NULL END,
CASE WHEN chk.[CM Cover Days] >= DATEDIFF(d,cts.[CM Cover Start Date (Min)],cts.[CM Cover End Date (Max)])-cts.[Total Rows] THEN cts.[CM Cover End Date (Max)] ELSE NULL ENd,
a2a.[A2A Decision],
a2a.[A2A Decision Reason],
cts.[Case Number],
cts.[Involved From],
cts.[Involved To] ,
cts.[Claims Made Notification Date],
cts.[MPS Claim Date],
cts.[Medical/Dental],
cts.[Case Country],
cts.[Incident Primary Case Type],
cts.[Incident Case Types],
cts.[Member Involved Case Types],
mbr.[# Members Involved],
cts.TotalClaimPayments ,
CAST(cts.Liability*(cts.TotalClaimPayments/100) AS NUMERIC(14,2)),
cts.TotalNonClaimPayments,
cts.Liability
ORDER BY cts.[Involved From] ASC
示例数据已缩短。它本质上是一个数据提取。
Name|Membership Number|CoverStatus|Cover Date |Involved From
Bob |984684638 |Active |2017-03-01 00:00:00.000|2017-10-27 00:00:00.000
Test|135486968 |Active |2017-07-01 00:00:00.000|2018-03-04 00:00:00.000
第一行将在SSRS 中正常显示,第二行将根本不可见。这将解决的主要日期是最后一个 [Involved From]。 我唯一能想到的是 BST 转换可能会以某种方式影响它,因为它是我对日期所做的唯一事情。
将此查询加载到 SP 并提供来自 SP 的源,我希望这能解决问题,因为代码很大。
You have SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
May be Same query returning two different data sets due to this isolation level session. As if any table is updated (insert or update or delete) under a transaction and same transaction is not completed that is not committed or roll backed then uncommitted values will displaly (Dirty Read) in select query of "Read Uncommitted" isolation transaction sessions.
So, SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
大家好,抱歉,我似乎想多了。感谢那些提出想法的人。
原来是Shared Data Source,我们有一个live和一个测试环境。数据源说它指向实时,但是当我检查连接字符串时(为什么我之前没有想到它)它实际上是在查找数据有限的测试服务器。
这个不能见树不见林:) 所以吸取了教训。请记住检查您的连接字符串,而不仅仅是名称。