SQL - select max of duplicated items with different value - postgresql

I have a situation like this:
name, age
Tom, 30
Tom, 30
Sam, 35
Sam, 34
...
I would like to remove duplicated id, but if their value (in this case age has different values) I would like to keep the max. So my output should be like this:
name, age
Tom, 30
Sam, 35
Obviously, SELECT DISTINCT won't work.
Any suggestion, on how to handle this situation?
Thank you!

You want to aggregate the data with the same name and, in particular, you are searching for the max, then you can use the corrispondent aggregation function
select name, max(age) from table group by 1

Related

Changing a functional qSQL query to involve multiple columns in calculation KDB+/Q

I have a ? exec query like so:
t:([]Quantity: 1 2 3;Price 4 5 6;date:2020.01.01 2020.01.02 2020.01.03);
?[t;enlist(within;`date;(2020.01.01,2020.01.02));0b;(enlist `Quantity)!enlist (sum;(`Quantity))]
to get me the sum of the Quantity in the given date range. I want to adjust this to get me the sum of the Notional in the date range; Quantity*Price. So the result should be (1x4)+(2x5)=14.
I tried things like the following
?[t;enlist(within;`date;(2020.01.01,2020.01.02));0b;(enlist `Quantity)!enlist (sum;(`Price*`Quantity))]
but couldn't get it to work. Any advice would be greatly appreciated!
I would advise in such a scenario to think about the qSql style query that you are looking for and then work from there.
So in this case you are looking, I believe, to do something like:
select sum Quantity*Price from t where date within 2020.01.01 2020.01.02
You can then run parse on this to break it into its function form i.e the ? exec query you refer to.
q)parse"select sum Quantity*Price from t where date within 2020.01.01 2020.01.02"
?
`t
,,(within;`date;2020.01.01 2020.01.02)
0b
(,`Quantity)!,(sum;(*;`Quantity;`Price))
This is your functional form that you need; table, where clause, by and aggregation.
You can see your quantity here is just the sum of the multiplication of the two columns.
q)?[t;enlist(within;`date;(2020.01.01;2020.01.02));0b;enlist[`Quantity]!enlist(sum;(*;`Quantity;`Price))]
Quantity
--------
14
You could also extend this to change the column as necessary and create a function for it too, if you so wish:
q)calcNtnl:{[sd;ed] ?[t;enlist(within;`date;(sd;ed));0b;enlist[`Quantity]!enlist(sum;(*;`Quantity;`Price))]}
q)calcNtnl[2020.01.01;2020.01.02]
Quantity
--------
14

How to select distinct combinations in T-SQL

I'm using SQL in Devexpress dashboard designer. I want to select distinct combinations of two parameters.
Perhaps Devexpress uses Transact-SQL but at the same time GROUP BY clause never works for me.
At the same time DISTINCT BY somehowe doesn't work as well.
Example:
There are two IDs 11 and 22
And there are two values of Date for 11, as an example: 21.01.2000 and 22.01.2000. And there's one for 22 as an example: 23.05.2008
Problem here is that I can't coose DISTINCT by date because there are many other IDs which have the same dates.
So I expect to have one distinct combination of ID and Date.
Does anyone faced with the same problem, can you advice any solution / code example?
Using select distinct will filter duplicates if you leave unique row properties out of the selected fields.
so:
Mike Smit
Mike Smit
Will be reduced to
Mike Smit
But if you're also asking for a PK like a Id field you get the following because id makes both rows distinct
1 Mike Smit
2 Mike Smit
Does this help?

Tableau: Distinct count of a field which occurs more than once

I have a field customer_id and I need to track the number of unique users and repeat users. For example the table is as below:
customer_id
11
22
33
11
44
22
Here, the no. of unique users is 4 (11,22,33,44) and number of repeat users are 2 (11,22).
I am calculating unique users as COUNTD([customer_id]).
How can I calculate repeat users? It is basically the distinct count of the values which appear more than once. I tried with the following expression:
COUNTD(IF COUNT([customer_id]) > 1
THEN [customer_id]
END)
but I'm getting an error: Cannot mix aggregate and non-aggregate arguments comparisons or results in IF expressions
How else can I calculate the repeat users?
Thanks in advance.
According to your filter needs, you can rely on LOD using FIXED/INCLUDE:
{ FIXED [Customer Id] : if sum({ FIXED [Customer Id] : COUNT([Customer Id])}) > 1 then 1 end }
Basically, in the inner LOD you count the occourrences, and then you just take in consideration records having 2+ (>1) of them:
A simple alternative to Fabio's answer can also do the job. Just create a calculated field
COUNT([customer id]) >1
and add this to filter shelf.
You can filter out false candidates to remove unique users and taking returning customers only.

Find closest number to one number in postgresql

I have a database like this:
people
id name zip
1 bill 84058
2 susan 90001
3 john 64354
Say I have an input number of 65432
I want to write a query something like this:
SELECT * FROM people WHERE zip CLOSEST TO 65432 LIMIT 1
And get john as the row returned.
I can't find what the closest to command is in Postgresql
You could use ABS function:
SELECT *
FROM people
ORDER BY ABS(65432 - zip) ASC LIMIT 1

JPA Select only one of each value in one column

I have a list like:
Name Age
Charles 18
Anna 20
Anna 19
Tomas 44
Karla 13
Charles 88
I would write a JPQL statement that give me:
Charles 18
Anna 20
Tomas 44
Karla 13
In other words, how can I get a list with unique names where the age dont care?
Best regards
Carl
If you really don't care about the age, don't select it:
select distinct u.name from User u
If you'd like to get a valid age with each user, but don't care which one, select the min or max of the ages:
select u.name, max(u.age) from User u group by u.name