SQL:将2个表分组为1,使用join,union,然后?

时间:2009-05-22 14:05:14

标签: sql select join union

我有5张桌子:

客户 id - name

p_orders id - id_customer - 代码 - 日期

p_items id - id_order - description - price

和h_orders和h_items,它们正是p_orders和p_items的副本。

当p_表达到大量行时,我将最旧的行移动到h_ tables ..它们作为历史记录。

所以,我的问题是:如何从p_表和h_中检索数据,将它们视为一个唯一的表

例如,我想检索每个客户的订单数量和总价格(所有客户的订单),然后我使用该查询:

SELECT
    customer.id,
    customer.name,
    count(DISTINCT p_orders.id) AS num_orders,
    sum(p_items.price) AS total_money
FROM
    customer
    INNER JOIN p_orders ON p_orders.id_customer = customer.id
    INNER JOIN p_items ON p_items.id_order = p_orders.id
GROUP BY
    customer.id,
    customer.name,
    p_orders.id_customer
ORDER BY
    customer.id

它仅适用于一组'p'或h _)..但我想要它们。

我尝试使用UNION:

(
    SELECT
        customer.id,
        customer.name,
        count(DISTINCT p_orders.id) AS num_orders,
        sum(p_items.price) AS total_money
    FROM
        customer
        INNER JOIN p_orders ON p_orders.id_customer = customer.id
        INNER JOIN p_items ON p_items.id_order = p_orders.id
    GROUP BY
        customer.id,
        customer.name,
        p_orders.id_customer
)
UNION
(
    SELECT
        customer.id,
        customer.name,
        count(DISTINCT h_orders.id) AS num_orders,
        sum(h_items.price) AS total_money
    FROM
        customer
        INNER JOIN h_orders ON h_orders.id_customer = customer.id
        INNER JOIN h_items ON h_items.id_order = h_orders.id
    GROUP BY
        customer.id,
        customer.name,
        h_orders.id_customer
)
ORDER BY id ASC

这个有效,但如果客户在p_表和h_表中都有订单,那么我将为该客户提供2行,其中包含2个不同的num_orders和total_money(分别来自p_ tables和h_ tables)< / p>

我尝试在union之外添加GROUP BY id:

(
    --SELECT 2
)
UNION
(
    --SELECT 1
)
GROUP BY id
ORDER BY id ASC

但查询失败时出现错误:字符948 处的“GROUP”处或附近的语法错误,似乎不能以这种方式使用GROUP BY。

有什么建议吗?

修改

对于uriDium,是的,所有表都将id列作为主键,并且引用的字段(也称为p_orders.id_customer)也是外键。 这里测试db结构转储(我在创建表后添加了一些索引和外键,但我不认为这意味着什么):

CREATE TABLE customer (
    id serial NOT NULL,
    name character(50)
);
CREATE TABLE p_orders (
    id serial NOT NULL,
    id_customer integer NOT NULL,
    date date DEFAULT now(),
    code character(5)
);
CREATE TABLE p_items (
    id serial NOT NULL,
    id_order integer NOT NULL,
    descr character(250),
    price money
);
CREATE TABLE h_orders (
    id integer NOT NULL,
    id_customer integer NOT NULL,
    date date,
    code character(5)
);
CREATE TABLE h_items (
    id integer NOT NULL,
    id_order integer NOT NULL,
    descr character(250),
    price money
);
CREATE UNIQUE INDEX id_h_orders ON h_orders USING btree (id);
CREATE INDEX id_h_o_c ON h_orders USING btree (id_customer);
CREATE UNIQUE INDEX id_items_h ON h_items USING btree (id);
CREATE INDEX id_ordinr_dsve ON h_items USING btree (id_order);

ALTER TABLE ONLY customer
    ADD CONSTRAINT customer_pkey  (id);
ALTER TABLE ONLY p_orders
    ADD CONSTRAINT p_orders_pkey PRIMARY KEY (id);
ALTER TABLE ONLY p_items
    ADD CONSTRAINT p_items_pkey PRIMARY KEY (id);
ALTER TABLE ONLY stats
    ADD CONSTRAINT stats_pkey PRIMARY KEY (id);
ALTER TABLE ONLY p_orders
    ADD CONSTRAINT "$1" FOREIGN KEY (id_customer) REFERENCES customer(id) ON DELETE CASCADE;
ALTER TABLE ONLY p_items
    ADD CONSTRAINT "$1" FOREIGN KEY (id_order) REFERENCES p_orders(id) ON DELETE CASCADE;
ALTER TABLE ONLY h_orders
    ADD CONSTRAINT "$1" FOREIGN KEY (id_customer) REFERENCES customer(id) ON DELETE CASCADE;
ALTER TABLE ONLY h_items
    ADD CONSTRAINT "$1" FOREIGN KEY (id_order) REFERENCES h_orders(id) ON DELETE CASCADE;

6 个答案:

答案 0 :(得分:4)

您应该创建两个表的视图:

CREATE VIEW All_Orders
AS
     SELECT
          id,
          id_customer,
          code,
          date,
          'H' AS order_type
     FROM
          h_orders
     UNION ALL
     SELECT
          id,
          id_customer,
          code,
          date,
          'P' AS order_type
     FROM
          p_orders

CREATE VIEW All_Order_Items  -- A table name of "items" is pretty bad in my opinion
AS
     SELECT
          id,
          id_order,
          description,
          price,
          'H' AS order_item_type
     FROM
          h_items
     UNION ALL
     SELECT
          id,
          id_order,
          description,
          price,
          'P' AS order_item_type
     FROM
          p_items

现在您可以加入这些观点了。我包含了类型(P&amp; H),以便您知道“id”列现在指的是什么。如果你的两个表中的id(“h”和“p”可能有重复项,那么你必须在All_Order_Items视图中加入Orders表。否则你将在两个视图之间加入很多麻烦。希望你的id列是智能设计的,而不仅仅是自动填充或标识列。

答案 1 :(得分:2)

你可以试试这个:

SELECT tbl.ID, 
       tbl.Name, 
       sum(tbl.num_orders) num_orders, 
       sum(tbl.total_money) total_money
FROM (    
      SELECT customer.id, 
             customer.name,        
             count(DISTINCT p_orders.id) AS num_orders,        
             sum(p_items.price) AS total_money    
      FROM customer        
            INNER JOIN p_orders 
                ON p_orders.id_customer = customer.id        
            INNER JOIN p_items 
                ON p_items.id_order = p_orders.id    
      GROUP BY customer.id, customer.name, p_orders.id_customer

      UNION

      SELECT customer.id, 
             customer.name,        
             count(DISTINCT h_orders.id) AS num_orders,
             sum(h_items.price) AS total_money    
      FROM  customer        
             INNER JOIN h_orders 
                 ON h_orders.id_customer = customer.id
             INNER JOIN h_items 
                 ON h_items.id_order = h_orders.id    
      GROUP BY customer.id, customer.name, h_orders.id_customer
    ) tbl
 GROUB BY tbl.id, tbl.name
 ORDER BY tbl.id ASC

答案 2 :(得分:1)

使用两个查询的并集创建一个视图但没有聚合函数。 USe Union All作为相同的记录不在两个表中,并且您不需要服务器浪费时间来查看它。您可能有其他时间想要在查询中访问这两个表。

然后使用视图编写查询。

查看代码会是这样的(您可能还希望其他字段用于其他目的:

Create view customerOrders
AS
SELECT      customer.id as CustomerID,  customer.name, p_orders.id as OrderID,  p_items.price  as price
FROM        customer        
INNER JOIN  p_orders ON p_orders.id_customer = customer.id        
INNER JOIN  p_items ON p_items.id_order = p_orders.id
union all
SELECT      customer.id,  customer.name,  h_orders.id as id, H_items.price           
FROM        customer        
INNER JOIN  h_orders ON h_orders.id_customer = customer.id        
INNER JOIN  h_items ON h_items.id_order = h_orders.id

然后对您的查询的调用就像(没有一个被测试可能需要调整)

SELECT    CustomerID,    customer.name,    count(DISTINCT OrderID) AS num_orders,    
sum(price) AS total_money
FROM    customerOrders
GROUP BY     CustomerID,    customer.name
ORDER BY    CustomerID

答案 3 :(得分:0)

据我所知,SQL Server应该自动消除重复项。使用UNION ALL将包含重复项。我想,SQL Server会使用主键作为计算重复内容的方法。这些表上的主键是否由相同的数据类型组成,并且在p表中是ID 1,在h表中也是ID 1?

答案 4 :(得分:0)

最简单的方法是创建视图(例如“a_orders”和“a_items”)。视图将被定义为:

SELECT * FROM p_orders
UNION
SELECT * FROM h_orders

如果在将a_orders插入h_orders时删除a_orders中的行(因此给定的顺序不在两个表中),使用UNION ALL而不是UNION会更有效。

答案 5 :(得分:0)

感谢所有的回复,伙计们......

Jimmie R. Houts的'views way'和'subquery way'都很完美,也许这些视图使用起来更方便..而且它们都应该花费相同的时间(或不是?)

所以我会将第一个关于观点的答案标记为最佳答案。

无论如何,如果可以,请问您使用的结构和索引是好还是可以优化?