解决方案 »

  1.   

        SELECT a.商户号,
               a.全部,
               ISNULL(b.夜间消费,0) 夜间消费,
               Convert(varchar(6),
                       Convert(decimal(6,2),
                               100.0*ISNULL(b.夜间消费,0)/a.全部
                               )
                      )+'%' 比例      FROM (
                    select 商户号,count(*) as 全部
                      from aa
                  group by 商户号
               ) a
     LEFT JOIN (
                    select 商户号,count(*) as 夜间消费
                      from aa
                     where aa.交易金额>=10000
                       and datepart(hh,交易时间)>=20
                  group by 商户号
               ) b
           ON a.商户号 = b.商户号
      

  2.   


         --错误很明显,先看看下面的查询结果就知道了,结果集中有没有列名为‘全部’的? 两个不同字段union all 应该是取第一个‘夜间消费’吧
                select 商户号,count(*) as 夜间消费  from aa   where aa.交易金额>=10000
                                            and  datepart(hh,交易时间)>=20 or datepart(hh,交易时间)<=24  --or 应该改为and吧
               group by 商户号
              union all
                 select 商户号,count(*) as 全部  from aa
                  group by 商户号
      

  3.   

    Tiger_Zhao如果我想把交易金额 
    ≥10000,20000,30000,50000的一起弄出来能弄么
      

  4.   

    按照子查询b的格式,写出c、d、e来,一样 LEFT JOIN 上去不就可以了。
      

  5.   

    SELECT a.商户号,
               a.全部,
               ISNULL(b.夜间消费1W,0) 夜间消费1W,
               Convert(varchar(6),
                       Convert(decimal(6,2),
                               100.0*ISNULL(b.夜间消费1W,0)/a.全部
                               )
                      )+'%' 比例,
               ISNULL(c.夜间消费2W,0) 夜间消费2W,
               Convert(varchar(6),
                       Convert(decimal(6,2),
                               100.0*ISNULL(c.夜间消费2W,0)/a.全部
                               )
                      )+'%' 比例,  
               ISNULL(d.夜间消费3W,0) 夜间消费3W,
               Convert(varchar(6),
                       Convert(decimal(6,2),
                               100.0*ISNULL(d.夜间消费3W,0)/a.全部
                               )
                      )+'%' 比例,  
               ISNULL(e.夜间消费5W,0) 夜间消费5W,
               Convert(varchar(6),
                       Convert(decimal(6,2),
                               100.0*ISNULL(e.夜间消费5W,0)/a.全部
                               )
                      )+'%' 比例               
          FROM (
                    select 商户号,count(*) as 全部
                      from aa
                  group by 商户号
               ) a
     LEFT JOIN (
                    select 商户号,count(*) as 夜间消费1W
                      from aa
                     where aa.交易金额>=10000
                       and datepart(hh,交易时间)>=20
                  group by 商户号
               ) b
     LEFT JOIN (
                    select 商户号,count(*) as 夜间消费2W
                      from aa
                     where aa.交易金额>=20000
                       and datepart(hh,交易时间)>=20
                  group by 商户号
               ) c
     LEFT JOIN (
                    select 商户号,count(*) as 夜间消费3W
                      from aa
                     where aa.交易金额>=30000
                       and datepart(hh,交易时间)>=20
                  group by 商户号
               ) d
      LEFT JOIN (
                    select 商户号,count(*) as 夜间消费5W
                      from aa
                     where aa.交易金额>=50000
                       and datepart(hh,交易时间)>=20
                  group by 商户号
               ) e 
                                   
             ON a.商户号 = b.商户号
    a.商户号 =c.商户号
    a.商户号 = d.商户号
    a.商户号 = e.商户号
    这样吗
      

  6.   

    语法基础啊,应该是
          FROM (...) a
     LEFT JOIN (...) b
            ON a.商户号 = b.商户号
     LEFT JOIN (...) c
            ON a.商户号 = c.商户号
     LEFT JOIN (...) d
            ON a.商户号 = d.商户号
     LEFT JOIN (...) e 
            ON a.商户号 = e.商户号
      

  7.   

    这样写能运行 你看看对么SELECT a.商户号,
               a.全部,
               ISNULL(b.夜间消费1W,0) 夜间消费1W,
               Convert(varchar(6),
                       Convert(decimal(6,2),
                               100.0*ISNULL(b.夜间消费1W,0)/a.全部
                               )
                      )+'%' 比例,
               ISNULL(c.夜间消费2W,0) 夜间消费2W,
               Convert(varchar(6),
                       Convert(decimal(6,2),
                               100.0*ISNULL(c.夜间消费2W,0)/a.全部
                               )
                      )+'%' 比例,  
               ISNULL(d.夜间消费3W,0) 夜间消费3W,
               Convert(varchar(6),
                       Convert(decimal(6,2),
                               100.0*ISNULL(d.夜间消费3W,0)/a.全部
                               )
                      )+'%' 比例,  
               ISNULL(e.夜间消费5W,0) 夜间消费5W,
               Convert(varchar(6),
                       Convert(decimal(6,2),
                               100.0*ISNULL(e.夜间消费5W,0)/a.全部
                               )
                      )+'%' 比例               
          FROM (
                    select 商户号,count(*) as 全部
                      from aa
                  group by 商户号
               ) a
     LEFT JOIN (
                    select 商户号,count(*) as 夜间消费1W
                      from aa
                     where aa.交易金额>=10000
                       and datepart(hh,交易时间)>=20
                  group by 商户号
               ) b
                    ON a.商户号 = b.商户号
     LEFT JOIN (
                    select 商户号,count(*) as 夜间消费2W
                      from aa
                     where aa.交易金额>=20000
                       and datepart(hh,交易时间)>=20
                  group by 商户号
               ) c
                     ON a.商户号 = c.商户号
     LEFT JOIN (
                    select 商户号,count(*) as 夜间消费3W
                      from aa
                     where aa.交易金额>=30000
                       and datepart(hh,交易时间)>=20
                  group by 商户号
               ) d
                     ON a.商户号 = d.商户号
      LEFT JOIN (
                    select 商户号,count(*) as 夜间消费5W
                      from aa
                     where aa.交易金额>=50000
                       and datepart(hh,交易时间)>=20
                  group by 商户号
               ) e 
                      ON a.商户号 = e.商户号