简体   繁体   中英

www.sql-ex.ru #14

I am trying to solve problem number 14 from the well known(I believe) website www.sql-ex.ru because I need to learn sql(I am quite new to this language) This is the same question answered here on stackoverflow Problem 14 question StackOverflow. My first approach was something like :

select maker, type
from product
group by maker, type
having count(type)=1 and count(model)>1

Obviously this was wrong but I could not understand why, I've been looking around and I found this on google:

select maker, MAX(type)
from product
group by maker, type
having count(type)=1 and count(model)>1

Maybe I am retarted but I don't understand what is the MAX doing on a char value and why everything is working now.

Can somebody please shed some light on this problem?

cheers,

Characters have a "collating sequence" that (with other things) gives an ordering for ">", ORDER BY and MAX/MIN on them.

In most SQLs when you GROUP BY (or use HAVING without it, which implicitly GROUPs BY all columns) you can only SELECT columns that either were in the GROUP BY or that appear as arguments of aggregates. That's because there can be multiple rows with multiple values per group so a column name doesn't mean a particular value. Although in Standard SQL and some products you can also use a column when the DBMS has been told that there is only one value of that column per subrow of the grouping columns.

That allegedly linked query you show is not what is in the linked answer. And it is not a correct answer to the question. It returns what yours does.

 select maker, MAX(type) from product group by maker, type having count(type)=1 and count(model)>1 

The MAX, as in your solution, can be dropped, because type is a grouping column. But this is not a solution because no matter what types appear it has one group per maker-type, so one type per group. It doesn't restrict the output to makers having only one type.

The actual code at the link is: (Note the distinct that your copy is missing.)

 select maker, max(type) from product group by maker, type having count(distinct type)=1 and count(model)>1 

This makes a group for each maker-type pair. So naturally the MAX of the one type value of a group will be the type value of the group. And there will only be one distinct value per group. So it also returns what yours does.

That is not what they wanted. What they were trying to write was:

select maker, max(type)
from product
group by maker
having count(distinct type)=1 and count(model)>1

This selects only the groups with one distinct type value among the possibly many rows for a given maker. It is ok to mention type in the SELECT because it's in aggregate MAX. That MAX of type will be the maximum value from among a bunch of rows that all have the same (distinct) value.

But now the MAX can't be dropped, because type is not a grouping column.

PS What if the table is empty?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM