把两个表都贴出来,
查询1:
SELECT LEFT(a.bh, 6) AS bh,b.shuliang, 
              c.danhao
        FROM Y_baseinfo a inner JOIN
              Y_liushui b ON a.idno = b.ypid inner JOIN
              Y_hetong_jiagongdan c ON b.hetongbianhao = c.danhao 
        WHERE (LEFT(a.bh, 4) = '0102') AND 
              (LEN(a.bh) >= 4) 
结果样式:
bh       shuliang    danhao
010201         10       A01
010202         20       A02
查询2:实际上类别表,查询1与查询2 bh相同
select rtrim(bh),rtrim(leibie) from y_leibie where left(bh,4)='0102' and len(bh)=6
类别表内容
bh       leibie
010201 铁板0.8
010202 铁板1.4
010203 大边框
010204 暗插销
010205 粉末
010206 合页
010207 背板
010208 防火锁
010209 闭门器
010210 管井锁
010211 防盗锁
010212 明插
010213 轴合页
010214 执手
010215 门插
010216 顺位器
010217 珍珠岩板
010218 延绵条
010219 防火胶
010220 防火胶条
010221 胶水查询1的结果集,有没有danhao都要把leibie表里的列别都列出来,顺序就是按bh由小到大,从左到右列出行。并能根据danhao进行类别聚合,满足danhao都为null 和有danhao leibie数量能聚合就行

解决方案 »

  1.   

    上面两查询聚合后的结果为:
    danhao  铁板0.8  铁板1.4  大边框 .. 胶水
    A01         10        0      0 ..   0
    A02          0       20      0 ..   0

      

  2.   

    如果查询1是这样
    bh shuliang danhao
    010201 10 A01
    010201 15 A01
    010202 20 A02
    结果相同类别数量相加就行了此结果为:danhao 铁板0.8 铁板1.4 大边框 .. 胶水
    A01 25 0 0 .. 0
    A02 0 20 0 .. 0


    如果所有danhao都为null
    结果为:(只列出表头,或者列出数量都为0也行)
    danhao 铁板0.8 铁板1.4 大边框 .. 胶水
      

  3.   

    试试以下语句
    if object_id('[ta]') is not null drop table [ta]
    go
    create table [ta]([bh] varchar(6),[shuliang] int,[danhao] varchar(3))
    insert [ta]
    select '010201',10,'A01' union all
    select '010202',20,'A02'
    go
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([bh] varchar(6),[leibie] varchar(8))
    insert [tb]
    select '010201','铁板0.8' union all
    select '010202','铁板1.4' union all
    select '010203','大边框' union all
    select '010204','暗插销' union all
    select '010205','粉末' union all
    select '010206','合页' union all
    select '010207','背板' union all
    select '010208','防火锁' union all
    select '010209','闭门器' union all
    select '010210','管井锁' union all
    select '010211','防盗锁' union all
    select '010212','明插' union all
    select '010213','轴合页' union all
    select '010214','执手' union all
    select '010215','门插' union all
    select '010216','顺位器' union all
    select '010217','珍珠岩板' union all
    select '010218','延绵条' union all
    select '010219','防火胶' union all
    select '010220','防火胶条' union all
    select '010221','胶水'
    godeclare @sql varchar(8000)
    select @sql=isnull(@sql+',','')+'['+leibie+']'+
    '=sum(case when b.leibie='+QUOTENAME(leibie,'''')+' then a.shuliang else 0 end)'
    from tb
    group by leibie order  by min(bh)
    --print @sql
    set 
      @sql='select a.danhao,'
      +@sql
      +' from tb b left join ta a on a.bh=b.bh group by a.danhao order by case when danhao is null then 2 else 1 end,danhao'
    --print @sql
    exec(@sql)/**
    danhao 铁板0.8       铁板1.4       大边框         暗插销         粉末          合页          背板          防火锁         闭门器         管井锁         防盗锁         明插          轴合页         执手          门插          顺位器         珍珠岩板        延绵条         防火胶         防火胶条        胶水
    ------ ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
    A01    10          0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
    A02    0           20          0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
    NULL   0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0(3 行受影响)
    **/