Solved

SQl query help

Posted on 2013-01-09
3
288 Views
Last Modified: 2013-01-10
Hello all,
Please find below the query.How should I modify the query to include a group by clause on "groupby NDC"
Thanks.

 select a.PatName as Patient,a.DrugLabelName as medication,a.dispenseDt as DispensedDate, a.daysSupply, a.PatID, a.NDC, a.RxNo,
   a.RoNo,d.Sig
   from (select * from Rx..HRxs union all select * from FwArchive..HRxs)
   as a
   left join HRxDirections as d on a.PatID = d.PatID and a.FacID = d.FacID and a.RxNo = d.RxNo and a.RoNo = d.RoNo and a.Posted =d.Posted
   left outer join Pat..Patients as b on a.PatID = b.PatID and a.FacID = b.FacID
   left outer join Fac..NursingStations as c on b.NsID = c.NsID and a.FacID = c.FacID
    where a.FacId = '74' and a.DispenseDt >= '07/01/2012'  and a.DispenseDt <= '01/09/2013'  and a.PatId = '22'
     and TransType in ('P', 'R', 'U', 'Q', 'B') order by  a.DrugLabelName,a.DispenseDt
0
Comment
Question by:Star79
3 Comments
 
LVL 8

Accepted Solution

by:
virtuadept earned 250 total points
ID: 38760831
You can't group by just one column of the SELECT, unless you have agregate functions for the rest of the columns (like if you were grouping by a patient name and putting sums of columns on order records or something). It sounds like you just want to sort the data by NDC and if that is the case change the "order by" line to "order by a.NDC, a.DrugLabelName, a.DispenseDt".

Also you may wish to put NDC at the start of the SELECT just so it makes more sense. But doesn't have to be.
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38761463
Can you post some sample data and desired output?  What is your desired aggregate for the grouping?
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 38762178
please read this article, it explaines the "issues" around the GROUP BY / DISTINCT and how to "solve" them:
http://www.experts-exchange.com/Database/Miscellaneous/A_3203-DISTINCT-vs-GROUP-BY-and-why-does-it-not-work-for-my-query.html
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

707 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

16 Experts available now in Live!

Get 1:1 Help Now