1,113 views
0 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 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 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 

)

Position:
Show:

Related questions

1 1 vote
2 2 answers
987
987 views
Purple asked Jan 9, 2017
987 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...
0 0 votes
0 0 answers
365
365 views
Balaji Jegan asked Oct 23, 2018
365 views
2 2 votes
1 1 answer
1.4k
1.4k views
reena_kandari asked Nov 8, 2016
1,424 views
1 1 vote
2 2 answers
595
595 views
gatecse asked Feb 23
595 views
Consider a table Employee(EmpID, TeamID), where the column EmpID (ID of an employee) is the primary key. The column TeamID denotes the team ID of the team of which the em...