Linking different ids through interactor - sql

I want to link different ids through the common interactor. It is bit complex but i will try my best to frame the problem.
Here are the list of steps
1. Extract id from table A.
Table A
ID Interactor
1 30
2 40
Get the list of interactors corresponding to id from table B. for example,
select * from table B where id = 1
Table B
ID Interactors
1 30
1 32
1 33
1 36
1 38
1 39
Iterate through each interactor from the list and get the list of ids from table A.
Table A
ID Interactors
1 30
70 32
76 33
Null 36
89 38
75 39
2 45
2 40
2 43
4.Join these different ids so that when i select 1 i should get the below result.
Select * where id = 1
Result
ID Interactors
1 30
70 32
76 33
89 38
75 39
I want to achieve this using sql.

Try this:
select B.ID, B.Interactors
from A inner join B
where A.Interactors = B.Interactors
and A.ID = 1

From step 3 you have table A, and before that you have table B.
You can use simple inner join with some where condition to get your desired result.
Select Id, Interactors from
( select tableA.id, tableA.Interactors
from tableA
inner join tableB
on tableA.Interactors = tableB.Interactors
and tableA.Id is not null --- this is required since in your output record having NULL id correspond to tableA is not considered
) as db
where db.Id = 1 ---- you can apply any filter over there to get your desired result.

Related

Join two tables get count without using any where condition

I am trying to join two tables, prcshd (head table) and prcsdt (detail table), and need to get count of prod_prcshd_id in prcsdt (detail table) without using where (if required can use sub-query). Not sure ...
Tried like
select distinct count(b.prod_prcshd_id), b.prod_prcshd_id
from tra_pharmacy_prod_prcshd a join
tra_pharmacy_prod_prcsdt b
on b.prod_prcshd_id = a.id
group by b.dt_id
My tables:
prcshd (head table)
id(pk) | medi_name_id | med_prep_id
1 83 1
2 83 2
prcsdt (detail table)
dt_id(pk) | prod_prcshd_id(fk) | type_id | prod_name_id |medi_prep_id
1 1 4 83 1
2 1 5 83 1
3 1 6 83 1
4 2 4 83 2
still no luck.
id | prod_prcshd_id | medi_name_id
1 3 83
2 1 83
I'm not sure if this will fix your problem. But, you almost never need select distinct with group by. I suspect you want one of these this:
select b.prod_prcshd_id, count(b.prod_prcshd_id)
from tra_pharmacy_prod_prcshd a join
tra_pharmacy_prod_prcsdt b
on b.prod_prcshd_id = a.id
group by b.prod_prcshd_id;
Under most circumstances, you don't even need the join:
select ppp.prod_prcshd_id, count(*)
from tra_pharmacy_prod_prcsdt ppp
group by ppp.prod_prcshd_id;

Re-Organize Access Table by converting Rows to Columns

I'm pretty new to access and SQL and need some help re-organizing a table. I have the following table (sorry for the table below - having trouble posting):
ID GroupID Distance Code Start_Finish
1 44 7 A S1
2 44 14 A F1
3 45 12 B S1
4 45 16 B F1
5 45 31 C S2
6 45 36 C F2
7 45 81 B S3
8 45 88 B F3
And need for the table to be transformed into:
GroupID Code Start_Distance Finish_Distance
44 A 7 14
45 B 12 16
45 C 31 36
45 B 81 88
try something like this
Select GroupID, Code, min(distance) as Start_distance, max(distance) as Finish_distance
from Table
group by GroupID, Code
If the min and max functions don't give you what you need, try it with First() and Last() instead.
Oops - just noticed you have 2 different entries in the output for GroupID 45 Code B - is that a requirement? With that data structure and requirement, the problem gets much more difficult.
Now I see the final column in the 1st table - I think that can be used to get the output you want:
Select GroupID, Code, mid(start_finish,2) as T, min(distance) as Start_distance, max(distance) as Finish_distance
from Table
group by GroupID, Code, T
You can use conditional aggregation for this.
select GroupID
, CODE
, max(case when Left(Start_Finish, 1) = 'S' then Distance end) as Start_Distance
, max(case when Left(Start_Finish, 1) = 'F' then Distance end) as Finish_Distance
from SomeTable
group by GroupID
, CODE

Keeping track of auto increment columns in a cursor

I wrote a cursor where I needed to copy some rows and insert it into a table with only 1 column different. Say for example I have a table A like this
Id | CID | Country
1 X A
2 X B
3 X C
'Id' in this table is auto increment and primary key and Country is the countries visited by that CID. So when I was asked to update the table where every country visited by CID 'X' has also been visited by CID 'Y'. I wrote a cursor and did an insert into with CID 'Y' and the Country. After I executed my cursor, table A became this:
Id | CID | Country
1 X A
2 X B
3 X C
4 Y A
5 Y B
6 Y C
Now there is another table B that is as follows, where AId is the foreign key referencing Id in A.
AId | AgeOfKids
1 20
1 23
1 28
2 21
2 24
2 29
3 22
3 25
I want to be able to add rows to this table such that it becomes:
AId | AgeOfKids
1 20
1 23
1 28
2 21
2 24
2 29
3 22
3 25
4 20
4 23
4 28
5 21
5 24
5 29
6 22
6 25
To explain in words, the AIds that have the same countries in table A must have the same BeIds in table B. Is it possible to accomplish this? Is so, how can I accomplish this?
Use the OUTPUT clause:
declare #t table (Id int not null,Country char(1) not null)
insert into TableA (CID,Country)
output inserted.id,inserted.Country into #t
select 'Y',Country from TableA where CID = 'X'
And then (should be part of the same batch as the above):
insert into TableB (AId,AgeOfKids)
select t.ID,b.AgeOfKids
from #t t
inner join
TableA a
on
t.Country = a.Country and
a.CID = 'X'
inner join
TableB b
on
a.ID = b.AId
To populate table B.

How to extract the latest row

I have a table like this:
Item Serial_No Grade Size Location Date
CQ35 A243911 4 36 A 20110127
CQ35 A243911 4 36 B 20110329
CQ35 A243911 4 36 C 20110330
CQ38 A244567 3 38 A 20110127
CQ35 A244567 3 38 B 20110128
CQ38 A244567 3 38 C 20110129
CQ35 A244567 3 38 D 20110130
CQ40 A244568 3 41 A 20110127
CQ40 A244568 3 41 B 20110129
CQ36 A244570 2 37 A 20110125
The expected results shd look like this using SQL:
Item Serial_No Grade Size Location Date
CQ35 A243911 4 36 C 20110330
CQ35 A244567 3 38 D 20110130
CQ40 A244568 3 41 B 20110129
CQ36 A244570 2 37 A 20110125
I'm trying to extract the latest row of each Serial No.
Thanks
UPDATE : with date
select *
from table as outertable
where date = (select max(date) from table where Serial_No = outertable.Serial_No)
The trick here is to do comparison in the range of Serial_No only, and the way to achieve this is with a Correlated Subquery as shown here.
This assumes that there is only one entry for each date and serial number.
SELECT MyTable.*
FROM MyTable
INNER JOIN (SELECT SerialNo, MAX(Date)
FROM MyTable
GROUP BY SerialNo) AS MyTableLatest ON MyTable.SerialNo = MyTableLatest.SerialNo
AND MyTable.Date = MyTableLatest.Date
Something like this:
SELECT *
FROM table
WHERE Date
IN (
SELECT MAX( Date )
FROM table
GROUP BY Serial_No
)

Database Query Help required in MySQL

I am looking for help in writing a query of retrieving the values from 2 tables in MySQL.
The scenario is
Table A
ID Name Marks
===================
23 John 67
45 Mark 45
12 Ram 87
Table B has the following Structure
ID Name Evaluation Marks
==============================
45 Mark 34
78 Chris 09
98 Nancy 10
23 John 12
I am trying to write a query, where if I execute the following query
Select "SOMETHING" from Table A where Id=45
I should get Marks Column as 45+34=79, which should fetch and sum from the both the Tables A and Table B.
If I execute the query with the Id=12.
Since the Id=12, does not exists in the Table B, I should get the Marks as 87.
What would a query for the above?
I assume that the id occurs only once in your tables table a, but could be missing in both. If it always exists in table a, you can use a LEFT JOIN instead of the UNION.
SELECT COALESCE(SUM(marks), 0)
FROM
(
SELECT marks FROM a WHERE id = 45
UNION ALL
SELECT SUM(evaluation_marks) AS marks FROM b WHERE id = 45
) x
Edit
If you have all users in table a, then use
SELECT a.marks + COALESCE( SUM( b.evaluation_marks ), 0 )
FROM a
LEFT OUTER JOIN b ON ( b.id = a.id )
WHERE a.id = 45
GROUP BY a.id, a.marks
You should consider changing your table model though. Why do you store name and id twice? Can't you do it like that:
id name marks evaluation marks
=======================================
12 Ram 87 0
23 John 67 12
45 Mark 45 34
78 Chris 0 9
98 Nancy 0 10