Dynamic SQL - povit table with row total and column total

Q1, Q2, Q3:





Code:

CREATE TABLE #PovitTable
(
RowHeader varchar(15),
ColumnHeader varchar(20),
VariableValue int
)

INSERT INTO #PovitTable VALUES ('North America','Sales','2000000')
INSERT INTO #PovitTable VALUES ('North America','Expenses','1250000')
INSERT INTO #PovitTable VALUES ('North America','Taxes','250000')
INSERT INTO #PovitTable VALUES ('North America','Profit','500000')
INSERT INTO #P

...繼續閱讀 »

SQL 合併數行資料為一行 Combine Row data to a column

合併Q1中的數行資料為Q2/Q3/Q4.

Combine the row data in Q1 Table to Q2/Q3/Q4.

Q1 = 資料源, table source

Q2 = Example 1

Q3 = Example 2

Q4 = Example 3

個人覺得使用stuff 比 substring 好, 因為不用寫上長度.



Q1,Q2,Q3,Q4 result:





Code:


create table #temp(Staff varchar(50),Department varchar(50))

insert into #temp values('A','IT')
insert into #temp values('B','IT')
insert i

...繼續閱讀 »