2015-10-06 45 views
1
CREATE TABLE "spiral_articleinfo" (
    "id" integer NOT NULL PRIMARY KEY, 
    "url" varchar(255) NOT NULL UNIQUE, 
    "articletitle" varchar(1000) NOT NULL, 
    "articleimage" text, 
    "articlecontent" text NOT NULL, 
    "articlehtml" text NOT NULL, 
    "articledate" date, 
    "articlecategory" varchar(1000) NOT NULL, 
    "s_articlecategory" varchar(1000), 
    "articlesubcategory" varchar(1000) NOT NULL, 
    "articledomain" varchar(255), 
    "domainrank" integer NOT NULL, 
    "articletags" text NOT NULL, 
    "articleprice" decimal NOT NULL, 
    "contenthash" varchar(255) UNIQUE 
); 

這是表格模式。我希望通過articledomain獲得每篇文章呈現組的不同關鍵詞。 我所做的:使用group by by postgresql獲取多於2列的不同數據

select distinct articledomain, articlecategory 
from spiral_articleinfo  
group by articledomain 
order by articledomain; 

錯誤到來是:

ERROR: column "spiral_articleinfo.articlecategory" must appear in the GROUP BY clause or be used in an aggregate function.

如何獲得articlecategories?

當我運行此查詢:

select distinct articledomain, articlecategory, articlesubcategory 
from spiral_articleinfo; 

我得到:

articledomain  | articlecategory | articlesucategory 
androidadvices.com| Android Apps | Books & Reference 
androidadvices.com| Android Apps | Business 

我想要的結果是繼其articlecategory和articlesubcategory領域一個領域。 樣本輸出:

articledomain  | articlecategory | articlesucategory 
androidadvices.com | Android Apps | Books & Reference, Business 
www.womensweb.in |    Home|Social Lens, Crime, Law, Safety 
suitcaseofstories.in|   Himachal|Ladakh, People, Street Photography 
+1

請根據樣本數據添加一些樣本數據和預期輸出。但看起來你只需要將'group by'部分全部刪除。 –

+1

數據非常龐大,我運行這個:從spiral_articleinfo中選擇不同的articledomain,articlecategory,articlesubcategory;但是這給了我:androidadvices.com | Android應用程序|商業 androidadvices.com | Android應用程序|通訊 androidadvices.com | Android應用程序|教育 androidadvices.com | Android應用程序|娛樂..但我希望所有的articledomains被毆打在一起,然後這些類別連在一起。 –

+0

@NamitaMaharanwar根據你發佈的輸出結果是什麼樣的結果?,_I想要結果成爲一個域,跟隨它的articlecategory和articlesubcategory字段_>顯示它應該如何看起來像.. –

回答

1

可以使用string_agg()功能以及GROUP BY以達到所需的輸出:

與以下爲例:

create table foow (articledomain text,articlecategory text,articlesucategory text); 

insert into foow values ('androidadvices.com','Android Apps','Books & Reference'); 
insert into foow values ('androidadvices.com','Android Apps','Business'); 

和select語句應該是

select articledomain 
     ,articlecategory 
     ,string_agg(articlesucategory,',') 
from foow 
group by articledomain, articlecategory 

SQLFIDDLE DEMO