473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Select a group of numbers in sequence

I have a Postgresql table with the following structure:

id | value
----+--------
1 | 1
2 | 2
3 | 3
4 | 4
5 | 1
6 | 2
7 | 3
8 | 1
9 | 2
10 | 3
11 | 4
12 | 5

I need to group these rows by (value) numbers in sequence. So I need to query the table and return
rows with ids 1-4 in one group, rows with ids 5 -7 in another group, and rows with ids 8-12 in another group.
Sep 26 '14 #1

✓ answered by Rabbit

Left join the table to itself on the current id to the next id. Keep the one where the value in the second instance is a 1 or is a null. That will give you just the last number in each sequence. Then you just need to create a calculated column that appends the string '1-' before the current value.

1 2028
Rabbit
12,516 Expert Mod 8TB
Left join the table to itself on the current id to the next id. Keep the one where the value in the second instance is a 1 or is a null. That will give you just the last number in each sequence. Then you just need to create a calculated column that appends the string '1-' before the current value.
Sep 26 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Disco | last post by:
Hi, I would like to be able to do something like this... select name,dob from employees where > 10 and < 20; .... results from this would show me data from the 10th, 11th,... and 19th...
0
by: cooldv | last post by:
i have an *access database + ASP newsletter* that is working fine and it sends newsletter to ALL the email addresses in the DB. i want to send this newsletter to only a select group (like people...
4
by: Mike Nolan | last post by:
I'm trying to create a summary log by hour. Here's the query (somewhat simplified): select to_char(mtrantime,'mm-dd hh AM') as datetime, count(*) as tot from memtran group by datetime order...
5
by: jacob.dba | last post by:
I have a table with first name, last name, SSN(social security number) and other columns. I want to assign group number according to this business logic. 1. Records with equal SSN and (similar...
10
by: Chris | last post by:
I am using an access database and have range of years for customers and want to group them together. for example: 1990 1991 1992 1993 1994 1998
1
by: ywangmda | last post by:
Hi everyone! I am a new user of Crystal Reports and have a question on Crystal Reports 10. I have a set of data with values such as 1.01 to 400.99, and would like to group them in every 1, that is,...
2
by: gimme_this_gimme_that | last post by:
The following statement doesn't work and I need a tip. Here is a draft: This selects data from the z table. And fetches the next value for r_id_seq and creates a table named b. select...
2
by: pedalpete | last post by:
I seem to have a few duplicates entries in a very large database. My database has two identical tables. One is written to on a regular basis (every few seconds) and the other is used for queries,...
2
by: RolfK | last post by:
Dear ALL, I need some help on xsl:sequence. I'm using the Altova XSLT processor, but I'm quite confident it is not an processor issue. It's probably my bad knowledge of xslt 2.0. I have probably...
1
by: Al Holliday | last post by:
I have the following SELECT statement SELECT MU.SUBID, MU.LOCID, MU.MENUCAT, MU.MENUNAME, MU.RECIPENAME, ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.