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

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

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

SQL Sorted Count

I have the following table sorted by date:
date
id
9/1/20
1
9/1/20
2
9/3/20
1
9/4/20
3
9/4/20
2
9/6/20
1
I'd like to add a count column for each id so that the first count for each id is the earliest date and latest date would receive the highest count for each id:
date
id
count
9/1/20
1
1
9/1/20
2
1
9/3/20
1
2
9/4/20
3
1
9/4/20
2
2
9/6/20
1
3
How can I structure my Postgresql query to assemble this count column?
This looks like row_number():
select t.*,
row_number() over (partition by id order by date) as seqnum
from t
order by date, id;

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

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

How to get minimum date by group id per client id

I am trying to Select Group ID, and minimum dates per clients ID.
This is sample data set.
ClientID GroupID DataDate
1 9 2016-05-01
2 8 2015-04-01
3 7 2016-07-05
1 6 2015-01-05
1 5 2014-11-12
2 4 2016-11-02
1 3 2013-02-14
2 2 2011-04-01
I wrote
SELECT
clientID, MIN(DataDate)
FROM sampleTable
GROUP BY clientID
But in this query, I do not have GroupID selected. I need to include GroupID to join another table.
If I do:
Select
ClientID, GroupID, MIN(DataDate)
FROM sampleTable
GROUP BY ClientID, GroupID
It won't really get minimum dates per client.
Could you help me. How I should do this?
You can use ROW_NUMBER instead:
SELECT
ClientID, GroupID, DataDate
FROM (
SELECT *,
rn = ROW_NUMBER() OVER(PARTITION BY ClientID ORDER BY DataDate)
FROM SampleData
) t
WHERE rn = 1
If you want to include ties, use RANK instead of ROW_NUMBER.
I hope i understood your question correctly .
You want to display min dates for each client id's
If my table has data like this:
CID GID D1
1 9 03-06-2016
1 6 01-06-2017
1 5 01-06-2015
1 3 01-06-2014
2 4 01-06-2017
2 8 01-06-2014
3 5 03-06-2016
2 4 01-06-2011
Output :
CID GID D1
1 3 01-06-2014
2 4 01-06-2011
3 5 03-06-2016
This is what i think you can go with .
select cx.cid,cx.gid, cx.d1 from cli cx where cx.d1=(select min(c1.d1) from cli c1 where c1.cid=cx.cid)
group by cx.cid,cx.gid,cx.d1
order by cx.gid
Hope it helps.

Select Query to Get Unique Cells in Two Columns

I have an SQL Server database, that logs weather device sensor data.
The table looks like this:
Id DeviceId SensorId Value
1 1 1 42
2 1 1 3
3 1 2 30
4 2 2 0
5 2 1 1
6 3 1 26
7 3 1 23
8 3 2 1
In return the query should return the following:
Id DeviceId SensorId Value
2 1 1 3
3 1 2 30
4 2 2 0
5 2 1 1
7 3 1 23
8 3 2 1
For each device the sensor should be unique. i.e. Values in Columns DeviceId and SensorId should be unique (row-wise).
Apologies if I'm not clear enough.
If you don't want to sum Value as your desired result suggest, so you just want to take an "arbitrary" row of each "DeviceId + SensorId"-group:
WITH CTE AS
(
SELECT Id, DeviceId, SensorId, Value,
RN = ROW_NUMBER() OVER (PARTITION BY DeviceId, SensorId ORDER BY ID DESC)
FROM dbo.TableName
)
SELECT Id, DeviceId, SensorId, Value
FROM CTE
WHERE RN = 1
ORDER BY ID
This returns the row with the highest ID per group. You need to change ORDER BY ID DESC if you want a different result. Demo: http://sqlfiddle.com/#!6/8e31b/2/0 (your result)