【问题标题】:SQL query to show good records as well as null recordsSQL 查询以显示良好记录以及空记录
【发布时间】:2016-07-11 15:32:59
【问题描述】:

我的查询非常适合查找具有真实值的记录,但是,我还需要我的查询来显示具有空值的记录。到目前为止,我尝试重新创建此查询以显示空值已导致我的至少 1 列结果丢失,所以现在我正在寻求帮助。

这是我目前的查询:

SELECT sq.*, sq.TransactionCountTotal - sq.CompleteTotal as InProcTotal from 
(
select 
c.CustName,
t.[City],
sum (t.TransactionCount) as TransactionCountTotal
sum (
    case 
        when (
            [format] in (23,25,38) 
            or [format] between 400 and 499 
            or format between 800 and 899
            )
    then t.TransactionCount
    else 0
    end
) as CompleteTotal
FROM [log].[dbo].[TransactionSummary] t
INNER JOIN [log].[dbo].[Customer] c
    on t.CustNo = c.CustNo
    and t.City = c.City
    and t.subno = c.subno
where t.transactiondate between '7/1/16' and '7/11/16'
group by c.CustName,t.City
) sq

这是目前我的查询结果显示的:

CustName    City    InProcTotal TransactionCountTotal   Complete Total
Cust 1     City(a)      23               7                    30
Cust 2     City(b)      74               2                    76
Cust 3     City(c)      54               4                    58

这就是我希望我的查询结果显示的内容:

CustName    City    InProcTotal TransactionCountTotal   Complete Total
Cust 1     City(a)      23               7                    30
Cust 2     City(b)      74               2                    76
Cust 3     City(c)      54               4                    58
Cust 4     City(d)      0                0                    0
Cust 5     City(e)      0                0                    0

【问题讨论】:

    标签: sql-server null left-join inner-join


    【解决方案1】:

    我建议你使用RIGHT JOIN 代替INNER JOIN。然后,您应该保留 Customer 中在 TransactionSummary 中没有匹配行的行。

    您可能还想像这样重构查询,以便使用LEFT JOIN。下一个处理查询的人会感谢你; LEFT JOIN 操作更常见。

    FROM [log].[dbo].[Customer] c
    LEFT JOIN [log].[dbo].[TransactionSummary] t
    on t.CustNo = c.CustNo
    and t.City = c.City
    

    【讨论】:

    • 当我使用 RIGHT JOIN 时,我得到了一些额外的结果,但是当我应该得到 sum (t.TransactionCount) as TransactionCountTotal 等于 NULL 时,它们显示 Custname = NULL
    • 对不起,我尝试了 Left Join 并在下面的答案中分享了结果。我觉得添加有关 Right Join 的信息也可能会有所帮助,因此我在此答案下发布了这些结果..
    【解决方案2】:

    jwabsolution,您的问题源于获取所有交易而不是所有客户。我的想法是这样运作的:你想选择所有的客户并找到所有的交易状态。因此,您应该从客户表中进行选择。此外,您不应使用 INNER JOIN,否则您将忽略任何没有交易的客户。相反,使用左连接事务表。通过这种方式,您将检索所有客户(即使是没有交易的客户)。这是 SQL 连接的良好视觉效果:http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_orig.jpg

    所以你的查询应该是这样的:

    SELECT sq.*, sq.TransactionCountTotal - sq.CompleteTotal as InProcTotal from 
    (
    select 
    c.CustName,
    t.[City],
    sum (t.TransactionCount) as TransactionCountTotal
    sum (
        case 
            when (
                [format] in (23,25,38) 
                or [format] between 400 and 499 
                or format between 800 and 899
                )
        then t.TransactionCount
        else 0
        end
    ) as CompleteTotal
    FROM [log].[dbo].[Customer] c
    LEFT JOIN [log].[dbo].[TransactionSummary] t
        on c.CustNo = t.CustNo
        and c.City = t.City
        and c.subno = t.subno
    where t.transactiondate between '7/1/16' and '7/11/16'
    group by c.CustName,t.City
    ) sq
    

    【讨论】:

    • 不幸的是,这给了我一组相同的结果,但我会坚持下去,也许我忽略了一些东西 - 这是一个漫长的周末。
    【解决方案3】:

    修复它。需要使用合并来使值正确显示。

    如果我想查询个别客户,还添加了“位置”选项

    SELECT sq.* ,sq.TransactionCountTotal - sq.CompleteTotal as [InProcTotal]
        from
    (
    select
            c.custname
            ,c.port
            ,sum(coalesce(t.transactioncount,0)) as TransactionCountTotal
            ,sum(
                case when (
                        [format]in(23,25,38)
                        or[format]between 400 and 499
                        or[format]between 800 and 899)
                    then t.TransactionCount
                    else 0 
                    end) as CompleteTotal
        from log.dbo.customer c
        left join log.dbo.TransactionSummary t
        on c.custNo=t.custno
        and c.subno=t.subno
        and c.city=t.city
        and t.transactiondate between '7/1/16' and '7/12/16'
        /*where c.custname=''*/
        group by c.custname,c.city
    ) sq
    

    【讨论】:

      猜你喜欢
      • 2016-04-16
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多