Select SQL 服务器中 Table 的子列表数量

Select Sublist Quantity from Table in SQL Server

我正在使用 SQL 服务器 table,在技术问题上我被卡住了。 下面我附上 screen-shot 的 table 查询和结果也是所需的逻辑。 查询有两个部分,一个是没有任何条件的,第二个是有条件的 select "qty" when parent_id is NULL else print 0,

我想打印 sub-item 的 "qty",当其数量与其父项的 "qty"

不同时

这是脚本:

        GO

        CREATE TABLE [dbo].[#Temp_order](
            [order_id] [int] IDENTITY(1,1) NOT NULL,
            [orderdate] [datetime] NULL
        ) ON [PRIMARY]

        GO
        GO

        CREATE TABLE [dbo].[#Temp_order_list](
            [order_list_id] [int] IDENTITY(1,1) NOT NULL,
            [order_id] [int] NOT NULL,
            [qty] [int] NOT NULL,
            [price] [money] NOT NULL,
            [type] [int] NOT NULL,
            [parent_id] [int] NULL,
         CONSTRAINT [PK_Temp_order_list] PRIMARY KEY CLUSTERED 
        (
            [order_list_id] ASC
        )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
        ) ON [PRIMARY]

        GO

        /*
        */
        INSERT INTO #Temp_order
                ( orderdate )
        VALUES  ( '2015-01-16 05:08:53'  -- orderdate - datetime
                  )
        INSERT INTO #Temp_order_list
                ( order_id, qty, price, type, parent_id )
        VALUES  ( (SELECT MAX(order_id) FROM #Temp_order), -- order_id - int
                  1, -- qty - int
                  10, -- price - money
                  2, -- type - int
                  NULL  -- parent_id - int
                  )  
        DECLARE @ParentID  AS INTEGER
        SELECT @ParentID =  MAX(order_list_id) FROM #Temp_order_list
        INSERT INTO #Temp_order_list
                ( order_id, qty, price, type, parent_id )
        VALUES  ( (SELECT MAX(order_id) FROM #Temp_order), -- order_id - int
                  1, -- qty - int
                  12, -- price - money
                  3, -- type - int
                   @ParentID -- parent_id - int
                  ) 
        INSERT INTO #Temp_order_list
                ( order_id, qty, price, type, parent_id )
        VALUES  ( (SELECT MAX(order_id) FROM #Temp_order), -- order_id - int
                  4, -- qty - int
                  13, -- price - money
                  3, -- type - int
                  @ParentID  -- parent_id - int
                  )    


        SELECT * FROM #Temp_order_list WHERE order_id = 1
        SELECT #Temp_order.order_id ,#Temp_order_list.order_list_id,#Temp_order_list.price,#Temp_order_list.type,
                CASE WHEN #Temp_order_list.parent_id IS NOT NULL THEN 0
                ELSE
                    ISNULL(#Temp_order_list.qty ,'') END AS quantity
                    FROM #Temp_order
                    INNER JOIN #Temp_order_list ON #Temp_order.order_id = #Temp_order_list.order_id

                    WHERE #Temp_order.order_id = 1

        DROP TABLE #Temp_order
        DROP TABLE #Temp_order_list

有更好的解决方案吗?

查看您最后的评论,我可以建议添加自连接并检查该条件:

SELECT ordr.order_id,
       list.order_list_id,
       list.price,
       list.type,
       CASE
         WHEN parentlist.order_list_id IS NOT NULL THEN list.qty
         WHEN list.parent_id IS NOT NULL THEN 0
         ELSE ISNULL(list.qty, '')
       END AS quantity
FROM   #Temp_order ordr
       INNER JOIN #Temp_order_list list
               ON ordr.order_id = list.order_id
       LEFT JOIN #Temp_order_list parentlist
              ON parentlist.order_list_id = list.parent_id
                 AND list.qty > parentlist.qty
                 AND list.type = 3
WHERE  ordr.order_id = 1