我正在尝试在此处按新列分组-一分为三。 在这种情况下,我想查看每个日期的未结订单,已关闭订单和计划订单。
目前,使用下面的代码,我得到了结果,但是我想创建一个包含主要日期的列,并为每个日期计数。
我得到的结果如下:
Date ClosedNo OpenNo PlanNo
22/10/2018 3 3
23/10/2018 1 1
24/10/2018 1
25/10/2018 1 1 1
26/10/2018 1
27/10/2018 2
所需的结果是:
CREATE TABLE Orders
(Closed DATE,
Open DATE,
Plan DATE);
insert into Orders values ("2018-10-23",NULL,NULL);
insert into Orders values ("2018-10-22",NULL,NULL);
insert into Orders values ("2018-10-22",NULL,NULL);
insert into Orders values ("2018-10-22",NULL,NULL);
insert into Orders values (NULL,NULL,"2018-10-23");
insert into Orders values (NULL,NULL,"2018-10-22");
insert into Orders values (NULL,NULL,"2018-10-22");
insert into Orders values (NULL,NULL,"2018-10-22");
insert into Orders values (NULL,"2018-10-26",NULL);
insert into Orders values (NULL,"2018-10-27",NULL);
insert into Orders values (NULL,"2018-10-27",NULL);
insert into Orders values (NULL,"2018-10-25",NULL);
insert into Orders values (NULL,NULL,"2018-10-24");
insert into Orders values ("2018-10-25",NULL,NULL);
insert into Orders values (NULL,NULL,"2018-10-25");
SELECT
Closed
, COUNT(Closed) AS 'ClosedNo'
,Open
, COUNT(Open) AS 'OpenNo'
,Plan
, COUNT(Plan) AS 'PlanNo'
FROM
Orders
GROUP BY
Closed, Open, Plan;
这是我使用的代码。
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="sonata.media.admin.media.class">Application\Sonata\MediaBundle\Admin\MediaAdmin</parameter>
</parameters>
<services>
<service id="sonata.media.admin.media" class="%sonata.media.admin.media.class%" public="true">
<tag name="sonata.admin" manager_type="orm" group="%sonata.media.admin.groupname%" label_catalogue="%sonata.media.admin.media.translation_domain%" label="media" label_translator_strategy="sonata.admin.label.strategy.underscore" icon="%sonata.media.admin.groupicon%"/>
<argument/>
<argument>%sonata.media.admin.media.entity%</argument>
<argument>%sonata.media.admin.media.controller%</argument>
<argument type="service" id="sonata.media.pool"/>
<argument type="service" id="sonata.media.manager.category" on-invalid="null"/>
<call method="setModelManager">
<argument type="service" id="sonata.media.admin.media.manager"/>
</call>
<call method="setTranslationDomain">
<argument>%sonata.media.admin.media.translation_domain%</argument>
</call>
<call method="setTemplates">
<argument type="collection">
<argument key="inner_list_row">@SonataMedia/MediaAdmin/inner_row_media.html.twig</argument>
<argument key="outer_list_rows_mosaic">@SonataMedia/MediaAdmin/list_outer_rows_mosaic.html.twig</argument>
<argument key="base_list_field">@SonataAdmin/CRUD/base_list_flat_field.html.twig</argument>
<argument key="list">@SonataMedia/MediaAdmin/list.html.twig</argument>
<argument key="edit">@SonataMedia/MediaAdmin/edit.html.twig</argument>
</argument>
</call>
</service>
答案 0 :(得分:1)
Open
和Date
是Keyword(s) in MySQL。您应该将您的专栏真正命名为其他名称;否则您将不得不使用反引号。Coalesce()
函数来获取Date
。它将考虑第一个非空值。Count()
聚合函数,我们可以为Date
组计算非空行。请尝试以下操作:
SELECT
COALESCE(Closed, `Open`, Plan) AS `Date`,
COUNT(Closed) AS ClosedNo,
COUNT(`Open`) AS OpenNo,
COUNT(Plan) AS PlanNo
FROM Orders
GROUP BY `Date`
结果
| Date | ClosedNo | OpenNo | PlanNo |
| ---------- | -------- | ------ | ------ |
| 2018-10-22 | 3 | 0 | 3 |
| 2018-10-23 | 1 | 0 | 1 |
| 2018-10-24 | 0 | 0 | 1 |
| 2018-10-25 | 1 | 1 | 1 |
| 2018-10-26 | 0 | 1 | 0 |
| 2018-10-27 | 0 | 2 | 0 |