簡體   English   中英

如何使用聯合和識別表 a 和表 b 中的數據編寫 sql 語句

[英]How to write a sql statement using union and identifying data from table a and table b

我正在嘗試編寫一個 SQL 查詢,在我使用聯合后,我想確定哪些數據來自銷售表,哪些數據來自庫存表

Select customer 
     , calendar date 
     , sales amount 
     , location 
  From Sales 
 Union 
Select customer 
     , cal date
     , sales price 
     , distribution location 
  From inventory

經典的方法是使用額外的列

Select 'SALES' origin,
     , customer 
     , calendar date 
     , sales amount 
     , location 
  From Sales 
 Union 
Select 'INVENTORY'
     , customer 
     , cal date
     , sales price 
     , distribution location 
  From inventory

UPD:僅供參考,“union”子句會刪除所有影響性能的重復項,因此如果不打算刪除重復項,最好使用“union all”代替

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM