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 7574347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:24:52+00:00 2026-05-30T16:24:52+00:00

I’m writing a program for a running contest using SQLite database, and I have

  • 0

I’m writing a program for a running contest using SQLite database, and I have to determine teams from the finish data. There are schools taking part of the contest, and one team consist of four(exactly four) people from one school. A school can have more than one team. The team position is determined by the sum of the members positions. The contestants are stored in one table with their finish position name and school.

Can it be done by SQL query or should I solve it in code?

Example:
pos     name    school            pos    name     school
1      person1   foo               1     person1   foo
2      person2   foo               2     person2   foo
3      person3   bar               6     person6   foo
4      person4   bar               8     person8   foo
5      person5   bar     ->        3     person3   bar
6      person6   foo               4     person4   bar
7      person7   bar               5     person5   bar
8      person8   foo               7     person7   bar
9      person9   foo
10     person10  foo
11     person11  bar
  • 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-05-30T16:24:54+00:00Added an answer on May 30, 2026 at 4:24 pm

    I know there is nothing like ROW_NUMBER() OVER… in SQLite, but I cannot find anything about something similar to a CROSS APPLY.

    If there is something equivalent to a CROSS APPLY, then you can do the following. (EDIT: I noticed the requirement for schools to be able to have multiple teams. This solution would only work with one team per school. You will need a recursive CTE and ROW_NUMBER as far as I can tell, otherwise—which are not available in SQLite to my knowledge)

    SELECT  TeamTable.*
    FROM    Table
    CROSS APPLY
        (
            SELECT  TOP 4 *
            FROM Table AS InnerTable
            WHERE   InnerTable.school = Table.School
            ORDER BY InnerTable.Pos
        ) AS TeamTable
    

    If not, then you would probably have to use a while loop and temp tables to fill this. If that is the case, then there is no real gain from using the SQL and I would suggest going the code route.

    EDIT:
    However, this is the temp table solution as was requested. You need the inner while since you could have multiple teams within the school (something I had disregarded before and makes the CROSS APPLY solution not work without a recursive CTE and ROW_NUMBER, which has been edited to acknowledge)

    CREATE TABLE #SchoolList 
        (Id INT IDENTITY(1,1), School VARCHAR(50))
    
    INSERT INTO #SchoolList
    SELECT DISTINCT School
    FROM TeamTable
    
    CREATE TABLE #TeamList
        (TeamNumber INT IDENTITY(1,1), Pos INT, Name VARCHAR(50),
            School VARCHAR(50))
    
    DECLARE @CurrentSchool VARCHAR(50), @CurrentSchoolPos INT
    DECLARE @CurrentSchoolLookupId INT
    SET @CurrentSchoolId = 1
    WHILE EXISTS (SELECT 1 FROM #SchoolList WHERE Id > @CurrentSchoolLookupId)
    BEGIN
        SELECT @CurrentSchool = School FROM #SchoolList
            WHERE Id = @CurrentSchoolLookupId
        SET @CurrentSchoolPos = SELECT TOP 1 Pos FROM TeamTable 
                                WHERE School = @CurrentSchool 
                                ORDER BY POS
        WHILE ISNULL(@CurrentSchoolPos, 0) > 0
        BEGIN
            INSERT INTO #TeamList
            SELECT Pos, Name, School 
            FROM TeamTable 
            WHERE School = @CurrentSchool AND Pos = @CurrentSchoolPos
    
            SET @CurrentSchoolPos = SELECT TOP 1 Pos FROM TeamTable 
                                    WHERE School = @CurrentSchool 
                                        AND Pos > @CurrentSchoolPos ORDER BY POS
        END
        SET @CurrentSchoolLookupId = @CurrentSchoolLookupId + 1
    END
    
    SELECT * FROM #TeamList
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.