查询多次返回同一行

时间:2019-09-27 21:50:43

标签: sql sql-server

我的查询多次返回一个结果。我知道答案必须很简单,但我很困惑。任何帮助表示赞赏。

我的代码如下。

USE ODS

DECLARE @recalctime AS TIME
DECLARE @portfolio AS VARCHAR(MAX)
DECLARE @loannumber AS BIGINT
DECLARE @zipcode AS VARCHAR(10)
DECLARE @timezone AS VARCHAR(10)
DECLARE @calltime AS TIME
DECLARE @cst AS DATETIME
DECLARE @calldate AS DATETIME

SELECT
@portfolio = CASE
    WHEN r.PortfolioID = '10' THEN 'A'
    WHEN r.portfolioid = '11' THEN 'B'
    WHEN r.PortfolioID = '12' THEN 'C'
    ELSE CAST(r.portfolioid AS VARCHAR)
    END 
, @calldate = CAST(r.RowCreatedDate AS DATE) 
, @loannumber = r.loannumber 
, @zipcode = r.zipcode 
, @timezone = r.zone 
, @calltime = CONVERT(char(10), r.contacttime, 108) 
, @recalctime = CASE
    WHEN r.Zone = 'PST' THEN DATEADD(HOUR, -3, r.contacttime)
    WHEN r.Zone = 'CST' THEN DATEADD(HOUR, -1, r.contacttime)
    WHEN r.Zone = 'EST' THEN r.contacttime
    WHEN r.Zone = 'MST' THEN DATEADD(HOUR, -2, r.contacttime)
    ELSE CONVERT(char(10), r.contacttime, 108)
    END 

FROM dbo.table r (nolock)


SELECT 
@portfolio AS 'Product'
, @loannumber AS 'Loan Number'
, CAST(@calldate AS DATE) AS 'Call Date'
, @zipcode AS 'Zip Code'
, CONVERT(char(10), @calltime, 108) AS 'Timezone'
, CONVERT(char(10), @recalctime, 108) AS 'Call Time'
, DATEPART(hh, @recalctime)
, @timezone AS 'Recalc Time'

FROM dbo.table r (nolock)

我得到89,928行相同的结果。

3 个答案:

答案 0 :(得分:2)

您的第一个查询将为表中的每一行执行本地var分配,为每一行重写此值,最后保存从最后一行派生的值。您的第二个查询返回的值与行数一样多。

如果只想输出派生的值,请将这两个查询合并为一个,而根本不用局部变量。

答案 1 :(得分:1)

第一个查询是为每行分配变量,这可能不是您要尝试的。

通常,第一个查询的大多数用法将包含where子句,因此值将来自特定行:

SELECT
@portfolio = CASE
    WHEN r.PortfolioID = '10' THEN 'A'
    WHEN r.portfolioid = '11' THEN 'B'
    WHEN r.PortfolioID = '12' THEN 'C'
    ELSE CAST(r.portfolioid AS VARCHAR)
    END 
, @calldate = CAST(r.RowCreatedDate AS DATE) 
, @loannumber = r.loannumber 
, @zipcode = r.zipcode 
, @timezone = r.zone 
, @calltime = CONVERT(char(10), r.contacttime, 108) 
, @recalctime = CASE
    WHEN r.Zone = 'PST' THEN DATEADD(HOUR, -3, r.contacttime)
    WHEN r.Zone = 'CST' THEN DATEADD(HOUR, -1, r.contacttime)
    WHEN r.Zone = 'EST' THEN r.contacttime
    WHEN r.Zone = 'MST' THEN DATEADD(HOUR, -2, r.contacttime)
    ELSE CONVERT(char(10), r.contacttime, 108)
    END 

FROM dbo.table r (nolock)
WHERE [MyColumn] = something

在第二个查询中不需要“ from dbo.table”,因为它将选择表中每个记录的变量。

 SELECT 
    @portfolio AS 'Product'
    , @loannumber AS 'Loan Number'
    , CAST(@calldate AS DATE) AS 'Call Date'
    , @zipcode AS 'Zip Code'
    , CONVERT(char(10), @calltime, 108) AS 'Timezone'
    , CONVERT(char(10), @recalctime, 108) AS 'Call Time'
    , DATEPART(hh, @recalctime)
    , @timezone AS 'Recalc Time'

;

如果不将变量用于其他任何用途,则可以在单个查询中将它们全部组合在一起:

SELECT
    [Product] = CASE
        WHEN r.PortfolioID = '10' THEN 'A'
        WHEN r.portfolioid = '11' THEN 'B'
        WHEN r.PortfolioID = '12' THEN 'C'
        ELSE CAST(r.portfolioid AS VARCHAR)
        END 
   -- Note you can also use the 'AS' syntax depending on version of sql server
    , CAST(r.RowCreatedDate AS DATE) AS 'Call Date'
    ...etc        
    FROM dbo.table r (nolock)
    WHERE [MyColumn] = something

答案 2 :(得分:0)

因为表“ dbo.table”中共有89,928行,所以它显示了89,928次。

您只需要选择变量

SELECT 
@portfolio AS 'Product'
, @loannumber AS 'Loan Number'
, CAST(@calldate AS DATE) AS 'Call Date'
, @zipcode AS 'Zip Code'
, CONVERT(char(10), @calltime, 108) AS 'Timezone'
, CONVERT(char(10), @recalctime, 108) AS 'Call Time'
, DATEPART(hh, @recalctime)
, @timezone AS 'Recalc Time'