SQL在另一个groupby之上做一个groupby

时间:2014-08-29 08:05:47

标签: mysql sql group-by

我有这两个表:

+---------------+-------------+---------------------+----------+---------+
| items_ordered |             |                     |          |         |
+---------------+-------------+---------------------+----------+---------+
| customerid    | order_date  | item                | quantity | price   |
| 10330         | 30-Jun-1999 | Pogo stick          | 1        | 28.00   |
| 10101         | 30-Jun-1999 | Raft                | 1        | 58.00   |
| 10298         | 01-Jul-1999 | Skateboard          | 1        | 33.00   |
| 10101         | 01-Jul-1999 | Life Vest           | 4        | 125.00  |
| 10299         | 06-Jul-1999 | Parachute           | 1        | 1250.00 |
| 10339         | 27-Jul-1999 | Umbrella            | 1        | 4.50    |
| 10449         | 13-Aug-1999 | Unicycle            | 1        | 180.79  |
| 10439         | 14-Aug-1999 | Ski Poles           | 2        | 25.50   |
| 10101         | 18-Aug-1999 | Rain Coat           | 1        | 18.30   |
| 10449         | 01-Sep-1999 | Snow Shoes          | 1        | 45.00   |
| 10439         | 18-Sep-1999 | Tent                | 1        | 88.00   |
| 10298         | 19-Sep-1999 | Lantern             | 2        | 29.00   |
| 10410         | 28-Oct-1999 | Sleeping Bag        | 1        | 89.22   |
| 10438         | 01-Nov-1999 | Umbrella            | 1        | 6.75    |
| 10438         | 02-Nov-1999 | Pillow              | 1        | 8.50    |
| 10298         | 01-Dec-1999 | Helmet              | 1        | 22.00   |
| 10449         | 15-Dec-1999 | Bicycle             | 1        | 380.50  |
| 10449         | 22-Dec-1999 | Canoe               | 1        | 280.00  |
| 10101         | 30-Dec-1999 | Hoola Hoop          | 3        | 14.75   |
| 10330         | 01-Jan-2000 | Flashlight          | 4        | 28.00   |
| 10101         | 02-Jan-2000 | Lantern             | 1        | 16.00   |
| 10299         | 18-Jan-2000 | Inflatable Mattress | 1        | 38.00   |
| 10438         | 18-Jan-2000 | Tent                | 1        | 79.99   |
| 10413         | 19-Jan-2000 | Lawnchair           | 4        | 32.00   |
| 10410         | 30-Jan-2000 | Unicycle            | 1        | 192.50  |
| 10315         | 2-Feb-2000  | Compass             | 1        | 8.00    |
| 10449         | 29-Feb-2000 | Flashlight          | 1        | 4.50    |
| 10101         | 08-Mar-2000 | Sleeping Bag        | 2        | 88.70   |
| 10298         | 18-Mar-2000 | Pocket Knife        | 1        | 22.38   |
| 10449         | 19-Mar-2000 | Canoe paddle        | 2        | 40.00   |
| 10298         | 01-Apr-2000 | Ear Muffs           | 1        | 12.50   |
| 10330         | 19-Apr-2000 | Shovel              | 1        | 16.75   |

+ --------------- + ------------- + ---------------- ----- + ---------- + --------- +

+------------+-----------+----------+--------------+----------------+---+
| customers  |           |          |              |                |   |
+------------+-----------+----------+--------------+----------------+---+
| customerid | firstname | lastname | city         | state          |   |
| 10101      | John      | Gray     | Lynden       | Washington     |   |
| 10298      | Leroy     | Brown    | Pinetop      | Arizona        |   |
| 10299      | Elroy     | Keller   | Snoqualmie   | Washington     |   |
| 10315      | Lisa      | Jones    | Oshkosh      | Wisconsin      |   |
| 10325      | Ginger    | Schultz  | Pocatello    | Idaho          |   |
| 10329      | Kelly     | Mendoza  | Kailua       | Hawaii         |   |
| 10330      | Shawn     | Dalton   | Cannon Beach | Oregon         |   |
| 10338      | Michael   | Howell   | Tillamook    | Oregon         |   |
| 10339      | Anthony   | Sanchez  | Winslow      | Arizona        |   |
| 10408      | Elroy     | Cleaver  | Globe        | Arizona        |   |
| 10410      | Mary Ann  | Howell   | Charleston   | South Carolina |   |
| 10413      | Donald    | Davids   | Gila Bend    | Arizona        |   |
| 10419      | Linda     | Sakahara | Nogales      | Arizona        |   |
| 10429      | Sarah     | Graham   | Greensboro   | North Carolina |   |
| 10438      | Kevin     | Smith    | Durango      | Colorado       |   |
| 10439      | Conrad    | Giles    | Telluride    | Colorado       |   |
| 10449      | Isabela   | Moore    | Yuma         | Arizona        |   |
+------------+-----------+----------+--------------+----------------+---+

我想要做的是能够将每个州的总价格加起来。这将总结客户根据其位置花费的总价。但我无法想象你如何在SQL中做到这一点。我能够做的是通过userid多次获取状态列表以获得下表:


+-----------------+-------+---------+
| Arizona         | 10339 | 4.50    |
| Arizona         | 10449 | 970.79  |
| Arizona         | 10298 | 147.88  |
| Arizona         | 10413 | 128.00  |
| Colorado        | 10439 | 139.00  |
| Colorado        | 10438 | 95.24   |
| Oregon          | 10330 | 156.75  |
| Sourth Carolina | 10410 | 281.72  |
| Washington      | 10299 | 1288.00 |
| Washington      | 10101 | 813.95  |
| Wisconsin       | 10315 | 8.00    |
+-----------------+-------+---------+

我使用以下操作来获取上述表格:

>   SELECT c.State, i.customerid, SUM(i.quantity * i.price)   
>   FROM items_ordered i, customers c
>   WHERE c.customerid = i.customerid   
>   GROUP BY i.customerid
>   ORDER BY c.State;

以下是我试图展示的表格:

+-----------------+-------+---------+
| Arizona         | 10339 | 1251.17 |
| Colorado        | 10438 | 234.24  |
| Oregon          | 10330 | 156.75  |
| Sourth Carolina | 10410 | 281.72  |
| Washington      | 10299 | 2101.95 |
| Wisconsin       | 10315 | 8.00    |
+-----------------+-------+---------+

2 个答案:

答案 0 :(得分:0)

查看您正在使用的数据库的GROUP BY文档。

将GROUP BY更改为c.state应该给你我认为你想要的东西。

SQL Fiddle example <-- click me!

答案 1 :(得分:0)

您希望按州加总 - 所以只需从选择列表中删除客户ID,然后按州分组:

SELECT   c.State, SUM(i.quantity * i.price)   
FROM     items_ordered i, customers c
WHERE    c.customerid = i.customerid   
GROUP BY c.State