表1id  username
1    admin
2    lcs
3    lcs9702表2username   jfstyle   jf
admin        使用分  20
admin        使用分  30
admin        订单分  50
admin        订单分  10
lcs          使用分  -10
lcs          订单分  40
lcs          使用分  30
lcs          订单分  24
lcs9702      使用分  100
lcs9702      订单分  50显示的结果:
username    使用分  订单分  总分
admin        50       60   110
lcs          20       64    84
lcs9702      100      50    150这个SQL语句要怎么写

解决方案 »

  1.   

    显示的结果: 
    username    使用分  订单分  总分    排名
    lcs9702      100      50    150   1
    admin        50      60  110      2
    lcs          20      64    84     3
     
      

  2.   

    select username,sum(case jfstyle when '使用分' then jf else 0 end) as '使用分',sum(case jfstyle when '订单分' then jf else 0 end) as '订单分'
    from 表2
    group by username
      

  3.   

    select username,sum(case jfstyle when '使用分' then jf else 0 end) as '使用分',sum(case jfstyle when '订单分' then jf else 0 end) as '订单分',sum(jfstyle) as '总分'
    from 表2
    group by username
      

  4.   

    SELECT A.username,
    SUM(CASE WHEN jfstyle='使用分' THEN JF ELSE 0 END )AS '使用分',
    SUM(CASE WHEN jfstyle='订单分' THEN JF ELSE 0 END )AS '订单分',
    SUM(JF)AS '总分'
    FROM TB1 A LEFT JOIN TB2 B ON A.username=B.username
    GROUP BY A.username
      

  5.   

    select 
    a.username,
    使用分=SUM(case when jfstyle='使用分' then jf else 0 end),
    订单分=SUM(case when jfstyle='订单分' then jf else 0 end),
    总分=SUM(isnull(jf,0))
    into #
    from 表1 a join 表2 b on a.username=b.username
    group by a.usernameselect * ,
    排名=(select COUNT(*) from # where 总分>=t.总分) 
    from # t 
      

  6.   


    select 
    a.username,
    使用分=SUM(case when jfstyle='使用分' then jf else 0 end),
    订单分=SUM(case when jfstyle='订单分' then jf else 0 end),
    总分=SUM(isnull(jf,0))
    into #
    from 表1 a left join 表2 b on a.username=b.username
    group by a.usernameselect * ,
    排名=(select COUNT(*) from # where 总分>=t.总分) 
    from # t 
      

  7.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-25 09:56:30
    ----------------------------------------------------------------
    --> 测试数据:[ta]
    if object_id('[ta]') is not null drop table [ta]
    go
    create table [ta]([id] int,[username] varchar(7))
    insert [ta]
    select 1,'admin' union all
    select 2,'lcs' union all
    select 3,'lcs9702'
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([username] varchar(7),[jfstyle] varchar(6),[jf] int)
    insert [tb]
    select 'admin','使用分',20 union all
    select 'admin','使用分',30 union all
    select 'admin','订单分',50 union all
    select 'admin','订单分',10 union all
    select 'lcs','使用分',-10 union all
    select 'lcs','订单分',40 union all
    select 'lcs','使用分',30 union all
    select 'lcs','订单分',24 union all
    select 'lcs9702','使用分',100 union all
    select 'lcs9702','订单分',50
    --------------开始查询--------------------------
    select 
     a.username,
     sum(case jfstyle when '使用分' then jf else 0 end) as '使用分',
     sum(case jfstyle when '订单分' then jf else 0 end) as '订单分',
     sum(jf) as '总分'
    from 
      ta a
    left join
      tb b
    on
      a.username=b.username
    group by 
      a.username
    ----------------结果----------------------------
    /*username 使用分         订单分         总分
    -------- ----------- ----------- -----------
    admin    50          60          110
    lcs      20          64          84
    lcs9702  100         50          150(3 行受影响)
    */
      

  8.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-25 09:56:30
    ----------------------------------------------------------------
    --> 测试数据:[ta]
    if object_id('[ta]') is not null drop table [ta]
    go
    create table [ta]([id] int,[username] varchar(7))
    insert [ta]
    select 1,'admin' union all
    select 2,'lcs' union all
    select 3,'lcs9702'
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([username] varchar(7),[jfstyle] varchar(6),[jf] int)
    insert [tb]
    select 'admin','使用分',20 union all
    select 'admin','使用分',30 union all
    select 'admin','订单分',50 union all
    select 'admin','订单分',10 union all
    select 'lcs','使用分',-10 union all
    select 'lcs','订单分',40 union all
    select 'lcs','使用分',30 union all
    select 'lcs','订单分',24 union all
    select 'lcs9702','使用分',100 union all
    select 'lcs9702','订单分',50
    --------------开始查询--------------------------
    select 
     a.username,
     sum(case jfstyle when '使用分' then jf else 0 end) as '使用分',
     sum(case jfstyle when '订单分' then jf else 0 end) as '订单分',
     sum(jf) as '总分'
    from 
      ta a
    left join
      tb b
    on
      a.username=b.username
    group by 
      a.username
    order by
     sum(jf) desc
    ----------------结果----------------------------
    /*username 使用分         订单分         总分
    -------- ----------- ----------- -----------
    lcs9702  100         50          150
    admin    50          60          110
    lcs      20          64          84(3 行受影响)
    */