Solved

Sort a Union Query

Posted on 2008-06-19
3
238 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now