Get first row of each group in access2010 - sql

There are multiple way to get first row from each group, but none of my idea is working with access2010.
Do you have a solution to get first row in access2010 ?
Or
ID Name Age
1 Name1 3
2 Name2 4
3 Name1 2
4 Name2 5
It should get the top row in each group (name column) so the output would be
1 Name1 3
2 Name2 4

Here's a solution that still uses a sub query, but only once, instead of on each record.
SELECT T1.*
FROM mytable AS T1
WHERE T1.id IN (SELECT First(T2.id)
FROM mytable T2
GROUP BY T2.name)

select * from table t1 where ID in
(sel min(ID) from table where t1.name=name);

Related

Update table with different values from a different table which is grouped

I have a oracle table called table1 that has a foreign key from table2.
table2 has 2 columns: id, name
table2.name has a lot of duplicates that need to be sorted out, so I grouped table2 by name and kept only 1 id for each name.
However, table1.table2_id uses a lot of the duplicated fields.
How can I change all the table1.table2_id fields so that there are no duplicate names?
Currently:
table1:
id
blabla
table2_id
1
row
1001
2
row
1002
3
row
1003
4
row
1004
5
row
1004
6
row
1005
table2:
id
name
1001
Bob
1002
Bob
1003
Bob
1004
Jack
1005
Jack
Desired Result:
table1:
id
blabla
table2_id
1
row
1001
2
row
1001
3
row
1001
4
row
1004
5
row
1004
6
row
1004
table2:
id
name
1001
Bob
1004
Jack
So imo, I would need to:
update all table1.table2_id to top 1 table2.id grouped by name
delete duplicate rows from table2
And as there are thousands of duplicate fields in table2, I cannot use case for this..
The solution I am trying returns 1 id for each name, but I am failing at the part where I can update the table.
update table1 d
set d.table2_id =
(select
b.id
from table2 b
where b.name in (select min(id) keep(dense_rank first order by id) id
, name
from table2
group by name)
order by b.id ASC
)
where d.table2_id in ( SELECT b.id FROM table2 b WHERE b.name in (select min(id) keep(dense_rank first order by id) id
, name
from table2
group by name));
Please help :)
Here are syntactically correct Oracle statements
update table1 t1
set t1.table2_id = (
select new_id
from (
select id, min(id) over (partition by name) new_id
from table2
) d
where d.id = t1.table2_id
);
delete from table2
where id not in (
select min(id) from table2 group by name
);
The analytic function min(id) over (partition by name) is used here so that you can have all original ids together with their new ids (the min ids from the set where the name is the same).
here is one way:
update table1 d
set d.table2_id = x.id_tokeep
from
(
select name , id , min(id) over (partition by name ) as id_tokeep
from table2
) x
where x.id = d.table2_id
delete from table2
where id not in ( select min(id) from table2 group by name)

How to get Oracle to return unique results in a one to many relationship tables with a left join

I have a three tables
Table 1
Id Department
1 A
2 B
3 C
4 D
Table 2
Id DepartId Name
1 1 ABC
2 1 DEF
3 1 ASD
4 2 FGH
5 2 HJK
6 3 ZXC
Table 3
Id Depart Area
1 A pp
2 B
3 C nn
4 D oo
I need the result
Id Depart Name Area
1 A ABC pp
2 B FGH Null
3 C ZXC nn
4 D NULL oo
I need one matching entry from table 2 and table 3 to corresponding entry in the table 1
Do a left join to also get t1 rows without any reference in the t2 table. GROUP BY to get only 1 row per Department.
select t1.id, t1.Department, min(t2.Name)
from t1
left join t2 on t1.id = t2.DepartId
group by t1.id, t1.Department
I think I would do this with a correlated subquery:
select t1.*,
(select t2.name
from t2
where t1.id = t2.DepartId and rownum = 1
) as t2name
from t1;
This saves the overhead of an aggregation. An index on t2(DepartId, name) is optimal for this query.
by the way not the answer to your specific question but if instead of just one you want all the names you can use listagg
SELECT t1.id,
department,
LISTAGG (name, ',') WITHIN GROUP (ORDER BY name) names
FROM t1, t2
WHERE t1.id = t2.departId(+)
GROUP BY t1.id, department
ORDER BY 1
ID Department Names
1 A ABC,ASD,DEF
2 B FGH, HJK
3 C ZXC
4 D

multiply several tables in big one

There is a task about sql.
I have tree tables
Table_1
Id Name
1 Name1
2 Name2
Table_2
Id Name
3 Name3
4 Name4
And final table
Table_3
Id Table1_Id Table2_Id Value
1 1 3 Some Text
2 1 4 Some another text
So i would like two more rows to be generated for table_3
Id Table1_Id Table2_Id Value
1 1 3 Some Text
2 1 4 Some another text
null 2 3 null
null 2 4 null
How can i do this ?
In fact, i have more then 5 tables with same signature as Table_1 and Table_2.
And each time i add one row to the one of this table, the result Table_3's values count should be multiplied.
Is Table_3 created from a JOIN of Table_1 and Table_2?
If so what JOIN are you using?
Have you tried CROSS APPLY or OUTER JOIN?

sql with two table and group and result from that table?

I have two tables. I need to take away from the first table second table. Stim to another table without all the rows of the first table.
Table1
Table2
Result = Table1(value1) – Table2(value1) -----groupe no. 2 or no.1
Result (groupe no. 2)
Result
id value1 groupe
_______________________
1 10 2
2 9 2
3 10 2
5 5 2
6 11 2
7 12 2
I need the result of which I can write the group number and get result for that group.
Try this query:
Select
t1.id,
t1.value1-t2.value1 as value,
t1.groupe from
table1 t1,table2 t2
where t1.id=t2.id and t1.groupe=2;
try this:
SELECT
T1.id, T2.group, T1.valor - T2.valor AS value
FROM
Table1 T1 INNER JOIN
Table2 T2 ON T1.id = T2.id AND T1.group = T2.group
WHERE (T1.group = 2)

Select Row from Other Table if Doesn't Exist in Current Table

I have two tables:
Table1
id name qty
1 Tedd 6
2 Jim 7
3 Sally 8
4 Victoria 1
Table2
id name qty
1 Tedd 2
2 Jim 2
3 Sally 2
4 Victoria 1
5 Alex 9
I need to select all the rows from Table1. However, if a row exists in Table2 that doesn't exist in Table1, I need to include that in the result set. So, in the end, my query should return this:
id name qty
1 Tedd 6
2 Jim 7
3 Sally 8
4 Victoria 1
5 Alex 9
Is there a way I can do this? Thanks.
You can use a FULL OUTER JOIN:
select
coalesce(t1.id, t2.id) id,
coalesce(t1.name, t2.name) name,
coalesce(t1.qty, t2.id) qty
from table1 t1
full outer join table2 t2
on t1.id = t2.id
See SQL Fiddle with Demo