1

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

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 ;

我在声明中有语法错误?

4

1 回答 1

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;
于 2012-10-22T14:21:42.650 回答