动态添加列到SQL查询

时间:2018-01-23 11:48:55

标签: sql-server

我正在观看代表预约活动中的会议。我希望动态列出事件中的每个会话,以查看每个代表预订了多少票证,但会话数量因事件而异。

我目前通过LEFT JOIN中的子查询添加有关会话的列,但这意味着我必须手动添加每个会话。

有没有办法动态列出事件中的会话以便在where子句中使用?

更好的是,将动态列出列...我看起来是使用PIVOT,但这似乎并没有帮助我作为会议中的代表。

下面是创建表和插入数据的SQL代码 - 目前只有一个会话链接,但是可以动态列出这个的解决方案很棒。

感谢您的任何指示! (对于错误格式化,我的第一个更复杂的查询道歉!)

创建表/数据的代码:

CREATE TABLE delegate(
   DELEGATE_REF      INTEGER  NOT NULL PRIMARY KEY 
  ,code              INTEGER  NOT NULL
  ,name              VARCHAR(10) NOT NULL
  ,MEMBER_REF        INTEGER  NOT NULL
  ,TOTAL_AMOUNT      INTEGER  NOT NULL
  ,DELEGATE_SESS_REF INTEGER  NOT NULL
  ,EVENT_REF         INTEGER  NOT NULL
);
INSERT INTO delegate(DELEGATE_REF,code,name,MEMBER_REF,TOTAL_AMOUNT,DELEGATE_SESS_REF,EVENT_REF) VALUES (26174,51,'Delegate A',1077419,280,58136,378);
INSERT INTO delegate(DELEGATE_REF,code,name,MEMBER_REF,TOTAL_AMOUNT,DELEGATE_SESS_REF,EVENT_REF) VALUES (26183,52,'Delegate B',1110544,302,58157,378);
INSERT INTO delegate(DELEGATE_REF,code,name,MEMBER_REF,TOTAL_AMOUNT,DELEGATE_SESS_REF,EVENT_REF) VALUES (26206,53,'Delegate C',1084626,169,58209,378);
INSERT INTO delegate(DELEGATE_REF,code,name,MEMBER_REF,TOTAL_AMOUNT,DELEGATE_SESS_REF,EVENT_REF) VALUES (26210,54,'Delegate D',1092456,257,58218,378);
INSERT INTO delegate(DELEGATE_REF,code,name,MEMBER_REF,TOTAL_AMOUNT,DELEGATE_SESS_REF,EVENT_REF) VALUES (26212,55,'Delegate E',1055867,221,58223,378);
INSERT INTO delegate(DELEGATE_REF,code,name,MEMBER_REF,TOTAL_AMOUNT,DELEGATE_SESS_REF,EVENT_REF) VALUES (26220,56,'Delegate F',1109833,169,58240,378);
INSERT INTO delegate(DELEGATE_REF,code,name,MEMBER_REF,TOTAL_AMOUNT,DELEGATE_SESS_REF,EVENT_REF) VALUES (26229,57,'Delegate G',266050,0,58258,378);
INSERT INTO delegate(DELEGATE_REF,code,name,MEMBER_REF,TOTAL_AMOUNT,DELEGATE_SESS_REF,EVENT_REF) VALUES (26230,58,'Delegate H',1110868,0,58260,378);
INSERT INTO delegate(DELEGATE_REF,code,name,MEMBER_REF,TOTAL_AMOUNT,DELEGATE_SESS_REF,EVENT_REF) VALUES (26231,59,'Delegate I',1110890,0,58262,378);
INSERT INTO delegate(DELEGATE_REF,code,name,MEMBER_REF,TOTAL_AMOUNT,DELEGATE_SESS_REF,EVENT_REF) VALUES (26232,60,'Delegate J',1110891,0,58264,378);

CREATE TABLE event(
   code      VARCHAR(6) NOT NULL
  ,event_ref INTEGER  NOT NULL PRIMARY KEY 
  ,name      VARCHAR(12) NOT NULL
);
INSERT INTO event(code,event_ref,name) VALUES ('AC2017',378,'MyConference');


CREATE TABLE delegate_session(
   DELEGATE_REF      INTEGER  NOT NULL
  ,DELEGATE_SESS_REF INTEGER  NOT NULL PRIMARY KEY 
  ,SESSION_REF       INTEGER  NOT NULL
);
INSERT INTO delegate_session(DELEGATE_REF,DELEGATE_SESS_REF,SESSION_REF) VALUES (26183,58157,460);
INSERT INTO delegate_session(DELEGATE_REF,DELEGATE_SESS_REF,SESSION_REF) VALUES (26206,58209,460);
INSERT INTO delegate_session(DELEGATE_REF,DELEGATE_SESS_REF,SESSION_REF) VALUES (26212,58223,460);
INSERT INTO delegate_session(DELEGATE_REF,DELEGATE_SESS_REF,SESSION_REF) VALUES (26220,58240,460);
INSERT INTO delegate_session(DELEGATE_REF,DELEGATE_SESS_REF,SESSION_REF) VALUES (26229,58258,460);
INSERT INTO delegate_session(DELEGATE_REF,DELEGATE_SESS_REF,SESSION_REF) VALUES (26230,58260,460);
INSERT INTO delegate_session(DELEGATE_REF,DELEGATE_SESS_REF,SESSION_REF) VALUES (26231,58262,460);
INSERT INTO delegate_session(DELEGATE_REF,DELEGATE_SESS_REF,SESSION_REF) VALUES (26232,58264,460);
INSERT INTO delegate_session(DELEGATE_REF,DELEGATE_SESS_REF,SESSION_REF) VALUES (26174,58136,460);
INSERT INTO delegate_session(DELEGATE_REF,DELEGATE_SESS_REF,SESSION_REF) VALUES (26210,58218,460);

CREATE TABLE session(
   SESSION_REF INTEGER  NOT NULL PRIMARY KEY 
  ,NAME        VARCHAR(16) NOT NULL
);
INSERT INTO session(SESSION_REF,NAME) VALUES (460,'Delegate booking');

这是我目前用于通过子查询手动拉入会话的查询:

select e.NAME, d.code, d.name, d.MEMBER_REF, d.TOTAL_AMOUNT, x1.t1 as 'Session 1'

from DELEGATE as d
INNER JOIN EVENT as e on d.EVENT_REF=e.EVENT_REF

left join (select d.DELEGATE_REF, s.name as 't1'
FROM DELEGATE as d
INNER JOIN DELEGATE_SESSION as ds on d.DELEGATE_REF=ds.DELEGATE_REF
INNER JOIN SESSION as s on ds.SESSION_REF=s.SESSION_REF
where s.SESSION_REF=460
) as x1 on d.DELEGATE_REF=x1.delegate_ref

where d.code > 50 and d.code < 61 and e.code like 'ac2017'

group by e.NAME, d.code, d.name, d.MEMBER_REF, d.TOTAL_AMOUNT, x1.t1

这是它产生的格式化输出,我想动态生成:

<table><tbody><tr><th>NAME</th><th>code</th><th>name</th><th>MEMBER_REF</th><th>TOTAL_AMOUNT</th><th>Session 1</th><th>Number</th><th>Rate</th></tr><tr><td>My Main Event</td><td>51</td><td>Delegate A</td><td>1077419</td><td>280</td><td>Delegate booking</td><td>1</td><td>Standard</td></tr><tr><td>My Main Event</td><td>52</td><td>Delegate B</td><td>1110544</td><td>302</td><td>Delegate booking</td><td>1</td><td>Standard</td></tr><tr><td>My Main Event</td><td>53</td><td>Delegate C</td><td>1084626</td><td>169</td><td>Delegate booking</td><td>1</td><td>Standard</td></tr><tr><td>My Main Event</td><td>54</td><td>Delegate D</td><td>1092456</td><td>257</td><td>Delegate booking</td><td>1</td><td>Standard</td></tr><tr><td>My Main Event</td><td>55</td><td>Delegate E</td><td>1055867</td><td>221</td><td>Delegate booking</td><td>1</td><td>Standard</td></tr><tr><td>My Main Event</td><td>56</td><td>Delegate F</td><td>1109833</td><td>169</td><td>Delegate booking</td><td>1</td><td>Standard</td></tr><tr><td>My Main Event</td><td>57</td><td>Delegate G</td><td>266050</td><td>0</td><td>Delegate booking</td><td>1</td><td>Standard</td></tr><tr><td>My Main Event</td><td>58</td><td>Delegate H</td><td>1110868</td><td>0</td><td>Delegate booking</td><td>1</td><td>Standard</td></tr><tr><td>My Main Event</td><td>59</td><td>Delegate I</td><td>1110890</td><td>0</td><td>Delegate booking</td><td>1</td><td>Standard</td></tr><tr><td>My Main Event</td><td>60</td><td>Delegate J</td><td>1110891</td><td>0</td><td>Delegate booking</td><td>1</td><td>Standard</td></tr></tbody></table>

0 个答案:

没有答案