select the row with max values grouped by other rows - sql

I have a table like this
Name
Project Name
Version deployed
Deployment date
John
Car
1.9.8
2022-09-23
John
Car
2.2.4
2022-10-15
John
Car
2.2.5-beta3
2022-10-14
John
Plane
4.9.345
2020-03-12
John
Plane
6.7.89
2022-05-05
Jack
Plane
6.7.89
2022-05-05
Jack
Plane
6.5
2022-05-07
Jack
Dog
4.6.6
2022-08-23
Jack
Plane
6.7.89
2022-05-05
...
And I would like to have a SQL query that selects ONE line grouped by Name, Project Name and that contains the last deployed version with its deployment date (and possibly other columns not displayed here) and in the example outputs then
Name
Project Name
Version deployed
Deployment date
John
Car
2.2.4
2022-10-15
John
Plane
6.7.89
2022-05-05
Jack
Plane
6.5
2022-05-07
Jack
Dog
4.6.6
2022-08-23
...
What I tried already different answers from this website, but the ones I found either group on one element or do not provide the other columns.
EDIT:
The Question that this has been marked as duplicate of, asks exactly what I mentioned last: when grouping on one column.
The answers there do not explain how to extend themselves to grouping by multiple columns.
The answer provided by #YuriLevinsky in the comments here, on the other hand, solves exactly what I wanted.

Related

Get name(s) from JSON format column, that not in 2 other columns with names

I need to create column with name(s) (Supervisors - can be multiple supervisors at the same time, but also there might not be supervisor at all) from JSON format column, that not in 2 other column with names (Employee and Client).
Id
Employee
Client
AllParticipants
1
Justin Bieber
Ariana Grande
[{"ParticipantName":"Justin Bieber"},{"ParticipantName":"Ariana Grande"}]
2
Lionel Messi
Christiano Ronaldo
[{"ParticipantName":"Christiano Ronaldo"},{"ParticipantName":"Lionel Messi"}]
3
Nicolas Cage
Robert De Niro
[{"ParticipantName":"Robert De Niro"},{"ParticipantName":"Nicolas Cage"},{"ParticipantName":"Brad Pitt"}]
4
Harry Potter
Ron Weasley
[{"ParticipantName":"Ron Weasley"},{"ParticipantName":"Albus Dumbldor"},{"ParticipantName":"Harry Potter"},{"ParticipantName":"Lord Voldemort"}]
5
Tom Holland
Henry Cavill
[{"ParticipantName":"Henry Cavill"},{"ParticipantName":"Tom Holland"}]
6
Spider Man
Venom
[{"ParticipantName":"Venom"},{"ParticipantName":"Iron Man"},{"ParticipantName":"Superman"},{"ParticipantName":"Spider Man"}]
7
Andrew Garfield
Leonardo DiCaprio
[{"ParticipantName":"Tom Cruise"},{"ParticipantName":"Andrew Garfield"},{"ParticipantName":"Leonardo DiCaprio"}]
8
Dwayne Johnson
Jennifer Lawrence
[{"ParticipantName":"Jennifer Lawrence"},{"ParticipantName":"Dwayne Johnson"}]
The output column I need:
Supervisors
NULL
NULL
Brad Pitt
Albus Dumbldor, Lord Voldemort
NULL
Iron Man, Superman
Tom Cruise
NULL
I've tried to create extra columns to use Case expression after that, but it seems too complex.
SELECT *,
JSON_VALUE(w.AllParticipants,'$[0].ParticipantName') AS ParticipantName1,
JSON_VALUE(w.AllParticipants,'$[1].ParticipantName') AS ParticipantName2,
JSON_VALUE(w.AllParticipants,'$[2].ParticipantName') AS ParticipantName3,
JSON_VALUE(w.AllParticipants,'$[3].ParticipantName') AS ParticipantName4
FROM Work AS w
I'm wondering if there is an easy way to compare values and extract only unique ones.

How to continue a sequence when inserting

I have tried to simplify my question with the following example:
I have a table with the following data:
Marker Name Location
1 Eric Benson Mixed
2 John Smith Rural
3 A David Rural
4 B John Mixed
And i want to insert into the table:
Name Location
Andy Jones Mixed
Ian Davies Rural
How can i continue the sequencein the Marker column to end up with:
Marker Name Location
1 Eric Benson Mixed
2 John Smith Rural
3 A David Rural
4 B John Mixed
5 Andy Jones Mixed
6 Ian Davies Rural
If you make this with a Stored Procedure you can ask the max of the Marker before to insert.
(That only works if the Marker Column is not identity)
Like This:
declare #max_marker int
set #max_marker=isnull((select max(marker) from table),0)
--Insert comes here
Insert into table (Marker,Name,Location) Values(#max_marker+1,'Andy Jones','Mixed')

appropriate method for text match in one column to other column in oracle

I have to write a query in Oracle. I have a table called 'Entity' with 2 columns 'Pref_mail_name' and 'spouse_name'.
Now i want list of all spouse_name where the last name of the spouse_name is not populated from pref_mail_name.
For example my table has following data
Pref_mail_name spouse_name
Kunio Tanaka | Lorraine
Mrs. Betty H. Williams | Chester Williams
Mr. John Baranger | Mrs. Cathy Baranger
William kane Gallio | Karen F. Gallio
Sangon Kim | Jungja
i need output as 1st and 5th row only. I did some analysis and came up with oracle built in function
SELECT PREF_MAIL_NAME, SPOUSE_NAME, UTL_MATCH.JARO_WINKLER_SIMILARITY(a, b)
similarity from entity
order by similarity;
But above query is not looking genuine.Even though spouse last name is not populated from pref_mail_name its giving a value above 80 for similarity.

Issue with "MembersWithData" property of Parent-Child hierarchy

Assume Jack & Jill are two managers. Jack has one reportee called James and Jill has none. So when we establish Parent-Child relationship, Jack & Jill will be at Level 1 and James will be at Level 2. In our project all the 3 people make sales. Hence a manager like Jack will have his group sales and his own sales.
To represent it, i have set the property "MembersWithData = NonLeafDataVisible" for parent attribute. When I browse the cube the data is represented something like this
Jack --- $100
Jack (Direct)------$60
James ------$40
Jill ---- $200
We can clearly see that Jill doesn't have any children unlike Jack. It seems SSAS adding a member (in our case "Jack (Direct)") as its own children only when it really has some children.
What should I have to do if I want to have "Jill (Direct)" under Jill, something like following.
Jack --- $100
Jack (Direct)------$60
James ------$40
Jill ---- $200
Jill (Direct)------$200

Want unique records - what is the SQL?

I have this table
User | File | SubmittedDate
Joe 1223.txt 2011-11-12
Joe 3321.txt 2011-11-13
Jack 4332.txt 2012-11-22
Jane 2344.txt 2012-11-10
I want to select so I only get one record of Joe's, one of Jack's, and one of Jane's.
e.g.
Joe 1223.txt 2011-11-12
Jack 4332.txt 2012-11-22
Jane 2344.txt 2012-11-10
In other words, I want a result set of rows that has a unique user field. What's the SQL to get this?
A very quick Google search would show you there are several possible options:
https://www.google.com/search?client=safari&rls=en&q=select+unique+values+from+sql&ie=UTF-8&oe=UTF-8
One of which is to use the DISTINCT keyword:
SELECT DISTINCT User FROM ....