2015-11-05 173 views
-1

我需要跟踪访问者在我的网站上,所以我使用这个存储过程,但它没有给我想要的结果,任何人都可以帮助我。 我需要一月最后.....我的意思后,十二月SQL Server存储过程(需要最近5个月onlly)

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 

以下是结果,我需要的月份每月最后,但在前面

enter image description here

是给我这里是我的表enter image description here

说明期望我需要前5九月,十月,十一月,十二月,一月.....意味着过去的5月,当新的记录fabruary被发现,就会丢弃月

+1

ORDER BY年,月 – wiretext

+0

@tinka使用后,我没有12月的价值,但一月是最后,我确实选择top 5 –

+0

你应该更好地描述你想要的结果是什么..给我们一个正确顺序的例子。 – tobypls

回答

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排序。

+0

太棒了!先生,我错了吗? –

+1

@JotDhaliwal,你在子查询排序中错了。您错过了按月份订购的订单。此外,外部订购应该是第一年,然后是第一个月。 –

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放在那里。

+0

先生,它给12月顶部,十二月,十一月,八月,九月,八月,januery –

+0

这就是你想要的,对吧?你提到Jan是顶级的,你希望它是最后的。 –

+0

请向我们展示预期的输出。 –

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 
+0

当我选择Top 5时,它考虑八月,但删除十二月 –

0

我通过完整的日期排序解决它。要做到这一点,我从产生一个monthValueDATETIMEYear

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 

输出:

enter image description here

+0

在我身边这是11月接收 –

+0

输出在我身边是oct,nov,nov,dece,jabuary –

+0

选择DateName(月,DateAdd(月, A.monthValue,-1))为一个月,A.Year 从(选择前5个月 (t.VistorDate)作为monthValue, 年份(如 年从jot.tbl_vistor牛逼 组用t t.VistorDate)。 (CAST(A.monthValue AS varchar)+' - '+ CAST(A.Year AS varchar)+'-01'AS DATETIME)ASC –