Using columns as references - sql

everyone.
My question is, if I'm using a column with SUM or COUNT in my SQL, for exemple:
Select ITEM, sum(PRICE), count(ITEM), (<column2>/<column3>)... from TABLE..
Is there a way to use the columns as references?

You need to use a subquery or CTE:
WITH t as (
Select ITEM, sum(PRICE) as price, count(ITEM) as cnt
from TABLE..
)
SELECT item, price, cnt, price / cnt
FROM t;
You can also repeat the expressions. Or, in your particular case, just use AVG():
Select ITEM, SUM(PRICE) as price, count(ITEM) as cnt, AVG(PRICE)
from TABLE..

Related

How do I use COUNT function on another aggregate function in SQL?

I would like to count the aggregate function, for eg:
SELECT customer_id, SUM(amount)
FROM payment
GROUP BY customer_id
HAVING SUM(amount)>100;
So, how do I use COUNT() on SUM() to count the filtered SUM()?
You wrap it in an outer query.
select count(*) from (
SELECT customer_id, SUM(amount)
FROM payment
GROUP BY customer_id
HAVING SUM(amount)>100
) big_spenders
If you want to count the number of customers for each amount that you get from your query, you need a 2nd level of aggregation:
SELECT amount, COUNT(*) counter
FROM (
SELECT customer_id, SUM(amount) amount
FROM payment
GROUP BY customer_id
HAVING SUM(amount)>100
) t
GROUP BY amount;
Or, with COUNT() window function:
SELECT DISTINCT SUM(amount) amount,
COUNT(*) OVER (PARTITION BY SUM(amount)) counter
FROM payment
GROUP BY customer_id
HAVING SUM(amount)>100;
See a simplified demo.

Mysql query to PostgreSQL query

select storeID, itemID, custID, sum(price)
from Sales F
group by storeID, custID, itemID
with cube(storeID, custID);
I am going to use this query in postgreSQL but it doesn't work in postgreSQL directly
how can I convert this query into postgreSQL query?
Your code will work without the with keyword:
select storeID, itemID, custID, sum(price)
from Sales F
group by itemID, cube(storeID, custID);
I prefer grouping sets for expressing groupings:
select storeID, itemID, custID, sum(price)
from Sales F
group by grouping sets ( (storeID, custID, itemID),
(custID, itemID),
(storeID, itemID),
(itemID)
);
If I understand what you want to do, this should be exactly the same.
You could also use cube and then filter:
select storeID, itemID, custID, sum(price)
from Sales F
group by cube(storeID, custID, itemID)
having itemId is not null
Try this:
select storeID, itemID, custID, sum(price)
from Sales F
group by cube (storeID, itemID, custID)
Check this post for more details on CUBE,GROUPING SETS and ROLLUP.

Count Function after query in postgres sql

I have written some query and I wanted to know the number of rows or total rows count. Is there a "count" function that can be applied on top of the query?
My query is like:
select customer_id, sum(amount)
from payment
group by customer_id having sum(amount)>100;
You could use count() as a window function, like in
SELECT *,
count(*) OVER () AS total_count
FROM (SELECT customer_id,
sum(amount)
FROM payment
GROUP BY customer_id HAVING sum(amount)>100) AS q;
That will return the total count in an additional column.
You can use a subquery or CTE:
select count(*)
from (select customer_id, sum(amount)
from payment
group by customer_id
having sum(amount)>100
) c;

Combine two SQL statements with sum

Two statements need to combine.
select INVOICEAMOUNT, itemid
from MTS_NONPAYMENT
select SUM(AMOUNT) AS SUM, ITEMID
from CUS_GLACCOUNT
Common column itemid. Each time I try and join, it fails. What am I doing wrong?
Fundamentally, you appear to be missing group by. I suspect the following does what you want:
select itemid, sum(invoiceamount) as invoiceamount, sum(sum) as sum
from ((select itemid, sum(INVOICEAMOUNT) as invoiceamount, 0 as sum
from MTS_NONPAYMENT
group by itemid
) union all
(select itemid, 0, SUM(AMOUNT)
from CUS_GLACCOUNT
group by itemid
)
) x
group by itemid;
To get unequal values, use:
having sum(invoiceamount) <> sum(sum)
at the end of the query.

SQL query for table with multiple keys?

I am sorry if this seems too easy but I was asked this question and I couldn't answer even after preparing SQL thoroughly :(. Can someone answer this?
There's a table - Seller id, product id, warehouse id, quantity of products at each warehouse for each product as per each seller.
We have to list the Product Ids with Seller Id who has highest number of products for that product and the total number of units he has for that product.
I think I got confused because there were 3 keys in the table.
It's not quite clear which DBMS you are using currently. The below should work if your DBMS support window functions.
You can find count of rows for each product and seller, rank each seller within each product using window function rank and then use filter to get only top ranked sellers in each product along with count of units.
select
product_id,
seller_id,
no_of_products
from (
select
product_id,
seller_id,
count(*) no_of_products,
rank() over (partition by product_id order by count(*) desc) rnk
from your_table
group by
product_id,
seller_id
) t where rnk = 1;
If window functions are not supported, you can use correlated query to achieve the same effect:
select
product_id,
seller_id,
count(*) no_of_products
from your_table a
group by
product_id,
seller_id
having count(*) = (
select max(cnt)
from (
select count(*) cnt
from your_table b
where b.product_id = a.product_id
group by seller_id
) t
);
Don't know why having id columns would mess you up... group by the right columns, sum up the totals and just return the first row:
select *
from (
select sellerid, productid, sum(quantity) as total_sold
from theres_a_table
group by sellerid, productid
) x
order by total_sold desc
fetch first 1 row only
If I do not think about optimization, straight forward answer is like this
select *
from
(
select seller_id, product_id, sum(product_qty) as seller_prod_qty
from your_table
group by seller_id, product_id
) spqo
inner join
(
select product_id, max(seller_prod_qty) as max_prod_qty
from
(
select seller_id, product_id, sum(product_qty) as seller_prod_qty
from your_table
group by seller_id, product_id
) spqi
group by product_id
) pmaxq
on spqo.product_id = pmaxq.product_id
and spqo.seller_prod_qty = pmaxq.max_prod_qty
both spqi (inner) and sqpo (outer) give you seller, product, sum of quantity across warehouses. pmaxq gives you max of each product again across warehouses, and then final inner join picks up sum of quantities if seller has highest (max) of the product (could be multiple sellers with the same quantity). I think this is the answer you are looking for. However, I'm sure query can be improved, since what I'm posting is the "conceptual" one :)