简体   繁体   English

由不同用户运行时,SQL查询中的MAX和COALESCE导致奇怪的结果

[英]Weird result with MAX and COALESCE in a SQL query when run by different users

I have a SQL Server query that tries to choose NULL if there is at least one NULL in column PredComplDate or the latest date stored as NVARCHAR(30) 我有一个SQL Server查询,如果列PredComplDate中至少有一个NULL或存储为NVARCHAR(30)的最新日期,则尝试选择NULL

The weird thing is that when I run the query it returns correct results, but when my co-worker runs the query on the same computer it returns different results. 奇怪的是,当我运行查询时,它会返回正确的结果,但是当我的同事在同一台计算机上运行查询时,它会返回不同的结果。

I saved the table created when my co-worker ran the query under a different name and compared it to the one that was created when I run the query. 我保存了当我的同事以不同的名称运行查询时创建的表,并将其与运行查询时创建的表进行比较。 everything is the same, number of records, data type etc, but the below part of the query does not return NULL for some reason: 一切都是一样的,记录数,数据类型等,但由于某种原因,查询的下面部分不返回NULL

SELECT [Reference Number],
    CASE 
        WHEN LEN(Predecessors)>0 AND MAX(COALESCE(PredComplDate,'31/12/2099'))='31/12/2099' THEN NULL
        ELSE MAX(PredComplDate)
    END AS LatestPredComplDate
    FROM mytable33
    GROUP BY [Reference Number],Predecessors

what is the problem with this query? 这个查询有什么问题?

mytable33 has been created by me and mytable22 has been created by my co-worker. mytable33由我创建, mytable22由我的同事创建。 when I query the results are identical: 当我查询结果相同时:

SELECT Predecessors,[Reference Number], PredRefNo,PredComplDate  FROM mytable22

SELECT Predecessors,[Reference Number], PredRefNo,PredComplDate  FROM mytable33

在此输入图像描述

when I run the above-mentioned query on both tables, the result is different: 当我在两个表上运行上述查询时,结果是不同的:

SELECT [Reference Number],
    CASE 
        WHEN LEN(Predecessors)>0 AND MAX(COALESCE(PredComplDate,'31/12/2099'))='31/12/2099' THEN NULL
        ELSE MAX(PredComplDate)
    END AS LatestPredComplDate
    FROM mytable22
    GROUP BY [Reference Number],Predecessors

SELECT [Reference Number],
    CASE 
        WHEN LEN(Predecessors)>0 AND MAX(COALESCE(PredComplDate,'31/12/2099'))='31/12/2099' THEN NULL
        ELSE MAX(PredComplDate)
    END AS LatestPredComplDate
    FROM mytable33
    GROUP BY [Reference Number],Predecessors

在此输入图像描述

The expected result is NULL for LastestPredComplDate 对于LastestPredComplDate ,预期结果为NULL

也许您可以将两个子句放在窗口函数中,如下所示(如果只是为了清晰起见):

  MAX(CASE WHEN LEN(Predecessors)>0 THEN NULL ELSE PredComplDate END) AS LatestPredComplDate

Eventually, I found out the problem. 最终,我发现了问题。 The date (stored as NVARCHAR) format was the culprit. 日期(存储为NVARCHAR)格式是罪魁祸首。 If you have another look at the first image that I posted you can see that the same dates have different formats. 如果您再看一下我发布的第一张图片,您可以看到相同的日期有不同的格式。 One is 04/12/2019 and the other is 4/12/2019 . 一个是04/12/2019 ,另一个是4/12/2019 It turns out the MAX function would consider 4/12/2019 larger than 04/12/2019 and that would generate different results. 事实证明MAX函数会考虑4/12/2019大于04/12/2019 ,这会产生不同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM