I need to intersect two tables based
on a column,in both tables.
Here's my code snippet :
SELECT b.VisitID, b.CarrierName, b.PhoneNum, b.PatientName, b.SubscriberID, b.SubscriberName,
b.ChartNum, b.DoB, b.SubscriberEmp, b.ServiceDate, b.ProviderName, b.CPTCode, b.AgingDate,
b.BalanceAmt, f.FollowUpNote, f.InternalStatusCode FROM billing b JOIN followup f
USING (VisitID) WHERE b.VisitID = f.VisitID
In the 'followup' table I've 281 rows AND 'billing' table contains 2098 rows. When I execute this query I'm getting 481 rows.
Did anyone faces this sort of problem?
Could you help me to INTERSECT these tables?
Thanx in advance..
I think you like to do a left join here (not an inner join as in your example):
SELECT b.VisitID, b.CarrierName, b.PhoneNum, b.PatientName,
b.SubscriberID, b.SubscriberName, b.ChartNum, b.DoB,
b.SubscriberEmp, b.ServiceDate, b.ProviderName, b.CPTCode,
b.AgingDate, b.BalanceAmt,
f.FollowUpNote, f.InternalStatusCode
FROM billing b
LEFT JOIN followup f ON b.VisitID = f.VisitID
This will also return rows from the 'billing' table that don't have corresponding fields in the 'followup' table.
It seems to me that you'd be very likely to have multiple follow ups. Thus the 481 records from the notes table is likely accurate.
Perhaps add an
ORDER BY b.SubscriberID
to JochenJung's answer above and accept that you have the right number of rows.
alternately a
GROUP BY b.SubscriberID
Would give you one row per customer
Related
I have two tables.
Table_Sale
S_Date
S_Store
S_Item_ID
S_Qty
Table_Return
R_Date
R_Store
R_Item_ID
R_Qty
Imagine Table_Sale have 1000 row and Table_Return have 250 rows.I want to do this cindition. (S_Date=R_Date and S_Store=R_Store and S_Item_ID=R_Item_ID)
Think there are 150 rows match with that condition. Then there are 850 Rows from Table_Sale and 100 row in Table_Return which are not matching with the condition. Now I want 150+100+850 all data in a one table. How can I make the join sir.?Please anyone help me.
You should use a FULL OUTER JOIN. Something like this...
SELECT *
FROM Table_Sale a
FULL OUTER JOIN Table_Return b ON a.S_Date = b.R_Date
and a.S_Store = b.R_Store
and a.S_Item_ID = b.R_Item_ID
This is my query:
create table vi_all as
select
d.primaryid, d.age, d.gndr_cod, d.wt, d.wt_cod, d.reporter_country,
dr.primaryiddrug, dr.role_cod, dr.drug_name,
r.primaryidreac, r.pt,
o.primaryidoutc, o.outc_cod,
i.primaryidindi, i.indi_pt
FROM demo d,
drug dr,
reac r,
outc o,
indi i;
Each table contains at least 80K records and more than 20 fields so its getting really tough to execute select statement on multiple tables; and i just want 4 or 3 fields from each table so i thought of this, but the above query has taken more than 5 hours but still has not given back any result.
My crystal ball says you need something like this:
create table vi_all as
select
d.primaryid, d.age, d.gndr_cod, d.wt, d.wt_cod, d.reporter_country,
dr.primaryiddrug, dr.role_cod, dr.drug_name,
r.primaryidreac, r.pt,
o.primaryidoutc, o.outc_cod,
i.primaryidindi, i.indi_pt
FROM demo d
LEFT JOIN drug dr ON d.drug=dr.id
LEFT JOIN reac r ON d.reac=r.id
LEFT JOIN outc o ON d.outc=o.id
LEFT JOIN indi i ON d.indi=i.id;
As far as I can tell your query is selecting all results from all tables but not assocating them in anyways so, you can maybe get duplicate data in the newly created table. Also, if you have some good foreign keys to associate those tables, the performance will be considerably better.
I have two tables. One of them named files and there is al list of all files. the second table called payments, and there is in there a list of payments for some files.
Payments:
id | fileid | {...}
1 2
2 3
3 2
Files:
id | {...}
1
2
3
I want to select all files, and join the table payments to order by count of this table.
In this case, the first row will be file #2, because it repeats the most in the payments table.
I tried to do it, but when I do it - not all of the rows are shown!
I think it happens because not all of the files are in the payments table. So in this case, I think that it won't display the first row.
Thanks, and sorry for my English
P.S: I use mysql engine
** UPDATE **
My Code:
SELECT `id`,`name`,`size`,`downloads`,`upload_date`,`server_ip`,COUNT(`uploadid`) AS numProfits
FROM `uploads`
JOIN `profits`
ON `uploads`.`id` = `profits`.`uploadid`
WHERE `uploads`.`userid` = 1
AND `removed` = 0
ORDER BY numProfits
As others have noted you need to use LEFT JOIN. - This tells MySQL that entries from the tables to the left should be included even if no corresponding entries exists in the table on the right.
Also you should use GROUP BY to indicate how the COUNT should be deliminated.
So the SQL should be something like;
SELECT Files.ID, count(Payments.FileID) as numpays FROM
Files
LEFT OUTER JOIN
Payments
ON Files.id=Payments.FileID
GROUP BY files.ID
ORDER BY numpays desc
SQL Fiddle
Try this:
select B.fileid,A.{}.....
from
(select id,.....
from files A
inner join
(select count(*),fileid,.....
from payments
group by fileid) B
on files.id=payments.fileid)
I hope this helps. I'm assuming that all ID in files table are unique. In this answer, you can apply an order by clause as per your wish. I've left the select statement to you to select whatever data you want to fetch.
As far as your problem is described, I think this should work. If any problems, do post a comment.
Try LEFT JOIN - in MySQL, the default JOIN is actually an INNER JOIN. In an INNER JOIN, you will only get results back that are in both sides of the join.
See: Difference in MySQL JOIN vs LEFT JOIN
And, as noted in the comments, you may need a GROUP BY with your COUNT as well, to prevent it from just counting all the rows that come back.
I have a query I need to perform on a table that is roughly 1M records. I am trying to reduce the churn, but unfortunately there is a UNION involved (after i figure this join out), so that may be a question for another day.
The records and data I need to get reference 3 fields in a table that need each pull a description from another table and return it in the same record, but when i do the Inner join i was thinking, it either returns only 1 field fromt he other table, or multiple records from he original table.
Here are some screen shots of the tables and their relationship:
Primary table containing records (1 each) with the physician record I want to pull, including up to 3 codes that can be listed in the "taxonomy" table.
Secondary table containing records (1 each) with the "Practice" field I want to pull.
A Quick glance of the relationship i'm talking about
I presume that if perform an inner join matching the 3 fields in the physicians table, that it will have to iterate that table multiple times to pull each taxonomy code .. but I still can't even figure the syntax to easily pull all of these codes instead of just 1 of them.
i've tried this:
SELECT
taxonomy_codes.specialization,
physicians.provider_last_name,
physicians.provider_first_name,
physicians.provider_dba_name,
physicians.legal_biz_name,
physicians.biz_practice_city
FROM
taxonomy_codes
INNER JOIN physicians ON physicians.provider_taxonomy_code_1 = taxonomy_codes.taxonomy_codes OR physicians.provider_taxonomy_code_2 = taxonomy_codes.taxonomy_codes OR physicians.provider_taxonomy_code_3 = taxonomy_codes.taxonomy_codes
First, the query churns a lot and it only returns one taxonomy specialty result which I presume is because of the OR in the join statement. Any help would be greatly appreciated.
Thank you,
Silver Tiger
You have to join the taxonomy_codes table multiple times:
SELECT p.provider_last_name, p...., t1.specialization as specialization1, t2.specialization as specialization2, t3.specialization as specialization3
FROM physicians p
LEFT JOIN taxonomy_codes t1 ON t1.taxonomy_codes = provider_taxonomy_code_1
LEFT JOIN taxonomy_codes t2 ON t2.taxonomy_codes = provider_taxonomy_code_2
LEFT JOIN taxonomy_codes t3 ON t3.taxonomy_codes = provider_taxonomy_code_3
So I've asked a couple of questions about performing joins and have had great answers, but there's still something I'm completely stumped by.
I have 3 tables. Let us call them table-b, table-d and table-e.
Table-b and table-d share a column called p-id.
Table-e and table-b share a column called ev-id.
Table-e also has a column called date.
Table-b also has a unique id column called u-id.
I'd like to write a query which returns u-id under the following conditions:
1) Restriced to a certain value in table-e.date.
2) Where table-b.p-id does not match table-d.p-id.
I think I need to inner join table-b and and table-e on the e-id column. I then think I need to perform a left join on table-d and and table-b where p-id is null.
My problem is that I don't know the syntax of writing this query. I know how to write multiple inner joins and I know how to write a left join. How do I combine the two?
Thanks so much to everyone who is helping me out. I'm (obviously!) a newbie to databases and am struggling to get my head around it all!
You just write the joins one after the other:
SELECT b.uid
FROM b
INNER JOIN e USING(evid)
LEFT JOIN d USING(pid)
WHERE e.date = :whatever
AND d.pid IS NULL
I think it's something like this:
SELECT uid
FROM table-b
INNER JOIN table-e
ON table-b.ev_id = table-e.ev_id
WHERE table-b.p_id NOT IN (SELECT p_id from table-d)