返回前4行,然后重复进行分组

时间:2015-03-20 14:26:38

标签: sql sql-server tsql

我正在尝试返回最终会填充标签的数据。 每个标签都进入一个盒子,盒子里面只能有4个物品。 如果交货超过4件,那么每4件我需要一个标签。

返回的每一行数据都会填充一个标签,因此如果交付包含9个项目,那么我需要返回3行数据。

下面是我当前的查询,它使用Stuff将所有项目返回到逗号分隔值。

我想要它,所以第一行中的前4行返回,然后是第二行中的下一行,依此类推。

如果调度中有超过4行,我的Field LineOrd会正确返回。

select Distinct
delivery_header.dh_datetime,
delivery_header.dh_number,
order_header.oh_order_number as 'Order No',
order_header_detail.ohd_delivery_name,
order_header_detail.ohd_delivery_address1,
order_header_detail.ohd_delivery_address2,
order_header_detail.ohd_delivery_address3,
order_header_detail.ohd_delivery_town,
order_header_detail.ohd_delivery_county,
order_header_detail.ohd_delivery_postcode,
order_header_detail.ohd_delivery_country,


STUFF((Select  ', '+convert(varchar(50),convert(decimal(8,0),DL.dli_qty))+'x '+OLI.oli_description  
from delivery_header DH join delivery_line_item DL on DL.dli_dh_id = DH.dh_id join order_line_item OLI on OLI.oli_id = DL.dli_oli_id 
Outer APPLY
(select 
case when DelCurLine.CurLine <= 4 
    then '1'
    Else 
        Case when DelCurLine.CurLine <= 8 
        then '2'
        Else '3' 
        End
    End +'-'+order_header.oh_order_number as LineOrd) as StuffLineOrder 

Where DH.dh_id = delivery_header.dh_id And StuffLineOrder.LineOrd = LineOrder.LineOrd
FOR XML PATH('')),1,1,'')  as Items,

LineOrder.LineOrd



from delivery_header
join delivery_line_item on delivery_line_item.dli_dh_id = delivery_header.dh_id
join order_line_item on order_line_item.oli_id = delivery_line_item.dli_oli_id
join order_header on order_header.oh_id = order_line_item.oli_oh_id
join order_header_detail on order_header_detail.ohd_oh_id = order_header.oh_id
join variant_detail on variant_detail.vad_id = order_line_item.oli_vad_id
join stock_location on stock_location.sl_id = order_line_item.oli_sl_id
Outer APPLY
    (select count(DLI.dli_id) CurLine from delivery_line_item DLI where DLI.dli_dh_id = delivery_header.dh_id and DLI.dli_id <= delivery_line_item.dli_id)
     as DelCurLine


Outer APPLY
(select 
case when DelCurLine.CurLine <= 4 
    then '1'
    Else 
        Case when DelCurLine.CurLine <= 8 
        then '2'
        Else '3' 
        End
    End +'-'+order_header.oh_order_number as LineOrd) as LineOrder   

Outer APPLY
(select convert(varchar(50),convert(decimal(8,0),delivery_line_item.dli_qty))+'x '+order_line_item.oli_description as LineName) as LineName 

where 
delivery_header.dh_datetime between @DateFrom and @DateTo
and stock_location.sl_id = @StockLoc
and (order_header.oh_order_number = @OrderNo or @AllOrder = 1)


order by
delivery_header.dh_datetime,
delivery_header.dh_number,
order_header.oh_order_number,
order_header_detail.ohd_delivery_name,
order_header_detail.ohd_delivery_address1,
order_header_detail.ohd_delivery_address2,
order_header_detail.ohd_delivery_address3,
order_header_detail.ohd_delivery_town,
order_header_detail.ohd_delivery_county,
order_header_detail.ohd_delivery_postcode,
order_header_detail.ohd_delivery_country

1 个答案:

答案 0 :(得分:0)

你可以使用除以4的ROW_NUMBER()。这会截断小数,因为numerator是一个整数。这为您提供了每组最多四行的组号。然后,您可以调整查询以在&#34;组中使用此组号。将分组行返回到单个行的子句。

例如:

SELECT  RawData.BoxGroup,
        MIN(dh_datetime),
        MIN(dh_number),
        MIN(order_header.oh_order_number) as 'Order No'
        --And so on
FROM 
    (SELECT BoxGroup = (ROW_NUMBER() OVER(ORDER BY (SELECT 1)) - 1) / 4,
            *
     FROM [TableNameOrQuery]) AS RawData
GROUP BY RawData.BoxGroup

希望得到这个帮助。