获取每年的数据

时间:2012-10-22 14:17:37

标签: sql sql-server sql-server-2008

我的查询有问题,所以我有两个表:工作和工作答案,所以要获得每个工作的答案数量我喜欢这样:

and ((month(a.created_at) = 10 and day(a.created_at) >= 1) 
      or (month(a.created_at) between 11 and 12) 
      or (month(a.created_at) between 1 and 8) 
      or (month(a.created_at) = 9 and day (a.created_at) <= 30))

现在我还有另一个修改是每年和两个日期之间(每年9月1日至12月30日)获得新的工作答案数量[field:created_at],

     +----------+-------+---------+------+----- +
     | year     | job title | number of answers |
     +----------+-------+---------+------+------+
     | 2008     | Job1      | 58                | 
     | 2010     | Job2      | 45                | 
     | 2012     | Job3      | 122               |
     +----------+-------+---------+------+------

年份和日期位于created_at字段...

编辑:

01/10到30/9我这样做:

  and ( (month(a.created_at) = 10 and day(a.created_at) >= 1) 
        or month(a.created_at)   between 11 and 12 
        or month(a.created_at) between 1 and 8 
        or (month(a.created_at) = 9 and day (a.created_at) <= 30))

编辑2:

 delimiter $$
begin

declare @StartDay INT, @EndDay INT, @StartMonth INT, @EndMonth INT
Select @StartDay = 01, @StartMonth = 10, @EndDay = 30, @EndMonth = 09

Select year(a.created_at) as years , jd.JobDomain, count(a.Id) as nb_answer,   a.Job_id,   j.JobTitle 
 from JobAppliance a
 inner join Job j on a.Job_id =  j.PublicId
 inner join JobDestination d on j.Id=d.Job_id
 inner join Jjobdomain jd on  j.Id = jd.Job_id

 and 
(
@StartMonth = @EndMonth And 
Month(a.created_at) = @StartMonth And 
Day(a.created_at) >= @StartDay And 
Day(a.created_at) <= @EndDay
) Or (
@StartMonth != @EndMonth And (
(
 Month(a.created_at) = @StartMonth And
  Day(a.created_at) >= @StartDay
) Or (
  Month(a.created_at) Between @StartMonth + 1 And @EndMonth - 1
) Or (
  Month(a.created_at) = @EndMonth And
  Day(a.created_at) <= @EndDay
)
)
)

group by year(a.created_at) , a.Job_id, j.JobTitle;

end$$
delimiter ;

我在声明中有语法错误?

1 个答案:

答案 0 :(得分:2)

这应该给你想要的东西:

select year(a.created_at) , count(a.Id) as nb_answer, a.Job_id, j.JobTitle 
from JobAnswer a
inner join Job j on a.Job_id =  j.job_id
where month(a.created_at) between 9 and 11 Or (
  month(a.created_at) = 12 and
  day(a.created_at) = 1
)
group by year(a.created_at), a.Job_id, j.JobTitle;

更新为新指定的日期范围

更通用的解决方案是拥有一个带有start_date的表,end_date用于定义感兴趣的范围并改为加入。

这可能更适用于一年内的连续日期范围,但我还没有对其进行测试。这个想法是你填写你想要的@StartDay等值

Declare @StartDay int, @EndDay int, @StartMonth int, @EndMonth int
Select @StartDay = 30, @StartMonth = 9, @EndDay = 1, @EndMonth = 12

Select Year(a.created_at), j.JobTitle, count(a.Id) as nb_answer 
from JobAnswer a
inner join Job j on a.Job_id =  j.job_id
where (
  @StartMonth = @EndMonth And 
  Month(a.created_at) = @StartMonth And 
  Day(a.created_at) >= @StartDay And 
  Day(a.created_at) <= @EndDay
) Or (
  @StartMonth != @EndMonth And (
    (
  Month(a.created_at) = @StartMonth And
      Day(a.created_at) >= @StartDay
    ) Or (
      Month(a.created_at) Between @StartMonth + 1 And @EndMonth - 1
    ) Or (
      Month(a.created_at) = @EndMonth And
      Day(a.created_at) <= @EndDay
    )
  )
)
Group By Year(a.created_at), j.JobTitle;