duangou1868 2012-01-11 04:49
浏览 28

是否可以在单个查询中选择MonthToDate,LastYearMonthToDate,YearToDate和LastYearToDate?

I'm trying to select the following data in a single query: I have a date variable in standard mySQL format, which allows mySQL select statements such as YEAR(date) = '2011' and MONTH(date) = '11' to select all data from November 2011.

I'm trying to SUM a variable called sales for four distinct timeframes: Month to Date, Last Year Month to Date, Year to Date, and Last Year to Date.

Please assume I have these three variables defined before hand. $currentYear, $lastYear, and $currentMonth.

I would like to try and reduce load on database and see if it's possible to select this in a single query.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongpiaozhao6836 2012-01-11 07:45
    关注

    You could use the SUM combined with CASE trick. It has got me out of many an analytical hole in the past!

    Below is an example with a query at the bottom. I am not 100% sure what should be in 'Last Year Month to Date' and 'Last Year to Date' but hopefully you will get the principle of what I am driving at. In the query you'll need to replace 2012, 2011 and 1 with your $currentYear, $lastYear and $currentMonth variables respectively.

    create table date_timeframe_example
    (myDate datetime not null,
    sales int not null default 0
    );
    
    insert into date_timeframe_example values (now() - interval 1 year + interval 3 day,51);
    insert into date_timeframe_example values (now() - interval 1 year + interval 4 day,5);
    insert into date_timeframe_example values (now() - interval 1 year + interval 5 day,7);
    insert into date_timeframe_example values (now() - interval 1 year + interval 6 day,87);
    insert into date_timeframe_example values (now() - interval 1 year + interval 7 day,12);
    insert into date_timeframe_example values (now() - interval 1 year + interval 12 day,0);
    insert into date_timeframe_example values (now() - interval 1 year - interval 4 day,1);
    insert into date_timeframe_example values (now() - interval 1 year - interval 5 day,1235);
    insert into date_timeframe_example values (now() - interval 1 year - interval 12 day,76);
    insert into date_timeframe_example values (now() - interval 1 year - interval 6 day,98);
    insert into date_timeframe_example values (now() - interval 1 year - interval 2 day,4);
    
    insert into date_timeframe_example values (now() - interval 3 day,8);
    insert into date_timeframe_example values (now() - interval 4 day,21);
    insert into date_timeframe_example values (now() - interval 5 day,67);
    insert into date_timeframe_example values (now() - interval 6 day,73);
    insert into date_timeframe_example values (now() - interval 7 day,67);
    insert into date_timeframe_example values (now() - interval 12 day,4);
    insert into date_timeframe_example values (now() - interval 4 day,9);
    insert into date_timeframe_example values (now() - interval 5 week,124);
    insert into date_timeframe_example values (now() - interval 12 week,42);
    insert into date_timeframe_example values (now() - interval 6 week,9);
    insert into date_timeframe_example values (now() - interval 2 week,7);
    
    select 
    sum(case when year(myDate) = 2012 and month(myDate) = 1 then sales else 0 end) as month_to_date,
    sum(case when year(myDate) = 2011 and month(myDate) = 1 then sales else 0 end) as last_year_month_to_date,
    sum(case when year(myDate) = 2012 then sales else 0 end) as year_to_date,
    sum(case when year(myDate) = 2011 then sales else 0 end) as last_year_to_date
    from date_timeframe_example;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法