解决方案 »

  1.   

    use master
    go
    create table #t
    (
    lrry varchar(2),
    jyry varchar(2)
    )
    go
    insert #t
    select 'a','b' union all
    select 'a','b' union all
    select 'a','b'
    go;with t
    as(
    select lrry as ry,* from #t
    union all
    select jyry,* from #t
    )
    select ry,
    SUM(case when ry=lrry then 1 else 0 end) as lrry,
    SUM(case when ry=jyry then 1 else 0 end) as jyry
    from t group by ry
    /*
    ry lrry jyry
    --------------------------
    a 3 0
    b 0 3
    */
    drop table #t
      

  2.   

    ;with cte(lrry,jyry) as
    (select 'a','b'
    union all select 'a','b'
    union all select 'a','b'
    )
    select lrry=(select count(*) from cte t where t.lrry=a.name),jyry=(select count(*) from cte t where t.jyry=a.name),name
    from (select distinct lrry as name from cte
    union all select distinct jyry from cte
    )a/*
    lrry jyry name
    3 0 a
    0 3 b
    */
      

  3.   

     版主大人,我是新手,下面那个表集合能不能和SQL语句写成一条?
      

  4.   

    谢谢大大,我是新人,我想问下下面那个表集合能不能和SQL语句写成一条?
      

  5.   

    谢谢大大,我是新人,我想问下下面那个表集合能不能和SQL语句写成一条?他的with是造测试表,你只需要关注selectxxx 那部分
      

  6.   

    谢谢大大,我是新人,我想问下下面那个表集合能不能和SQL语句写成一条?他的with是造测试表,你只需要关注selectxxx 那部分  抱歉,看错了,还以为和一个版主写的一样