简体   繁体   English

SQL 从一列返回值的查询是重复的,但在另一列中有 1 null 和一个值

[英]SQL query that returns the value from one column which is duplicated but in other column has 1 null and one value

Important: Metabase SQL is READ only, this metabase is using a POSTGRESQL.重要提示:元数据库 SQL 是只读的,此元数据库使用的是 POSTGRESQL。

The query I need is: Return all the values in Shipment that in the shipment col has the same number and in the col Origin has 1 null and 1 value (coupled).我需要的查询是:返回 Shipment 中的所有值,这些值在 shipment col 中具有相同的编号,并且在 col Origin 中具有 1 null 和 1 个值(耦合)。

This is my table:这是我的桌子:

Origin起源 Shipment运输 Product产品 Quantity数量
Purchase.23购买.23 shipment.42装运.42 356 356 2 2个
shipment.42装运.42 356 356 2 2个
Purchase.25购买.25 shipment.422装运.422 256 256 4 4个
shipment.422装运.422 256 256 4 4个
Purchase.18购买.18 shipment.427装运.427 666 666 4 4个
shipment.427装运.427 666 666 4 4个
Purchase.20购买.20 shipment.427装运.427 666 666 3 3个
shipment.427装运.427 666 666 3 3个
Purchase.22购买.22 shipment.427装运.427 677 677 4 4个
shipment.427装运.427 677 677 3 3个
shipment.9999发货.9999 222 222 9 9
shipment.427装运.427 677 677 1 1个
shipment.9999发货.9999 222 222 9 9
shipment.1111发货.1111 7 7 11 11
shipment.1111发货.1111 564 564 6 6个
shipment.5555装运.5555 756 756 1 1个
shipment.4444装运.4444 755 755 5 5个
Purchase.77购买.77 shipment.3333装运.3333 751 751 9 9

Notice in the table above, that Purchase.22 has 4 in Quantity and there are two shipments with the same shipment as the Purchase.22 row with 1 and 3 in quantity, both summed are the 4 of the Purchase.22 row.请注意上表中,Purchase.22 的 Quantity 为 4,并且有两批货与 Purchase.22 行相同,数量为 1 和 3,两者总和为 Purchase.22 行的 4。

The query should return this:查询应该返回这个:

Origin起源 Shipment运输 Product产品 Quantity数量
Purchase.23购买.23 shipment.42装运.42 356 356 2 2个
shipment.42装运.42 356 356 2 2个
Purchase.25购买.25 shipment.422装运.422 256 256 4 4个
shipment.422装运.422 256 256 4 4个
Purchase.18购买.18 shipment.427装运.427 666 666 4 4个
shipment.427装运.427 666 666 4 4个
Purchase.20购买.20 shipment.427装运.427 666 666 3 3个
shipment.427装运.427 666 666 3 3个
Purchase.22购买.22 shipment.427装运.427 677 677 4 4个
shipment.427装运.427 677 677 4 4个

And if posible another query that returns this, products that doesn't have any couple Origin null with Shipment.xx, Origin Purchase with Shipment.xx:如果可能的话,另一个查询返回这个,没有任何一对 Origin null 与 Shipment.xx,Origin Purchase 与 Shipment.xx 的产品:

Origin起源 Shipment运输 Product产品 Quantity数量
shipment.9999发货.9999 222 222 9 9
shipment.9999发货.9999 222 222 9 9
shipment.1111发货.1111 7 7 11 11
shipment.1111发货.1111 564 564 6 6个
shipment.5555装运.5555 756 756 1 1个
shipment.4444装运.4444 755 755 5 5个
Purchase.77购买.77 shipment.3333装运.3333 751 751 9 9

A little bit more of info, I'm working on a BI tool named Metabase, I'm a junior I just started a month ago and they gave me this stuff to do and the query I'm making is to know how many articles I have on the ivnentory.更多信息,我正在开发一个名为 Metabase 的 BI 工具,我是一名大三学生,一个月前刚开始工作,他们给了我这些事情要做,我正在做的查询是要知道有多少篇文章我有在ivnentory。 Unfortunately some articles are duplicated, if they were all duplciated I could easily divide the quantity by 2. But i's never easy.不幸的是,有些文章是重复的,如果它们都是重复的,我可以很容易地将数量除以 2。但我从来都不容易。 I have values that completly lack of origin and values that lack of shipment and both aren't coupled with other value that has both, and other products that are triplicated and have different quantitites, hence I cannot divide it that easily.我有完全没有来源的价值和没有装运的价值,并且两者都没有与具有这两种价值的其他价值相结合,以及其他一式三份且具有不同数量的产品,因此我不能那么容易地划分它。

I tried to find any pattern with other columns form the table, but nothing worked at least on shipment.out while in other shipment values like shipment.out.return I found patterns that helped me without the necessity of using a SQL query.我试图在表中找到其他列的任何模式,但至少在 shipment.out 上没有任何效果,而在 shipment.out.return 等其他装运值中,我找到了无需使用 SQL 查询即可帮助我的模式。

I tried to find any pattern with other columns form the table, but nothing worked at least on shipment.out while in other shipment values like shipment.out.return I found patterns that helped me without the necessity of using a SQL query.我试图在表中找到其他列的任何模式,但至少在 shipment.out 上没有任何效果,而在 shipment.out.return 等其他装运值中,我找到了无需使用 SQL 查询即可帮助我的模式。

First you need to aggregate your quantities across <shipment, product>.首先,您需要汇总 <shipment, product> 的数量。

Then you can select:然后你可以select:

  • the last value for each <shipment, product, quantity>每个 <shipment, product, quantity> 的最后一个值
  • the count of each <shipment, product, quantity>每个 <shipment, product, quantity> 的计数

The filter out all rows that have less than 1 count and which have both <origin, last origin> set to null.过滤掉所有计数小于 1 且 <origin, last origin> 都设置为 null 的行。

Check the demo here .此处查看演示。


If you can have multiple records for <shipment, product> and need to aggregate quantity, you need to do it before the execution of this query.如果<shipment, product> 可以有多条记录并且需要汇总数量,则需要在执行此查询之前进行汇总。 Here's how it would look like:它看起来像这样:

WITH cte AS (
    SELECT MAX(Origin) AS Origin, Shipment, Product, SUM(Quantity) AS Quantity
    FROM tab
    GROUP BY COALESCE(Origin, 'NULL'), Shipment, Product
), cte2 AS (
    SELECT *, 
           LAG(Origin) OVER(PARTITION BY Shipment, Product, Quantity ORDER BY Shipment, Origin) AS last_val, 
           COUNT(*) OVER(PARTITION BY Shipment, Product, Quantity) AS cnt
    FROM cte
)
SELECT Origin,
       Shipment,
       Product, 
       Quantity
FROM cte2
WHERE COALESCE(Origin, last_val) IS NOT NULL AND cnt > 1
ORDER BY shipment

Output: Output:

origin起源 shipment运输 product产品 quantity数量
Purchase.23购买.23 shipment.42装运.42 356 356 2 2个
null null shipment.42装运.42 356 356 2 2个
Purchase.25购买.25 shipment.422装运.422 256 256 4 4个
null null shipment.422装运.422 256 256 4 4个
Purchase.20购买.20 shipment.427装运.427 666 666 3 3个
null null shipment.427装运.427 666 666 3 3个
Purchase.18购买.18 shipment.427装运.427 666 666 4 4个
null null shipment.427装运.427 666 666 4 4个
Purchase.22购买.22 shipment.427装运.427 677 677 4 4个
null null shipment.427装运.427 677 677 4 4个

Check the demo here .此处查看演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM