回答

收藏

Row_Number()需要动态枢轴

技术问答 技术问答 113 人阅读 | 0 人回复 | 2023-09-13

我正在使用Microsoft SQL Server Management Studio 2008。" A& Y3 T( a5 R2 Q
我有看起来像这样的数据:$ r' q6 s6 L9 g" D! u* X  e9 R
Client ID        Value" e( h! }/ a; w6 B
-------------------------------
* O+ W! v- t7 f2 F4 ?2 l) @( ]12345            Did Not Meet
( ~4 x( I  C9 f12345            Did Not Meet/ u0 s9 t' q: P  x+ ]' A- h/ N, }
12345            Partially Met
' S+ D5 Z6 R3 `. P) u12346            Partially Met
; i2 p: A% ^, p12346            Partially Met
# A+ {) o% ?$ d4 [6 Z" Y12346            Partially Met
0 R  n. p, u5 d1 }, a& S# W( N12347            Partially Met
& b  _" b  U& K) Y# `) P12347            Partially Met3 c8 ^( ?' _6 W) e" h  E0 u
12347            Did Not Meet
$ }2 F, M( o) n7 i: }, Q5 N12347            Met
4 s. U5 L$ M2 o/ F/ ^8 ?! |我希望结果显示如下:+ t! p9 X, [9 S
Client ID                Value1                Value2                   Value3                      Value4
+ F; _. n) N: a2 M2 I$ O12345                    Did Not Meet           Did Not Meet            Partially Met                NULL
- j- S4 S5 P/ D# E) C  k12346                    Partially Met          Partially Met           Partially Met                NULL7 s5 U1 l/ U( f/ [" W* L4 R
12347                    Partially Met          Partially Met           Did Not Meet                 Met0 o- [* ~2 Q: h0 X% ^' n8 F
这些列是未知的,所以我知道我需要一个动态查询。我已经尝试过使用数据透视功能进行动态查询,但只在相同类型的值下进行分组。所以聚合函数对我不利。
& R- v. Q6 B+ g* |1 H4 H这是我尝试过的查询:
' ~9 O* l% Z! Y* \+ h! h# L% MDeclare @Columns nvarchar(max);
  }. s0 i' n- C7 f" wDeclare @DynamicPivotQuery nvarchar(max);
5 q& @5 W& D' b8 P4 L5 I, G: MSelect @Columns=   
9 D' u& h" H; @9 N# p- v( |COALESCE(@Columns+',','')+QUOTENAME(Value)  
- Y  t4 U2 U( A' b1 _: r3 V: Kfrom (select distinct Document.Value
# Q% F+ u8 C  xfrom Document d 1 g" Z) H  }- a! B
join Client c on d.clientid=c.id   
$ _6 W2 `4 X( C& X6 x# q    )
4 n! v. q, n! @( U as t1' _3 n6 |3 E2 v/ W( M4 |2 r, r; W

4 d5 D% B3 c4 Y/ VSet @DynamicPivotQuery= ) M3 j* a7 R  W/ e1 z# O
N'Select ClientID, ' + @Columns + '    8 ]4 ~* k& U7 `
from   
: z% z1 n5 x$ I; C2 `$ n (select Document.ClientID,
& [! n6 T" Z- Z2 ?7 I  e  s  DocumentFact.Value,   
1 b& s  r1 J) E% i5 d, z9 {   from Document d
1 e: n; o( R' {  n1 W4 v1 U% o   join Client c on d.clientid=c.id
$ \) _$ j% B6 Z* L- Z9 z ) p    4 p1 z9 L5 O3 L2 x
Pivot (max(Value) for Value in ('+@Columns+'))  ) ]- Q% E7 \* b/ l) ]
as pivottable  
, G% _5 Z4 M: L* c4 H order by ClientID;'7 F3 c% H2 G; Q5 k
执行(@DynamicPivotQuery): @& R6 Y1 Y# A/ @
接下来,我添加了row_number和partition函数,但似乎无法对其进行调试。我得到的错误是:6 B3 h& d; }% h8 y9 ?- w

- J/ C) F3 d' a& E. A& X消息102,级别15,状态1,第29行
. A# y- n/ z' A5 f+ F2 m: h ‘)’附近的语法不正确。
. i) W5 ^$ j" y

3 m8 s, {/ ~& X) T/ T' `$ ?靠近XML Path函数。3 O$ R( }! L3 t, R! I
任何帮助,将不胜感激。谢谢。
( z8 n9 d! X+ w' L; bselect @Columns=   
  D) ^& w% i! y4 K4 pCOALESCE(@Columns+',','')+QUOTENAME(Value)  
7 G) u6 E5 {, l3 W! I' S5 Yfrom (select distinct Document.Value    ! K! j. f9 ?1 @! _& d
, 'name'+ CAST (row_number() over   
1 C; ~# t2 B7 h0 B! K(Partition BY clientid order by clientid) as NVARCHAR (10)) as Cols
& W2 o4 U5 p# jfrom document d
6 [1 I; A7 i. U% k) P+ D0 b, mjoin Clients c on d.clientid=c.id& s7 {" _2 H, B6 S
t1- X& z5 Q& C; K* g; V
--FOR XML PATH('')),      1, 1, N'');% h$ G! |5 G7 X! F0 h
FOR XML PATH('')), TYPE).value('.','NVARCHAR(MAX)'),1,2,'')   
3 y- b2 ~. b  ^2 ~/ p  order by ClientID
$ f8 o2 \3 f( [$ [" r0 c               
# b( Z- O! j: n" |) I  D4 D解决方案:8 y& v4 R& k5 B7 U# A; x" W
                : z, p4 _# K4 J
$ c6 D9 N$ |/ O; u6 z: ^' R
; c2 _9 H# X6 \, |
                与一起使用cte,row_number您可以达到以下结果:4 y' c, C4 ?  f
您的架构:1 [, v$ v+ Z+ ~: g! J
create table your_table([Client ID] int ,Value varchar(50));
7 e, B2 h# \5 l0 h0 I  oinsert into your_table values
) A5 U2 P4 U* n% w+ C(12345,            'Did Not Meet'),
- ]9 V* W" w" X8 h( V) w(12345,            'Did Not Meet'),; ~* o  o1 W  v& V0 J- l1 }
(12345,            'Partially Met'),2 B: m1 c/ C: S& A3 \
(12346,            'Partially Met'),
8 ]. G9 M' l8 N$ t+ q* n0 F(12346,            'Partially Met'),$ n7 d+ \; F+ P% l. {' ]0 N$ J
(12346,            'Partially Met'),! A3 e0 U9 ]. X& M' d6 x( O
(12347,            'Partially Met'),' G6 j7 T0 }+ ~+ i
(12347,            'Partially Met'),+ o: A4 }& Q0 A* R- D
(12347,            'Did Not Meet'),, m0 r3 g) Q" D; r4 Q% E
(12347,            'Met');  ~; q0 y: D$ n5 H! b+ p* K8 @4 y
查询:
/ F# T7 V" n( zwith cte as+ I/ p) O# r  z+ M; h. b
(% Q( g7 w7 U& \8 H
select [Client ID] ci,value,3 C  W- `4 C/ P
        row_number() over(partition by [Client ID] order by value) as rn) C9 X& _$ O/ O/ [# f0 C3 A
from your_table
3 B8 o% p) O" [* @/ `)" d: \$ J3 H$ c4 s4 K
select distinct ci as [Client ID],; L: ]) \5 i$ Q% t, J# y
       (select ct.value from cte ct where ct.ci=cte.ci and ct.rn=1) value1,+ s/ o. n& q+ v$ o9 ~. x* W
       (select ct.value from cte ct where ct.ci=cte.ci and ct.rn=2) value2,
( }# k5 O# ~, v% R+ o- c4 D: x, O       (select ct.value from cte ct where ct.ci=cte.ci and ct.rn=3) value3,/ X' h9 F, _9 S
       (select ct.value from cte ct where ct.ci=cte.ci and ct.rn=4) value4
" c/ D& t+ p4 e- yfrom cte
2 s: Q* y! |7 F7 j5 v* P结果:
1 |& V, ?  }( q' ?  aClient ID   value1          value2          value3          value4
3 S% {% ?5 X' L+ t- s' l  m12345       Did Not Meet    Did Not Meet    Partially Met   (null)1 w0 \$ m& K  v. {- z
12346       Partially Met   Partially Met   Partially Met   (null)- `( E" n- [) n8 w: {8 K
12347       Did Not Meet    Met Partially   Met Partially    Met
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则