union的指令,遇到兩筆一樣的資料時會只取一筆資料,
若要兩筆都取的話,在兩個table中加上一個固定的值即可,
則即便兩筆資料是一樣的,也會因為 該固定值不同,而不會變成同一筆
select spid,spname,isnull(worktypeid,'') Worktypeid,sum(cntemp) cntemp,
Sum(cnttotal) cntTotal, department, departmentName, worktype ,1 table1
from monsaleproduct where 1=1 and left(Department,1) = 'W'
and DataDate = '200712' AND LEFT(Division,1) = 'W'
and LEFT(Division,1) = 'W' and worktypeid='bsn00s'
group by spid,spname,worktypeid,department,departmentName,worktype
union
select spid,spname,isnull(worktypeid,'') Worktypeid,sum(cntemp) cntemp,
Sum(cnttotal) cntTotal, division, divisionName, worktype,2 table2
from monsaleproduct
where 1=1 and left(Department,1) <> left(division,1) and left(division,1)='W'
and department in ( select distinct department from monsaleproduct
where left(Department,1) <> left(division,1) and left(division,1)='W' )
and DataDate = '200712' AND LEFT(Division,1) = 'W'
and LEFT(Division,1) = 'W' and worktypeid='bsn00s'
group by spid,spname,worktypeid,division,divisionName,worktype
- 1月 04 週五 200810:22
【SQL】使用union時遇到相同的資料時要取二筆資料的解決方法
文章標籤
全站熱搜
