U
U
ummik2021-08-29 10:38:42
SQL
ummik, 2021-08-29 10:38:42

Help in solving a problem using SQL queries?

There are tasks: https://i.imgur.com/h1X72Fo.png

Please help me solve task 2.a.
I'm a very young Padawan, trying to learn from assignments. Simple tasks are clear and have already been tested, but a little more complicated and that's it, plug.

These are sketches, I understand that this is most likely an abracadabra, but for now it’s like this:

select CUSTOMER.customer_key, CUSTOMER.age, PURCHASE.customer_key, PURCHASE.product_key, PURCHASE.date, PRODUCT.name, PRODUCT.product_key
FROM PRODUCT JOIN PURCHASE
on (PRODUCT.product_key = PURCHASE.product_key and product.name = 'плюшевый мишка')
FROM PURCHASE JOIN CUSTOMER
on CUSTOMER.customer_key = PURCHASE.customer_key
where product.name = 'плюшевый мишка';
where purchase.date = '2018' (тут нужно вроде интервал)
//WHERE `time_stamp` BETWEEN '2013-01-01 00:00:00' AND '2013-12-31 23:59:59'//
SELECT AVG(AGE) AS avg_age FROM CUSTOMER;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-08-29
@ummik

SELECT AVG(`age`)
  FROM `CUSTOMER`
  WHERE `CUSTOMER_KEY` IN (
    SELECT `CUSTOMER_KEY`
      FROM `PURCHACE`
      WHERE `PRODUCT_KEY` IN (
        SELECT `PRODUCT_KEY`
          FROM `PRODUCT`
          WHERE `NAME` = 'плюшевый мишка'
      ) AND `DATE` BETWEEN '2018-01-01' AND '2018-12-31'
  )

If you want with JOINs, then
SELECT AVG(`age`)
  FROM (
    SELECT DISTINCT `c`.`CUSTOMER_KEY`, `c`.`AGE` AS `age`
      FROM `PRODUCT` AS `p`
      JOIN `PURCHACE` AS `pu`
        ON `p`.`NAME` = 'плюшевый мишка'
        AND `pu`.`DATE` BETWEEN '2018-01-01' AND '2018-12-31'
        AND `pu`.`PRODUCT_KEY` = `p`.`PRODUCT_KEY`
      JOIN `CUSTOMER` AS `c`
        ON `c`.`CUSTOMER_KEY` = `pu`.`CUSTOMER_KEY`
  ) AS `t`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question