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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:32:39+00:00 2026-06-14T09:32:39+00:00

Following my [question]: T-SQL Query a matrix table for free position I’ve now trying

  • 0

Following my [question]: T-SQL Query a matrix table for free position
I’ve now trying to handle my matrix table as a LIFO. Each couple of (X,Z) represent a channel in which I can store an element. When I generate a location I’m now using the query provided in the above question and here below.

SELECT x, z, MAX(CASE WHEN disabled = 0 AND occupiedId IS NULL THEN Y ELSE 0 END) firstFreeY
FROM matrix
GROUP BY x, z
ORDER BY x, z;

This is working but it doesn’t handle “holes”. In fact It’s possible that a Disabled flag is removed from the table or an element is manually deleted.

In case my Matrix table will look like this:

X   Z   Y   Disabled    OccupiedId
--------------------------------------------------
1   1   1   0   591
1   1   2   0   NULL
1   1   3   1   NULL
1   1   4   0   524
1   1   5   0   523
1   1   6   0   522
1   1   7   0   484
1   2   1   0   NULL
1   2   2   0   NULL
1   2   3   0   NULL
1   2   4   0   NULL
1   2   5   0   NULL
1   2   6   0   589
1   2   7   0   592 

the result of the above query is:

X   Z   firstFreeY
------------------------
1   1   2
1   2   5

instead of:

X   Y   firstFreeY
------------------------
1   1   0
1   2   5

Any suggestions on how to achieve this?

  • 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-14T09:32:40+00:00Added an answer on June 14, 2026 at 9:32 am

    This query looks for the largest Y that is smaller than all other occupied Y’s:

    select  m1.X
    ,       m1.Z
    ,       max(
            case
            when m2.MinOccupiedY is null or m1.Y < m2.MinOccupiedY then m1.Y
            else 0
            end
            ) as FirstFreeY
    from    matrix m1
    join    (
            select  X
            ,       Z
            ,       min(
                    case 
                    when disabled <> 0 or occupiedId is not null then Y
                    end
                    ) as MinOccupiedY
            from    matrix 
            group by
                    X
            ,       Z
            ) m2
    on      m1.X = m2.X
            and m1.Z = m2.Z
    group by
            m1.X
    ,       m1.Z
    

    Live example at SQL Fiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Updated question: Django is giving me the following sql query: SELECT auth_user.id, auth_user.username, auth_user.first_name,
Following on from my last question Sql Server query performance , and discovering that
I am stuck trying to build the following SQL query in JPA (still new
I have a situation similar to the following question: Insert Data Into SQL Table
I'm trying to implement the following SQL query with QueryOver: SELECT [Time]/1000 FROM TableName
I have the following recursion code, at each node I call sql query to
Good day, my question is about optimizing sql query. The following query is slow:
I'm trying to write a sql query to the following (from this site http://sqlzoo.net/1b.htm
I am trying to write the following SQL query as a JPA query. The
I have the following Linq-to-SQL query. On line #5 I'm trying to get the

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.