繁体   English   中英

如何在SQL Server中对具有不同列名的两个表进行求和和组联合

[英]How to Sum and Group Union of Two Tables with Different Column Names in SQL Server

以下查询成功返回了信用卡并按供应商检查了日期范围内的付款。 我一直无法使用别名写一个外部选择语句来对金额和供应商分组。 我该如何查询?

SELECT
BillPaymentCreditCard.PayeeEntityRefFullName as Vendor, BillPaymentCreditCard.Amount as Amount 
FROM
BillPaymentCreditCard NOSYNC 
WHERE
BillPaymentCreditCard.TxnDate >= {d'2014-01-01'} and BillPaymentCreditCard.TxnDate <= {d'2014-02-01'} 

UNION ALL

SELECT
BillPaymentCheck.PayeeEntityRefFullName as Vendor, BillPaymentCheck.Amount as Amount 
FROM
BillPaymentCheck NOSYNC 
WHERE 
BillPaymentCheck.TxnDate >= {d'2014-01-01'} and  BillPaymentCheck.TxnDate <= {d'2014-02-01'}

这应该做。 -每个注释-删除nosync表提示。

SELECT Vendor, SUM(Amount) AS TotalAmount
FROM (

    SELECT BillPaymentCreditCard.PayeeEntityRefFullName as Vendor, BillPaymentCreditCard.Amount as Amount
    FROM BillPaymentCreditCard
    WHERE BillPaymentCreditCard.TxnDate >= {d'2014-01-01'}
         and BillPaymentCreditCard.TxnDate <= {d'2014-02-01'}

    UNION ALL

    SELECT BillPaymentCheck.PayeeEntityRefFullName as Vendor, BillPaymentCheck.Amount as Amount
    FROM BillPaymentCheck
    WHERE BillPaymentCheck.TxnDate >= {d'2014-01-01'}
         and BillPaymentCheck.TxnDate <= {d'2014-02-01'}
    ) AS Vendors
GROUP BY Vendor

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM