2

[コメント]を昇順または降順で並べ替えられるようにしたい。「orderby」句で集計を再実行しようとしましたが、機能しませんでした。また、エイリアス[comment]を使用できるサブクエリの外部で並べ替えを試しましたが、行0と行10の間でしか並べ替えられませんでした。

このクエリは、はるかに大きなクエリの小さなバージョンです。


SELECT *
FROM   (SELECT ISNULL((SELECT COUNT("order")
                       FROM   order_comment
                       WHERE  "order" = "order"."id"
                       GROUP  BY "order"), 0)                 AS [comment],
               Row_number() OVER (ORDER BY "order"."id" DESC) AS [rownum]
        FROM   [order]
        WHERE  ISNULL((SELECT COUNT("order")
                       FROM   order_comment
                       WHERE  "order" = "order"."id"
                       GROUP  BY "order"), 0) > 0) AS [filter]
WHERE  [rownum] BETWEEN 0 AND 10

MySqlでとても簡単です!


SELECT Ifnull((SELECT COUNT(`order`)
               FROM   order_comment
               WHERE  `order` = `order`.`id`
               GROUP  BY `order`), 0) AS `comment`
FROM   `order`
HAVING `comment` > 0
ORDER  BY `comment` DESC
LIMIT  0, 10  
4

3 に答える 3

5

最新の編集容疑者に続いて、このようなものが必要です

;WITH oc AS
(
SELECT [order], 
       COUNT([order]) AS order_count
FROM   order_comment 
GROUP  BY [order]
), occ AS
(
SELECT o.*,
       order_count AS comment,
       ROW_NUMBER() OVER (ORDER BY order_count DESC) AS  [rownum]
FROM [order] o
INNER JOIN oc ON oc.[order] = o.id
)
SELECT *
FROM occ 
WHERE  [rownum] BETWEEN 0 AND 10
ORDER BY [rownum]

INNER JOIN、子行のない行をすでに除外しますorder_comment

于 2012-04-09T18:07:56.473 に答える
0

集計関数でOVERを使用できることを知らないと思います。

COUNT(order) OVER(PARTITION BY id) AS [comment]
....
ORDER BY [comment]
于 2012-04-05T23:20:17.613 に答える
0

SQL Serverでは、ORDER BY 1のように、クエリから列番号で注文できます 詳細情報はこちらhttp://blog.sqlauthority.com/2010/12/27/sql-server-order-by-columnname-vs-order- by-columnnumber/

SELECT *
FROM   (SELECT ISNULL((SELECT COUNT("order")
                      FROM   order_comment
                   WHERE  "order" = "order"."id"
                   GROUP  BY "order"), 0)                 AS [comment],
           Row_number() OVER (ORDER BY "order"."id" DESC) AS [rownum]
    FROM   [order]
    WHERE  ISNULL((SELECT COUNT("order")
                   FROM   order_comment
                   WHERE  "order" = "order"."id"
                   GROUP  BY "order"), 0) > 0) AS [filter]
WHERE  [rownum] BETWEEN 0 AND 10
 ORDER BY 1
于 2012-04-05T23:53:02.303 に答える