Solved

Sort a Union Query

Posted on 2008-06-19
3
243 Views
Last Modified: 2010-03-20
How can I sort the query below by dtmcontact desc.  Thanks
create procedure spPANotes          
(                                  
 @paramid varchar(15)                                  
                                 
)            
as          
          
          
(SELECT dtmcontact as date,tblPAVisit.lngPatientID, tblPAVisit.lngVisitID, tblPAMfg.strDenied AS 'memo', 'Mfg Notes' as notetype           
FROM tblPAVisit INNER JOIN tblPAMfg ON tblPAVisit.lngVisitID = tblPAMfg.lngVisitID           
WHERE (tblPAVisit.lngPatientID = @paramid) AND (tblPAMfg.strDenied IS NOT NULL)          
UNION ALL          
SELECT dtmcontact as date,tblPAVisit.lngPatientID , tblPAVisit.lngVisitID, tblPADiabetic.memVoucher AS 'memo' , 'Diabetic Notes'as notetype         
FROM tblPAVisit INNER JOIN tblPADiabetic ON tblPAVisit.lngVisitID = tblPADiabetic.lngVisitID           
WHERE (tblPAVisit.lngPatientID = @Paramid)         
UNION ALL          
SELECT dtmcontact as date, tblPAVisit.lngPatientID , tblPAVisit.lngVisitID, tblPAVoucher.memVoucher AS 'memo'  , 'Voucher Notes'as notetype         
FROM tblPAVoucher INNER JOIN tblPAVisit ON tblPAVoucher.lngVisitID = tblPAVisit.lngVisitID           
WHERE (tblPAVisit.lngPatientID = @Paramid) AND (tblPAVoucher.memVoucher IS NOT NULL)  )

Open in new window

0
Comment
Question by:running32
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 21825074
create procedure spPANotes          
(                                  
 @paramid varchar(15)                                  
                                 
)            
as          
         
         
SELECT dtmcontact as date,tblPAVisit.lngPatientID, tblPAVisit.lngVisitID, tblPAMfg.strDenied AS 'memo', 'Mfg Notes' as notetype          
FROM tblPAVisit INNER JOIN tblPAMfg ON tblPAVisit.lngVisitID = tblPAMfg.lngVisitID          
WHERE (tblPAVisit.lngPatientID = @paramid) AND (tblPAMfg.strDenied IS NOT NULL)          
UNION ALL          
SELECT dtmcontact as date,tblPAVisit.lngPatientID , tblPAVisit.lngVisitID, tblPADiabetic.memVoucher AS 'memo' , 'Diabetic Notes'as notetype        
FROM tblPAVisit INNER JOIN tblPADiabetic ON tblPAVisit.lngVisitID = tblPADiabetic.lngVisitID          
WHERE (tblPAVisit.lngPatientID = @Paramid)        
UNION ALL          
SELECT dtmcontact as date, tblPAVisit.lngPatientID , tblPAVisit.lngVisitID, tblPAVoucher.memVoucher AS 'memo'  , 'Voucher Notes'as notetype        
FROM tblPAVoucher INNER JOIN tblPAVisit ON tblPAVoucher.lngVisitID = tblPAVisit.lngVisitID          
WHERE (tblPAVisit.lngPatientID = @Paramid) AND (tblPAVoucher.memVoucher IS NOT NULL)  
ORDER BY dtmcontact desc
0
 

Author Comment

by:running32
ID: 21825159
I get the error below when I try to run the query

Server: Msg 8626, Level 16, State 1, Procedure spPANotes, Line 9
Only text pointers are allowed in work tables, never text, ntext, or image columns. The query processor produced a query plan that required a text, ntext, or image column in a work table.
0
 

Author Comment

by:running32
ID: 21825241
there was a ntext value in the query.  thanks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Help - SELECT Statement 6 52
Updating a table from a temp table 4 36
SQL Help 27 41
performance query 4 20
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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