我在此处使用以下查询来获取我的数据并每周显示该值,但是,当有新年且结果将显示为周50, 51, 52 of 2016
和周1, 2, 3 of 2017
时,它将会导致order by
的{{1}}周数表示2017年的结果出现在2016年之前,我该如何更改?
1,2,3,50,51,52
答案 0 :(得分:5)
也许不是最优雅的解决方案,但你可以在你所分组的领域中包括年份:
SELECT
CONVERT(VARCHAR,DATEPART(year, date)) + '_' + CONVERT(VARCHAR,DATEPART(wk, date)) AS Year_Week,
SUM((kg * rep * sett)) AS weight,
SUM(kg / max * rep * sett) AS avg,
SUM((rep * sett)) AS reps,
MAX(kg / max) AS peak
FROM
Test
WHERE
date BETWEEN @0 AND @1 AND exercise < 4
GROUP BY CONVERT(VARCHAR,DATEPART(year, date)) + '_' + CONVERT(VARCHAR,DATEPART(wk, date))
ORDER BY CONVERT(VARCHAR,DATEPART(year, date)) + '_' + CONVERT(VARCHAR,DATEPART(wk, date))
或(可能更好),只需将它们视为完全独立的字段:
SELECT
DATEPART(year, date) AS Year,
DATEPART(wk, date) AS Week,
SUM((kg * rep * sett)) AS weight,
SUM(kg / max * rep * sett) AS avg,
SUM((rep * sett)) AS reps,
MAX(kg / max) AS peak
FROM
Test
WHERE
date BETWEEN @0 AND @1 AND exercise < 4
GROUP BY DATEPART(year, date), DATEPART(wk, date)
ORDER BY DATEPART(year, date), DATEPART(wk, date)
答案 1 :(得分:0)
您可以按年份部分的汇总添加订单:
SELECT
DATEPART(wk, date) AS number,
SUM((kg * rep * sett)) AS weight,
SUM(kg / max * rep * sett) AS avg,
SUM((rep * sett)) AS reps,
MAX(kg / max) AS peak
FROM
Test
WHERE
date BETWEEN @0 AND @1 AND exercise < 4
GROUP BY DATEPART(wk, date)
order by max(datepart(year,date)), number;
然而,上面的数据汇集在同一周的数据中,即使它们属于不同的年份(可能不是你想要的)。如果要将不同年份的数据分开,可以在输出中添加年份列。
SELECT
datepart(year,date) as year,
DATEPART(wk, date) AS number,
SUM((kg * rep * sett)) AS weight,
SUM(kg / max * rep * sett) AS avg,
SUM((rep * sett)) AS reps,
MAX(kg / max) AS peak
FROM
Test
WHERE
date BETWEEN @0 AND @1 AND exercise < 4
GROUP BY datepart(year,date), DATEPART(wk, date)
order by year, number;