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

文章標籤
全站熱搜
創作者介紹
創作者 可樂果公主 的頭像
可樂果公主

聽說幸福…在山的那邊 (浪機子)

可樂果公主 發表在 痞客邦 留言(0) 人氣(475)