Count Function after query in postgres sql - 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;

Related

Grouping in SQL using CASE Statements

Hello I am trying to group multiple customer orders into buckets in SQL, the output should look something like it does below. Do I have to use a case statement to group them?
Table1 looks like:
CustomerID
Order_date
1
somedate
2
somedate
3
somedate
2
somedate
Edit: # of customers meaning if CustomerID 2 had 2 orders he/she would be of the in the bucket of #of orders of 2.
Output should be something like this?
# of Customers
# of Orders
2
1
1
2
My code so far is:
select count(*) CustomerID
FROM Table1
GROUP BY CustomerID;
Use a double aggregation:
SELECT COUNT(*) AS num_customers, cnt AS num_orders
FROM
(
SELECT CustomerID, COUNT(*) AS cnt
FROM Table1
GROUP BY CustomerID
) t
GROUP BY cnt;
The inner subquery finds the number of orders for each customer. The outer query then aggregates by number of orders and finds out the number of customers having each number of orders.
If you want to sort your tables and your users depending on the number of orders they made, this query should work:
SELECT CustomerID, COUNT(CustomerID) as NbOrder
FROM Table1
GROUP BY(NbOrder)
I believe what you want to do is get the count of orders by customer, first, via aggregation. Then get the count of customers by order count from that query.
SELECT count(*) as count_of_customers, count_of_orders
FROM
(
SELECT customerid, count(*) as count_of_orders
FROM your_table
GROUP BY customerid
) sub
GROUP BY count_of_orders
ORDER BY count_of_orders

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.

Oracle SQL Count distinct values in a certain column

I am trying to query a table with a certain logic and I want to remove the records which have a count of 2 or more distinct values in PERSON_ID column. I cannot find an appropriate window query to achieve this. I already tried using:
SELECT
CUSTOMER_ID, PERSON_ID, CODE,
DENSE_RANK() OVER (PARTITION BY CUSTOMER_iD, PERSON_ID ORDER BY PERSON_ID ASC) AS NR
FROM TBL_1;
But I get the following result:
I want to achieve the result below, which counts the distinct values within PERSON_ID column based on a certain CUSTOMER_ID. In my case Customer "444333" would be a record which I want to remove because it has 2 distinct Person_Id's
here is what you need:
SELECT
customer_id, count(distinct PERSON_ID) distinct_person_count
FROM TBL_1
group by customer_id
and if you want to show it for eahc row , you can join it again with the table :
select * from TBL_1 t
join (
select customer_id, count(distinct PERSON_ID) distinct_person_count
from TBL_1
group by customer_id
) tt
on t.customer_id = tt.customer_id
note: you can't have distinct within window functions
If you want the distinct count on each row, then use a window function:
select t.*,
count(distinct person_id) over (partition by customer_id)
from t;
Oracle does support distinct in window functions.

Extracting all the ID's whose sum of values is greater than another value

i have an output table like this -
In here, i want to get all the ID's whose sum of amount is greater or equal to 40 & order by date. I tried using having condition but it isn't working. This is what i used -HAVING sum(a.amount) >= 40
What can i do to get this done?
Try using subquery:
select tablename.* from tablename
inner join
(select id, sum(amount) from tablename
group by id
having sum(amount)>=40)a
tablename.id=a.id
order by appl_date
You seems want window function :
select t.*
from (select t.*, sum(amount) over (partition by appl_date, valid_from) as totalamount
from table t
) t
where totalamount >= 40;
If I assume that "id" means application_id and that your table has multiple rows for a given application_id, then you can just use aggregation and having.
It is unclear what you mean by "order by date", but you can do:
select application_id, sum(amount)
from t
group by application_id
having sum(amount) >= 40
order by min(appl_date) asc;

Get the row with maximum nested table entries

I have a nested table in a object database. Now I want that row, that has the most rows in the nested table.
The problem is, I don't have a object database, but I have to learn some SQL statements for a exam. For now my query looks like this:
SELECT o.Order_Number
FROM Order o, TABLE(o.Position) pos
GROUP BY o.Order_Number
HAVING COUNT (*) >= all
(SELECT COUNT(*)
FROM pos);
With ANSI SQL for most DBs you can use a little trickery with the RANK() function
SELECT Order_Number, Order_Count
FROM (
SELECT Order_Number, COUNT(1) Order_Count,
RANK() OVER (ORDER BY COUNT(1) DESC) Ranking
FROM [Position]
GROUP BY Order_Number
) a
WHERE Ranking = 1