T-SQL 查询在彼此之后立即运行时返回不同的行数

T-SQL Query returning different number rows when run immediately after each other(T-SQL 查询在彼此之后立即运行时返回不同的行数)
本文介绍了T-SQL 查询在彼此之后立即运行时返回不同的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

下午所有

奇怪的是这个.我写了一个相当简单的查询,我认为它工作正常.但是,当我一次又一次地多次运行查询时,每次都会得到不同数量的行.它是一个实时数据库,但除了我之外,今天没有人使用它.我在参数中设置的日期范围是过去的,所以没有新的\删除的记录会影响它.所以我有点困惑,为什么有时我会得到更多的行,有时我应该每次都返回相同的行,但有时会得到更少的行.复制下面的代码

Strange one this. I have written a query which is fairly simple and I had assumed it was working ok. However, when I run the query, one after another, multiple times, I get a different number of rows every time. It is a live database, but apart from me, nobody is using it today. The date range I have set in my parameters is way in the past, so no new\deleted records should affect it. So I'm a little confused as to why sometimes I get more rows, sometimes less rows when I should get back the same rows every time. Copy of the code below

DECLARE 
        @From date,
        @To date

SET @From = '01/07/2015'
SET @To = '31/12/2015'

--AS


SELECT DISTINCT
    BR.Branch, 
    DIV.Division,
    BCM.ClientRef@ AS 'Client Reference',
    BPY.PolicyRef@ AS 'Policy Reference',
    AE.Name AS 'Account Executive',
    DIV2.#Name AS 'Account Handler',
    BTX.Dt_raised AS 'Effective Date',
    BTX.Ledger_dt AS 'Ledger Date',
    INS.VTDescription AS 'Insurer',
    BPY.Ptype AS 'Policy Type',
    BTX.Orig_Debt AS 'GWP',
    COALESCE(BTX.Comm_amt, 0) AS 'Commission',
    CASE
        WHEN BTX.Ccode = '7' THEN 0
        ELSE COALESCE(CHG.Orig_Debt, 0)
    END AS 'Fee',
    COALESCE(BTX.Comm_amt, 0) + CASE WHEN BTX.Ccode = '7' THEN 0 ELSE COALESCE(CHG.Orig_Debt, 0) END AS 'Income',
    CASE DATENAME(MONTH, BTX.Dt_Raised)
        WHEN 'July' THEN YEAR(BTX.Dt_Raised)
        WHEN 'August' THEN YEAR(BTX.Dt_Raised)
        WHEN 'September' THEN YEAR(BTX.Dt_Raised)
        WHEN 'October' THEN YEAR(BTX.Dt_Raised)
        WHEN 'November' THEN YEAR(BTX.Dt_Raised)
        WHEN 'December' THEN YEAR(BTX.Dt_Raised)
        WHEN 'January' THEN YEAR(DATEADD(YEAR, -1, BTX.Dt_Raised))
        WHEN 'February' THEN YEAR(DATEADD(YEAR, -1, BTX.Dt_Raised))
        WHEN 'March' THEN YEAR(DATEADD(YEAR, -1, BTX.Dt_Raised))
        WHEN 'April' THEN YEAR(DATEADD(YEAR, -1, BTX.Dt_Raised))
        WHEN 'May' THEN YEAR(DATEADD(YEAR, -1, BTX.Dt_Raised))
        WHEN 'June' THEN YEAR(DATEADD(YEAR, -1, BTX.Dt_Raised))
    END AS 'FinYear',
    CASE DATENAME(MONTH, BTX.Dt_Raised)
        WHEN 'January' THEN 'Ja'
        WHEN 'February' THEN 'Fe'
        WHEN 'March' THEN 'Ma'
        WHEN 'April' THEN 'Ap'
        WHEN 'May' THEN 'My'
        WHEN 'June' THEN 'Ju'
        WHEN 'July' THEN 'Jy'
        WHEN 'August' THEN 'Au'
        WHEN 'September' THEN 'Se'
        WHEN 'October' THEN 'Oc'
        WHEN 'November' THEN 'No'
        WHEN 'December' THEN 'De'
    END AS 'Month Letter',
    DATENAME(MONTH, BTX.Dt_Raised) AS 'MonthName',
    CASE DATENAME(MONTH, BTX.Dt_Raised)
        WHEN 'January' THEN 7
        WHEN 'February' THEN 8
        WHEN 'March' THEN 9
        WHEN 'April' THEN 10
        WHEN 'May' THEN 11
        WHEN 'June' THEN 12
        WHEN 'July' THEN 1
        WHEN 'August' THEN 2
        WHEN 'September' THEN 3
        WHEN 'October' THEN 4
        WHEN 'November' THEN 5
        WHEN 'December' THEN 6
    END AS 'Order',
    CASE ROW_NUMBER() OVER (PARTITION BY BCM.ClientRef@ ORDER BY BCM.ClientRef@)
        WHEN 1 THEN 1 ELSE 0
    END AS 'Client Count', 
    CASE ROW_NUMBER() OVER (PARTITION BY BPY.PolicyRef@ ORDER BY BPY.PolicyRef@)
        WHEN 1 THEN 1 ELSE 0
    END AS 'Policy Count'
FROM icp_yyclient AS BCM 
INNER JOIN icp_brpolicy AS BPY ON BCM.ClientRef@ = BPY.ClientRef@ AND BCM.Branch@ = BPY.Branch@
INNER JOIN icp_brcledger AS BTX ON BPY.PolicyRef@ = BTX.PolicyRef@ AND BPY.Branch@ = BTX.Branch@
LEFT JOIN icp_brcledger AS CHG ON BTX.Chg_ptr = CHG.Suffix AND BTX.PolicyRef@ = CHG.PolicyRef@ AND BTX.Branch@ = CHG.Branch@
LEFT JOIN AccountExecutives AS AE ON BCM.Job1 = AE.Code
LEFT JOIN icp_bredetail AS DIV2 ON BPY.#Exec = DIV2.Code AND BPY.Branch@ = DIV2.Branch@
LEFT JOIN Division_VT AS DIV ON DIV2.Ext_department = DIV.Division
LEFT JOIN Branch_VT AS BR ON DIV.Branch = BR.VTId
LEFT JOIN icp_INSC_VT AS INS ON BPY.Insco_INSC_VTId = INS.VTId

WHERE
    BPY.Branch@ = 0
    AND BTX.Dt_Raised BETWEEN @From AND @To
    AND (BTX.Trantype IN ('New Business') AND BTX.Trantype <> 'Charge')
    AND (BPY.Term_code IS NULL AND BPY.Term_date IS NULL)
ORDER BY BR.Branch, DIV.Division, BCM.ClientRef@

推荐答案

以下是 ROW_NUMBER() 查询如何影响结果数量的简单示例:

Here's a simple example of how the ROW_NUMBER() query can affect the number of results:

declare @t table (a int, b int, c int)
insert into @t(a,b,c) values
(1,1,1),
(1,1,1),
(1,1,2)

select distinct a,c,CASE ROW_NUMBER() OVER (ORDER BY b) WHEN 1 THEN 1 ELSE 0 END
from @t

您会注意到我的ORDER BY 子句不足以知道哪些行将被分配哪些行号.运行这个查询,我通常得到:

You'll notice that my ORDER BY clause is insufficient to know which rows will be assigned which row numbers. Running this query, I usually get:

a           c           
----------- ----------- -----------
1           1           0
1           1           1
1           2           0

但是,这个结果集也是可能的:

However, this result set is also possible:

a           c           
----------- ----------- -----------
1           1           0
1           2           1

这取决于哪一行被分配了行号 1.

And that just depends on which row gets assigned row number 1.

如果您需要确定性,请确保在 PARTITIONORDER BY 子句中指定足够的表达式,以便知道每一行的分配的行号将唯一确定.

If you want determinism, make sure that you specify enough expressions in your PARTITION and ORDER BY clauses so that you know that each row's assigned row number will be uniquely determined.

这篇关于T-SQL 查询在彼此之后立即运行时返回不同的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Number of working days between two dates(两个日期之间的工作日数)
How do I use dateadd to get the first day of last year?(如何使用 dateadd 获取去年的第一天?)
SQL- Count occurrences of a specific word within all stored procedures(SQL- 计算所有存储过程中特定单词的出现次数)
SQL query to make a column of numbers a string(使一列数字成为字符串的 SQL 查询)
T-SQL: Best way to replace NULL with most recent non-null value?(T-SQL:用最新的非空值替换 NULL 的最佳方法?)
Count days in date range with set of exclusions which may overlap(使用一组可能重叠的排除项计算日期范围内的天数)