2

単一のテーブルがあり、テーブル構造は次のようになります。

unique_id     vendor_name      price1     price2     price3    code 

1             Vendor 1         0.0012     0.0014     0.0054     125
2             Vendor 2         0.0015     0.0016     0.0050     125
3             Vendor 3         0.0011     0.0019     0.0088     125
4             Vendor 1         0.0025     0.0024     0.0034     126
5             Vendor 2         0.0043     0.0019     0.0065     126
6             Vendor 3         0.0019     0.0085     0.0082     126        

各価格列グループの最小価格をコードで取得する必要があります。そして、私の予想される出力は次のとおりです。

Code          price1          price2      price3     vendor for price1      vendor for price 2      vendor for price 3

125           0.0011          0.0014      0.0050     Vendor3                Vendor1                 Vendor 2 
126           0.0019          0.0019      0.0034     Vendor3                Vendor2                 Vendor 1

では、このようなレコードを取得するための MySQL クエリは何でしょうか? また、テーブルから最大値と 2 番目に高い値を取得するクエリを作成する必要があり、単一のコードで任意の数の行が存在する可能性があります。

私のデータはこのSQL Fiddleにあります。

2 番目に高い値の場合、出力は次のようになります。

Code          price1          price2      price3     vendor for price1      vendor for price 2      vendor for price 3
125           0.0012          0.0016      0.0054     Vendor1                Vendor2                 Vendor 1 
126           0.0025          0.0024      0.0065     Vendor1                Vendor1                 Vendor 2
4

4 に答える 4

1
SELECT
    data.*,
    v1.vendor_name 'vendor for price1',
    v2.vendor_name 'vendor for price2',
    v3.vendor_name 'vendor for price3'
FROM
    (
        SELECT
            Code,
            MIN(price1) price1,
            MIN(price2) price2,
            MIN(price3) price3
        FROM
            tbl
        GROUP BY Code
    ) data
    LEFT JOIN
    (
        SELECT 
            MIN(vendor_name) vendor_name,
            Code
        FROM
            tbl
        WHERE
            price1 = 
            (
                SELECT
                    MIN(price1)
                FROM
                    tbl t
                WHERE
                    t.Code = tbl.Code
            )
        GROUP BY Code
    ) v1 ON data.Code = v1.Code
    LEFT JOIN
    (
        SELECT 
            MIN(vendor_name) vendor_name
            Code
        FROM
            tbl
        WHERE
            price2 = 
            (
                SELECT
                    MIN(price2)
                FROM
                    tbl t
                WHERE
                    t.Code = tbl.Code
            )
        GROUP BY Code
    ) v2 ON data.Code = v2.Code
    LEFT JOIN
    (
        SELECT 
            MIN(vendor_name) vendor_name
            Code
        FROM
            tbl
        WHERE
            price3 = 
            (
                SELECT
                    MIN(price3)
                FROM
                    tbl t
                WHERE
                    t.Code = tbl.Code
            )
        GROUP BY Code
    ) v3 ON data.Code = v3.Code

クエリ自体はかなり大きく見えますが、結合は 3 回繰り返されます。

更新の代わりに削除LIMITおよび追加するクエリを更新しました。MIN(vendor_name) vendor_namevendor_name

デモ

ここ

出力

| CODE | PRICE1 | PRICE2 | PRICE3 | VENDOR FOR PRICE1 | VENDOR FOR PRICE2 | VENDOR FOR PRICE3 |
-----------------------------------------------------------------------------------------------
|  125 | 0.0011 | 0.0014 | 0.0050 |          Vendor 3 |          Vendor 1 |          Vendor 2 |
|  126 | 0.0019 | 0.0019 | 0.0034 |          Vendor 3 |          Vendor 2 |          Vendor 1 |
于 2013-04-25T10:17:07.693 に答える
0

これは良い習慣であり、私は最初に野蛮な方法で試してみます。

select 1 as wh,code,min(price1) as price,vendor_name
from(
  select code,price1,vendor_name
  from ForgeRock
  order by code,price1
  )t1
group by code
union all
select 2 as wh,code,min(price2) as price,vendor_name
from(
  select code,price2,vendor_name
  from ForgeRock
  order by code,price2
  )t1
group by code
union all
select 3 as wh,code,min(price3) as price,vendor_name
from(
  select code,price3,vendor_name
  from ForgeRock
  order by code,price3
  )t1
group by code

これはジョインを使わない非常にシンプルで簡単な方法で、これで十分だと思います。必要に応じて結果をピボットできる列 Wh を追加します。ただし、上記のコードを使用して 2 番目に低い結果を取得することはできません。この目標を達成する方法はいくつかありますが、次のようなパーティションを作成することをお勧めします。

CREATE TABLE ForgeRock
    (`unique_id` tinyint, `vendor_name` varchar(8), `price1` float(5,4)
    , `price2` float(5,4), `price3` float(5,4),`code` tinyint)
PARTITION BY KEY(code)
PARTITIONS 2;

INSERT INTO ForgeRock
    (`unique_id`, `vendor_name`, `price1`, `price2`, `price3`, `code`)
VALUES
(1,'Vendor 1',0.0012,0.0014,0.0054,125),
(2,'Vendor 2',0.0015,0.0016,0.0050,125),
(3,'Vendor 3',0.0011,0.0019,0.0088,125),
(4,'Vendor 1',0.0025,0.0024,0.0034,126),
(5,'Vendor 2',0.0043,0.0019,0.0065,126),
(6,'Vendor 3',0.0019,0.0085,0.0082,126);

これは少しトリッキーですが、 にさまざまな値がそれほど多くない場合に非常に便利ですcode。次のように結果を取得できます。

select * from
(select code,price1 as price,vendor_name
from ForgeRock partition (p0)
order by price1
Limit 1,1
)t1
union all
select * from
(select code,price1 as price,vendor_name
from ForgeRock partition (p1)
order by price1
Limit 1,1
)t1
union all
select * from
(select code,price2 as price,vendor_name
from ForgeRock partition (p0)
order by price2
Limit 1,1
)t1
union all
select * from
(select code,price2 as price,vendor_name
from ForgeRock partition (p1)
order by price2
Limit 1,1
)t1
union all
select * from
(select code,price3 as price,vendor_name
from ForgeRock partition (p0)
order by price3
Limit 1,1
)t1
union all
select * from
(select code,price3 as price,vendor_name
from ForgeRock partition (p1)
order by price3
Limit 1,1
)t1

明らかに私の答えを改善できると思いますが、その仕事はあなたに任せます。

于 2016-08-02T07:51:17.693 に答える
0

これを試して :

select table1.vendor_name as vendor_name_for_price1,table2.vendor_name as vendor_name_for_price2, table3.vendor_name as vendor_name_for_price3

from 
table table1, table table2, table table3,
(Select code, min(price1),min(price2),min(price3) from table group by code ) TMP1

where
table1.price1=min(price1) and table1.code=TMP1.code
and
table2.price2=min(price2) and table2.code=TMP2.code
and
table3.price3=min(price3) and table3.code=TMP2.code ;
于 2016-08-01T22:04:28.303 に答える