You may try the following
Select K1, K2, Data, Row_Number() Over(Partition By K1 Order By K1, K2)
Row#
From ....
Order By K1, K2
Mit freundlichen Grüßen / Best regards
Birgitta Hauser
"Shoot for the moon, even if you miss, you'll land among the stars." (Les
Brown)
"If you think education is expensive, try ignorance." (Derek Bok)
"What is worse than training your staff and losing them? Not training them
and keeping them!"
-----Ursprüngliche Nachricht-----
Von: MIDRANGE-L [mailto:midrange-l-bounces@xxxxxxxxxxxx] Im Auftrag von Alan
Cassidy
Gesendet: Friday, 10.7 2015 18:08
An: Midrange Systems Technical Discussion
Betreff: SQL Row # within group
Good morning, folks,
I am trying to find out how to do the following without having to jump
through coding loops to get there.
I need to come up with an SQL statement that generates a sequence number (1,
2, 3, etc) (aka "row number") within a group of records having the same
(composite) key value, with separate numbering within each key value group.
For example, from the following:
K1 K2 data
1 ABC ...
1 DEF ...
1 GHI ...
2 any ...
2 more ...
3 justone
4 xyz
Output would look like this, generated row numbers within groups of K1
values:
K1 K2 data row#
1 ABC ... 1
1 DEF ... 2
1 GHI ... 3
2 any ... 1
2 more ... 2
4 xyz ... 1
I've looked at the IBM I documentation and done a search and the closest I
came up to what I need is here:
http://archive.midrange.com/midrange-l/201505/msg00438.html
And I found the SQL reference for this row-number() clause, but it has
nothing about row numbers within a group value (or "group partition")
select row_number() over (order by colb asc) as number, cola, colb, colc
from qtemp.mytable where cola is not null order by colb
NUMBER COLA COLB COLC
1 B 2 3
2 A 65 1
3 D 72 2
4 C 101 4
Alan Cassidy
Softare Engineer
Direct Phone - 954-693-0000 Ext. 3433
ACassidy@xxxxxxxxxxxxxx<mailto:ACassidy@xxxxxxxxxxxxxx>
www.hppartners.com<
http://www.hppartners.com/>
[HPP Logo FINAL with name-tag-logo-TM]
----------------------------------------------------------------------------
----
Confidentiality Notice: This email may contain confidential information or
information covered under the Privacy Act, 5 USC 552(a), and/or the Health
Insurance Portability and Accountability Act (PL 104-191) and its various
implementing regulations and must be protected in accordance with those
provisions. It contains information that is legally privileged, confidential
or otherwise protected from use or disclosure. This e-mail message,
including any attachments, is for the sole use of the intended recipient(s).
Any unauthorized review, use, disclosure or distribution is prohibited. You,
the recipient, are obligated to maintain it in a safe, secure and
confidential manner. If you are not the intended recipient, please contact
the sender by reply e-mail and destroy all copies of the original message.
Thank you.
----------------------------------------------------------------------------
----
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list
To post a message email: MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe,
or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting, please take a
moment to review the archives at
http://archive.midrange.com/midrange-l.
As an Amazon Associate we earn from qualifying purchases.