JPA Select only one of each value in one column - jpa

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

Related

limit results based upon 1 column

I'm a SQL newbie, any help is greatly appreciated! The id is the ID of a person that is in a program, and STI is the step ID that correlates to the ID of the step they are currently on or have completed. What I'm having a hard time figuring out is how to limit the results to only show unique Person ID's. I can't get DISTINCT to work at all.
Here is the query:
SELECT
s.[PersonAliasId] AS [id]
, s.[StepTypeId] AS [sti]
FROM
[Step] s
ORDER BY s.[PersonAliasId]
The results from the above query are:
id sti
11126 19
47331 19
66693 7
68110 19
74838 7
89867 1
89867 2
110105 19
122059 19
122059 21
130273 7
139876 19
150180 19
161929 7
165926 19
169329 19
171922 19
There are multiple steps that we are tracking for each person. When they have completed one step and then moved to another, they show in this query twice. For example, person 122059 has completed step id 19 and are currently on step id 21. I don't really care about the multiple step numbers showing, I really only need the person ID to return once. Can anyone help me figure out what I'm doing wrong?
From my understanding of your question. You only care about unique id. So you could try to use the MAX() function to get the max step for each id.
select
distinct(s.PersonAliasId) AS id,
max(s.StepTypeId) AS sti
FROM Step s
GROUP BY s.PersonAliasId
ORDER BY s.PersonAliasId
db fiddle link
Let me know if I misunderstood anything

SQL - select max of duplicated items with different value

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

How do I remove row with duplicate value in kdb?

I have a table of data in kdb and I would like to use q to remove the rows which contain a duplicate value in one column.
For example, if I have the following table where there is a duplicate value in the Age column:
Name Age Degree
---------------------
Alice 26 Science
Bob 34 Arts
Carrie 26 Engineering
How would I delete the third row so I end up with the following:
Name Age Degree
---------------------
Alice 26 Science
Bob 34 Arts
Thanks!
You could do
select from t where i=(first;i)fby Age
You can delete any of the duplicates in any columns using this:
q)delete from t where ({not x in 1#x};i) fby Age
Name Age Degree
-----------------
Alice 26 Science
Bob 34 Arts
Could also be solved using a by clause instead of fby, but in this case to get the first occurrence of each age you have to use reverse
q)0!select by Age from reverse t
Age Name Degree
-----------------
26 Alice Science
34 Bob Arts

DB2 9 Fundamentals

Given the following two tables:
NAMES
NAME NUMBER
---------- -------
Wayne Gretzky 99
Jaromir Jagr 68
Bobby Orr 4
Bobby Hull 23
Mario Lemieux 66
POINTS
-----------------------------
NAME POINTS
---------- ------
Wayne Gretzky 244
Bobby Orr 129
Brett Hull 121
Mario Lemieux 189
Joe Sakic 94
How many rows would be returned using the following statement?
SELECT name FROM names, points
Can someone explain why the answer is 25?
Thanks in advance for any help provided
I guess this instruction is equivalent to a cross join in standard SQL. Hence the number of records returned is 5 records in names * 5 records in points = 25.
Also known as the "Cartesian Product"
"The Cartesian product, also referred to as a cross-join, returns all the rows in all the tables listed in the query. Each row in the first table is paired with all the rows in the second table. This happens when there is no relationship defined between the two tables."
from:
http://www.dba-oracle.com/t_garmany_9_sql_cross_join.htm

Grouping the Details in Crystal Reports

I am not much familiar with Crystal Reports. I facing one issue regarding the Detail fields in Crystal report.
Lets assume I have 5 fields in Details band.
Field a, b, c, d, e and the record set for the below fields will be as below
A B C D E
111 1 20 30 222
111 1 21 31 222
111 1 22 32 222
111 2 11 11 222
111 2 12 12 222
111 2 13 13 222
Now I need to display the details by grouping the field B and get the sum for C and D. And at the end my result should be as below
A B C D E
111 1 20 30 222
21 31
22 32
------- ------
Total 63 93
111 2 11 11 222
12 12
13 13
------- ------
Total 36 36
Can some one help me how do I do this kind of Stuff.
You should use the group option (Insert --> Group) and select field B.
Then, use running total to sum the wanted fields. (View --> Field Explorer, then, on the field explorer right-click on 'Running Total Fields' --> New). Create the wanted running total using this expert and then drag into your report in order to show it.
Insert text boxes as needed for the caption of the group header and "Total".
Urik is correct !
I just want to add some points as par the output user1464855 want.
Add groups for two columns field A , field B . add these fields in the detail section also add the C, D and E in
the section.
To display Total you can use either of the following ways
a. you can add new "Running Total field" for Columns C and Column D as Urik said.
b. you can right click on the field "C" and select "Insert --> Summary" then select the field "C" select the "sum"
and select the summary location "Group # 2" in "Insert Summary" dialoge box and click "OK".
To avoid displaying the duplicate values (repeatation of values) select the field on the report e.g. field "E"
right click on it Select "Format field" select "Common" Tab select check box "SUppress of Duplicated".
you can repetae this step for other fields A, B.
I hope this will help you. kindly correct me if I am wrong !