我的表格中有一个日期栏CreateDate (datetime, not null)
。
使用以下where子句:
Where
-- Code for 12 Months Back at a Time
CreateDate >= dateadd(month,datediff(month, 0, getdate())-12,0)
如果我在2017年3月1日发表报告,我应该获得12个月的数据。我做。
我想按CreateDate
订购并获得以下订单
Mar 2017
Feb 2017
Jan 2017
Dec 2016
Nov 2016
Oct 2016
Sep 2016
Aug 2016
Jul 2016
Jun 2016
May 2016
Apr 2016
以下是完整的查询:
SELECT DISTINCT
tblCourtAttorneysAssigned.CourtAssignment AS [CourtAssignment],
tblCourtAttorneysAssigned.AttorneyBarNumber AS [AttorneyBarNumber],
tblCourtAttorneysAssigned.AttorneyFirstName AS [AttorneyFirstName],
tblCourtAttorneysAssigned.AttorneyLastName AS [AttorneyLastName],
--tblCourtAttorneysAssigned.CaseNo,
--tblPeople.LastName,
--tblPeople.FirstName,
tblCourtAttorneysAssigned.HowAssigned AS [HowAssigned],
CONVERT( VARCHAR(10), tblCourtAttorneysAssigned.CreateDate, 101) AS[CreateDate]
FROM tblBookIn
INNER JOIN tblOffense ON tblBookIn.BookInID = tblOffense.BookInID
INNER JOIN tblPeople ON tblBookIn.PersonID = tblPeople.PersonID
INNER JOIN tblCourtAttorneysAssigned ON tblOffense.CaseNo = tblCourtAttorneysAssigned.CaseNo
WHERE CourtAssignment LIKE 'M%'
AND
-- Code for 12 Months Back at a Time
CreateDate >= dateadd(month,datediff(month, 0, getdate())-12,0)
ORDER by
[CreateDate] asc
--courtassignment ASC,
-- AttorneyLastName ASC;
由于
答案 0 :(得分:2)
我猜您使用的是order by CreateDate desc
,但您在查询中使用了别名的CreateDate,就像format(CreateDate, 'MMM yyyy') as CreateDate
因此,您的order by
需要知道您希望它使用原始值而不是使用相同名称返回的新值。
尝试order by mytablename.CreateDate desc
<小时/> 编辑问题后更新以包括查询:
将您的order by
更改为:
order by tblCourtAttorneysAssigned.CreateDate desc
答案 1 :(得分:0)
我认为你应该在最后使用order by子句
SELECT { fn MONTHNAME(OrderDate) } AS MonthName, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profits
FROM [Order]
WHERE (YEAR(OrderDate) = @year)
GROUP BY { fn MONTHNAME(OrderDate) }, YEAR(OrderDate)
ORDER BY { fn MONTH(OrderDate) }, YEAR(OrderDate)
或
你可以这样做
SELECT CASE { fn MONTH(OrderDate) }
when 0 then 'JAN'
when 1 then 'FEB'
when 2 then 'MAR'
when 3 then 'APR'
when 4 then 'MAY'
when 5 then 'JUN'
when 6 then 'JUL'
when 7 then 'AUG'
when 8 then 'SEP'
when 9 then 'OCT'
when 10 then 'NOV'
when 11 then 'DEC'
END
AS MonthName, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profits
FROM [Order]
WHERE (YEAR(OrderDate) = @year)
GROUP BY { fn MONTH(OrderDate) }, YEAR(OrderDate)
ORDER BY { fn MONTH(OrderDate) }, YEAR(OrderDate)
答案 2 :(得分:0)
谢谢大家,我通过删除日期字段转换并修复SSRS格式来解决问题。
谢谢,