How to get unique result in mysql - sql

I have table:
name marks
aaa 100
aaa 56
aaa 120
bbb 56
bbb 60
The result should be:
aaa 120
bbb 60
ie unique name with maximum marks. Is there any query in mysql?

SELECT name, MAX(marks) marks
from table
group by name

Related

SQL left join without deleting data

I have a table (T_Individus) in Microsoft Access, and I would like to:
append and update this table from another table (T_importTable)
without overwriting existing data in T_Individus with empty data from T_imporTable (unlike the upsert solution suggested here):
T_Individus
dbid name Age
------------------------------
100 xyz 5
101 aaa 41
102 bbb 25
103 ccc
T_importTable
dbid name Age
------------------------------
101 aaa 42
102 bbb
103 ccc 52
104 ddd 27
Desired output for T_Individus:
correction of age for dbid 101
adding age for dbid 103
adding row for dbid 104
no modification for dbid 102
dbid name Age
------------------------------
100 xyz 5
101 aaa 42
102 bbb 25
103 ccc 52
104 ddd 27
Currently I use this code
UPDATE T_importTable LEFT JOIN T_Individus ON T_importTable.dbid = T_Individus.dbid
SET
T_Individus.name = [T_importTable.name],
T_Individus.Age = [T_importTable.Age];
But with this solution, the age of dbid 102 is deleted (as for the upset solution). Is there another function, or should we use an if condition?
Best,

SQL Server 2008 - Fill Same Value Based On The Name

I am working on a query and having a difficult time to figure out how to fill the same value based on one column. Let me explain what I am trying to accomplish....
Says, I have a table like this below with too columns: Name & Value. So, "Select Name, Value FROM Table1 Order By Name" will produce the following result.
Table1
Name Value
AAA 111
AAA
BBB 222
BBB
BBB
BBB
CCC 333
CCC
DDD 444
DDD
DDD
Now, What I am trying to accomplish is producing the result below with the "Select .... from Table1" query.
Table1
Name Value
AAA 111
AAA 111
BBB 222
BBB 222
BBB 222
BBB 222
CCC 333
CCC 333
DDD 444
DDD 444
DDD 444
Please help and provide the sample code if possible.
Thanks in advance
You can use MAX() as a window function:
SELECT Name, Value,
MAX(Value) OVER (PARTITION BY Name) as imputed_name
FROM Table1
ORDER BY Name;

DB2:how to get top

I have a table having data like
pin id name
3 33 jjj
2 22 bbb
1 111 aaaa
1 112 aa
1 113 aaa
4 44 kkk
I want to print rows of the table where if count(*) group by pin =1 (i.e single entry in table ) print the row
if count(*) group by pin >2 then print first two rows
so my out put should be
pin id name
3 33 jjj
2 22 bbb
1 111 aaaa
1 112 aa
4 44 kkk
Use row_number() OVER(partion by pin order by id) as rownum function . Where rownum <3
. As #Clockwork-Muse said, you need to define an order becase you need to say what do you want to see if there are more than 2 rows for a particular pin.
This will generate you desired output.

Access SQL - Select only the last sequence

I have a table with an ID and multiple informative columns. Sometimes however, I can have multiple data for an ID, so I added a column called "Sequence". Here is a shortened example:
ID Sequence Name Tel Date Amount
124 1 Bob 873-4356 2001-02-03 10
124 2 Bob 873-4356 2002-03-12 7
124 3 Bob 873-4351 2006-07-08 24
125 1 John 983-4568 2007-02-01 3
125 2 John 983-4568 2008-02-08 13
126 1 Eric 345-9845 2010-01-01 18
So, I would like to obtain only these lines:
124 3 Bob 873-4351 2006-07-08 24
125 2 John 983-4568 2008-02-08 13
126 1 Eric 345-9845 2010-01-01 18
Anyone could give me a hand on how I could build a SQL query to do this ?
Thanks !
You can calculate the maximum sequence using group by. Then you can use join to get only the maximum in the original data.
Assuming your table is called t:
select t.*
from t join
(select id, MAX(sequence) as maxs
from t
group by id
) tmax
on t.id = tmax.id and
t.sequence = tmax.maxs

MSAccess: Ranking rows based upon column criteria

I have a dataset that looks like this:
Account Cost Centre TransNo
aaa 111 43443
aaa 111 32112
aaa 111 43211
aaa 112 32232
aaa 113 56544
bbb 222 43222
bbb 222 98332
ccc 111 88778
I need a column added that is a counter of the number of rows that relate to that Account/Cost Centre combination:
Account Cost Centre TransNo rCounter
aaa 111 43443 1
aaa 111 32112 2
aaa 111 43211 3
aaa 112 32232 1
aaa 112 56544 2
bbb 222 43222 1
bbb 222 98332 2
ccc 111 88778 1
Is this possible to do in MSAccess using SQL? and how would I go about it (ie what would be the SQL script I would need to write)?
Thanks in advance.
Something like:
SELECT a.Account, a.[Cost Centre], a.TransNo, (SELECT Count(*)
FROM table4 b
WHERE b.Account=a.Account
AND b.[Cost Centre]=a.[Cost Centre]
AND b.TransNo<=a.TransNo) AS AccountNo
FROM Table4 AS a
ORDER BY a.Account, a.[Cost Centre], a.TransNo;