Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-11-05
6
Medium Priority
?
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39625083
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
ID: 39625162
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
ID: 39625316
It would appear your article has what I need, let me work with it for a bit.
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 39625640
SELECT
    *, MIN(admdate) OVER(PARTITION BY providerhs, linkno) AS MinAdmitDate
--INTO <new_table_name>
FROM PatientDataArr20132
0
 

Author Closing Comment

by:JamesDBuskirk
ID: 39626311
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 70

Expert Comment

by:Scott Pletcher
ID: 39627467
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article I will describe the Copy Database Wizard 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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 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