这是我的查询,我在这里重复输入,我想避免这种情况。
select TB100Desc, QNo,SerieShortName,LotID,LotName,
QuantityBalance as SendQty,WeightBalance as SendWt,InitialProportionalWeight as SendRWt,
0 as tnQty,0 as RtnWt, 0 as RtnRWt,
EstimateSizeID,NextProcessName,EstimateQualityID,previousprocessname,
case when NextProcessID IN (453,536) then 'Helium'
when NextProcessID IN (78) then 'Blocking'
when NextProcessID IN (204) then 'Rejection'
when NextProcessID IN (366) then 'Indore'
when NextProcessID IN (240) then 'Orchid'
when NextProcessID IN (203) then 'RO Rej stock'
else 'Others'
end as mgroup,
YEAR(trsdate) as myear,
MONTH(trsdate) as mmonth,
DAY(trsdate) as mday
from V_LotTransactions (nolock)
inner join R_Serie (nolock) on R_Serie.SerieID=V_LotTransactions.SerieID
where status='a'
and trstypeid in(609)
and DepartmentAccountID in (30,31)
and (previousprocessid in ('3101'))
and NextProcessID=205
and trsdate between '2016-04-26 00:00:01' and '2016-04-26 23:59:59'
union
select TB100Desc, QNo,SerieShortName,LotID,LotName,
QuantityBalance as SendQty,WeightBalance as SendWt,InitialProportionalWeight as SendRWt,
0 as tnQty,0 as RtnWt, 0 as RtnRWt,
EstimateSizeID,NextProcessName,EstimateQualityID,previousprocessname,
case when NextProcessID IN (453,536) then 'Helium'
when NextProcessID IN (78) then 'Blocking'
when NextProcessID IN (204) then 'Rejection'
when NextProcessID IN (366) then 'Indore'
when NextProcessID IN (240) then 'Orchid'
when NextProcessID IN (203) then 'RO Rej stock'
else 'Others'
end as mgroup,
YEAR(trsdate) as myear,
MONTH(trsdate) as mmonth,
DAY(trsdate) as mday
from V_LotTransactions (nolock)
inner join R_Serie (nolock) on R_Serie.SerieID=V_LotTransactions.SerieID
where status='a'
and trstypeid in(1002)
and DepartmentAccountID in (30,31)
and (previousprocessid in (3101,3105))
and NextProcessID=205
and trsdate between '2016-04-26 00:00:01' and '2016-04-26 23:59:59'
答案 0 :(得分:1)
我对你的2个联合查询进行了比较,结果只显示了以下差异:
...
and trstypeid in(1002)
...
and (previousprocessid in (3101,3105))
...
UNION
...
and trstypeid in(609)
...
and (previousprocessid in ('3101'))
首先,为什么previousprocessid
的数据类型在2个查询中有所不同?首先,您搜索数字,第二次搜索字符串
然后,提供以下事实:
我没有看到UNION的原因
为什么不这样做
select DISTINCT TB100Desc, QNo,SerieShortName,LotID,LotName,
QuantityBalance as SendQty,WeightBalance as SendWt,InitialProportionalWeight as SendRWt,
0 as tnQty,0 as RtnWt, 0 as RtnRWt,
EstimateSizeID,NextProcessName,EstimateQualityID,previousprocessname,
case when NextProcessID IN (453,536) then 'Helium'
when NextProcessID IN (78) then 'Blocking'
when NextProcessID IN (204) then 'Rejection'
when NextProcessID IN (366) then 'Indore'
when NextProcessID IN (240) then 'Orchid'
when NextProcessID IN (203) then 'RO Rej stock'
else 'Others'
end as mgroup,
YEAR(trsdate) as myear,
MONTH(trsdate) as mmonth,
DAY(trsdate) as mday
from V_LotTransactions (nolock)
inner join R_Serie (nolock) on R_Serie.SerieID=V_LotTransactions.SerieID
where status='a'
-------------------------
-- THIS :
and (
(trstypeid in(1002) and previousprocessid in (3101,3105))
OR
(trstypeid in(609) and previousprocessid in (3101))
)
-------------------------
and DepartmentAccountID in (30,31)
and NextProcessID=205
and trsdate between '2016-04-26 00:00:01' and '2016-04-26 23:59:59'
如果我错了,请解释UNION的原因