Solved

MS-SQL query question

Posted on 2012-12-21
5
271 Views
Last Modified: 2013-01-25
Hi I am doing a query using MSSQL Mgt Studio in design view ( because I no nothing). On MS SQL server 2005

SELECT     patient_name.*, patient_address.*, doctor.*
FROM         patient_name CROSS JOIN
                      patient_address CROSS JOIN
                      doctor

I need to limit the query to the past 12 months (so the server doesn't gag) and produce an output I can eventually import into Excel
0
Comment
Question by:Openallnight
5 Comments
 
LVL 18

Expert Comment

by:x-men
ID: 38713804
SELECT     patient_name.*, patient_address.*, doctor.*
FROM         patient_name CROSS JOIN
                      patient_address CROSS JOIN
                      doctor
where <myDatecolumn> > dateadd("m",-12,getdate())
0
 

Author Comment

by:Openallnight
ID: 38714162
I tried to grab billing dates from a billing submission table called reconsideration_table and it bombs

Msg 170, Level 15, State 1, Line 6
Line 6: Incorrect syntax near '<'.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 38714186
The server will gag from those CROSS JOINs, because they are inappropriate here.

Something like below will be close, although it's probably still not quite right, since there should be a "doctor_patient" table and/or a "patient_visit" table that would be more accurate to use for this.


SELECT     patient_name.*, patient_address.*, doctor.*
FROM         dbo.patient_name pn
INNER JOIN dbo.patient_address pa ON
    pa.patient_id = pn.patient_id
INNER JOIN dbo.doctor d ON
    d.doctor_id = pn.doctor_id
0
 
LVL 3

Accepted Solution

by:
expert_dharam earned 500 total points
ID: 38716034
SELECT     patient_name.*, patient_address.*, doctor.*
FROM         dbo.patient_name pn
INNER JOIN dbo.patient_address pa ON
    pa.patient_id = pn.patient_id
INNER JOIN dbo.doctor d ON
    d.doctor_id = pn.doctor_id
Where YourTableDateColumn > dateadd("m",-12,getdate())

--Note: YourTableDateColumn = Column where your date values reside in the table
--Ensure YourTableDateColumn is of Datetime data type..
0
 

Author Comment

by:Openallnight
ID: 38732966
Too many errors, I don't know enough about SQL to interpret directions
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Agent Timeout 5 65
Run program by typing the .exe in the start>run dialog 5 61
Query group by data in SQL Server - cursor? 3 47
Are triggers slow? 7 12
This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

856 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