Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据其他列中的条件选择值对-PostgreSQL_Sql_Postgresql - Fatal编程技术网

根据其他列中的条件选择值对-PostgreSQL

根据其他列中的条件选择值对-PostgreSQL,sql,postgresql,Sql,Postgresql,在过去的几天里,我一直在试图解决一个问题,但却不知道解决方案是什么 我有如下表格: +--------+-----------+-------+ | ShopID | ArticleID | Price | +--------+-----------+-------+ | 1 | 3 | 150 | | 1 | 2 | 80 | | 3 | 3 | 100 | | 4 |

在过去的几天里,我一直在试图解决一个问题,但却不知道解决方案是什么

我有如下表格:

+--------+-----------+-------+
| ShopID | ArticleID | Price |  
+--------+-----------+-------+
|      1 |         3 |   150 | 
|      1 |         2 |    80 |  
|      3 |         3 |   100 |  
|      4 |         2 |    95 |  
+--------+-----------+-------+
SELECT ShopID AS ShopID_1, ShopID AS ShopID_2, ArticleID FROM table
WHERE table.ArticleID=table.ArticleID and table.Price > table.Price
我想选择同一商品价格更高的两对店铺ID。 F.e.这应该看起来像:

+----------+----------+---------+
| ShopID_1 | ShopID_2 |ArticleID|
+----------+----------+---------+
|        4 |        1 |       2 |
|        1 |        3 |       3 |
+----------+----------+---------+
。。。表明Shopid4中的第2条比Shopid2中的第2条更昂贵。等

到目前为止,我的代码如下所示:

+--------+-----------+-------+
| ShopID | ArticleID | Price |  
+--------+-----------+-------+
|      1 |         3 |   150 | 
|      1 |         2 |    80 |  
|      3 |         3 |   100 |  
|      4 |         2 |    95 |  
+--------+-----------+-------+
SELECT ShopID AS ShopID_1, ShopID AS ShopID_2, ArticleID FROM table
WHERE table.ArticleID=table.ArticleID and table.Price > table.Price
但它并没有给出我正在寻找的结果


有人能帮我实现这个目标吗?非常感谢。

此查询应该可以:

SELECT t1.ShopID AS ShopID_1, t2.ShopID AS ShopID_2, t1.ArticleID
FROM <yourtable> t1 JOIN
     <yourtable> t2
     ON t1.ArticleID = t2.ArticleID AND t1.Price > t2.Price;
选择t1.ShopID作为ShopID\u 1,t2.ShopID作为ShopID\u 2,t1.ArticleID
从t1连接
t2
关于t1.ArticleID=t2.ArticleID和t1.Price>t2.Price;

也就是说,您需要一个自联接和适当的表别名。

此查询应该可以:

SELECT t1.ShopID AS ShopID_1, t2.ShopID AS ShopID_2, t1.ArticleID
FROM <yourtable> t1 JOIN
     <yourtable> t2
     ON t1.ArticleID = t2.ArticleID AND t1.Price > t2.Price;
选择t1.ShopID作为ShopID\u 1,t2.ShopID作为ShopID\u 2,t1.ArticleID
从t1连接
t2
关于t1.ArticleID=t2.ArticleID和t1.Price>t2.Price;

也就是说,您需要一个自联接和适当的表别名。

这里的问题是如何计算每组的前N个项目

假设您有以下数据,在表
sales

# select * from sales;
 shopid | articleid | price 
--------+-----------+-------
      1 |         2 |    80
      3 |         3 |   100
      4 |         2 |    95
      1 |         3 |   150
      5 |         3 |    50
通过以下查询,我们可以为每个
ArticleId

select 
  ArticleID, 
  ShopID, 
  Price, 
  row_number() over (partition by ArticleID order by Price desc) as Price_Rank from sales;
这将导致:

 articleid | shopid | price | price_rank 
-----------+--------+-------+------------
         2 |      4 |    95 |          1
         2 |      1 |    80 |          2
         3 |      1 |   150 |          1
         3 |      3 |   100 |          2
         3 |      5 |    50 |          3
 articleid | shopid | price 
-----------+--------+-------
         2 |      4 |    95
         2 |      1 |    80
         3 |      1 |   150
         3 |      3 |   100
然后,我们只需为每个AritcleId选择前2项:

select 
  ArticleID,  
  ShopID, 
  Price
from (
  select 
    ArticleID, 
    ShopID, 
    Price, 
    row_number() over (partition by ArticleID order by Price desc) as Price_Rank 
  from sales) sales_rank
where Price_Rank <= 2;
最后,我们可以使用
交叉表
函数来获得预期的透视图

select * 
from crosstab(
  'select 
    ArticleID,  
    ShopID, 
    ShopID
  from (
    select 
      ArticleID, 
      ShopID, 
      Price, 
      row_number() over (partition by ArticleID order by Price desc) as Price_Rank 
    from sales) sales_rank
  where Price_Rank <= 2')
AS sales_top_2("ArticleID" INT, "ShopID_1" INT, "ShopID_2" INT);
注:
您可能需要调用
CREATE EXTENSION tablefunc如果您得到的错误
函数交叉表(未知)不存在

这里的问题是关于计算每组前N项的问题

假设您有以下数据,在表
sales

# select * from sales;
 shopid | articleid | price 
--------+-----------+-------
      1 |         2 |    80
      3 |         3 |   100
      4 |         2 |    95
      1 |         3 |   150
      5 |         3 |    50
通过以下查询,我们可以为每个
ArticleId

select 
  ArticleID, 
  ShopID, 
  Price, 
  row_number() over (partition by ArticleID order by Price desc) as Price_Rank from sales;
这将导致:

 articleid | shopid | price | price_rank 
-----------+--------+-------+------------
         2 |      4 |    95 |          1
         2 |      1 |    80 |          2
         3 |      1 |   150 |          1
         3 |      3 |   100 |          2
         3 |      5 |    50 |          3
 articleid | shopid | price 
-----------+--------+-------
         2 |      4 |    95
         2 |      1 |    80
         3 |      1 |   150
         3 |      3 |   100
然后,我们只需为每个AritcleId选择前2项:

select 
  ArticleID,  
  ShopID, 
  Price
from (
  select 
    ArticleID, 
    ShopID, 
    Price, 
    row_number() over (partition by ArticleID order by Price desc) as Price_Rank 
  from sales) sales_rank
where Price_Rank <= 2;
最后,我们可以使用
交叉表
函数来获得预期的透视图

select * 
from crosstab(
  'select 
    ArticleID,  
    ShopID, 
    ShopID
  from (
    select 
      ArticleID, 
      ShopID, 
      Price, 
      row_number() over (partition by ArticleID order by Price desc) as Price_Rank 
    from sales) sales_rank
  where Price_Rank <= 2')
AS sales_top_2("ArticleID" INT, "ShopID_1" INT, "ShopID_2" INT);
注:
您可能需要调用
CREATE EXTENSION tablefunc如果您得到错误
函数交叉表(未知)不存在

如果每篇文章有两个以上的商店,这可能不起作用。@Sithroo。这就回答了这个问题:“我想选择同一物品价格较高的商店ID对。”如果有多个商店,OP将为每一物品获得多行。我看不出问题中有任何东西表明OP想要其他东西。如果每篇文章有两个以上的商店,这可能不起作用。@Sithroo。这就回答了这个问题:“我想选择同一物品价格较高的商店ID对。”如果有多个商店,OP将为每一物品获得多行。我看不出问题中有什么暗示OP还想要别的东西。