如何进行动态查询?

时间:2019-03-29 03:30:56

标签: sql

我需要在单行中显示信息

CREATE TABLE #DATOS(
iCodRegistro INT,   
iCodTipoFoto INT,   
dtFechaCaptura VARCHAR(100),
vLatitud VARCHAR(100),  
vLongitud VARCHAR(100), 
vAltitud VARCHAR(100)
)

INSERT INTO #DATOS VALUES(1355332, 12, '2018-02-28 11:03:18.000','-7.9393047','-78.5107495', '0.0')
INSERT INTO #DATOS VALUES(1355332, 13, '2018-02-28 11:03:26.000','-7.9393071',' -78.510752','0.0')
INSERT INTO #DATOS VALUES(1355332, 14, '2018-02-28 11:04:02.000','-7.9393071',' -78.510752','0.0')
INSERT INTO #DATOS VALUES(1355332, 15, '2018-02-28 11:04:09.000','-7.9393071',' -78.510752','0.0')
INSERT INTO #DATOS VALUES(1355360, 12, '2018-02-27 08:48:37.000','-7.944077','-78.5785364','0.0')
INSERT INTO #DATOS VALUES(1355360, 13, '2018-02-27 08:48:43.000','-7.9440546',' -78.5785394','0.0')
INSERT INTO #DATOS VALUES(1355360, 14, '2018-02-27 08:49:48.000','-7.9440546',' -78.5785394','0.0')
INSERT INTO #DATOS VALUES(1355360, 15, '2018-02-27 08:49:58.000','-7.9440546',' -78.5785394','0.0')

SELECT * FROM #DATOS

--YOU WANT TO OBTAIN

SELECT 1355332 iCodRegistro, 12 Tipo12, '2018-02-28 11:03:18.000' Fecha12,'-7.9393047' vLatitud12,'-78.5107495' vLongitud12,'0.0' vAltitud12, 
                             13 Tipo13, '2018-02-28 11:03:26.000' Fecha13,'-7.9393071' vLatitud13,'-78.510752'  vLongitud13,'0.0' vAltitud13, 
                             14 Tipo14, '2018-02-28 11:04:02.000' Fecha14,'-7.9393071' vLatitud14,'-78.510752'  vLongitud14,'0.0' vAltitud14, 
                             15 Tipo15, '2018-02-28 11:04:09.000' Fecha15,'-7.9393071' vLatitud15,'-78.510752'  vLongitud15,'0.0' vAltitud15
UNION ALL
SELECT 1355360, 12, '2018-02-27 08:48:37.000','-7.944077','-78.5785364','0.0',
                13, '2018-02-27 08:48:43.000','-7.9440546','-78.5785394','0.0',
                14, '2018-02-27 08:49:48.000','-7.9440546','-78.5785394','0.0',
                15, '2018-02-27 08:49:58.000','-7.9440546','-78.5785394','0.0'

2 个答案:

答案 0 :(得分:0)

这似乎是一个冗长的代码,但想法很简单,将您的多个iCodTipoFoto 12、13、14、15作为子查询,并由iCodRegistro加入以获得所需的输出。

SELECT  i12.iCodRegistro,
Tipo12, 
Fetcha12,
 vLatitud12 ,  
vLongitud12, 
vAltitud12,
Tipo13, 
Fetcha13,
 vLatitud13 ,  
vLongitud13, 
vAltitud13,
Tipo14, 
Fetcha14,
 vLatitud14 ,  
vLongitud14, 
vAltitud14,
Tipo15, 
Fetcha15,
 vLatitud15 ,  
vLongitud15, 
vAltitud15
FROM

(SELECT  iCodRegistro ,
12 as Tipo12, 
dtFechaCaptura as Fetcha12,
vLatitud as vLatitud12 ,  
vLongitud as vLongitud12, 
vAltitud as vAltitud12 FROM #DATOS
where iCodTipoFoto=12)i12 

INNER JOIN

(SELECT  iCodRegistro ,
13 as Tipo13, 
dtFechaCaptura as Fetcha13,
vLatitud as vLatitud13 ,  
vLongitud as vLongitud13, 
vAltitud as vAltitud13 FROM #DATOS
where iCodTipoFoto=13) i13 on i12.iCodRegistro=i13.iCodRegistro 

INNER JOIN

(SELECT  iCodRegistro ,
14 as Tipo14, 
dtFechaCaptura as Fetcha14,
vLatitud as vLatitud14 ,  
vLongitud as vLongitud14, 
vAltitud as vAltitud14 FROM #DATOS
where iCodTipoFoto=14) i14 on i12.iCodRegistro=i14.iCodRegistro 

INNER JOIN

(SELECT  iCodRegistro ,
15 as Tipo15, 
dtFechaCaptura as Fetcha15,
vLatitud as vLatitud15 ,  
vLongitud as vLongitud15, 
vAltitud as vAltitud15 FROM #DATOS
where iCodTipoFoto=15 ) i15 on i12.iCodRegistro=i15.iCodRegistro

答案 1 :(得分:0)

请推断以下查询以添加更多列。我把两列。您可以根据需要添加任意数量。当我们应用DISTINCT时,对于大量的行,性能可能会不好。在那种情况下,我建议您编写游标并将结果填充到临时表中,最后加载它。

DECLARE @sql VARCHAR(max) = ''
DECLARE @finalSql VARCHAR(max) = ''
DECLARE @iCodeRegistros VARCHAR(500) = '1355332,1355360'
DECLARE @currentiCodeRegistro VARCHAR(30)
WHILE len(@iCodeRegistros) > 0
BEGIN
    SET @currentiCodeRegistro = left(@iCodeRegistros, charindex(',', @iCodeRegistros + ',') - 1)
    SET @iCodeRegistros = stuff(@iCodeRegistros, 1, charindex(',', @iCodeRegistros + ','), '')
    IF @currentiCodeRegistro IS NOT NULL
    BEGIN
        SET @sql = CONCAT (
                'SELECT DISTINCT ', @currentiCodeRegistro, ' AS iCodRegistro ,* FROM ', STUFF((
                        SELECT CONCAT (' cross join ', '(SELECT ', iCodTipoFoto, ' AS Tipo', CAST(iCodTipoFoto AS VARCHAR(20)), ', ''', dtFechaCaptura, ''' AS Fecha', cast(iCodTipoFoto AS VARCHAR(30)), ' FROM #DATOS) AS t', cast(iCodTipoFoto AS VARCHAR(30)))
                        FROM #DATOS AS t
                        WHERE iCodRegistro = @currentiCodeRegistro
                        FOR XML PATH('')
                        ), 1, 11, '')
                )
        SET @finalSql += ' UNION ALL ' + @sql
    END
END
SET @finalSql = stuff(@finalSql, 1, 10, '')
EXEC (@finalSql)    

我将生成的结果集放在下面以供参考。

+--------------+--------+-------------------------+--------+--------------------------+--------+-------------------------+--------+-------------------------+
| iCodRegistro | Tipo12 |          Fecha12        | Tipo13 |          Fecha13         | Tipo14 |          Fecha14        | Tipo15 |          Fecha15        |
+--------------+--------+-------------------------+--------+--------------------------+--------+-------------------------+--------+-------------------------+
|      1355332 |     12 | 2018-02-28 11:03:18.000 |     13 | 2018-02-28 11:03:26.000  |     14 | 2018-02-28 11:04:02.000 |     15 | 2018-02-28 11:04:09.000 |
|      1355360 |     12 | 2018-02-27 08:48:37.000 |    13  | 2018-02-27 08:48:43.000  |     14 | 2018-02-27 08:49:48.000 |     15 | 2018-02-27 08:49:58.000 |
+--------------+--------+-------------------------+--------+--------------------------+--------+-------------------------+--------+-------------------------+