602 views
0 votes
0 votes

Commodity items have some positive or negative changes in their
prices each week. Each trading company picks a portfolio of com-
modity items, that is, they have one or more items and they own
some non-zero quantity of each one. The database table for this
problem consists of the following two relations:


Commodity(item-number int, price-change float, week int),
Owns(company-name text, item-number int, quantity float).

Write an SQL query which returns the item-numbers of commodities for which, in any given week, the price change is greater than
or equal to that of all other items and there exists at least one
company selling that item only (i.e., not selling any other item)
in that week

2 Answers

0 votes
0 votes

I have made the following attempt. Can any please confirm if I am write?

SELECT
  item_number
FROM
  comodity
WHERE
  price_change =(
  SELECT MAX
    (price_change)
  FROM
    comodity
  WHERE WEEK
    = 1 AND item_number IN(
    SELECT
      item_number
    FROM
      (
      SELECT
        company,
        item_number,
        COUNT(item_number) AS ct
      FROM
        owns
      GROUP BY
        company
    ) AS t1
  WHERE
    t1.ct = 1
  )
)
0 votes
0 votes

select a.item-number from commodity a

where a.price-change >= ALL( select b.price-change from commodity b where b.week=a.week)

and

exists

( select company-name from owns

where company-name in ( select c.company-name from owns c where c.item-number=a.item-number)

group by company-name

having count(item-number)=1 

)

Related questions

1 votes
1 votes
2 answers
1
Purple asked Jan 9, 2017
501 views
Consider the following instances $R.$A1234BNull122The number of tuples returned by the following $\text{SQL query}$ is$?$$>$select $*$ from $R$ as $R_{1}$ where not exist...
2 votes
2 votes
1 answer
2
reena_kandari asked Nov 8, 2016
623 views