我需要跟踪我网站上的访问者,所以我正在使用这个存储过程,但它没有给我想要的结果,任何人都可以帮助我。 我最后需要一月......我的意思是在十二月之后
Select A.TotalPerMonth , DateName( month , DateAdd( month , A.monthValue , -1 ) ) as month , A.Year
from(Select Top 5 count(pk_id) as TotalPerMonth,
month(VistorDate) as monthValue,
Year(VistorDate) as Year
from jot.tbl_vistor
group by month(VistorDate) ,YEAR((VistorDate))
order by YEAR((VistorDate)) desc) A order by A.monthValue , A.Year ASC
以下是结果,我最后需要一月份,但是在前面给我
说明所需: 我需要前5个sep,oct,nov,dec,jan .....意味着最后5个月,当发现新记录时,它将丢弃sep
答案 0 :(得分:1)
目前尚不清楚,但我认为你想要这个:
SELECT A.TotalPerMonth ,
DATENAME(MONTH, DATEADD(MONTH, A.monthValue, -1)) AS month ,
A.Year
FROM ( SELECT TOP 5
COUNT(pk_id) AS TotalPerMonth ,
MONTH(VistorDate) AS monthValue ,
YEAR(VistorDate) AS Year
FROM jot.tbl_vistor
GROUP BY MONTH(VistorDate) ,
YEAR(( VistorDate ))
ORDER BY YEAR(( VistorDate )) DESC ,
MONTH(VistorDate) DESC
) A
ORDER BY A.Year ,
A.monthValue
即。在子查询中,您按年份desc和月份desc订购以获得最近5个月。在外部查询中,您按年份asc和月份asc。
进行排序答案 1 :(得分:0)
试试这个:已修改
Select A.TotalPerMonth , DateName( month , DateAdd( month , A.monthValue , -1 ) ) as month , A.Year
from(Select Top 6 count(pk_id) as TotalPerMonth,
month(VistorDate) as monthValue,
Year(VistorDate) as Year
from jot.tbl_vistor
group by month(VistorDate) ,YEAR((VistorDate))
order by VistorDate desc) A
order by A.monthValue , A.Year ASC
您需要指定A.monthValue DESC。你忘了把DESC放在那里。
答案 2 :(得分:0)
试试这个:
Select A.TotalPerMonth , DateName( month, DateAdd( month , A.monthValue , -1 ) ) as month , A.Year from(
Select Top 6 count(pk_id) as TotalPerMonth, month(VistorDate) as monthValue, Year(VistorDate) as Year
from jot.tbl_vistor
group by month(VistorDate) ,YEAR((VistorDate))
order by YEAR((VistorDate)) desc, month(VistorDate)
) A order by A.Year, A.monthValue
答案 3 :(得分:0)
我通过完整日期订购解决了它。为此,我从DATETIME
和monthValue
Year
Select DateName( month , DateAdd( month , A.monthValue , -1 ) ) as month , A.Year
from(SELECT Top 5
month(t.TestDate) as monthValue,
Year(t.TestDate) as Year
from #test t
group by month(t.TestDate), YEAR(TestDate)
order by month(t.TestDate), YEAR(TestDate) desc) A order BY CAST(CAST(A.monthValue AS varchar) + '-' + CAST(A.Year AS varchar) + '-01' AS DATETIME) ASC
输出: