Solved

MsSQL tables join

Posted on 2011-09-29
5
216 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
  • 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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