带有dayValue的SQL pivot [在pivot-table中处理日期] MYSQL-solution

时间:2014-08-25 10:54:33

标签: php mysql sql

我有以下数据库:

           ...
           `id` int(8) NOT NULL auto_increment,
           `title` varchar(60) NOT NULL default '',
           `date_firstcall` timestamp NOT NULL default CURRENT_TIMESTAMP,
           `cid` int(8) NOT NULL default '1',
           ...

使用此SQL语句:

SELECT apv.title,apv.date_firstcall, COUNT( date_firstcall ) AS totalViews, DAYNAME(apv.date_firstcall) AS Tag FROM AnalysePageview apv  GROUP BY TO_DAYS(date_firstcall), apv.title ORDER BY apv.date_firstcall DESC

我会得到一张这样的表:

title   date_firstcall      totalViews  Tag
name A  2014-08-25 10:19:49     2   Monday
name B  2014-08-25 07:04:36     2   Monday
name B  2014-08-24 16:03:36     3   Sunday
name C  2014-08-24 14:54:47     2   Sunday
name D  2014-08-24 10:25:35     1   Sunday
name A  2014-08-24 00:01:45     24  Sunday
name C  2014-08-23 11:06:19     3   Saturday
name A  2014-08-23 00:05:35     16  Saturday
name B  2014-08-22 10:05:53     4   Friday
name A  2014-08-22 00:11:28     25  Friday
name C  2014-08-21 19:28:54     1   Thursday
name A  2014-08-21 08:44:05     13  Thursday
name C  2014-08-20 22:42:49     1   Wednesday
name E  2014-08-20 03:04:03     1   Wednesday
name A  2014-08-20 00:25:01     23  Wednesday
name C  2014-08-19 16:27:03     2   Tuesday
name D  2014-08-19 16:22:42     2   Tuesday
name A  2014-08-19 15:43:57     10  Tuesday
name B  2014-08-19 09:36:52     1   Tuesday
name E  2014-08-18 20:31:06     1   Monday
name C  2014-08-18 18:51:15     19  Monday
name B  2014-08-18 17:52:21     4   Monday
name D  2014-08-18 14:55:52     3   Monday

我想在今天(今天是星期一)开始的最后一周获得一张桌子上的女巫大喊:

       Monday  Sunday  Saturday  Friday  ...
name A   2       24      0
name B   2       3

是否有可能以不同的语言获取日期名称?

请在将它标记为double之前:我已经看到了其他的数据问题,没有人处理变量日格式。

2 个答案:

答案 0 :(得分:0)

关于您的语言问题:

您可以使用查询查看选择的语言:     SELECT @@ lc_time_names;

之后你可以通过以下方式设置自己的:     SET lc_time_names =' YOUR_LC_TIME';

并改变"命令"在你的一周工作中,你可以制定程序来改变它,或者只是创建一个if / else语句来改变"这几天的原始顺序。

答案 1 :(得分:0)

我找到了解决方案:

首先我创建了一个help-table而不是pivot-sql-statement。

          $sql = "DROP VIEW IF EXISTS tmp";
          $result = mysqli_query($link, $sql);
          $sql =  "CREATE VIEW tmp AS SELECT apv.title,apv.date_firstcall,
                   COUNT( date_firstcall ) AS totalViews, 
                   DAYNAME(apv.date_firstcall) AS Tag FROM AnalysePageview apv 
                   WHERE apv.date_firstcall > CURRENT_DATE - INTERVAL 6 DAY 
                   GROUP BY TO_DAYS(date_firstcall), 
                   apv.title ORDER BY apv.date_firstcall DESC";

          $result = mysqli_query($link, $sql);

          $sql =  "SELECT title as Seite,
                    sum( if( Tag = 'Monday', totalViews, 0 ) ) AS Montag,
                    sum( if( Tag = 'Tuesday', totalViews, 0 ) ) AS Dienstag,
                    sum( if( Tag = 'Wednesday', totalViews, 0 ) ) AS Mittwoch,
                    sum( if( Tag = 'Thursday', totalViews, 0 ) ) AS Donnerstag,
                    sum( if( Tag = 'Friday', totalViews, 0 ) ) AS Freitag,
                    sum( if( Tag = 'Saturday', totalViews, 0 ) ) AS Samstag,
                    sum( if( Tag = 'Sunday', totalViews, 0 ) ) AS Sonntag
                    FROM tmp  GROUP BY title";