SQL Select Distinct ONLY只给我另一个表的标题而不是不同的行

时间:2014-09-26 22:04:48

标签: sql postgresql select distinct

我试图在PostgreSQL中仅为查询选择不同的行。

我有以下代码:

 INSERT INTO Products
 ("Unique ID","Manufacturer Name","Manufacturer Part Number","Packaging UOM")
 SELECT 
  'Unique ID',
  'Manufacturer Name',
  'Manufacturer Part Number',
  'Packaging UOM'
 FROM 
  All_Catalogs

当我用Select Selectinin替换Select时,我最终只是标题表All_Catalogs的标题,而不是所有的值。

我不想选择所有内容并删除唯一ID的副本,我最初只会使用唯一的ID'

来抓取行

我感谢任何帮助/建议。

尝试在线查看时: Confusion about SELECT DISTINCT in SQL - 这表明我得到了“非int错误”组。所以它对我不起作用 SELECT DISTINCT values and INSERT INTO table - 这再次提示我得到非int错误的组

1 个答案:

答案 0 :(得分:0)

此查询仅选择UniqueID

唯一All_Catalogs
 INSERT INTO Products
 ("Unique ID","Manufacturer Name","Manufacturer Part Number","Packaging UOM")
 SELECT 
  "Unique ID",
  "Manufacturer Name",
  "Manufacturer Part Number",
  "Packaging UOM"
 FROM 
  All_Catalogs
 WHERE "Unique ID" IN (SELECT "Unique ID" 
    FROM All_Catalogs 
    GROUP BY "Unique ID" 
    HAVING COUNT(*) = 1)