选择查询中的 SQL 条件变量

标签 sql sql-server greatest-n-per-group nested-sets

我正在尝试编写一个使用 Nested Interval Hierarchy 的 SQL 查询数据库模型。

--Given a parent Id, this query retrieves the position of the youngest child which can be inserted into the table
SELECT TOP 1
    --compute values based on the youngest "sibling id" (the one with the highest value)
    (parent.m_11 * (FLOOR(child.m_11/child.m_12)+2) - parent.m_12) as m_11,
    (parent.m_11) as m_12, 
    (parent.m_21 * (FLOOR(child.m_11/child.m_12)+2) - parent.m_22) as m_21,
    (parent.m_21) as m_22
    FROM my_so_table child
        --grabs all children of the parent
        JOIN my_so_table parent
          ON parent.Id = 1
         AND parent.m_21 = child.m_22
         AND parent.m_11 = child.m_12
    --the operation "Floor(child.m_11 / child.m_12) is the siblingId that I need to do math on above
    ORDER By FLOOR(child.m_11/child.m_12) DESC
GO

有模式:

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[my_so_table](
    [m_11] [int] NOT NULL,
    [m_12] [int] NOT NULL,
    [m_21] [int] NOT NULL,
    [m_22] [int] NOT NULL,
    [Id] [int] IDENTITY(1,1) NOT NULL)
GO

INSERT INTO [dbo].[my_so_table] VALUES (2,1,1,0); --1.
INSERT INTO [dbo].[my_so_table] VALUES (3,1,1,0); --2.
INSERT INTO [dbo].[my_so_table] VALUES (3,2,2,1); --1.1
INSERT INTO [dbo].[my_so_table] VALUES (4,1,1,0); --3.

使用上述架构,使用 1 的 parentId 运行上述查询正确返回

5,2,3,1

它正确地表示要插入到 Id 为 1 的父节点下的新节点的矩阵。在 2 的 parentId 上运行上述查询返回一个空列表,当它应该返回时

5,3,2,1

表示 id 为 2 的父项下的第一个子项的矩阵。

这是错误的,因为我的查询没有处理没有父项的子项的情况。如果没有 parent 的 child ,Floor(child.m_11 / child.m_12)的结果应该是-1。我如何更改我的查询以完成此操作?

最佳答案

我找到了我的解决方案。我一直在回避 greatest-n-by-group,而实际上我不明白 group-by 是如何工作的。

--Given a parent Id, this query retrieves the position of the youngest child which can be inserted into the table
--insert into my_so_table(m_11, m_12, m_21, m_22)
SELECT TOP 1
    --compute values based on the youngest 'sibling id' (the one with the highest value)
    (parent.m_11 * (ISNULL(siblingId, 0) + 2) - parent.m_12) as m_11,
    (parent.m_11) as m_12, 
    (parent.m_21 * (ISNULL(siblingId, 0) + 2) - parent.m_22) as m_21,
    (parent.m_21) as m_22
    FROM my_so_table parent
        --grabs all children of the parent
        LEFT JOIN (
            --Grabs the youngest sibling for each sibling chain
            SELECT
                child.m_12,
                child.m_22,
                Max(Floor(child.m_11 / child.m_12)) as siblingId
            FROM my_so_table child
            Group By child.m_12, child.m_22
        ) child
        on(parent.m_21 = child.m_22)
        AND(parent.m_11 = child.m_12)
    WHERE parent.Id = @parentId
    ORDER By siblingId DESC
GO

group-by 之前没有工作,因为我无法从子查询中检索到 m_12m_22,因为我不是t group by这两个值。切换到左连接会导致报告空值,这正是我所需要的!

关于选择查询中的 SQL 条件变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42141154/

相关文章:

mysql - 使用 SQL 计算出特定事件发生的次数百分比

sql - 为个人用户选择不同的 MAX NoteID

php - IS NULL 在列中找不到空行

php - 如何从 AJAX 调用中检索数组?

sql - 请求错误: Validation failed for parameter- Invalid buffer

c# - 如何将 int 转换为字节数组?

sql - 在一对多关系中,根据 MIN 值返回不同的行

sql - 寻找锦标赛中每个玩家组的获胜者 - PostgreSQL

JavaPreparedStatement语法问题

mysql - 在 SQL 中查询 JSON 字符串