zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    整个过程
    iframe 重新加载闪过白块问题
    C# 获得两日期之间所有月份(包括跨年)
    新手是个框,啥都往里装!---谨以此文致歉博友和自己的无知
    C# 和Java的foreach的不同用法
    终于鼓起勇气,辞掉了第一份工作
    Java Junit4测试功能
    自学Android的第一个小程序(小布局、button点击事件、toast弹出)
    JS--Div中数据滚动到最后一条重新从头开始滚动
    RelativeLayout与LinearLayout的区别
  • 原文地址:https://www.cnblogs.com/shida-liu/p/9887354.html
Copyright © 2011-2022 走看看