I have tbl1 Structured like this
Name Type
====== =====
John 1
David 1
Jane 2
William 3
Alex 2
Ryan 1
And tbl2 structured like this
Index Type Job
1 1 Clean
2 1 Wash
3 2 Carry
4 2 Package
5 3 Sell
I would like to join record with matched Type, but each record in tbl1 only join once with one record in tbl2
Ie:
If
John is joined with Clean then David must be joined with Wash. Or if John is joined with Wash then David must be joined with Clean.
Doesn't matter if David is joined with Wash or Clean , I only need them to be joined with record that match the criteria and be joined ONCE.
I will make sure for each Type in 'tbl1' there will be equivalent amount of record in 'tbl2'
I mainly work on MS Access so Query on this environment would be the best~ Thank you all for reading.
Best regards
Try below query.
select name, (select TOP(1) job from tbl2 where tbl1.type = tbl2.type) from tbl1
Hope it help
Related
I am unable to convert multiple columns into a single column keeping remaining columns intact
I have the following table table1
Name Category1 Category2 Category3 ..... Tag
======= =========== =========== =========== ========
Jason 5 6 4 senior
Walter 3 7 10 junior
.
.
.
How do I merge all categories into a single column called category_mix?
Basically, I want to use the data from table1 to populate another table, table2 such that it gets populated in this manner
Name category_mix Tag
======= ============== =======
Jason 5 senior
Jason 6 senior
Jason 4 senior
Walter 3 junior
Walter 7 junior
Walter 10 junior
How do I go about doing this in Postgres?
You can use a values clause to achieve the unpivot:
select t.name, c.category_mix, t.tag
from table1 t
cross join lateral (
values (category1), (category2), (category3)
) as c(category_mix);
Online example
I want to join two tables and use the column they both share to group the results, including a null result for those accountIds which only appear in one table.
Table a
AccountId
productApurchases
Steve
1
Jane
5
Bill
10
Abed
2
Table b
AccountId
productApurchases
Allan
1
Jane
10
Bill
2
Abed
1
Mike
2
Desired output
AccountId
productApurchases
productBpurchases
Steve
1
0
Jane
5
10
Bill
10
2
Abed
2
1
Mike
0
2
I've been trying with various joins but cannot figure out how to group by all the account ids.
Any advice much appreciated, thanks.
Use full join:
select accountid,
coalesce(productApurchases, 0) as productApurchases,
coalesce(productBpurchases, 0) as productBpurchases
from a full join
b
using (accountid);
I'm having trouble with a LIKE function in proc sql.
PROC SQL;
CREATE TABLE NAMES_IDS AS
SELECT DISTINCT
T1.*
,T2.NAMES
,T2.NAME_ID
FROM WORK.table1 T1
LEFT JOIN data.table2 T2 ON T2.NAMES like T1.NAMES1
;QUIT;
I have several names in t2, lets say for example theres John 1, John 2, John 3, John 4, etc and in t1.Names1 there is %John%
proc sql is just pulling in the first match, John 1 and its associated ID, and applying it to all the data in T1, instead of duplicated a match for all matching names (this is what I want to achieve).
So the end table would have something like
COLUMN A COLUMN B
John John 1
John John 2
John John 3
John John 4
But instead, what I get is:
COLUMN A COLUMN B
John John 1
John John 1
John John 1
John John 1
Hopefully this makes some sort of sense...
I think I figured it out, I added TRIM to my code and I guess there may have been some erroneous spaces somewhere because that seems to fix my issue. Thanks for your responses!
I have a requirement in such a way that it should join two tables with more than 100k records in one table and just 5 records in another table as shown below
Employee Dept Result
id Name deptid deptid Name Name deptid Name
1 Jane 1 1 Science Jane 1 Science
2 Jack 2 2 Maths Dane 1 Science
3 Dane 1 3 Biology Jack 2 Maths
4 Drack 3 4 Social Drack 3 Biology
5 Drim 5 Zoology Kery 4 Social
6 Drum 5 Drum 5 Zoology
7 Krack
8 Kery 4
.
.
100k
Which join need to be used to get the query in an better way to perform to get the result as shown.
I just want the query to join with other table from employee table only which has dept which i thought of below query but wanted to know is there any better way to do it.
Select e.name,d.deptid,d.Name from
(Select deptid,Name from Employee where deptid IS NOT NULL) A
and dept d where A.deptid=d.deptid;
Firstly not sure why you are performing your query the way you are. Should be more like
SELECT A.name, D.deptid,D.Name
FROM Employee A
INNER JOIN dept D
ON A.deptid = D.deptid
No need of the IS NOT NULL statement.
If this is a ONE TIME or OCCASIONAL thing and performance is key (not a permanent query in your DB) you can leave out the join altogether and do it using CASE:
SELECT
A.name, A.deptid,
CASE
WHEN A.deptid = 1 THEN "Science"
WHEN A.deptid = 2 THEN "Maths"
...[etc for the other 3 departments]...
END as Name
FROM Employee A
If this is to be permanent and performance is key, simply try applying an INDEX on the foreign key deptid in the Employee table and use my first query above.
I have a table that has information structured like this:
ID Points Name School
1 123 James A
2 534 Henry B
3 56 Henry B
4 153 Chris B
5 95 Chris B
6 83 Chris B
7 421 James A
And I need to get out of a query the rows that have the same name, but only the highest points for each like this:
ID Points Name School
7 421 James A
2 534 Henry B
4 153 Chris B
Any ideas on how this could be accomplished with a query? I've spent way too much time trying to figure this out.
select name,school,max(points) from table group by name,school
That will give you the max points per name/school combination. Join it to itself if you want the ID:
select table.* from table inner join
(select name,school,max(points) as points from table group by name,school) a
on a.name = table.name and a.school = b.school and a.points = table.points
edit : sorry, this is a SQL solution...just saw the MSACCESS tag. Logic is right, but you'll need to convert to access syntax.
edit to correct the second query, missed a column inh my join
SELECT
(SELECT TOP 1 ID FROM Table
WHERE
Name = t.Name AND
School=t.School AND
Points=t.Points
) as Id, t.Name, t.Points, t.School
FROM
(SELECT Name, School, max(Points) as Points
FROM Table
GROUP BY Name, School) t