列的不同值

时间:2017-01-03 17:06:55

标签: sql postgresql

我有很多单个地址,其中包含不同的Sq_ft值(不应该是这种情况)。我想解析具有相同地址的Sq_Ft的不同值的列。我可以在where子句中添加什么来帮助我这样做?我尝试使用Sq_ft添加不同的语法但是徒劳无功。

示例:

Address of New Home                      Sq_Ft
1234 Obama Drive                         2,578
1234 Obama Drive                         2,586

select "Address_of_New_Home","Sq_Ft"
from "Amazon".salesdatapcr
where "Sq_Ft" <>
order by 1

2 个答案:

答案 0 :(得分:3)

使用EXISTS运算符和相关子查询:

select "Address_of_New_Home","Sq_Ft"
from "Amazon".salesdatapcr t1
where EXISTS (
   SELECT null from "Amazon".salesdatapcr t2
   WHERE t1."Address_of_New_Home" = t2."Address_of_New_Home"
     and t1."Sq_Ft" <> t2."Sq_Ft"
)
order by 1

答案 1 :(得分:2)

这将列出一个表中的行,其中有多个地址出现不同的sq_ft:

SELECT salesdt.*
FROM "Amazon".salesdatapcr salesdt
JOIN (
  SELECT
    "Address_of_New_Home" AS home_address
  FROM
    "Amazon".salesdatapcr
  GROUP BY 1
  HAVING COUNT(DISTINCT "Sq_Ft") > 1
  ) multisqft ON
    salesdt."Address_of_New_Home" = multisqft.home_address
ORDER BY multisqft.home_address

您也可以使用EXISTSIN来实现这一目标。 ORDER可以让事情变得更加清晰。