如何在sql中获得最少和最昂贵的书的价值?

时间:2017-02-19 00:41:06

标签: sql group-by sqlplus

如何根据零售价格获得每个出版商最低和最贵的图书的价值?

我已经查询了下表。但是我只是不知道如何获得,我认为这将是最后5行,这个结果。此外,如果我理解正确的问题。

select name, title, retail, sum(quantity)
from books full outer join orderitems using(isbn)
join publisher using(pubid)
group by name, title, retail
order by retail;

NAME                    TITLE                              RETAIL SUM(QUANTITY)
----------------------- ------------------------------ ---------- -------------
REED-N-RITE             BIG BEAR AND LITTLE DOVE             8.95             4
READING MATERIALS INC.  COOKING WITH MUSHROOMS              19.95             8
PRINTING IS US          REVENGE OF MICKEY                      22             5
AMERICAN PUBLISHING     HANDCRANKED COMPUTERS                  25             2
READING MATERIALS INC.  THE WOK WAY TO COOK                 28.75
READING MATERIALS INC.  HOW TO GET FASTER PIZZA             29.95
READING MATERIALS INC.  BODYBUILD IN 10 MINUTES A DAY       30.95             1
PRINTING IS US          HOW TO MANAGE THE MANAGER           31.95             1
REED-N-RITE             SHORTEST POEMS                      39.95             1
PUBLISH OUR WAY         E-BUSINESS THE EASY WAY              54.5             2
AMERICAN PUBLISHING     DATABASE IMPLEMENTATION             55.95             7
PUBLISH OUR WAY         BUILDING A CAR WITH TOOTHPICKS      59.95
AMERICAN PUBLISHING     HOLY GRAIL OF ORACLE                75.95             3
REED-N-RITE             PAINLESS CHILD-REARING              89.95             6

1 个答案:

答案 0 :(得分:1)

有点难以分辨,因为你还没有向我们展示其他表格,但是怎么样:

.to_s

关键点:

  • 您希望发布商
  • 指定每个发布商的最小值和最大值
  • select publisher, min(retail), max(retail) from books join orderitems using(isbn) join publisher using(pubid) group by publisher; 为您提供组中所有图书(发布商)的最小值min(retail),以便告诉您每位发布商发布的最便宜的图书。
  • 同样,retail告诉你最贵的