Postgresql 但它是由“罗宾汉”而不是“用户2561626”发布的。什么?!你和@RobinHood是在同一个班还是什么?这是一个正确的方式来编码这个问题吗#5@craig RingerNo,这没有任何意义。您选择的是ID号最高的产品,而不是最畅销的产品。它可能恰好适用
Postgresql 但它是由“罗宾汉”而不是“用户2561626”发布的。什么?!你和@RobinHood是在同一个班还是什么?这是一个正确的方式来编码这个问题吗#5@craig RingerNo,这没有任何意义。您选择的是ID号最高的产品,而不是最畅销的产品。它可能恰好适用,postgresql,Postgresql,但它是由“罗宾汉”而不是“用户2561626”发布的。什么?!你和@RobinHood是在同一个班还是什么?这是一个正确的方式来编码这个问题吗#5@craig RingerNo,这没有任何意义。您选择的是ID号最高的产品,而不是最畅销的产品。它可能恰好适用于示例数据,但它绝对不正确。是的!你说得对,我真是个傻瓜,没有评估,谢谢你@craig Ringer经过编辑的查询解决了问题。。无论@craig RingerNicely做了什么,它都会出错!我知道有一个简单的标准方法可以做到这一点-我完全忘记
但它是由“罗宾汉”而不是“用户2561626”发布的。什么?!你和@RobinHood是在同一个班还是什么?这是一个正确的方式来编码这个问题吗#5@craig RingerNo,这没有任何意义。您选择的是ID号最高的产品,而不是最畅销的产品。它可能恰好适用于示例数据,但它绝对不正确。是的!你说得对,我真是个傻瓜,没有评估,谢谢你@craig Ringer经过编辑的查询解决了问题。。无论@craig RingerNicely做了什么,它都会出错!我知道有一个简单的标准方法可以做到这一点-我完全忘记了你可以在
中按顺序引用聚合结果+1看起来不错:
--6.find most sold product with with sales_id, product_name,quantity and sum(price)
select array_agg(s.sale_id),p.product_name,s.quantity,sum(s.price)
from products p
join sales s
on p.product_id=s.product_id;
ERROR: column "p.product_name" must appear in the GROUP BY clause or be used in an aggregate function:
SELECT
array_agg(s.sale_id) AS sales_ids,
(SELECT p.product_name FROM products p WHERE p.product_id = pp.product_id) AS product_name,
sum(s.quantity) AS total_quantity,
sum(s.price) AS total_price
FROM
(
-- Find the product with the largest number of sales
-- If multiple products have the same sales an arbitrary candidate
-- is selected; extend the ORDER BY if you want to control which
-- one gets picked.
SELECT
s2.product_id, sum(s2.quantity) AS total_quantity
FROM sales s2
GROUP BY s2.product_id
ORDER BY 2 DESC
LIMIT 1
) AS pp
INNER JOIN sales s ON (pp.product_id = s.product_id)
GROUP BY s.product_id, pp.product_id;
--1.write the query find the products which are not soled
select *
from products
where product_id not in (select distinct PRODUCT_ID from sales );
--9. select product details with sales_id, product_name,quantity and price those product names are started with letter ‘s’
--This selects my product details
select s.sale_id,p.product_name,s.quantity,s.price
from products p,sales s
where p.product_id=s.product_id ;
--This is'nt working to find those names which start with s.. is there any other way to solve this..
select s.sale_id,p.product_name,s.quantity,s.price
from products p,sales s
where p.product_id=s.product_id and product_name = 's%';
select array_agg(s.sale_id),p.product_name,sum(s.quantity) as Quantity ,sum(s.price) as Total_Price
from sales s,products p
where s.product_id =p.product_id
group by p.product_id
order by sum(s.quantity) desc limit 1 ;
select s.sale_id,p.product_name,s.quantity,s.price
from products p,sales s
where p.product_id=s.product_id and p.product_name LIKE 'S%';
CREATE OR REPLACE FUNCTION get_details()
RETURNS TABLE(sale_id integer,product_name varchar,quantity integer,price int) AS
$BODY$
BEGIN
RETURN QUERY
select s.sale_id,p.product_name,s.quantity,s.price
from products p
join sales s
on p.product_id =s.product_id ;
Exception WHEN no_data_found then
RAISE NOTICE 'No data available';
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;