我被这个查询卡住了,我从两个查询中得到以下结果:
查询一:
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
任何帮助将不胜感激。