[英]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.