how to group by column a but order by colum b in postgresql - sql

I trying to group one column and order by another column
my data is
carts
id product_id created_date group_id
1. 34 17:00:01 1
2. 35 17:00:02 1
3. 36 17:00:03 2
4. 37 17:00:04 1
5. 38 17:00:05 2
and i expect result like this
id product_id created_date group_id
1. 34 17:00:01 1
2. 35 17:00:02 1
4. 37 17:00:04 1
3. 36 17:00:03 2
5. 38 17:00:05 2
my sql that i write is not working
select group_id, product_id, created_date from carts group by group_id, product_id, created_date order by created_date

You need to order by 2 columns - first the group, then the creation date:
SELECT id, product_id, created_date, group_id
FROM table
ORDER BY group_id, created_date

Related

SUM a column in SQL, based on DISTINCT values in another column, GROUP BY a third column

I'd appreciate some help on the following SQL problem:
I have a table of 3 columns:
ID Group Value
1 1 5
1 1 5
1 2 10
1 2 10
1 3 20
2 1 5
2 1 5
2 1 5
2 2 10
2 2 10
3 1 5
3 2 10
3 2 10
3 2 10
3 4 50
I need to group by ID, and I would like to SUM the values based on DISTINCT values in Group. So the value for a group is only accounted for once even though it may appear multiple for times for a particular ID.
So for IDs 1, 2 and 3, it should return 35, 15 and 65, respectively.
ID SUM
1 35
2 15
3 65
Note that each Group doesn't necessarily have a unique value
Thanks
the CTE will remove all duplicates, so if there a sdiffrenet values for ID and Group, it will be counted.
The next SELECT wil "GROUP By" ID
For Pstgres you would get
WITH CTE as
(SELECT DISTINCT "ID", "Group", "Value" FROM tablA
)
SELECT "ID", SUM("Value") FROM CTE GROUP BY "ID"
ORDER BY "ID"
ID | sum
-: | --:
1 | 35
2 | 15
3 | 65
db<>fiddle here
Given what we know at the moment this is what I'm thinking...
The CTE/Inline view eliminate duplicates before the sum occurs.
WITH CTE AS (SELECT DISTINCT ID, Group, Value FROM TableName)
SELECT ID, Sum(Value)
FROM CTE
GROUP BY ID
or
SELECT ID, Sum(Value)
FROM (SELECT DISTINCT * FROM TableName) CTE
GROUP BY ID

Get earliest value from a column with other aggregated columns in postgresql

I have a very simple stock ledger dataset.
1. date_and_time store_id product_id batch opening_qty closing_qty inward_qty outward_qty
2. 01-10-2021 14:20:00 56 a 1 5 1 0 4
3. 01-10-2021 04:20:00 56 a 1 8 5 0 3
4. 02-10-2021 15:30:00 56 a 1 9 2 1 8
5. 03-10-2021 08:40:00 56 a 2 2 6 4 0
6. 04-10-2021 06:50:00 56 a 2 8 4 0 4
Output I want:
select date, store_id,product_id, batch, first(opening_qty),last(closing_qty), sum(inward_qty),sum(outward_qty)
e.g.
1. date store_id product_id batch opening_qty closing_qty inward_qty outward_qty
2. 01-10-2021 56 a 1 8 1 0 7
I am writing a query using First_value window function and tried several others but not able to get the out put I want.
select
date,store_id,product_id,batch,
FIRST_VALUE(opening_total_qty)
OVER(
partition by date,store_id,product_id,batch
ORDER BY created_at
) as opening__qty,
sum(inward_qty) as inward_qty,sum(outward_qty) as outward_qty
from table
group by 1,2,3,4,opening_total_qty
Help please.
As your expected result is one row per group of rows with the same date, you need aggregates rather than window functions which provide as many rows as the ones filtered by the WHERE clause. You can try this :
SELECT date_trunc('day', date),store_id,product_id,batch
, (array_agg(opening_qty ORDER BY datetime ASC))[1] as opening__qty
, (array_agg(closing_qty ORDER BY datetime DESC))[1] as closing_qty
, sum(inward_qty) as inward_qty
, sum(outward_qty ) as outward_qty
FROM table
GROUP BY 1,2,3,4
see the test result in dbfidle.

How to select row based on max column and latest in postgresql

Here is my table in PostgreSQL:
id
name
description
account_id
total_sales
create_at
1
Playstation 4
Console Game
1
21
2021-03-26
2
Playstation 2
Console Game
1
21
2021-03-27
3
Playstation 3
Console Game
1
20
2021-03-27
I would like to select row based on max(total_sales). If there are two rows with same total_sales, it will select the latest.
So, the result should be like this:
id
name
description
account_id
total_sales
create_at
2
Playstation 2
Console Game
1
21
2021-03-27
If there is only one row with max(total_sales) and there is no same value, it will return this row as the result.
select *
from your_table
order by toal_sales desc, created_at desc
limit 1

How to get rank of a user from all users

I have table called summary_coins , By ranking of coins I am trying to get an user ranking
I have tried like below
SELECT
user_id,
sum(get_count),
rank() over (order by sum(get_count) asc) as rank
FROM summary_coins
WHERE user_id = 2
GROUP BY user_id
sample data , without user_id = 2 in where I am getting below list
user_id sum rank
44 2 1
13 4 2
57 4 2
47 4 2
11 5 5
2 5 5
My desire out put :
2 5 5
Here I am always getting ranking 1 for user ID 2 , But from list of user it should be rank 5.
You want to apply WHERE user_id = 2 late. RANK OVER is the last thing to happen in your query, but you want to apply the WHERE clause afterwards. In order to do this make your query a subquery you select from:
SELECT user_id, sum_count, rank
FROM
(
SELECT
user_id,
sum(get_count) AS sum_count,
rank() over (order by sum(get_count) asc) as rank
FROM summary_coins
GROUP BY user_id
) all_users
WHERE user_id = 2;

Sum of Distinct count in Oracle

I have the following table:
CUST_PRODUCT_DTL
Cust_ID Product_ID QTY
1 10 5
2 10 2
3 10 5
1 11 5
2 12 1
How can I get Total Distinct CUST_ID, TOTAL DISTINCT PRODUCT_ID from the above table in Oracle 11 G
The below one doesn't work
SELECT SUM(COUNT(DISTINCT cust_id)), product_id
FROM CUST_PRODUCT_DTL
WHERE
GROUP BY product_id , cust_id
The desired result I am looking at is
Total Unique Cust_id: 3
Total Unique Product_id:3
sum is not involved, nor do you need the group by. Your desired output contains only one row. You just want two count distincts:
select count(distinct cust_id) as total_distinct_cust_id,
count(distinct product_id) as total_distinct_prod_id
from cust_product_dtl