Foxtable(狐表)用户栏目专家坐堂 → 获取不重复的对象属性后,怎么根据对象属性值去对应的遍历表中是否有符合条件的列,如果有就对应的计算值


  共有6384人关注过本帖树形打印复制链接

主题:获取不重复的对象属性后,怎么根据对象属性值去对应的遍历表中是否有符合条件的列,如果有就对应的计算值

帅哥哟,离线,有人找我吗?
cnsjroom
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1571 积分:11238 威望:0 精华:0 注册:2021/1/17 17:06:00
获取不重复的对象属性后,怎么根据对象属性值去对应的遍历表中是否有符合条件的列,如果有就对应的计算值  发帖心情 Post By:2022/8/6 11:01:00 [只看该作者]

 获取不重复的对象属性后,怎么根据对象属性值去对应的遍历表中是否有符合条件的列,如果有就对应的计算值

 
图片点击可在新窗口打开查看此主题相关图片如下:11.png
图片点击可在新窗口打开查看

当前代码如下:

Dim cmd As New SQL Command
cmd.Con necti
cmd.Comma ndText ="Sele ct Count(*) From {巡察工作安排}"
Dim dt As DataTable
dt = cmd.ExecuteReader()
Dim Products As List(Of String)
Products = dt.sqlGetValues("巡察对象属性")
Dim time As String =Date.today.Year
output.show(time)
For Each Product As String In Products
    For Each c As DataCol In dt.dataCols
        output.show(c.name)
        Output.Show(Product)
        cmd.CommandText ="Sel ect Count(*) From {巡察对象信息} Where 巡察对象属性 = '" & Product & "'"
        If cmd.ExecuteScalar > 0 Then
            output.show(cmd.ExecuteScalar)
        End If
        cmd.CommandText ="Sel ect Count(*) From {巡察工作安排} Where 巡察对象属性 = '" & Product & "'"
        If cmd.ExecuteScalar > 0 Then
            output.show(cmd.ExecuteScalar)
        End If
    Next
Next

 

输出值:

2022
Column1
党政机关
2
4



 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110574 积分:562760 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2022/8/6 11:05:00 [只看该作者]

表数据是怎么样的?希望得到什么结果?

 回到顶部
帅哥哟,离线,有人找我吗?
cnsjroom
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1571 积分:11238 威望:0 精华:0 注册:2021/1/17 17:06:00
回复:(有点蓝)表数据是怎么样的?希望得到什么结果...  发帖心情 Post By:2022/8/6 11:27:00 [只看该作者]

 

 
图片点击可在新窗口打开查看此主题相关图片如下:11.png
图片点击可在新窗口打开查看

 

想实现红色部分的值通过Product 来对应到列名  并赋值给对应的列下   然后对应汇总  并根据年度值 按年度生成对应的数据行

 

 

Dim cmd As New SQLCommand
cmd.C
cmd.CommandText ="Sel ect Count(*) From {巡察工作安排}"
Dim dt As DataTable
dt = cmd.ExecuteReader()
Dim Products As List(Of String)
Products = dt.sqlGetValues("巡察对象属性")
Dim time As String =Date.today.Year
Dim y As Integer = time '指定年份
Dim time1 As New Date(y, 1, 1)
Dim time2 As New Date(y, 12, 31)
For Each Product As String In Products
    cmd.CommandText ="Sele ct * From {巡察工作覆盖情况}"
    Dim dt1 As DataTable
    dt1 = cmd.ExecuteReader()
    'Output.Show(Product)
    For Each dc As DataCol In dt1.DataCols
        output.show(dc.name)
        If dc.name.Contains(Product) Then
            If dc.name.Contains(Product & "_应巡数") Then
                cmd.CommandText ="Se lect Count(*) From {巡察对象信息} Where 巡察对象属性 = '" & Product & "'"
                If cmd.ExecuteScalar > 0 Then
                    output.show(cmd.ExecuteScalar)
                End If
            End If
            If dc.name.Contains(Product & "_已巡数") Then
                cmd.CommandText ="Sel ect Count(*) From {巡察工作安排} Where 巡察对象属性 = '" & Product & "'"
                If cmd.ExecuteScalar > 0 Then
                    output.show(cmd.ExecuteScalar)
                End If
            End If
        If dc.name.Contains(Product & "_本年度") Then
                cmd.CommandText ="Sel ect Count(*) From {巡察工作安排} Where 巡察对象属性 = '" & Product & "' and 巡察开始时间 >= '" & time1 & "' And 巡察开始时间 <= '" & time2 & "'"
                If cmd.ExecuteScalar > 0 Then
                    output.show(cmd.ExecuteScalar)
                End If
            End If
        End If
    Next
Next

 

输出得到:

党政机关
guid
创建人guid
创建日期
编辑人guid
编辑日期
巡察届别
巡察开始时间
巡察结束时间
巡察轮次
党政机关_应巡数
2
党政机关_已巡数
4

党政机关_本年度
4

党政机关_本年度
事业单位_应巡数
事业单位_已巡数
事业单位_本年度
国有企业_应巡数
国有企业_已巡数
国有企业_本年度
乡镇街道_应巡数
乡镇街道_已巡数
乡镇街道_本年度
其他对象_应巡数
其他对象_已巡数
其他对象_本年度
总计_应巡数
总计_已巡数
总计_覆盖率
村级党组织_应巡数
村级党组织_已巡数
村级党组织_本年度

村级党组织_覆盖率
提级巡察
党委常委研究巡察工作次数



 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110574 积分:562760 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2022/8/6 11:51:00 [只看该作者]

请上传实例测试

 回到顶部
帅哥哟,离线,有人找我吗?
cnsjroom
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1571 积分:11238 威望:0 精华:0 注册:2021/1/17 17:06:00
回复:(有点蓝)请上传实例测试  发帖心情 Post By:2022/8/6 11:58:00 [只看该作者]

已初步实现,老师有没有办法简化代码提高运行效率呢?

 

 

当前代码如下:

 

 

Dim cmd As New SQLCommand
cmd.Connecti
cmd.CommandText ="Sel ect * From {巡察工作安排}"
Dim dt As DataTable
dt = cmd.ExecuteReader()
Dim Products As List(Of String)
Products = dt.sqlGetValues("巡察对象属性")
Dim time As String =Date.today.Year
Dim y As Integer = time '指定年份
Dim time1 As New Date(y, 1, 1)
Dim time2 As New Date(y, 12, 31)

cmd.CommandText ="Sel ect * From {巡察工作覆盖情况}"
Dim dt1 As DataTable
dt1 = cmd.ExecuteReader(True)
Dim bb As DataRow = dt1.AddNew


Dim yxs As Integer
Dim yxs1 As Integer
Dim bnd As Integer
For Each Product As String In Products
   
    Output.Show(Product)
    For Each dc As DataCol In dt1.DataCols
        output.show(dc.name)
        If dc.name.Contains(Product) Then
            If dc.name.Contains(Product & "_应巡数") Then
                cmd.CommandText ="Sel ect Count(*) From {巡察对象信息} Where 巡察对象属性 = '" & Product & "'"
                If cmd.ExecuteScalar > 0 Then
                    bb(Product & "_应巡数")=cmd.ExecuteScalar
                    'cmd.CommandText = "UPD ATE {巡察工作覆盖情况} SET " & Product & "_应巡数" & "= '"& cmd.ExecuteScalar & "'"
                    'cmd.ExecuteNonQuery()
                    yxs= yxs +  cmd.ExecuteScalar
                End If
            End If
            If dc.name.Contains(Product & "_已巡数") Then
                cmd.CommandText ="Sel ect Count(*) From {巡察工作安排} Where 巡察对象属性 = '" & Product & "'"
                If cmd.ExecuteScalar > 0 Then
                    bb(Product & "_已巡数")=cmd.ExecuteScalar
                    'cmd.CommandText = "UPD ATE {巡察工作覆盖情况} SET " & Product & "_已巡数" & "= '"& cmd.ExecuteScalar & "'"
                    'cmd.ExecuteNonQuery()
                    yxs1= yxs1 +  cmd.ExecuteScalar
                End If
            End If
            If dc.name.Contains(Product & "_本年度") Then
                cmd.CommandText ="Se lect Count(*) From {巡察工作安排} Where 巡察对象属性 = '" & Product & "' and 巡察开始时间 >= '" & time1 & "' And 巡察开始时间 <= '" & time2 & "'"
                If cmd.ExecuteScalar > 0 Then
                    bb(Product & "_本年度")=cmd.ExecuteScalar
                    'cmd.CommandText = "UP DATE {巡察工作覆盖情况} SET " & Product & "_本年度" & "= '"& cmd.ExecuteScalar & "'"
                    'cmd.ExecuteNonQuery()
                    bnd= bnd+  cmd.ExecuteScalar
                End If
            End If
        End If
        output.show(yxs)
        bb("总计_应巡数")=yxs
        bb("总计_已巡数")=yxs1
        bb("总计_覆盖率")=bnd
    Next
    bb.save
Next


 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110574 积分:562760 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2022/8/6 13:47:00 [只看该作者]

试试

Dim cmd As New SQLCommand
cmd.Connecti
cmd.CommandText = "Select distinct 巡察对象属性 From {巡察工作安排}"
Dim dt As DataTable
dt = cmd.ExecuteReader()
Dim Products As List(Of String)
Products = dt.GetValues("巡察对象属性")
Dim time As String = Date.today.Year
Dim y As Integer = time '指定年份
Dim time1 As New Date(y, 1, 1)
Dim time2 As New Date(y, 12, 31)

cmd.CommandText = "Select * From {巡察工作覆盖情况} where [_identify] = -1"
Dim dt1 As DataTable
dt1 = cmd.ExecuteReader(True)
Dim bb As DataRow = dt1.AddNew


Dim yxs As Integer
Dim yxs1 As Integer
Dim bnd As Integer
For Each Product As String In Products
    Dim cl As String = Product & "_应巡数"
    Output.Show(cl)
    If dt1.DataCols.Contains(cl) Then
        cmd.CommandText = "Select Count(*) From {巡察对象信息} Where 巡察对象属性 = '" & Product & "'"
        Dim sl2 As Integer = cmd.ExecuteScalar
        If sl2 > 0 Then
            bb(cl) = sl2
            yxs = yxs + sl2
        End If
    End If
    cl = Product & "_已巡数"
    If dt1.DataCols.Contains(cl) Then
        cmd.CommandText = "Select Count(*) From {巡察工作安排} Where 巡察对象属性 = '" & Product & "'"
        Dim sl2 As Integer = cmd.ExecuteScalar
        If sl2 > 0 Then
            bb(cl) = sl2
            yxs1 = yxs1 + sl2
        End If
    End If
    cl = Product & "_本年度"
    If dt1.DataCols.Contains(cl) Then
        cmd.CommandText = "Select Count(*) From {巡察工作安排} Where 巡察对象属性 = '" & Product & "' and 巡察开始时间 >= '" & time1 & "' And 巡察开始时间 <= '" & time2 & "'"
        Dim sl2 As Integer = cmd.ExecuteScalar
        If sl2 > 0 Then
            bb(cl) = sl2
            bnd = bnd + sl2
        End If
    End If
Next
output.show(yxs)
bb("总计_应巡数") = yxs
bb("总计_已巡数") = yxs1
bb("总计_覆盖率") = bnd
bb.save


 回到顶部
帅哥哟,离线,有人找我吗?
cnsjroom
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:七尾狐 帖子:1571 积分:11238 威望:0 精华:0 注册:2021/1/17 17:06:00
回复:(有点蓝)试试Dim cmd As New SQLCommandcmd.C...  发帖心情 Post By:2022/8/12 18:20:00 [只看该作者]

For Each Product As String In Products
    Output.Show(Product)
    For Each dc As DataCol In dt1.DataCols
        output.show(dc.name)
        If dc.name.Contains(Product) Then
            If dc.name.Contains(Product & "_应巡数") Then
                cmd.CommandText ="Sel ect Count(*) From {巡察对象信息} Where 巡察对象属性 = '" & Product & "' "
                If cmd.ExecuteScalar > 0 Then
                    bb(Product & "_应巡数")=cmd.ExecuteScalar
                    yxs= yxs +  cmd.ExecuteScalar
                    bb("总计_应巡数")=yxs
                End If
            End If
            If dc.name.Contains(Product & "_已巡数") Then
                cmd.CommandText ="Se lect Count(*) From {巡察工作安排} "
                Dim dt11 As DataTable
                dt11 = cmd.ExecuteReader()
                Dim Ps As List(Of String)
                Ps = dt11.sqlGetValues("巡察对象")
                For Each P As String In Ps
                    cmd.CommandText ="Sel ect Count(*) From {巡察工作安排} Where 巡察对象属性 = '" & Product & "' and 巡察对象 = '" & P & "'"
                    If cmd.ExecuteScalar > 0 Then
                        output.show(cmd.ExecuteScalar)  得到的数据是3,想进行筛选,对于同一个对象属性和巡察对象值得数据   只统计一次  预期统计数据为2  怎么操作呢?
                        bb(Product & "_已巡数")=cmd.ExecuteScalar
                        yxs1= yxs1 +  cmd.ExecuteScalar
                        bb("总计_已巡数")=yxs1
                    End If
                Next
            End If
           
            If dc.name.Contains(Product & "_本年度") Then
                Dim cmd2 As SQLCommand = rq.cmd
                cmd2.CommandText ="Se lect Count(*) From {巡察工作安排} Where 巡察对象属性 = '" & Product & "' and 巡察开始时间 >= '" & time1 & "' And 巡察开始时间 <= '" & time2 & "'"
                If cmd2.ExecuteScalar > 0 Then
                    bb(Product & "_本年度")=cmd2.ExecuteScalar
                    bnd= bnd+  cmd.ExecuteScalar
                    bb("总计_本年度")=bnd
                End If
            End If
        End If
    Next
    bb("总计_覆盖率")= Format(bb("总计_应巡数")/bb("总计_已巡数"),"0.00%")
    bb("村级党组织_覆盖率")= Format(bb("村级党组织_应巡数")/bb("村级党组织_已巡数"),"0.00%")
    bb.save
Next

 

 

比如巡察工作安排表中有如下记录:

巡察对象属性     巡察对象

事业单位           某某医院

企业单位           某某投资工资

企业单位           某某投资工资

统计数据的话  就只能算两个数据   不能计算为三个数据

[此贴子已经被作者于2022/8/12 18:30:25编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110574 积分:562760 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2022/8/13 8:34:00 [只看该作者]

数据统计的结果肯定没有问题,如果有问题也是条件不对,或者数据有问题

 回到顶部