SQL查询以显示良好的记录以及空记录

时间:2016-07-11 15:32:59

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

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

这是我目前的查询:

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

3 个答案:

答案 0 :(得分:3)

我建议您在function Delete(id) { var submitdelete=function(){ $.ajax({ url: '@Url.Action("/mycontroller/Delete)', type: 'Post', data: { id: id } }) .done(function() { $('#' + id).remove();//if you want to delete table row msgBox.success("Success","Ok"); });} msgBox.okToContinue("warning", "Are you sure to delete ?", "warning", "ok","cancel", submitdelete); }的位置使用 var msgBox = { message: { settings: { Title: "", OkButtonText: "", type:"info" } }, okToContinue: function(title, text, type, okButtonText,closeButtonText, isConfirmDo) { swal({ title: title, text: text, type: type, showCancelButton: true, confirmButtonClass: 'btn-danger', confirmButtonText: okButtonText, cancelButtonText: closeButtonText, closeOnConfirm: false, closeOnCancel: true }, function(isConfirm) { if (isConfirm) { isConfirmDo(); } }); }, confirmToContinue: function(title, text, type, confirmButtonText, cancelButtonText, isConfirmDo, isNotConfirmDo, showLoader) { if (!showLoader) { showLoader = false; } swal({ title: title, text: text, type: type, showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: confirmButtonText, cancelButtonText: cancelButtonText, closeOnConfirm: true, closeOnCancel: true, showLoaderOnConfirm: showLoader }, function(isConfirm) { if (isConfirm) { isConfirmDo(); } }); } , success: function (title, text,okButtontex) { swal({ title: title, text: text, type: "success", confirmButtonText: okButtontex }); }, info: function (title, text) { swal({ title: title, text: text, type: "info", confirmButtonText: "OK" }); }, warning: function (title, text) { swal({ title: title, text: text, type: "warning", confirmButtonText: "OK" }); }, error: function (title, text) { swal({ title: title, text: text, type: "error", confirmButtonText: "OK" }); }, } 。然后,您应该保留RIGHT JOIN中不具有INNER JOIN匹配行的Customer行。

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

LEFT JOIN

答案 1 :(得分:0)

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

答案 2 :(得分:0)

修正了它。需要使用coalesce来使值正确显示。

还添加了"其中"如果我想查询个人客户,请选择

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