anthonydb/practical-sql

Code Listing 5-7 returns duplicate rows

felixDK opened this issue · 4 comments

The query as listed on p. 62 and below:

SELECT geo_name,
state_us_abbreviation AS "st",
(CAST(p0010006 AS numeric(8,1)) / p0010001) * 100 AS "pct_asian"
FROM us_counties_2010
ORDER BY "pct_asian" DESC;

returns duplicate rows. One can suppress these with with a Select Distinct, but it's not clear why one should have to.

Results obtained under Postgres 2.3.3c (66) Mac and pgAdmin 4.17

Hi, would you please run the follow statement in pgAdmin and post the results here?

SELECT count(*) FROM us_counties_2010;

Thanks.

It returned:

6286

As is now clear from running:

SELECT * FROM us_counties_2010
ORDER BY geo_name, state_us_abbreviation;

I somehow have duplicate rows. I'll just drop the tables and reimport them.

That should work. It's possible the data was copied into the table twice since there's no primary key set on it.

Thanks for reading Practical SQL, and please post any other questions you have!