Solved

Sort a Union Query

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

740 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