我有两个观点。
我的第一视图定义
CREATE VIEW FIRST_VIEW AS
SELECT PT.F_PRODUCT AS F_PRODUCT, PT.F_TEXT_CODE AS F_TEXT_CODE, PHT.F_PHRASE AS F_PHRASE,F_COUNTER COUNTER1 FROM T_PROD_TEXT PT
LEFT JOIN T_PHRASE_LINKAGE PHL
ON PT.F_TEXT_CODE = PHL.F_TEXT_CODE
INNER JOIN T_PHRASE_TRANSLATIONS PHT
ON PHL.F_PHRASE_ID = PHT.F_PHRASE_ID
WHERE PT.F_DATA_CODE = 'MANU' AND PHT.F_LANGUAGE = 'EN'
UNION ALL
SELECT PT.F_ALIAS AS F_PRODUCT, PT.F_TEXT_CODE AS F_TEXT_CODE, PHT.F_PHRASE AS F_PHRASE,F_COUNTER COUNTER2 FROM T_PROD_ALIAS_TEXT PT
LEFT JOIN T_PHRASE_LINKAGE PHL
ON PT.F_TEXT_CODE = PHL.F_TEXT_CODE
INNER JOIN T_PHRASE_TRANSLATIONS PHT
ON PHL.F_PHRASE_ID = PHT.F_PHRASE_ID
WHERE PT.F_DATA_CODE = 'MANU' AND PHT.F_LANGUAGE = 'EN'
上方视图在下面提供输出。
F_PRODUCT F_TEXT_CODE F_PHRASE COUNTER1
WVTST]PROD00|5 MANU0001 A 7734163
WVTST]PROD00|5 MANU0002 B 7734162
ABPROD-BLACK MANU0001 C 7714721
使用填充函数从上方视图(FIRST_VIEW)创建了另一个视图。
CREATE VIEW [dbo].[V1_STUFFED] AS
SELECT V1.F_PRODUCT,
F_TEXT_CODES = STUFF((SELECT DISTINCT ',' + V2.F_TEXT_CODE FROM FIRST_VIEW V2 WHERE V1.F_PRODUCT = V2.F_PRODUCT FOR XML PATH('')),1,1,''),
F_PHRASES = STUFF((SELECT DISTINCT ' |par ' + V3.F_PHRASE FROM FIRST_VIEW V3 WHERE V1.F_PRODUCT = V3.F_PRODUCT FOR XML PATH('')),1,5,'')
FROM FIRST_VIEW V1 GROUP BY V1.F_PRODUCT
上面的视图在输出下方给出。它将产品的F_TEXT_CODES和F_PHRASES合并为一行,其中包含不止一个f_text_codes。
F_PRODUCT F_TEXT_CODES F_PHRASES
WVTST]PROD00|5 MANU0001,MANU0002 A,B
ABPROD-BLACK MANU0001 C
但是我需要在输出下面。
F_PRODUCT F_TEXT_CODES F_PHRASES
WVTST]PROD00|5 MANU0002,MANU0001 B,A
ABPROD-BLACK MANU0001 C
在上面的Required输出中,F_TEXT_CODES(MANU0002)在(MANU0001)Text_Code之前显示,因为MANU002包含 Counter1值(7734162)和MANU0001包含COUNTER(7734163),因此F_TEXT_CODES和F_PHRASE将显示基于Counter1的基于F_PRODUCT的顺序。
在使用满足以上要求的东西功能时,如何使用order by子句?