473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Join Two tables using the group by function

Hi All,

I tried lots ways to solve my problem.But i am not getting my required output.

I am explaining my requirement in detail below.

I have two tables in my database.From those two tables i want to get the entire data(all rows) of first table with join condition.
Along with that i am using where condition to get the maximum value records of the second table.
This is the query i am using:
Expand|Select|Wrap|Line Numbers
  1.    select pd.pid,pd.fname,pd.phone,pf.facilityid
  2.    from patient_data pd
  3.    left join tbl_patientfacility pf 
  4.    on pd.pid = pf.patientid
  5.    where pf.id in (select max(id) from tbl_patientfacility group by patientid) 
By applying this i am getting the data which id's are coming through the where condition,those records only displaying.But i need the records which are left in the first table.

I am attaching the images.
Could any one give me the suggestion on how to get my output.
Attached Images
File Type: jpg InPut.jpg (41.6 KB, 151 views)
File Type: png ExpectedOutput.png (3.5 KB, 89 views)
Mar 10 '15 #1
1 1342
Rabbit
12,516 Expert Mod 8TB
Join the facility table to your subquery to return just the max rows before outer joining to your patient table.
Mar 10 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Mark | last post by:
hi. i have two tables. table one ("users") contains two columns, "user_id", and "user_name" table two ("spam") contains three columns "msg_id","user_id", and "msg" basically, i want to...
2
by: zwasdl | last post by:
I'm using access to connect to Oracle via ODBC. I can also connect to Oracle via sql*plus. Can I write a query to join tables from different schema? If so, how? Thanks a million! Wei
2
by: =?Utf-8?B?VGVycnk=?= | last post by:
Is it possible to join tables from different databases? If so, what is the format of the select statement? Both databases are on the same server. TIA, -- Terry
3
by: nico3334 | last post by:
I currently have a query that Joins 2 Tables (Table1 and Table2) using LEFT OUTER JOIN. Here is an example of that query: SELECT a.supply, a.state, b.cost FROM Table1 a LEFT...
21
patjones
by: patjones | last post by:
Hi all: My newest project involves creating a small help desk database for our customer service division to use. What will happen is that an employee will call up, explain his/her issue, and...
1
by: bipinskulkarni | last post by:
How to count number of users created in a perticular year. ex.i want to count total number of users created in 2007using query.
0
by: jimgym1989 | last post by:
select ABS(ROUND((projectspecs.laborCost+projectspecs.contingencyCost + SUM(materialcost_details.qty*materialcost_details.unitCost))* (billing.accom/100) -...
2
by: buterfly0707 | last post by:
hi all.. i have 3 tables called itemMaster,ItemWh,whWiseItem(table names i took just for example). here i want to retrieve data from all the 3 tables. table details itemMaster having details of...
1
by: Jagdeep singh | last post by:
There are 2 tables 1.customers 2.quotations Here I fill my datagridview with this query SELECT quotations.CustomerKey as Id , customers.Name , SUM(quotations.AmountDue)...
4
by: shanboy | last post by:
I am newbie in sql server i'm using sql server 2005 and the northwind database i wanted to join order and order details tables and calculate the total purchase made by each customer by using group...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.