Solved

Sort a Union Query

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

'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 …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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