t-sql: pivot, unpivot, except, intersect

27
很很很很 T-SQL in PSC Bill Lin 2014/9/12

Upload: bill-lin

Post on 17-Jun-2015

177 views

Category:

Technology


2 download

DESCRIPTION

T-SQL: Pivot, Unpivot, Except, Intersect

TRANSCRIPT

Page 1: T-SQL: Pivot, Unpivot, Except, Intersect

很好用的 T-SQLin PSC

Bill Lin 2014/9/12

Page 2: T-SQL: Pivot, Unpivot, Except, Intersect

2

案例每次財測作業啟動後,複製上次財測作業各部門所屬計畫的分包設定及開放其他部門的待爭取計畫。( 跨年度時不複製 )

Page 3: T-SQL: Pivot, Unpivot, Except, Intersect

3

ERD

Foreign

Key

Page 4: T-SQL: Pivot, Unpivot, Except, Intersect

4

How?

Page 5: T-SQL: Pivot, Unpivot, Except, Intersect

5

2 月資料2 月資料 7 月資料

Page 6: T-SQL: Pivot, Unpivot, Except, Intersect

6

Except & IntersectA Except B B Except A

Intersectof A & B

Set A Set B

Page 7: T-SQL: Pivot, Unpivot, Except, Intersect

7

• 比較兩個查詢的結果來傳回個別值。• EXCEPT 會從左側查詢中傳回在右側查詢中找不到的任何個別值。• INTERSECT 會傳回 INTERSECT 運算元左右兩側查詢都傳回的任何個別值。• 使用 EXCEPT 或 INTERSECT 的兩個查詢,其結果集的基本組合規則如下:

• 在所有查詢中,資料行的數目和順序都必須相同。• 資料類型必須相容。

EXCEPT 和 INTERSECT (Transact-SQL)

{ <query_specification> | ( <query_expression> ) } { EXCEPT | INTERSECT } { <query_specification> | ( <query_expression> ) }

來源: http://msdn.microsoft.com/zh-tw/library/ms188055.aspx

Page 8: T-SQL: Pivot, Unpivot, Except, Intersect

8

DEMOSQL 檔案連結: http://1drv.ms/1o8hhkk

Page 9: T-SQL: Pivot, Unpivot, Except, Intersect

9

方便的代價?

Page 10: T-SQL: Pivot, Unpivot, Except, Intersect

10

Page 11: T-SQL: Pivot, Unpivot, Except, Intersect

11

In .NET

Page 12: T-SQL: Pivot, Unpivot, Except, Intersect

12

http://msdn.microsoft.com/zh-tw/library/vstudio/system.linq.enumerable.intersect(v=vs.110).aspx

http://msdn.microsoft.com/zh-tw/library/vstudio/system.linq.enumerable.except(v=vs.110).aspx

Page 13: T-SQL: Pivot, Unpivot, Except, Intersect

13

Page 14: T-SQL: Pivot, Unpivot, Except, Intersect

14

案例將各月人月數,依員工編號分列呈現。

Page 15: T-SQL: Pivot, Unpivot, Except, Intersect

15

ERD

1

N

Page 16: T-SQL: Pivot, Unpivot, Except, Intersect

16

How?

Page 17: T-SQL: Pivot, Unpivot, Except, Intersect

17

PROJ_MONTH MAN_MONTH

2014-01-01 0.5

2014-02-01 1

2014-03-01 1

2014-04-01 1

2014-05-01 0.5

2014-06-01 1

… …

JAN FEB MAR APR MAY JUN …

0.5 1 1 1 0.5 1 …

PIVOT/

p'ɪvət/

Page 18: T-SQL: Pivot, Unpivot, Except, Intersect

18

使用 PIVOT 和 UNPIVOT• 您可以使用 PIVOT 和 UNPIVOT 關係運算子,將資料表值運算式變更為另

一個資料表。• PIVOT 會將運算式內一個資料行中的唯一值轉成輸出中的多個資料行,以旋

轉資料表值運算式,然後依據最終輸出的需要,對其餘的任何資料行值執行必要的彙總。

• UNPIVOT 執行的作業則與 PIVOT 相反,它會將資料表值運算式旋轉為資料行值。

SELECT < 非樞紐資料行 >,    [ 第一個樞紐資料行 ] AS < 資料行名稱 >,    [ 第二個樞紐資料行 ] AS < 資料行名稱 >,    ...    [ 最後一個樞紐資料行 ] AS < 資料行名稱 >FROM    (< 產生資料的 SELECT 查詢 >)    AS < 來源查詢的別名 >PIVOT(    < 彙總函式 >(< 要彙總的資料行 >)FOR[< 包含將變成資料行標頭之值的資料行 >]    IN ( [ 第一個樞紐資料行 ], [ 第二個樞紐資料行 ],    ... [ 最後一個樞紐資料行 ])) AS < 樞紐分析表的別名 >< 選擇性的 ORDER BY 子句 >;

來源: http://technet.microsoft.com/zh-tw/library/ms177410(v=sql.105).aspx

Page 19: T-SQL: Pivot, Unpivot, Except, Intersect

19

DEMOSQL 檔案連結: http://1drv.ms/1o8hLqR

Page 20: T-SQL: Pivot, Unpivot, Except, Intersect

20

方便的代價?

Page 21: T-SQL: Pivot, Unpivot, Except, Intersect

21

Page 22: T-SQL: Pivot, Unpivot, Except, Intersect

22

In .NET

Page 23: T-SQL: Pivot, Unpivot, Except, Intersect

23

public decimal JANUARY { get; set; }

public decimal FEBRUARY { get; set; }

public decimal MARCH { get; set; }

public decimal APRIL { get; set; }     

public decimal MAY { get; set; }

public decimal JUNE { get; set; }

public decimal JULY { get; set; }      

public decimal AUGUST { get; set; }       

public decimal SEPTEMBER { get; set; } 

public decimal OCTOBER { get; set; }     

public decimal NOVEMBER { get; set; }

public decimal DECEMBER { get; set; }

PIVOT EntityOriginal Entities

LINQ query for PIVOT

Page 24: T-SQL: Pivot, Unpivot, Except, Intersect

24

List<CustData> myList = GetCustData();

var query = myList .GroupBy(c => c.EMPNO) .Select(g => new {

JANUARY = g.Where(c => c.PROJ_MONTH.Month == 1).Sum(c => c.MAN_MONTH), FEBURARY = g.Where(c => c.PROJ_MONTH.Month == 2).Sum(c => c.MAN_MONTH), MARCH = g.Where(c => c.PROJ_MONTH.Month == 3).Sum(c => c.MAN_MONTH),APRIL = g.Where(c => c.PROJ_MONTH.Month == 4).Sum(c => c.MAN_MONTH),MAY = g.Where(c => c.PROJ_MONTH.Month == 5).Sum(c => c.MAN_MONTH),JUNE = g.Where(c => c.PROJ_MONTH.Month == 6).Sum(c => c.MAN_MONTH),JULY = g.Where(c => c.PROJ_MONTH.Month == 7).Sum(c => c.MAN_MONTH),AUGUST = g.Where(c => c.PROJ_MONTH.Month == 8).Sum(c => c.MAN_MONTH),SEPTEMBER = g.Where(c => c.PROJ_MONTH.Month == 9).Sum(c => c.MAN_MONTH),OCTOBER = g.Where(c => c.PROJ_MONTH.Month == 10).Sum(c => c.MAN_MONTH),NOVEMBER = g.Where(c => c.PROJ_MONTH.Month == 11).Sum(c => c.MAN_MONTH), DECEMBER = g.Where(c => c.PROJ_MONTH.Month == 12).Sum(c => c.MAN_MONTH)

});

LINQ query for PIVOT

Page 25: T-SQL: Pivot, Unpivot, Except, Intersect

25

LINQ query for UNPIVOT

public decimal JANUARY { get; set; }

public decimal FEBRUARY { get; set; }

public decimal MARCH { get; set; }

public decimal APRIL { get; set; }     

public decimal MAY { get; set; }

public decimal JUNE { get; set; }

public decimal JULY { get; set; }      

public decimal AUGUST { get; set; }       

public decimal SEPTEMBER { get; set; } 

public decimal OCTOBER { get; set; }     

public decimal NOVEMBER { get; set; }

public decimal DECEMBER { get; set; }

PIVOT Entity UNPIVOT Entities

Page 26: T-SQL: Pivot, Unpivot, Except, Intersect

26

List<PEE_DETAIL> PDList = new List<PEE_DETAIL>();PEE_DETAIL PD;double MAN_MONTH;

for (int i = 1; i <= 12; i++){

MAN_MONTH = Convert.ToDouble(item.GetType().GetProperty(item.MonthMappingTable[i]).GetValue(item, null));                      if (i >= FF_START_MONTH.Month && MAN_MONTH > 0){                                    

PD = new PEE_DETAIL(){                                        

PEE_UNIQNO = PEE_UNIQNO,EMPNO = item.EMPNO, PROJ_MONTH = new DateTime(FF_START_MONTH.Year, i, 1),                                         MAN_MONTH = MAN_MONTH,                                         MODIFY_BY = UserID,                                         CREATE_BY = UserID,                                         MODIFY_DATE = DateTime.Now,CREATE_DATE = DateTime.Now

};PDList.Add(PD);

}                            }

public Constructor(){

_MonthMappingTable = new Dictionary<int, string>(12);_MonthMappingTable.Add(1, "JANUARY");_MonthMappingTable.Add(2, "FEBRUARY");_MonthMappingTable.Add(3, "MARCH");             _MonthMappingTable.Add(4, "APRIL");_MonthMappingTable.Add(5, "MAY");_MonthMappingTable.Add(6, "JUNE");_MonthMappingTable.Add(7, "JULY");             _MonthMappingTable.Add(8, "AUGUST");_MonthMappingTable.Add(9, "SEPTEMBER");_MonthMappingTable.Add(10, "OCTOBER");_MonthMappingTable.Add(11, "NOVEMBER");_MonthMappingTable.Add(12, "DECEMBER");

}

private Dictionary<int, string> _MonthMappingTable;public Dictionary<int, string> MonthMappingTable{

get{

return _MonthMappingTable;}

}

LINQ query for UNPIVOT

Page 27: T-SQL: Pivot, Unpivot, Except, Intersect

27

Q & A