1. 程式人生 > >VBA+SQL transform pivot union聯合查詢的基礎應用

VBA+SQL transform pivot union聯合查詢的基礎應用

Sub 專案狀態查詢()

'如果“專案狀態”是未轉運營那麼實施狀態是不能選擇的,因為還沒有實施。
'如果“專案狀態”選擇狀態後,那麼專案名稱裡面只顯示該狀態的專案名稱。如果“專案狀態”選擇的不是未轉運營,那麼“實施狀態”也要選擇,並確定專案名稱的選項。
'如果“統計值”是專案數,那麼標題行就是客戶角色。如果是實施時長,標題行就是實施的時間。
lst = Range("a1048576").End(xlUp).Row
If lst >= 4 Then
Range("a4:afd" & lst).ClearContents
End If

sAddress = ThisWorkbook.FullName

If Range("b1") = "未轉運營" Or Range("b1") = "全部" Then
sAddress1 = Sheets("銷售").Range("a3").CurrentRegion.Address(0, 0)

If Range("b1") = "全部" Then
temp1 = "輔助列"
Else
temp1 = "專案狀態"
End If

If Range("d1") = "全部" Then
temp2 = "輔助列"
Else
temp2 = "城市維度"
End If

If Range("d2") = "全部" Then
temp3 = "輔助列"
Else
temp3 = "加盟型別"
End If

If Range("h2") = "全部" Then
temp4 = "輔助列"
Else
temp4 = "專案名稱"
End If



Set cnn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cnn.Open "Provider=microsoft.Ace.oledb.12.0;Extended Properties=Excel 12.0;Data Source=" & sAddress

strsql = "select * from [銷售$" & sAddress1 & "] where " & temp1 & " = '" & Range("b1") & "' and " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "'"

strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , count(*) as 統計 from (select * from [銷售$" & sAddress1 & "] where " & temp1 & " = '" & Range("b1") & "' and " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "') group by " & Range("b3").Value & " , " & Range("b2").Value & ""

strsql = "transform count(*) select " & Range("b2").Value & " as 統計 from (select * from [銷售$" & sAddress1 & "] where " & temp1 & " = '" & Range("b1") & "' and " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "') group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""

strsql = "transform count(*) select " & Range("b3").Value & " as 統計 from (select * from [銷售$" & sAddress1 & "] where " & temp1 & " = '" & Range("b1") & "' and " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "') group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""

strsql = "transform count(*) select " & Range("b3").Value & " as 統計 from (select * from [銷售$" & sAddress1 & "] where " & temp1 & " = '" & Range("b1") & "' and " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "') group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""

strsql = "transform count(*) select " & Range("b2").Value & " as 統計 from (select * from [銷售$" & sAddress1 & "] where " & temp1 & " = '" & Range("b1") & "' and " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "') group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""


'sum(iif(isnull(專案名稱),0,1))

rs.Open (strsql), cnn
Cells(5, 1).CopyFromRecordset cnn.Execute(strsql)


For Each Field In rs.Fields
aa = Field.Name
[a4].Offset(0, i) = Field.Name
i = i + 1
Next


Set rs = Nothing
Set cnn = Nothing
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

ElseIf Range("b1") = "銷售轉運營" And Range("h1") = "專案數" Then
sAddress1 = Sheets("實施").Range("a3").CurrentRegion.Address(0, 0)



If Range("d1") = "全部" Then
temp2 = "輔助列"
Else
temp2 = "城市維度"
End If

If Range("d2") = "全部" Then
temp3 = "輔助列"
Else
temp3 = "加盟型別"
End If

If Range("h2") = "全部" Then
temp4 = "輔助列"
Else
temp4 = "專案名稱"
End If

If Range("f1") = "全部" Then
temp5 = "輔助列"
Else
temp5 = "實施進度"
End If

If Range("f2") = "全部" Then
temp6 = "輔助列"
Else
temp6 = "實施型別"
End If



Set cnn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cnn.Open "Provider=microsoft.Ace.oledb.12.0;Extended Properties=Excel 12.0;Data Source=" & sAddress

strsql = "select * from [實施$" & sAddress1 & "] where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "' and " & temp5 & " = '" & Range("f1") & "' and " & temp6 & " = " & "'" & Range("f2") & "'"

strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , count(*) as 統計 from (select * from [實施$" & sAddress1 & "] where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "' and " & temp5 & " = '" & Range("f1") & "' and " & temp6 & " = " & "'" & Range("f2") & "') group by " & Range("b3").Value & " , " & Range("b2").Value & ""

strsql = "transform count(*) select " & Range("b2").Value & " as 統計 from (select * from [實施$" & sAddress1 & "] where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "' and " & temp5 & " = '" & Range("f1") & "' and " & temp6 & " = " & "'" & Range("f2") & "') group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""

strsql = "transform count(*) select " & Range("b3").Value & " as 統計 from (select * from [實施$" & sAddress1 & "] where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "' and " & temp5 & " = '" & Range("f1") & "' and " & temp6 & " = " & "'" & Range("f2") & "') group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""

strsql = "transform count(*) select " & Range("b3").Value & " as 統計 from (select * from [實施$" & sAddress1 & "] where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "' and " & temp5 & " = '" & Range("f1") & "' and " & temp6 & " = " & "'" & Range("f2") & "') group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""

strsql = "transform count(*) select " & Range("b2").Value & " as 統計 from (select * from [實施$" & sAddress1 & "] where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "' and " & temp5 & " = '" & Range("f1") & "' and " & temp6 & " = " & "'" & Range("f2") & "') group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""


'sum(iif(isnull(專案名稱),0,1))

rs.Open (strsql), cnn
Cells(5, 1).CopyFromRecordset cnn.Execute(strsql)


For Each Field In rs.Fields
aa = Field.Name
[a4].Offset(0, i) = Field.Name
i = i + 1
Next


Set rs = Nothing
Set cnn = Nothing


'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ElseIf Range("b1") = "銷售轉運營" And Range("h1") <> "專案數" Then
sAddress1 = Sheets("實施").Range("a3").CurrentRegion.Address(0, 0)
If Range("d1") = "全部" Then
temp2 = "輔助列"
Else
temp2 = "城市維度"
End If

If Range("d2") = "全部" Then
temp3 = "輔助列"
Else
temp3 = "加盟型別"
End If

If Range("f1") = "全部" Then
temp4 = "輔助列"
Else
temp4 = "實施進度"
End If

If Range("f2") = "全部" Then
temp5 = "輔助列"
Else
temp5 = "實施型別"
End If

If Range("h2") = "全部" Then
temp6 = "輔助列"
Else
temp6 = "專案名稱"
End If


Set cnn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cnn.Open "Provider=microsoft.Ace.oledb.12.0;Extended Properties=Excel 12.0;Data Source=" & sAddress

str1 = "select 銷售轉運營日期,週期維度,專案名稱,城市維度,加盟型別,客戶角色,倉庫名,實施進度,專案階段,階段狀態,實施型別,輔助列,月份維度," & _
" datediff('d',專案開始時間,專案結束時間) as 天數,運營經理人數 as 人數,運營經理成本 as 成本,'運營經理' as 崗位,'城市運營組' as 組名 from [實施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"

str2 = "select 銷售轉運營日期,週期維度,專案名稱,城市維度,加盟型別,客戶角色,倉庫名,實施進度,專案階段,階段狀態,實施型別,輔助列,月份維度," & _
" datediff('d',倉調研開始日期,倉調研結束日期) as 天數,倉調研人數 as 人數,倉調研成本 as 成本,'倉調研' as 崗位,'倉運營組' as 組名 from [實施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"

str3 = "select 銷售轉運營日期,週期維度,專案名稱,城市維度,加盟型別,客戶角色,倉庫名,實施進度,專案階段,階段狀態,實施型別,輔助列,月份維度," & _
" datediff('d',配調研開始日期,配調研結束日期) as 天數,配調研人數 as 人數,配調研成本 as 成本,'配調研' as 崗位,'配運營組' as 組名 from [實施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"

str4 = "select 銷售轉運營日期,週期維度,專案名稱,城市維度,加盟型別,客戶角色,倉庫名,實施進度,專案階段,階段狀態,實施型別,輔助列,月份維度," & _
" datediff('d',倉庫規劃實施開始日期,倉庫規劃實施結束日期) as 天數,倉規劃人數 as 人數,規劃成本 as 成本,'倉規劃' as 崗位,'倉庫規劃組' as 組名 from [實施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"

str5 = "select 銷售轉運營日期,週期維度,專案名稱,城市維度,加盟型別,客戶角色,倉庫名,實施進度,專案階段,階段狀態,實施型別,輔助列,月份維度," & _
" datediff('d',實施籌備開始日期,系統上線日期) as 天數,實施人數 as 人數,實施人員成本 as 成本,'實施' as 崗位, '實施組' as 組名 from [實施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"

str6 = "select 銷售轉運營日期,週期維度,專案名稱,城市維度,加盟型別,客戶角色,倉庫名,實施進度,專案階段,階段狀態,實施型別,輔助列,月份維度," & _
" datediff('d',B2B進件資料稽核開始時間,商品B2B平臺釋出上架日期) as 天數,電商人數 as 人數,null as 成本,'電商客服' as 崗位,'客服組' as 組名 from [實施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"

str7 = "select 銷售轉運營日期,週期維度,專案名稱,城市維度,加盟型別,客戶角色,倉庫名,實施進度,專案階段,階段狀態,實施型別,輔助列,月份維度," & _
" datediff('d',首次入倉日期,首次發貨日期) as 天數,庫房人數 as 人數,null as 成本,'庫房' as 崗位,'倉庫運營組' as 組名 from [實施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"


str8 = "select 銷售轉運營日期,週期維度,專案名稱,城市維度,加盟型別,客戶角色,倉庫名,實施進度,專案階段,階段狀態,實施型別,輔助列,月份維度," & _
" datediff('d',駐場日期,離場日期) as 天數,駐場人數 as 人數,null as 成本,'駐場' as 崗位,'城市運營組' as 組名 from [實施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"


strtotal = str1 & " union all " & str2 & " union all " & str3 & " union all " & str4 & " union all " & str5 & " union all " & str6 & " union all " & str7 & " union all " & str8

If Range("h1") = "實施人天(avg)" Then

strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , avg(人數*天數) as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & ""

strsql = "transform avg(人數*天數) select " & Range("b2").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""

strsql = "transform avg(人數*天數) select " & Range("b3").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""

strsql = "transform avg(人數*天數) select " & Range("b3").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""

strsql = "transform avg(人數*天數) select " & Range("b2").Value & " as 統計 from (" & strtotal & ") group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""


ElseIf Range("h1") = "實施人天(sum)" Then

strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , sum(人數*天數) as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & ""

strsql = "transform sum(人數*天數) select " & Range("b2").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""

strsql = "transform sum(人數*天數) select " & Range("b3").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""

strsql = "transform sum(人數*天數) select " & Range("b3").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""

strsql = "transform sum(人數*天數) select " & Range("b2").Value & " as 統計 from (" & strtotal & ") group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""


ElseIf Range("h1") = "成本(avg)" Then

strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , avg(成本) as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & ""

strsql = "transform avg(成本) select " & Range("b2").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""

strsql = "transform avg(成本) select " & Range("b3").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""

strsql = "transform avg(成本) select " & Range("b3").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""

strsql = "transform avg(成本) select " & Range("b2").Value & " as 統計 from (" & strtotal & ") group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""

ElseIf Range("h1") = "成本(sum)" Then

strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , sum(成本) as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & ""

strsql = "transform sum(成本) select " & Range("b2").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""

strsql = "transform sum(成本) select " & Range("b3").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""

strsql = "transform sum(成本) select " & Range("b3").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""

strsql = "transform sum(成本) select " & Range("b2").Value & " as 統計 from (" & strtotal & ") group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""

'ElseIf Range("h1") = "專案數" Then
'
'strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , count(*) as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & ""
'
'strsql = "transform count(*) select " & Range("b2").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""
'
'strsql = "transform count(*) select " & Range("b3").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""
'
'strsql = "transform count(*) select " & Range("b3").Value & " as 統計 from (" & strtotal & ") group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""
'
'strsql = "transform count(*) select " & Range("b2").Value & " as 統計 from (" & strtotal & ") group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""


End If

 


rs.Open (strsql), cnn
Cells(5, 1).CopyFromRecordset cnn.Execute(strsql)


For Each Field In rs.Fields
aa = Field.Name
[a4].Offset(0, i) = Field.Name
i = i + 1
Next


Set rs = Nothing
Set cnn = Nothing

 


Else

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

 

End If

lst = Range("a1048576").End(xlUp).Row
If Range("b2") = "月份維度" Then

Range("A5:A" & lst).Select
Selection.NumberFormatLocal = "yyyy""-""m"
Range("A4:afd4").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("專案狀態查詢").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("專案狀態查詢").AutoFilter.Sort.SortFields.Add Key:=Range _
("A4"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("專案狀態查詢").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

ElseIf Range("b2") = "週期維度" Then
'Selection.NumberFormatLocal = "0""周"""
Range("A5:A" & lst).Select
Selection.NumberFormatLocal = "0""周"""
Range("A4:afd4").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("專案狀態查詢").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("專案狀態查詢").AutoFilter.Sort.SortFields.Add Key:=Range _
("A4"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("專案狀態查詢").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End If

 


End Sub