Solved

I need more columns in my SELECT portion than are contained in my GROUP BY

Posted on 2013-11-05
6
279 Views
Last Modified: 2013-11-06
Below is a query I created that works ok. I need the MINIMUM admdate by provider by linkno (I.E. the first admit for each patient in a hospital or health system)

select min(admdate) as MinAdmitDate, providerhs, linkno from PatientDataArr20132
group by providerhs, linkno
union
select min(admdate) as MinAdmitDate, providerhs, linkno from PatientDataArr20131
group by providerhs, linkno
order by providerhs, linkno, MinAdmitDate

What I need to do now is add all the columns to the query so that I can build a new table of the same structure but with one extra column. I ‘m sure I need an embedded select statement but I just can’t place it correctly. Can someone please help? .

PatientDataArr20131 is the input table
EventDateArr20131 is the output table, same as above but with one additional column

I have attached a screenshot with the structures of the two tables.
0
Comment
Question by:JamesDBuskirk
  • 3
  • 2
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
No screenshot.  Also not sure what you mean by input and output table.

In case it helps, I have an article on there on SQL Server GROUP BY Solutions, and if you scroll down to point #5 you'll see an example of how to JOIN tables with an aggregate like MIN() from the inner table.
0
 

Author Comment

by:JamesDBuskirk
Comment Utility
I am reading records from one table (input table-PatientDataArr20132) in the query shown above, then I need to insert these records into an almost identical table with one added column (the output table-EventDateArr20132).

I plan to do the inserting with a reader loop, but I need to pull every column from the input table.

I re-attached the JPG screenshot. I did the attach but never clicked UPLOAD, sorry.
sql.jpg
0
 

Author Comment

by:JamesDBuskirk
Comment Utility
It would appear your article has what I need, let me work with it for a bit.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
SELECT
    *, MIN(admdate) OVER(PARTITION BY providerhs, linkno) AS MinAdmitDate
--INTO <new_table_name>
FROM PatientDataArr20132
0
 

Author Closing Comment

by:JamesDBuskirk
Comment Utility
Thank you very much Scott, that worked perfectly. I am not familiar with the OVER or PARTITION commands, I need to get back into class...

Jim, thank you also. Your article was very helpful, I will reference it in the future.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Yeah, the new windowing functions are great.  But they take time to get used to using ... I'm still working on that part myself :-) .
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
OPENDATASOURCE 8 30
get all customers 2 38
Group by correlation 4 43
Truncate vs Delete 63 87
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

6 Experts available now in Live!

Get 1:1 Help Now