繁体   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