繁体   English   中英

没有公共列的 SQL/Power BI 联接

[英]SQL/Power BI Joins without common column

所以我有以下问题:

我有 2 个表,一个包含 product_type 的不同出价,另一个包含产品销售的价格、日期等。

表格如下所示:

表出价:

+----------+---------------------+---------------------+--------------+-------+
| Bid_id   | Start_time          | End_time            | Product_type | price |
+----------+---------------------+---------------------+--------------+-------+
| 1        | 18.01.2020 06:00:00 | 18.01.2020 06:02:33 | blue         | 5 €   |
| 2        | 18.01.2020 06:00:07 | 18.01.2020 06:00:43 | blue         | 7 €   |
| 3        | 18.01.2020 06:01:10 | 19.01.2020 15:03:15 | red          | 3 €   |
| 4        | 18.01.2020 06:02:20 | 18.01.2020 06:05:44 | blue         | 6 €   |
|          |                     |                     |              |       |
+----------+---------------------+---------------------+--------------+-------+

表卖:

+---------+---------------------+--------------+--------+
| Sell_id | Sell_time           | Product_type | Price  |
+---------+---------------------+--------------+--------+
| 1       | 18.01.2020 06:00:31 | Blue         | 6,50 € |
| 2       | 18:01.2020 06:51:03 | Red          | 2,50 € |
|         |                     |              |        |
+---------+---------------------+--------------+--------+

sell_id 和bid_id 彼此没有关系。 我想知道的是,我们出售 product_type 时的最高出价是多少。 因此,如果我们采用 sell_id 1,它应该检查在 sell_time 期间此特定 product_type 的哪些出价处于活动状态(在本例中为 bid_id 1 和 2)并返回更高的价格(在本例中为 bid_id 2)。

我试图在 Power Bi 中解决这个问题,但是,我无法得到解决方案。 我假设,我必须使用 SQL-Joins 来解决它。

是否可以根据条件而不是匹配的列加入? 就像是:

SELECT bids.start_time, bids.end_time, bids.product_type, MAX(bids.price), sells.sell_time, sells.product_type, sells.price
FROM sells
INNER JOIN bids ON bids.start_time<sells.sell_time AND bids.end_time > sells.sell_time;

如果这个问题令人困惑,我很抱歉,我对这个抱歉还是个新手。 在此先感谢您的任何帮助!

你的样本数据 Sell_time 应该是 18.01.2020 吧? 您可以尝试使用此代码(由于笛卡尔连接而导致的数据量可能会占用大量资源)。 如果您确定 Sell 日总是在 Bid Start 日,那么您可以将日期列添加到您的表中并使用额外的 TREATAS(VALUE(bids[day], sells[day])

Test =
VAR __tretasfilter =
    TREATAS ( VALUES ( bids[Product_type] ), sells[Product_type] )
RETURN
    SUMMARIZE (
        FILTER (
            SUMMARIZECOLUMNS (
                sells[Sell_id],
                bids[Price],
                bids[Start_time],
                sells[Sell_time],
                bids[End_time],
                sells[Product_type],
                __tretasfilter
            ),
            [Start_time] <= [Sell_time]
                && [End_time] >= [Sell_time]
        ),
        sells[Sell_id],
        "MaxPrice", MAX ( bids[Price] )
    )

暂无
暂无

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

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