【问题标题】:Sort as a result of another Grouping作为另一个分组的结果排序
【发布时间】:2016-11-20 14:37:24
【问题描述】:

我需要根据另一个查询的分组对一些结果进行排序。

我有一张交易表,其中一个字段是商店。我想退回按交易日期排序的所有交易,但在此期间,我需要先列出交易最多的商店的交易。

例如:

日期1,店铺A,金额 日期1,商店B,金额 日期2,商店A,金额 日期3,店铺A,金额 日期3,商店B,金额 日期3,商店B,金额 日期4,商店B,金额 日期5,商店B,金额 必须返回为: 日期1,商店B,金额 日期3,商店B,金额 日期3,商店B,金额 日期4,商店B,金额 日期5,商店B,金额 日期1,店铺A,金额 日期2,商店A,金额 日期3,店铺A,金额

因为商店 B 有更多的交易

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    我在子查询中使用了一个窗口函数来获得您想要的输出。输出应按商店名称记录计数交易日期排序。

    SELECT t.date, t.storeName, t.amount
    FROM
    (
        SELECT date, storeName, amount,
            COUNT(*) OVER(PARTITION BY storeName ORDER BY date) AS storeCount
        FROM yourTable
    ) t
    ORDER BY t.storeCount DESC, t.date
    

    【讨论】:

      【解决方案2】:

      根据您提供的详细信息,请检查输出:

      --table scripts
          CREATE TABLE Store_Data 
         (datevalue datetime, storeinfo nvarchar(40), Amount numeric(18,2))
      
      
          INSERT INTO Store_Data
          SELECT '2016-07-16 10:54:33.020','Store B' , 16000
          UNION ALL
          SELECT '2016-07-18 10:54:33.020','Store A' , 15000
          UNION ALL
          SELECT '2016-07-28 10:54:33.020','Store B' , 10800
          UNION ALL
          SELECT '2016-07-20 10:54:33.020','Store A' , 9000
          UNION ALL
          SELECT '2016-07-23 10:54:33.020','Store B' , 1000
          UNION ALL
          SELECT '2016-07-22 10:54:33.020','Store B' , 7000
          UNION ALL
          SELECT '2016-07-08 10:54:33.020','Store B' , 1000
          UNION ALL
          SELECT '2016-07-12 10:54:33.020','Store A' , 1000
          UNION ALL        
          SELECT '2016-07-15 10:54:33.020','Store A' , 11000
          UNION ALL        
          SELECT '2016-07-18 10:54:33.020','Store B' , 1000
          UNION ALL        
          SELECT '2016-07-02 10:54:33.020','Store A' , 5000
          UNION ALL        
          SELECT '2016-07-24 10:54:33.020','Store B' , 1000
          UNION ALL        
          SELECT '2016-07-08 10:54:33.020','Store A' , 100000
          UNION ALL        
          SELECT '2016-07-23 10:54:33.020','Store B' , 5000
          UNION ALL        
          SELECT '2016-07-18 10:54:33.020','Store B' , 10000
      
          -
      

      --最终查询

      SELECT a.datevalue,
             a.storeinfo,
             a.Amount 
      FROM Store_Data AS a 
      INNER JOIN
      (
          SELECT storeinfo,
                 COUNT(1) AS TotalTrans 
          FROM Store_Data
          GROUP BY storeinfo
      ) AS b
          ON b.storeinfo = a.storeinfo
      ORDER BY b.TotalTrans DESC,
               a.datevalue
      

      【讨论】:

        【解决方案3】:

        结合window函数使用count按store统计行数

        SELECT *, store_count = count(*) over (partition by Store)
        FROM   yourtalbe
        ORDER BY store_count desc
        

        【讨论】:

          猜你喜欢
          • 2011-11-30
          • 2018-03-29
          • 1970-01-01
          • 2019-03-31
          • 2022-01-11
          • 1970-01-01
          • 2017-04-28
          • 1970-01-01
          • 2021-11-20
          相关资源
          最近更新 更多