Solved

MsSQL tables join

Posted on 2011-09-29
5
221 Views
Last Modified: 2012-05-12
Hello all,

I have 2 tables that keep track of appointments. I am writing a code for scheduling and I need to query both tables, at the same time, and order the results by appointment time. (appt_time in one, and visit_time in the other). I am thinking I need to create a view, but I am not sure about the join. It would be helpful if it could also give me which table the record came from. There is no relation between the records. Could someone assist me with this?

Thanks,

Tales
0
Comment
Question by:cnshealthcare
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 500 total points
ID: 36817523
You just need to match up the fields from both  tables and do a UNION

SELECT Appt_Date AS ApptDate, Appt_Time as ApptTime FROM A
UNION
SELECT Visit_Date. Visit_Time FROM B

You can put any where clauses you wish on each query, or even one on the query as a whole
0
 
LVL 1

Author Comment

by:cnshealthcare
ID: 36817553
Could you please write it ou completely? I never used union before. Thanks,
0
 
LVL 1

Author Comment

by:cnshealthcare
ID: 36817572
acctually I got it! thanks!
0
 
LVL 18

Expert Comment

by:lludden
ID: 36817588
What all fields do you want with what criteria?

UNION just takes the results of two queries and returns them as a single recordset.  The query I gave you will work, and will show all of the times and dates from each table.

If you create a query showing the information you want from table A, and write a query that shows the information you want from table B, and both queries have the same number of columns (and really, the same types of data), then you can just put a UNION between them.

SELECT Appt_Date AS ApptDate, Appt_Time as ApptTime FROM A WHERE Appt_Date >= CAST(COVERT(varchar(10),getdate(),110) AS datetime)
UNION
SELECT Visit_Date. Visit_Time FROM B WHERE Visit_Date >= CAST(COVERT(varchar(10),getdate(),110) AS datetime)

this will return all appts that are dated for the current day or later.
0
 
LVL 1

Author Comment

by:cnshealthcare
ID: 36891342
COOL! Learn something new every day!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question