[英]Having separate columns as `type` with UNION, SQL query?
我有兩個表sales
和receivable
,以下是我正在運行的查詢,輸出是一個帶有 CREDIT 和 DEBIT 的列Type
但我想要的是有兩個單獨的列,一個用於CREDIT
,一個用於DEBIT
。 我怎么能有這個輸出?
SELECT Date,SaleInvoice,VendorCode,sum(sales.Total),'CREDIT' as Type from sales
UNION
SELECT Date,SaleInvoice,VendorCode,sum(receivable.Amount),'DEBIT' as Type from receivable
ORDER BY SaleInvoice
上述查詢的輸出;
Date SaleInvoice VendorCode sum(sales.Total) Type
2019-11-23 3 7100002 18613 CREDIT
2019-11-23 3 7100002 7200 DEBIT
我想要的是為 CREDIT 和 DEBIT 設置單獨的列。
Date SaleInvoice VendorCode sum(sales.Total) Type1 Type2
2019-11-23 3 7100002 18613 CREDIT
2019-11-23 3 7100002 7200 DEBIT
您可以在需要的地方使用空列
SELECT Date,SaleInvoice,VendorCode,sum(sales.Total), 'CREDIT' as Type1, null Type2
from sales
UNION
SELECT Date,SaleInvoice,VendorCode,sum(receivable.Amount), null, 'DEBIT'
from receivable
ORDER BY SaleInvoice
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.