过去一周左右我一直在研究这个问题,我似乎无法提出解决方案。 我正在尝试创建一个查询,我从各种表中获取数据,主要是我在几周(每周)汇总日期。因此,我创建了一个日历表,如下所示;
DATE, YEAR, MONTH_OF_YEAR, WEEK_OF_YEAR,
2014-01-01 2014 1 1
2014-01-02 2014 1 1
2014-01-03 2014 1 1
2014-01-04 2014 1 1
2014-01-05 2014 1 1
2014-01-06 2014 1 2
数据一直持续到。
所以,我已经汇总了所有这些表格和数据,并希望过去10周的结果以及没有活动的几周我想要0。
我写了这个查询来从2个表中获取数据并在日历表的week_of_year上加入它们(请不要把我钉在十字架上,SQL不是我的第一语言。)
select calendar.YEAR,
calendar.WEEK_OF_YEAR,
ifnull(count(data_transfer.id_data_transfer), 0) as DEVICE_TO_DEVICE,
ifnull(count(device.tag_code), 0) as DEVICE_REGISTRATION
from calendar
left join data_transfer
on data_transfer.WEEK_NUMBER=calendar.WEEK_OF_YEAR -- wee number
and data_transfer.year = calendar.YEAR -- need the year of week
and data_transfer.id_customer = 1 -- specific customer
and data_transfer.cloud_type is null -- specific type etc
and date(data_transfer.TRANSFER_START_TIME) > date(date_sub(now(), INTERVAL 1 YEAR)) -- for the final week to be this week
left join device
on device.WEEK_NUMBER = calendar.WEEK_OF_YEAR
and device.year = calendar.YEAR
and device.ID_CUSTOMER = 1
and date(device.DEVICE_CREATE_DATE) > date(date_sub(now(), INTERVAL 1 YEAR)) -- for the final week to be this week
where date(calendar.date) > date(date_sub(now(),INTERVAL 1 YEAR)) -- for the final week to be this week
group by calendar.year, calendar.WEEK_OF_YEAR
order by calendar.year, calendar.WEEK_OF_YEAR;
查询返回多个双打,我想这可能是由于日期表中有多个为week_of_year的条目,即由于一周中的天数而导致的数据?
例如,当我使用group_concat(specific_field)运行它时,我得到的结果如图所示。这是我试图解释的问题
YEAR, WEEK_OF_YEAR, DEVICE_TO_DEVICE, DEVICE_REGISTRATION, group_concat(data_transfer.id_data_transfer), id
2014 46 280 280 50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57
上面的问题是我想要解决的问题,对此有任何帮助将不胜感激。
答案 0 :(得分:1)
是的,问题是您有多个具有相同年份和周年值的记录。
创建一个日历表,其中表中只有一年 - 一年中的一对,并使用它来加入您的日志数据。
或者在查询中使用select distinct。
select distinct calendar.YEAR,
calendar.WEEK_OF_YEAR,
ifnull(count(data_transfer.id_data_transfer), 0) as DEVICE_TO_DEVICE,
ifnull(count(device.tag_code), 0) as DEVICE_REGISTRATIO
from...
select c.YEAR,
c.WEEK_OF_YEAR,
ifnull(count(data_transfer.id_data_transfer), 0) as DEVICE_TO_DEVICE,
ifnull(count(device.tag_code), 0) as DEVICE_REGISTRATION
from (select distinct YEAR, WEEK_OF_YEAR FROM calendar) c
left join data_transfer
on data_transfer.WEEK_NUMBER=c.WEEK_OF_YEAR -- wee number
and data_transfer.year = c.YEAR -- need the year of week
and data_transfer.id_customer = 1 -- specific customer
and data_transfer.cloud_type is null -- specific type etc
and date(data_transfer.TRANSFER_START_TIME) > date(date_sub(now(), INTERVAL 1 YEAR)) -- for the final week to be this week
left join device
on device.WEEK_NUMBER = c.WEEK_OF_YEAR
and device.year = c.YEAR
and device.ID_CUSTOMER = 1
and date(device.DEVICE_CREATE_DATE) > date(date_sub(now(), INTERVAL 1 YEAR)) -- for the final week to be this week
where date(calendar.date) > date(date_sub(now(),INTERVAL 1 YEAR)) -- for the final week to be this week
group by c.year, c.WEEK_OF_YEAR
order by c.year, c.WEEK_OF_YEAR;