Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7842149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:15:06+00:00 2026-06-02T16:15:06+00:00

I have a simple query. Something like this: SELECT l.list_name, COUNT(order_id) FROM orders o

  • 0

I have a simple query. Something like this:

SELECT l.list_name, COUNT(order_id) 
FROM orders o JOIN lists l ON l.order_id=o.order_id 
    WHERE l.list_name LIKE 'orders_1%' or l.list_name LIKE 'orders_2%'
GROUP BY l.list_name

The situation looks like this: overight a stored procedure is updating the lists table, but it chops lists in parts if there are more than 1000 orders.

If I have 1200 orders with criteria or list ‘orders_1’, then my procedure will create two lists: ‘orders_1_1’ ‘and orders_1_2’, the first having 1,000 and second 200 orders.

So when I run my query to count those orders I will get results like so:

list_name                  count

orders_1                   100
orders_1_more_than_100_1   1000       
orders_1_more_than_100_2   200
orders_2                   400 
orders_3_1                 1000       
orders_3_2                 1000
orders_3_3                 420  
orders_3_more_than_100_1   1000       
orders_3_more_than_100_2   900
orders_3_more_than_200_1   1000       
orders_3_more_than_200_2   1000
orders_3_more_than_200_3   100      
orders_4                   200
orders_4_more_than_300     200 

The result I would like to get should look like this:

list_name                  count

orders_1                   100
orders_1_more_than_100     1200       
orders_2                   400 
orders_3                   2420 
orders_3_more_than_100     1900 
orders_3_more_than_200     2100     
orders_4                   200 
orders_4_more_than_300     200 

So that it will sum all lists that start the same.

Any ideas on that? 🙂

These are the exact values that I have in my list_names column:

WYS_AUT_PISMO_NR_6
WYS_AUT_PISMO_NR_5_POWYZEJ_240
WYS_AUT_PISMO_NR_5_DO_240
WYS_AUT_PISMO_NR_4_POWYZEJ_240_5
WYS_AUT_PISMO_NR_4_POWYZEJ_240_4
WYS_AUT_PISMO_NR_4_POWYZEJ_240_3
WYS_AUT_PISMO_NR_4_POWYZEJ_240_2
WYS_AUT_PISMO_NR_4_POWYZEJ_240_1
WYS_AUT_PISMO_NR_4_DO_240
WYS_AUT_PISMO_NR_3_POWYZEJ_240
WYS_AUT_PISMO_NR_3_DO_240
WYS_AUT_PISMO_NR_2_POWYZEJ_240
WYS_AUT_PISMO_NR_2_DO_240
WYS_AUT_PISMO_NR_1

What I want is to group them like so:

WYS_AUT_PISMO_NR_6
WYS_AUT_PISMO_NR_5_POWYZEJ_240
WYS_AUT_PISMO_NR_5_DO_240
WYS_AUT_PISMO_NR_4_POWYZEJ_240 /*here I must group those 5 lists*/
WYS_AUT_PISMO_NR_4_DO_240
WYS_AUT_PISMO_NR_3_POWYZEJ_240
WYS_AUT_PISMO_NR_3_DO_240
WYS_AUT_PISMO_NR_2_POWYZEJ_240
WYS_AUT_PISMO_NR_2_DO_240
WYS_AUT_PISMO_NR_1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:15:07+00:00Added an answer on June 2, 2026 at 4:15 pm

    This monstruous expression will isolate string starting from beginning of parameter to last _ if there are more than one underscores:

    select case when len (l.list_name) - len (replace (l.list_name, '_', '')) > 1 
                then left(l.list_name, 
                          len (l.list_name) - charindex('_', reverse(l.list_name)))
                else l.list_name
            end
    

    Alternatively you might strip ‘orders_’ from string, replace underscore with dot and convert it to float, then to int to remove decimals, and then back to string using this monstrosity:

    select 'orders_' + cast (cast (cast (
            replace (substring (@str, 8, 100), '_', '.') 
            as float) as int) as varchar(100))
    

    To avoid repeating this blobs, use derived table instead of lists:

    SELECT l.TrimmedListName, COUNT(order_id) 
    FROM orders o 
    JOIN 
    (
       select lists.*,
           -- Remove optional list continuation number
              case when len (list_name) - len (replace (list_name, '_', '')) > 1 
                    then left(list_name, 
                              len (list_name) - charindex('_', reverse(list_name)))
                    else list_name
                end AS TrimmedListName
         from lists
    ) l ON l.order_id=o.order_id 
    WHERE (l.list_name LIKE 'orders_1%' or l.list_name LIKE 'orders_2%')
    GROUP BY l.TrimmedListName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple MySQL query that looks something like this SELECT * FROM
I have a simple query like this: select * from mytable where id >
I have this simple query: SELECT xObjectID, xObjectName FROM dbo.xObject where CONTAINS( xObjectRef, '1838
How can I do something like this in nHibernate: select count(*) from (subquery) It
I have a simple query in php on oracle: $query='select u.username,u.lastname,u.firstname,c.event,c.reason from users u,
I have a query that looks like the following SELECT t1.Name, t2.ID, t2.name FROM
This query fails when I add the line shown... Select Companyid, count(*) as cnt
I have already a query with multiple JOINs, simple list of reservations SELECT reservation.reservation_id,
I have a query that looks like the following: select c.[Name], c.Description, c.ID, cd.StartDateTime
I have below SQL query: SELECT * FROM ( SELECT S.aCol, S.bCol, S.cCol, ROW_NUMBER()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.