簡體   English   中英

COALESCE SUM GROUP?

[英]COALESCE SUM GROUP?

好的。 我有一個查詢,看起來像這樣:

SELECT
    SUM(`order_items`.`quantity`) as `count`,
    `menu_items`.`name`
FROM 
    `orders`,
    `menu_items`,
    `order_items` 
WHERE 
    `orders`.`id` = `order_items`.`order_id` AND 
    `menu_items`.`id` = `order_items`.`menu_item_id` AND 
    `orders`.`date` >= '2008-11-01' AND 
    `orders`.`date` <= '2008-11-30' 
GROUP BY 
    `menu_items`.`id`

該查詢的目的是顯示在給定日期范圍內售出的商品數量。 雖然這個作品,我現在需要它來顯示一個count0 ,如果一個特定的項目有日期范圍內沒有銷售。 我嘗試在SUM周圍使用COALESCE ,但這並沒有解決問題,而且我也沒有真正期望它能做到。 無論如何,有人知道我將如何實現這一目標嗎? 我正在那些時刻中的一刻,我覺得我應該知道這一點,但我想不起來。

干杯

如果將日期條件放在JOIN子句中,則無需任何子查詢即可完成此操作。

以下是我在MySQL 5.0上測試過的代碼。

SELECT m.name, COALESCE(SUM(oi.quantity), 0) AS count
FROM menu_items AS m
  LEFT OUTER JOIN (
    order_items AS oi JOIN orders AS o
      ON (o.id = oi.order_id)
  ) ON (m.id = oi.menu_item_id
      AND o.`date` BETWEEN '2008-11-01' AND '2008-11-30')
GROUP BY m.id;

輸出:

+--------+-------+
| name   | count |
+--------+-------+
| bread  |     2 | 
| milk   |     1 | 
| honey  |     2 | 
| cheese |     0 | 
+--------+-------+

這是MySQL風格的DDL和設置代碼:

DROP TABLE IF EXISTS menu_items;
CREATE TABLE menu_items (
  id            INT PRIMARY KEY,
  name          VARCHAR(10)
) TYPE=InnoDB;

DROP TABLE IF EXISTS orders;
CREATE TABLE orders (
  id            INT PRIMARY KEY,
  `date`        DATE
) TYPE=InnoDB;

DROP TABLE IF EXISTS order_items;
CREATE TABLE order_items (
  order_id      INT,
  menu_item_id  INT,
  quantity      INT,
  PRIMARY KEY (order_id, menu_item_id),
  FOREIGN KEY (order_id) REFERENCES orders(id),
  FOREIGN KEY (menu_item_id) REFERENCES menu_items(id)
) TYPE=InnoDB;

INSERT INTO menu_items VALUES
  (1, 'bread'),
  (2, 'milk'),
  (3, 'honey'),
  (4, 'cheese');

INSERT INTO orders VALUES
  (1, '2008-11-02'),
  (2, '2008-11-03'),
  (3, '2008-10-29');

INSERT INTO order_items VALUES
  (1, 1, 1),
  (1, 3, 1),
  (2, 1, 1),
  (2, 2, 1),
  (2, 3, 1),
  (3, 4, 10);

Randy的答案很接近,但是where語句刪除了該日期范圍內不屬於任何訂單的那些物品的任何提及。

請注意,“左聯接”與您按照已完成的方式(即內部聯接)在where子句中鏈接表不同。 我建議您閱讀不同類型的SQL連接(內部,外部,交叉)。

本質上,您需要將從Randy的查詢中獲得的數據與項目的源列表相結合。 使用子選擇將執行以下操作:

SELECT
    name
    , nvl(count, 0) as count
FROM 
    menu_items items 
    LEFT JOIN (
        SELECT
            menu_items.id
            , SUM(order_items.quantity) as count
        FROM 
            menu_items
            LEFT JOIN order_items ON menu_items.id = order_items.menu_item_id
            LEFT JOIN orders ON orders.id = order_items.order_id
        WHERE
            "date" between to_date('2008-11-01','YYYY-MM-DD') and to_date('2008-11-30','YYYY-MM-DD')
        GROUP BY
            menu_items.id
    ) counts on items.id = counts.id;

這在Oracle 10g BTW中。 我懷疑您使用的是Oracle,因此您需要轉換為自己的數據庫。

運行測試將顯示以下內容:

SQL> create table menu_items ( id number, name varchar2(10));
create table order_items (order_id number, menu_item_id number, quantity number);
create table orders (id number, "date" date);

Table created.

SQL> 
Table created.

SQL> 
Table created.

SQL> 
insert into menu_items values (1, 'bread');
insert into menu_items values (2, 'milk');
insert into menu_items values (3, 'honey');
insert into menu_items values (4, 'cheese');
SQL> 
1 row created.

SQL> 
1 row created.

SQL> 
1 row created.

SQL> 
1 row created.

SQL> 
insert into orders values (1, to_date('2008-11-02', 'YYYY-MM-DD'));
insert into orders values (2, to_date('2008-11-03', 'YYYY-MM-DD'));
insert into orders values (3, to_date('2008-10-29', 'YYYY-MM-DD'));SQL> 
1 row created.

SQL> 
1 row created.

SQL> 
insert into order_items values (1, 1, 1);
insert into order_items values (1, 3, 1);
1 row created.

SQL> 
1 row created.

SQL> 
insert into order_items values (2, 1, 1);
insert into order_items values (2, 2, 1);
insert into order_items values (2, 3, 1);

insert into order_items values (3, 4, 10);
1 row created.

SQL> 
1 row created.

SQL> 
1 row created.

SQL> 
1 row created.

SQL> SQL> 

1 row created.

SQL> 
SELECT
    name
    , nvl(count, 0) as count
FROM 
    menu_items items 
    LEFT JOIN (
        SELECT
            menu_items.id
            , SUM(order_items.quantity) as count
        FROM 
            menu_items
            LEFT JOIN order_items ON menu_items.id = order_items.menu_item_id
            LEFT JOIN orders ON orders.id = order_items.order_id
        WHERE
            "date" between to_date('2008-11-01','YYYY-MM-DD') and to_date('2008-11-30','YYYY-MM-DD')
        GROUP BY
            menu_iteSQL>   2    3    4    5    6    7  ms.id
    ) counts on items.id = counts.id;  8    9   10   11   12   13   14   15   16   17   18  

NAME            COUNT
---------- ----------
bread               2
milk                1
honey               2
cheese              0

SQL> 
drop table menu_items;
drop table order_items;
drop table orders;SQL> 
Table dropped.

SQL> 
Table dropped.

SQL> 

Table dropped.

SQL> 

PS:不好的做法是使用“日期”作為列名,因為它(在大多數情況下)是類型名,並可能導致查詢和解析問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM