SQL Server查询获取重复值

时间:2016-04-27 07:30:06

标签: sql-server

这是我的查询,我在这里重复输入,我想避免这种情况。

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'

1 个答案:

答案 0 :(得分:1)

我对你的2个联合查询进行了比较,结果只显示了以下差异:

...
and trstypeid in(1002)
...
and (previousprocessid in (3101,3105))
...

UNION

...
and trstypeid in(609)
...
and (previousprocessid in ('3101'))

首先,为什么previousprocessid的数据类型在2个查询中有所不同?首先,您搜索数字,第二次搜索字符串

然后,提供以下事实:

  1. 您在WHERE子句中只有轻微的差异
  2. 您在SELECT子句中没有特定的列可以"限定"返回的行在它们返回的查询之后具有固定值,以便进一步处理
  3. 你不想要重复
  4. 我没有看到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的原因