好吧,我正在尝试从同一服务器上的两个不同数据库中的两个表中检索数据,并在Reporting Services中创建一个图表。我目前正处于2008年的ssrs。对于图表,我需要按年份和月份排序,因为我有不同的年份,并计算每月被归类为有效的名称数量。我能够按年订购信息,但是按月按字母顺序排列,而不是按时间顺序排列。我试图删除ssrs中的默认排序但它仍然返回相同的结果,所以我认为必须在我的查询中完成排序。我的查询如下:
SELECT COUNT(c.name) AS name,
DATENAME(mm, c.expireson) AS Month,
DATENAME(yyyy, c.expireson) AS Year
FROM Database1.Table1 AS c
WHERE c.name = 'Active'
GROUP BY DATENAME(mm, c.expireson), DATENAME(yyyy, c.expireson)
UNION ALL
SELECT COUNT(d.name) AS name,
DATENAME(mm, d.expireson) AS Month,
DATENAME(yyyy, d.expireson) AS Year
FROM Database2.Table2 AS d
WHERE d.name = 'Active'
GROUP BY DATENAME(mm, d.expireson), DATENAME(yyyy, d.expireson)
我将非常感谢任何有关如何更改此订单以正确订购月份的帮助。提前谢谢!
答案 0 :(得分:2)
正如您所提到的,如果按月份名称排序,它将按字母顺序对数据进行排序
所以使用datepart
SELECT COUNT(c.name) AS name,
DATENAME(mm, c.expireson) AS Month,
DATENAME(yyyy, c.expireson) AS Year,
DATEPART(yyyy,c.expireson) as YearNum,
DATEPART(m, c.expireson) as MonthNum
FROM Database1.Table1 AS c
WHERE c.name = 'Active'
GROUP BY DATENAME(mm, c.expireson), DATENAME(yyyy, c.expireson),DATEPART(yyyy,c.expireson), DATEPART(m, c.expireson)
UNION ALL
SELECT COUNT(d.name) AS name,
DATENAME(mm, d.expireson) AS Month,
DATENAME(yyyy, d.expireson) AS Year,
DATEPART(yyyy,d.expireson) as YearNum,
DATEPART(m, d.expireson) as MonthNum
FROM Database2.Table2 AS d
WHERE d.name = 'Active'
GROUP BY DATENAME(mm, d.expireson), DATENAME(yyyy, d.expireson),DATEPART(yyyy,d.expireson), DATEPART(m, d.expireson)
order by YearNum, MonthNum
答案 1 :(得分:0)
我遇到了同样的问题。
事实证明,在“类别组属性”中,“排序”选项卡具有与我需要的字段排序数据不同的字段排序数据。
希望这有助于将来......