Sunday, 15 September 2013

sql - Create view from table with multiple primary key -



sql - Create view from table with multiple primary key -

i've table one:

column | type | modifiers username | character varying(12) | not null electioncode | integer | not null votes | integer | default 0 primary key (username, electioncode)

i need create view username, electioncode, max(votes) if utilize query works fine without username:

select electioncode, max(votes) table grouping electioncode;

if add together username asks me add together grouping if gives me entire table instead of username-electioncode-maxvotes

do want username associated number of votes? or username in given election code?

if first:

select distinct on ( electioncode ) * table order electioncode, votes desc;

if other:

select electioncode, min(username), max(votes) table grouping electioncode;

sql postgresql create-view

No comments:

Post a Comment