Tsql 质询 表1 表2 结果

Tsql 质询 表1 表2 结果,tsql,Tsql,我已经试过了 ;with T as (select Table2.ref-id, Table2.log_stamp, Table2 log.log_type when 1 then '1' when 2 then '2' when 3 then '3' end as title from Submission sb inner join submission_log log o

我已经试过了

;with T as (select
    Table2.ref-id, 
        Table2.log_stamp,
        Table2 log.log_type
        when 1 then '1'
        when 2 then '2'    
        when 3 then '3'

    end as title

from 
    Submission sb inner join submission_log log on Table1.[sub-id] = Table2.[sub-id]
)

select * from T
pivot (
    max(log_stamp)
    for title IN ([1],[2],[3],[5],[6],[9],[11])

我不能把它作为一个支点,我不认为这是可能的描述

DECLARE @table1 TABLE (sub_id INT, ref_id INT, name VARCHAR(50))
INSERT @table1 VALUES (1, 1, 'Project 1')
INSERT @table1 VALUES (2, 1, 'Project 2')   
INSERT @table1 VALUES (3, 2, 'Project 3' )
INSERT @table1 VALUES (4, 2, 'Project 4') 

DECLARE @Table2 TABLE (sub_id INT, ref_id INT, log_stamp DATETIME, recepient VARCHAR(10), logtype INT)

INSERT @table2 VALUES(1,1,'06/06/2011','person A',1) 
INSERT @table2 VALUES(1,1,'06/14/2011','person B',2) 
INSERT @table2 VALUES(1,1,'06/16/2011','person C',2)
INSERT @table2 VALUES(1,1,'06/17/2011','person D',3)
INSERT @table2 VALUES(2,1,'06/18/2011','person E',2)
INSERT @table2 VALUES(2,1,'06/19/2011','person F',2)
INSERT @table2 VALUES(3,2,'06/20/2011','person G',1)       
INSERT @table2 VALUES(3,2,'06/23/2011','person H',3) 

;WITH a as ( 
SELECT RN = ROW_NUMBER() OVER (PARTITION BY t1.sub_id, t1.ref_id, t1.name, t2.logtype ORDER BY log_stamp DESC), t1.sub_id, t1.ref_id, t1.name, t2.Recepient , t2.logtype ,log_stamp
FROM  @table1 t1 JOIN @table2 t2 ON t1.ref_id = t2.ref_id AND 
t1.sub_id = t2.sub_id),
b as (SELECT * FROM a WHERE RN = 1)
SELECT b1.name, b1.ref_id,b1.log_stamp start_date , b1.Recepient, b2.log_stamp latest_comment , b2.Recepient, b3.log_stamp completion_date , b3.Recepient
FROM b b1
LEFT JOIN b b2 ON b1.sub_id=b2.sub_id AND b1.ref_id = b2.ref_id AND b2.logtype = 2
LEFT JOIN b b3 ON b1.sub_id=b3.sub_id AND b1.ref_id = b3.ref_id AND b3.logtype = 3
WHERE b1.logtype = 1
结果:

name         ref_id      start_date              Recepient  latest_comment          Recepient  completion_date         Recepient
------------ ----------- ----------------------- ---------- ----------------------- ---------- ----------------------- ----------
Project 1    1           2011-06-06 00:00:00.000 person A   2011-06-16 00:00:00.000 person C   2011-06-17 00:00:00.000 person D
Project 3    2           2011-06-20 00:00:00.000 person G   NULL                    NULL       2011-06-23 00:00:00.000 person H

谢谢你的回复。当在表2中有3000多行时,对日期字段进行聚合时,执行查询需要2秒以上的时间选择b1.name、b1.ref\u id、max(b1.log\u stamp)start\u date、b1.Recepient、max(b2.log\u stamp)latest\u comment、b2.Recepient、max(b3.log\u stamp)completion\u date、,b3.Recepient FROM b b1 LEFT JOIN b b2 ON b1.sub_id=b2.sub_id和b1.ref_id=b2.ref_id和b2.logtype=2 LEFT JOIN b b3 ON b1.sub_id=b3.sub_id和b1.ref_id=b3.ref_id和b3.logtype=3其中b1.logtype=1按b1.name、b1.ref_id、b1.Recepient、b2.Recepient、b3.Recepient、b3.Recepientes分组,我想这需要一些时间,因为你的数据不是为完美世界而写的。简单联接通常比复杂联接快。你不能真的因为你的表现而责怪我的sql。
;with T as (select
    Table2.ref-id, 
        Table2.log_stamp,
        Table2 log.log_type
        when 1 then '1'
        when 2 then '2'    
        when 3 then '3'

    end as title

from 
    Submission sb inner join submission_log log on Table1.[sub-id] = Table2.[sub-id]
)

select * from T
pivot (
    max(log_stamp)
    for title IN ([1],[2],[3],[5],[6],[9],[11])
DECLARE @table1 TABLE (sub_id INT, ref_id INT, name VARCHAR(50))
INSERT @table1 VALUES (1, 1, 'Project 1')
INSERT @table1 VALUES (2, 1, 'Project 2')   
INSERT @table1 VALUES (3, 2, 'Project 3' )
INSERT @table1 VALUES (4, 2, 'Project 4') 

DECLARE @Table2 TABLE (sub_id INT, ref_id INT, log_stamp DATETIME, recepient VARCHAR(10), logtype INT)

INSERT @table2 VALUES(1,1,'06/06/2011','person A',1) 
INSERT @table2 VALUES(1,1,'06/14/2011','person B',2) 
INSERT @table2 VALUES(1,1,'06/16/2011','person C',2)
INSERT @table2 VALUES(1,1,'06/17/2011','person D',3)
INSERT @table2 VALUES(2,1,'06/18/2011','person E',2)
INSERT @table2 VALUES(2,1,'06/19/2011','person F',2)
INSERT @table2 VALUES(3,2,'06/20/2011','person G',1)       
INSERT @table2 VALUES(3,2,'06/23/2011','person H',3) 

;WITH a as ( 
SELECT RN = ROW_NUMBER() OVER (PARTITION BY t1.sub_id, t1.ref_id, t1.name, t2.logtype ORDER BY log_stamp DESC), t1.sub_id, t1.ref_id, t1.name, t2.Recepient , t2.logtype ,log_stamp
FROM  @table1 t1 JOIN @table2 t2 ON t1.ref_id = t2.ref_id AND 
t1.sub_id = t2.sub_id),
b as (SELECT * FROM a WHERE RN = 1)
SELECT b1.name, b1.ref_id,b1.log_stamp start_date , b1.Recepient, b2.log_stamp latest_comment , b2.Recepient, b3.log_stamp completion_date , b3.Recepient
FROM b b1
LEFT JOIN b b2 ON b1.sub_id=b2.sub_id AND b1.ref_id = b2.ref_id AND b2.logtype = 2
LEFT JOIN b b3 ON b1.sub_id=b3.sub_id AND b1.ref_id = b3.ref_id AND b3.logtype = 3
WHERE b1.logtype = 1
name         ref_id      start_date              Recepient  latest_comment          Recepient  completion_date         Recepient
------------ ----------- ----------------------- ---------- ----------------------- ---------- ----------------------- ----------
Project 1    1           2011-06-06 00:00:00.000 person A   2011-06-16 00:00:00.000 person C   2011-06-17 00:00:00.000 person D
Project 3    2           2011-06-20 00:00:00.000 person G   NULL                    NULL       2011-06-23 00:00:00.000 person H