0

我被这个查询卡住了,我从两个查询中得到以下结果:

查询一:

SELECT convert(varchar(10), date_creat, 101) as DateCreate, count(distinct packslip) as OrdersPendingPickUp
FROM pickhead
WHERE procstep = '17'
GROUP BY convert(varchar(10), date_creat, 101)
ORDER BY convert(varchar(10), date_creat, 101)

结果查询1:

DateCreate  OrdersPendingPickUp
2/11/2013   1
2/19/2013   1
2/21/2013   3
2/25/2013   1
2/26/2013   11
2/27/2013   6

查询 2:

SELECT convert(varchar(10), DateCreate, 101) as DateCreate, count(distinct PackSlip) as OrdersPendingInvoice
FROM tblB2B_Orders [to]
INNER JOIN tblTmpLsrType ttlt ON [to].LSRNbr = ttlt.shpno
WHERE 
    [to].PackSlip not in (SELECT PackSlip FROM pickhead)
    AND (ttlt.InvcNbr = '' OR ttlt.InvcNbr is null) 
GROUP BY convert(varchar(10), DateCreate, 101)
ORDER BY convert(varchar(10), DateCreate, 101)

结果查询2:

DateCreate  OrdersPendingInvoice
2/25/2013   1
2/26/2013   1

现在我想将两个结果合二为一,结果应如下所示:

DateCreate  OrdersPendingPickUp OrdersPendingInvoice
2/11/2013   1                     0
2/19/2013   1                     0
2/21/2013   3                     0
2/25/2013   1                     1
2/26/2013   11                    1
2/27/2013   6                     0

我做了以下,但它不能正常工作:

SELECT DISTINCT convert(varchar(10), DateCreate, 101) as DateCreate,
    isnull(
        (SELECT count(distinct packslip)
        FROM pickhead
        WHERE procstep = '17' AND (convert(varchar(10), [to].DateCreate, 101) = convert(varchar(10), date_creat, 101))
        GROUP BY convert(varchar(10), date_creat, 101)),0       
    ) as OrdersPendingPickUp,
    isnull(
        (SELECT count(distinct [to1].PackSlip)
        FROM tblB2B_Orders [to1]
        INNER JOIN tblTmpLsrType ttlt ON [to1].LSRNbr = ttlt.shpno
        WHERE 
            [to1].PackSlip not in (SELECT PackSlip FROM pickhead) 
            AND (ttlt.InvcNbr = '' OR ttlt.InvcNbr is null)
            AND [to1].DateCreate = [to].DateCreate
        GROUP BY convert(varchar(10), [to1].DateCreate, 101)),0 
    ) as OrdersPendingInvoice
FROM tblB2B_Orders [to]
ORDER BY convert(varchar(10), DateCreate, 101)

这是结果:

DateCreate  OrdersPendingPickUp OrdersPendingInvoice
2/11/2013   1                     0
2/19/2013   1                     0
2/20/2013   0                     0
2/21/2013   3                     0
2/25/2013   1                     0
2/25/2013   1                     1
2/26/2013   11                    0
2/26/2013   11                    1
2/27/2013   6                     0

任何帮助将不胜感激。

4

1 回答 1

1

这很容易通过完全外连接完成:

with query1 as (<query1>),
     query2 as (<query2>)
select coalesce(query1.dateCreate, query2.dateCreate) as DateCreate,
       coalesce(query2.OrdersPendingPickup, 0.0) as OrdersPendingPickup,
       coalesce(query1.OrdersPendingInvoice, 0.0) as OrdersPendingInvoice
from query1
     full outer join
     query2
     on query1.datecreate = query2.datecreated

请注意,您的查询进入该with子句。

你可以对子查询做同样的事情:

select coalesce(query1.dateCreate, query2.dateCreate) as DateCreate,
       coalesce(query2.OrdersPendingPickup, 0.0) as OrdersPendingPickup,
       coalesce(query1.OrdersPendingInvoice, 0.0) as OrdersPendingInvoice
from (<YOUR QUERY 1 GOES HERE>) query1
     full outer join
     (<YOUR QUERY 2 GOES HERE>) query2
     on query1.datecreate = query2.datecreated
于 2013-02-27T16:41:05.423 回答