Vba Excel分组(或使用SQL)

Vba Excel分组(或使用SQL),vba,excel,Vba,Excel,我相信我想得太多了。因此,创建了一个SQL报告来生成如下所示的数据: Client# Parent# Industry Client Name A B C D E F G 1 0 Agriculture ABC Co. 0 0 0 50 0 0 0 2 1 Agriculture DEF Co. 25 0 0 0 0 0 0 3

我相信我想得太多了。因此,创建了一个SQL报告来生成如下所示的数据:

Client# Parent# Industry    Client Name A   B   C   D   E   F   G   
1       0       Agriculture ABC Co.     0   0   0   50  0   0   0   
2       1       Agriculture DEF Co.     25  0   0   0   0   0   0   
3       2       Agriculture GHI Co.     0   0   0   0   0   0   75  
Client# Parent# Industry    Client Name A   B   C   D   E   F   G   Total
1       0       Agriculture ABC Co.     25  0   0   50  0   0   75  150 
大约有10000行。如果您注意到,有一个客户号码和一个家长号码。上面显示的3个结果都属于“ABC公司”业务的子公司。在Excel报表中,将它们全部组合成一行的最佳方式是什么?结果如下所示:

Client# Parent# Industry    Client Name A   B   C   D   E   F   G   
1       0       Agriculture ABC Co.     0   0   0   50  0   0   0   
2       1       Agriculture DEF Co.     25  0   0   0   0   0   0   
3       2       Agriculture GHI Co.     0   0   0   0   0   0   75  
Client# Parent# Industry    Client Name A   B   C   D   E   F   G   Total
1       0       Agriculture ABC Co.     25  0   0   50  0   0   75  150 
当时的想法是:

1) 自定义SQL代码以格式化数据,使其看起来符合我的要求(最好是每次运行报表时,在报表收集数据后,我只需做最少的工作)。我理解,不过使用SQL检索数据更合适

2) Excel中有一个选项可以实现这一点

3) 创建一个宏来对数据进行排序,并按照我想要的方式对其进行格式化

谢谢你的阅读

“因此创建了一个SQL报告来生成如下所示的数据:”

否决票的原因可能是您没有在此处发布SQL代码。让我猜不透你的表格格式,所以为了我的方便。。。从mytable中选择*是我猜您使用的。我也在猜测列名……在以后的问题中,更多的信息是有帮助的,所以我假设更少

select t1.client, t2.client, t3.client, t4.client, t1.parent, t2.parent, t3.parent, t1.*
from maytable t1
left join mytable t2 on t1.parent = t2.client
left join mytable t3 on t2.parent = t3.client
left join mytable t4 on t3.parent = t4.client
etc pending how deep this relation goes.
这会给你一张显示父母的名单。t1.*只是为了获取相关的数据列,您应该完整地写出您的列,我很懒,因为没有提供列列表。客户端ID在您的示例中应该是这样的(我在下面的列表中忽略了parentID)

现在我们需要一个case语句…当父项=0时,则显示该客户机id

select client_id, case when t1.parentid = 0 then t1.client_id
                      when t2.parentid = 0 then t2.client_id
                      when t3.parentid = 0 then t3.client_id
                      when t4.parentid = 0 then t4.clientID
                  end as parent_ID
      , t1.*
from (same as above query)
您可以将上面写的内容称为子查询并从中进行选择,这一次获取所需列的总和

select parent_id, sum(a), sum(b), etc...
from   
  (select client_id, case when t1.parentid = 0 then t1.client_id
                      when t2.parentid = 0 then t2.client_id
                      when t3.parentid = 0 then t3.client_id
                      when t4.parentid = 0 then t4.clientID
                  end as parent_ID
      , t1.*
       from maytable t1
left join mytable t2 on t1.parent = t2.client
left join mytable t3 on t2.parent = t3.client
left join mytable t4 on t3.parent = t4.client
etc pending how deep this relation goes. ) a
group by parent_id

不幸的是,这是我在您提供的有限信息的情况下所能提供的最好的代码。

子客户端的嵌套距离是否有限制(您的示例显示了3个,可以是5个还是50个?“我理解,但仅使用SQL检索数据更合适。”这是正确的,SQL不应该用来“显示”数据…我会考虑把一部分的检索这里。技术上没有限制,但母公司可以像3-4深。如果可能的话,在Excel中分组也是理想的选择。父id=0意味着它是最上面的记录(我猜是父记录)?是的,如果父id为0,那就是最上面的/父记录。有点可笑的是,一组VBA/Excel海报因为太宽而搁置了这个问题,但从SQL的角度来看,这一点也不太宽,太完美了。我一直在寻找一般性的建议,而使用case语句正是我应该想到的SQL方法!再次感谢你。