如何将单位显示为同一显示顺序的第二个显示项?

txu3uszq  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(412)

我在SQLServer2012上工作,我面临一个问题:我不能将包含单元的功能安排为同一显示顺序的第二项显示。
以特征族为例

'Family' as [Family], 'FamilyMaxValue' as [FamilyMaxValue], 'FamilyUnit' as [FamilyUnit]

我需要它如下

'Family' as [Family], 'FamilyUnit' as [FamilyUnit],'FamilyMaxValue' as [FamilyMaxValue]

功能和单位和最大值得到相同的显示顺序,但我的问题
如何获得单位作为第二显示相同的显示顺序。
意思改变将是以逗号分隔的项目顺序,以显示特征单元作为同一显示顺序的第二个显示。

create table #SplitNumberAndUnitsFinal
(
DKFeatureName  nvarchar(100),
DisplayOrder  int
)
insert into #SplitNumberAndUnitsFinal (DKFeatureName,DisplayOrder)
values
('package',1),
('packageUnit',1),
('Family',2),
('FamilyMaxValue',2),
('FamilyUnit',2),
('parts',3),
('partsMaxValue',3),
('partsUnit',3)
DECLARE @Header nvarchar(max)=( select
substring(
    (
        Select  ', '''+ DKFeatureName +''' as ['+ DKFeatureName +']' AS [text()]
        From #SplitNumberAndUnitsFinal 
        GROUP BY DKFeatureName
        ORDER BY MIN(DisplayOrder)
        --order by DisplayOrder

        For XML PATH ('')
    ), 2, 10000) [Columns])
    print @Header

预期结果将特征单元排列为第二个显示,如下所示:

Feature,FeatureUnit,FeatureMaxValue according to same display Order

'package' as [package], 'packageUnit' as [packageUnit],
'Family' as [Family], 'FamilyUnit' as [FamilyUnit],'FamilyMaxValue' as [FamilyMaxValue],
'parts' as [parts], 'partsUnit' as [partsUnit],'partsMaxValue' as [partsMaxValue]

我不需要显示如下:

Feature,FeatureMaxValue,FeatureUnit for same Display Order

'package' as [package], 'packageUnit' as [packageUnit],
 'Family' as [Family], 'FamilyMaxValue' as [FamilyMaxValue], 'FamilyUnit' as [FamilyUnit],
 'parts' as [parts], 'partsMaxValue' as [partsMaxValue], 'partsUnit' as [partsUnit]
lo8azlld

lo8azlld1#

如果需要,则需要一个附加列来控制显示顺序 DisplayOrder 孤独不是唯一的。这将允许您在 ORDER BY 所需订单的条款:

CREATE TABLE #SplitNumberAndUnitsFinal
(
    DKFeatureName  nvarchar(100),
    DisplayOrder  int,
    SecondaryDisplayOrder  int
);
INSERT INTO #SplitNumberAndUnitsFinal (DKFeatureName,DisplayOrder,SecondaryDisplayOrder)
VALUES
    ('package',1,1),
    ('packageUnit',1,2),
    ('Family',2,1),
    ('FamilyMaxValue',2,3),
    ('FamilyUnit',2,2),
    ('parts',3,1),
    ('partsMaxValue',3,2),
    ('partsUnit',3,3)
DECLARE @Header nvarchar(MAX)=( select
SUBSTRING(
    (
        SELECT  ', '''+ DKFeatureName +''' as ['+ DKFeatureName +']' AS [text()]
        FROM #SplitNumberAndUnitsFinal 
        ORDER BY DisplayOrder,SecondaryDisplayOrder
        FOR XML PATH ('')
    ), 2, 10000) [Columns]);
PRINT @Header;

相关问题