联合重复

Duplicates in Union

我是 SQL 的新手,所以这可能就是我的查询出现问题的原因,但我的问题是我 运行 下面的查询最终在我的 table.

两个 table 都有一个具有相同库存编号的项目列表,这就是我加入库存编号的原因。问题是我的列表出现重复,相同的库存编号,但一行有营业额和雇佣天数,另一行有第二个 select 减去营业额和雇佣天数的其余信息。

如有任何帮助,我们将不胜感激!


(SELECT NULL                           AS 'ITEM_ID',
        stock_items.stock_number       AS 'STOCK NUMBER',
        SUM(stock_assign.turnover)     AS 'TURNOVER',
        SUM(stock_assign.days_on_hire) AS 'DAYS ON HIRE',
        NULL                           AS ' CAT',
        NULL                           AS 'ITEM NAME',
        NULL                           AS 'DEPOT LOC',
        NULL                           AS 'DISPOSAL DATE',
        NULL                           AS 'DISPOSAL TYPE',
        NULL                           AS 'DATE CREATED',
        NULL                           AS 'CREATED BY',
        NULL                           AS 'COST PRICE'
 FROM   stock_assign
        left join stock_items
               ON stock_assign.stock_number = stock_items.stock_number
        left join depots
               ON depot_id = depots.dpt_id
 GROUP  BY stock_items.stock_number,
           depots.dpt_name)
UNION
(SELECT stock_items.item_id         AS 'ITEM_ID',
        stock_items.stock_number    AS 'STOCK NUMBER',
        NULL                        AS 'TURNOVER',
        NULL                        AS 'DAYS ON HIRE',
        stock_items.equipment_class AS ' CAT',
        stock_items.name            AS 'ITEM NAME',
        depots.dpt_name             AS 'DEPOT LOC',
        stock_items.disposal_date   AS 'DISPOSAL DATE',
        stock_items.disposal_type   AS 'DISPOSAL TYPE',
        stock_items.date_available  AS 'DATE CREATED',
        users.usr_name              AS 'CREATED BY',
        fixed_assets.cost_amount    AS 'COST PRICE'
 FROM   stock_items
        left join users
               ON users.usr_id = stock_items.userid_created
        left join depots
               ON stock_items.depot_id_located_at = depots.dpt_id
        left join fixed_assets
               ON stock_items.item_id = fixed_assets.stock_item_id)
ORDER  BY 'STOCK NUMBER' ASC  

当联合数据集中的每一列都匹配时,UNION 的行为是 return 唯一行。

您有两个选择,由于为许多列分配了空值,它们显然总是会给出不同的结果。

也许如果您将其重写为两个数据集的 JOIN,您会得到想要的结果。

假设您要加入的每个 cte 上只有一行,下面是一个示例;

WITH cte_Assignment AS
(
    SELECT
        stock_items.stock_number       AS 'STOCK NUMBER',
        SUM(stock_assign.turnover)     AS 'TURNOVER',
        SUM(stock_assign.days_on_hire) AS 'DAYS ON HIRE'
    FROM stock_assign
    LEFT JOIN stock_items
        ON stock_assign.stock_number = stock_items.stock_number
    LEFT JOIN depots
        ON depot_id = depots.dpt_id
    GROUP BY
        stock_items.stock_number
)

, cte_Item AS
(
    SELECT
        stock_items.item_id         AS 'ITEM_ID',
        stock_items.stock_number    AS 'STOCK NUMBER',
        stock_items.equipment_class AS ' CAT',
        stock_items.name            AS 'ITEM NAME',
        depots.dpt_name             AS 'DEPOT LOC',
        stock_items.disposal_date   AS 'DISPOSAL DATE',
        stock_items.disposal_type   AS 'DISPOSAL TYPE',
        stock_items.date_available  AS 'DATE CREATED',
        users.usr_name              AS 'CREATED BY',
        fixed_assets.cost_amount    AS 'COST PRICE'
    FROM stock_items
    LEFT JOIN users
        ON users.usr_id = stock_items.userid_created
    LEFT JOIN depots
        ON stock_items.depot_id_located_at = depots.dpt_id
    LEFT JOIN fixed_assets
        ON stock_items.item_id = fixed_assets.stock_item_id
)

SELECT
    item.[ITEM_ID],
    item.[STOCK NUMBER],
    assignment.[TURNOVER],
    assignment.[DAYS ON HIRE],
    item.[ CAT],
    item.[ITEM NAME],
    item.[DEPOT LOC],
    item.[DISPOSAL DATE],
    item.[DISPOSAL TYPE],
    item.[DATE CREATED],
    item.[CREATED BY],
    item.[COST PRICE]
FROM cte_Assignment assignment
INNER JOIN cte_Item item
    on item.[STOCK NUMBER] = assignment.[STOCK NUMBER]

ORDER  BY item.[STOCK NUMBER] ASC 

您的另一个选择是将您现在拥有的查询包装为子查询。

SELECT MAX(ITEM_ID) as ITEM_ID, SUM(TURNOVER) 
FROM 
(SELECT NULL                           AS 'ITEM_ID',
    stock_items.stock_number       AS 'STOCK NUMBER',
    SUM(stock_assign.turnover)     AS 'TURNOVER',
    SUM(stock_assign.days_on_hire) AS 'DAYS ON HIRE',
    NULL                           AS ' CAT',
    NULL                           AS 'ITEM NAME',
    NULL                           AS 'DEPOT LOC',
    NULL                           AS 'DISPOSAL DATE',
    NULL                           AS 'DISPOSAL TYPE',
    NULL                           AS 'DATE CREATED',
    NULL                           AS 'CREATED BY',
    NULL                           AS 'COST PRICE'
 FROM   stock_assign
    left join stock_items
           ON stock_assign.stock_number = stock_items.stock_number
    left join depots
           ON depot_id = depots.dpt_id
GROUP  BY stock_items.stock_number,
       depots.dpt_name)
UNION
(SELECT stock_items.item_id         AS 'ITEM_ID',
    stock_items.stock_number    AS 'STOCK NUMBER',
    NULL                        AS 'TURNOVER',
    NULL                        AS 'DAYS ON HIRE',
    stock_items.equipment_class AS ' CAT',
    stock_items.name            AS 'ITEM NAME',
    depots.dpt_name             AS 'DEPOT LOC',
    stock_items.disposal_date   AS 'DISPOSAL DATE',
    stock_items.disposal_type   AS 'DISPOSAL TYPE',
    stock_items.date_available  AS 'DATE CREATED',
    users.usr_name              AS 'CREATED BY',
    fixed_assets.cost_amount    AS 'COST PRICE'
FROM   stock_items
    left join users
           ON users.usr_id = stock_items.userid_created
    left join depots
           ON stock_items.depot_id_located_at = depots.dpt_id
    left join fixed_assets
           ON stock_items.item_id = fixed_assets.stock_item_id)
ORDER  BY 'STOCK NUMBER' ASC) a
GROUP BY a.[Stock Number]

然后您需要按 2 个查询中所有相似的列进行分组,即。股票代码。在两个查询中都有十进制值的地方使用 SUM,如果一个查询中有一个空值而另一个查询中有一个值,则使用 MAX。

如果您正确执行此操作,您最终会得到每个库存编号的单个分组行。

作为旁注,请尽量避免在列名称中使用 space,而是将其替换为 _