3

検索した問題があり、助けになる答えが見つかりません。

orderscustomers、 の3 つのテーブルがありますteams。各注文には顧客とチームがあります。私の問題は、顧客ごとに、合計注文を取得しようとしていて、最後の注文に割り当てられたチームを取得しようとしていることです。

これまでの私のクエリ(正常に動作します):

SELECT c.id, MAX(o.timestamp) AS "Last Order", COUNT(o.id) AS "Total Orders", SUM(o.price) AS "Total Spend"
FROM orders o
LEFT JOIN customers c ON o.customer = c.id
WHERE o.timestamp > '2012-01-01 00:00:00'
GROUP BY c.id
ORDER BY c.id

team.name私が持っている残りのクエリに影響を与えずに、彼らが出した最後の注文に参加する方法がわかりません。

サンプル テーブルと目的の結果:

顧客:

╔════╦═════════════╗
║ ID ║    NAME     ║
╠════╬═════════════╣
║  1 ║ John Smith  ║
║  2 ║ Jim Jimmers ║
╚════╩═════════════╝

チーム:

╔════╦═════════════╗
║ ID ║    NAME     ║
╠════╬═════════════╣
║  1 ║ Red         ║
║  2 ║ Blue        ║
╚════╩═════════════╝

注文:

╔═══════╦═════════════════════╦══════════╦══════╦═══════╗
║  ID   ║       TIMESTAMP     ║ CUSTOMER ║ TEAM ║ VALUE ║
╠═══════╬═════════════════════╬══════════╬══════╬═══════╣
║ 34656 ║ 2012-03-04 14:23:44 ║        1 ║    2 ║    20 ║
║ 37345 ║ 2012-04-12 11:32:07 ║        2 ║    2 ║    25 ║
║ 38220 ║ 2012-07-18 09:53:54 ║        1 ║    2 ║    15 ║
║ 39496 ║ 2012-07-03 10:11:32 ║        1 ║    1 ║    38 ║
║ 41752 ║ 2012-09-17 19:34:05 ║        1 ║    2 ║     9 ║
║ 43734 ║ 2012-11-23 07:52:12 ║        2 ║    1 ║    20 ║
╚═══════╩═════════════════════╩══════════╩══════╩═══════╝

次のような結果を選択するにはどうすればよいですか。

╔════╦═════════════╦═════════════════════╦═══════════╦══════════════╦═════════════╗
║ ID ║    NAME     ║      LAST_ORDER     ║ TEAM_NAME ║ TOTAL_ORDERS ║ TOTAL_SPEND ║
╠════╬═════════════╬═════════════════════╬═══════════╬══════════════╬═════════════╣
║  1 ║ John Smith  ║ 2012-09-17 19:34:05 ║ Blue      ║           4  ║          82 ║
║  2 ║ Jim Jimmers ║ 2012-11-23 07:52:12 ║ Red       ║           2  ║          45 ║
╚════╩═════════════╩═════════════════════╩═══════════╩══════════════╩═════════════╝
4

3 に答える 3

2

次のようなクエリを使用できます。

select c.id,
  c.name,
  o1.Last_Order,
  t.name Team_Name,
  o3.Total_Orders,
  o3.Total_Spend
from customers c
inner join
(
  select max(timestamp) Last_Order,
    max(id) Last_id,
    customer
  from orders
  WHERE timestamp > '2012-01-01 00:00:00'
  group by customer
) o1
  on c.id = o1.customer
inner join orders o2
  on c.id = o2.customer
  and o1.Last_Order = o2.timestamp
  and o1.Last_id = o2.id
inner join teams t
  on o2.team = t.id
inner join
(
  select count(team) Total_Orders,
    sum(value) Total_Spend,
    customer
  from orders 
  WHERE timestamp > '2012-01-01 00:00:00'
  group by customer
) o3
  on c.id = o3.customer
WHERE o2.timestamp > '2012-01-01 00:00:00'

デモで SQL Fiddle を参照してください

于 2013-03-04T15:36:56.537 に答える
1

これを試してみてください。

SELECT  a.ID, 
        a.Name Customer_Name,
        c.TimeStamp Last_ORDER,
        c.Name AS Team_Name,
        COUNT(b.Customer) AS Total_Order,
        SUM(b.Value) AS Total_Spend
FROM    Customers a
        INNER JOIN Orders b
            ON a.ID = b.Customer
        INNER JOIN
        (
            SELECT  o.Customer, 
                    o.TimeStamp,
                    q.Name
            FROM    Orders o
                    INNER JOIN
                    (
                        SELECT  Customer, MAX(ID) max_ID
                        FROM    Orders
                        GROUP   BY Customer
                    ) p ON o.Customer = p.Customer AND
                            o.ID = p.max_ID
                    INNER JOIN Team q
                        ON o.Team = q.ID
        ) c ON a.ID = c.Customer
GROUP   BY a.ID, a.Name
于 2013-03-04T15:26:38.897 に答える
1

group_concat私はトリックを使用してこれを行うのが好きです:

SELECT c.id, MAX(o.timestamp) AS "Last, Order", COUNT(o.id) AS "Total Orders",
       SUM(o.price) AS "Total Spend",
       substring_index(group_concat(teamname order by o.timestamp desc), ',', 1) as LastTeam
FROM orders o left outer join
     customers c ON o.customer = c.id left outer join
     teams t
     on o.teamid = t.id
WHERE o.timestamp > '2012-01-01 00:00:00'
GROUP BY c.id
ORDER BY c.id
于 2013-03-04T14:53:33.157 に答える