Solved

How can I get this column in my query?

Posted on 2016-11-02
2
45 Views
Last Modified: 2016-11-02
I want to have a column called "patientname" as part of my recordset. My strategy is to include what I'm working on with another query, so the columns have to match.

This works:

working query
This does not:

problem
The challenge is the name. I want to combine the first name (fname) with the last name (lname) and include that as a column in my recordset. But I can't do that without including "patientname" as part of the GROUP BY clause and that's where I'm running into problems.

How can I fix that?

SELECT
'C' AS txntype,
SUM(amount) AS amount,
dos,
pfirst+' '+plast as patientname, //how do I include "patientname" as a column when it's not a field in my table
'Medical Oncology' AS physicianid,
encountercode
FROM [PCAR_Data].[dbo].[txn]
WHERE
type='C'
AND
accountid=9949388
GROUP BY encountercode, dos
having max(txn.created) > DATEADD(DAY, -45, GETDATE())
ORDER by dos ASC
0
Comment
Question by:brucegust
2 Comments
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41870366
try this:

SELECT
'C' AS txntype,
SUM(amount) AS amount,
dos,
pfirst+' '+plast as patientname, //how do I include "patientname" as a column when it's not a field in my table
'Medical Oncology' AS physicianid,
encountercode
FROM [PCAR_Data].[dbo].[txn]
WHERE
type='C'
AND
accountid=9949388
GROUP BY encountercode, dos, pfirst+' '+plast
having max(txn.created) > DATEADD(DAY, -45, GETDATE())
ORDER by dos ASC
0
 

Author Closing Comment

by:brucegust
ID: 41870430
That'll do it!

Thank you!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

776 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