3

私はSQLクエリを次のように持っています:

select dateName(month, DateAccessed) "Month"
, count(1) totalVisits
, count(distinct l.userName) UsersVisit
from and where clause goes here
group by dateName(monthDateAccessed)
order by Month

私が得る出力は

Month   totalVisits   UsersVisit
April         100       25
February      200       35
July          300       45
March         400       55
May           500       65

しかし、私が望む出力は次の順序です。

February      200       35
March         400       55
April         100       25
May           500       65
July          300       45

どうすればこれを入手できますか?

4

4 に答える 4

5

または のいずれmonth(DateAccessed)datepart(month, DateAccessed)を使用して月番号を抽出し、それをorder by句で使用します。group byただし、句にも追加する必要があります。

SELECT 
    DATENAME(month, DateAccessed) "Month", 
    COUNT(1) totalVisits, 
    COUNT(DISTINCT l.userName) UsersVisit 
FROM and where clause goes here
GROUP BY 
    MONTH(dateaccessed), 
    DATENAME(month, DateAccessed)
ORDER BY 
    MONTH(dateaccessed);

データが 1 年以上のデータを保持している場合、その年を group- および order by 句 (および select ステートメント) に含める必要があります (where 句で 1 年分のデータのみを取得することをまだ確認していない場合)。

于 2015-07-06T14:21:14.283 に答える
-1

列を 1 つ追加 month(DateAccessed) as MonthNoし、この列を次の順序で設定します。

select dateName(month, DateAccessed) "Month", month(DateAccessed) as MonthNo,
, count(1) totalVisits
, count(distinct l.userName) UsersVisit
from and where clause goes here
group by dateName(monthDateAccessed)
order by MonthNo
于 2015-07-06T14:06:51.087 に答える