简体   繁体   中英

SQL/Power BI Joins without common column

So I have the following problem:

I have 2 tables, one containing different bids for a product_type, and one containing the price, date etc. to which the product was sold.

The tables look like this:

Table bids:

+----------+---------------------+---------------------+--------------+-------+
| 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 €   |
|          |                     |                     |              |       |
+----------+---------------------+---------------------+--------------+-------+

Table sells:

+---------+---------------------+--------------+--------+
| 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 € |
|         |                     |              |        |
+---------+---------------------+--------------+--------+

The sell_id and the bid_id have no relation with each other. What I want to find out is, what is the maximum bid to the time we sold the product_type. So if we take sell_id 1, it should check, which bids for this specific product_type were active during the sell_time (in this case bid_id 1 and 2) and give back the higher price (in this case bid_id 2).

I tried to solve this problem in Power Bi, however, I was not able to get a solution. I assume, that I have to work with SQL-Joins to solve it.

Is it possible, to join based on criteria instead of matching columns? Something like:

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;

I am sorry if this question is confusing, I am still new to this sorry. Thanks in advance for ANY help!

Your sample data Sell_time should be 18.01.2020, right? You Can try this code (can be resource-intensive in relation to the amount of data due to Cartesian joins). If you are sure that Sell day is always in Bid Start day, then you can add date column to yours tables and use additional 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] )
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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