我有一个sql查询,在这个sql查询中我想选择不同的列而不管列是什么。对于其他sql查询,我使用Row_number()OVER(分区BY ..),我还需要使用内连接。我想使用row_number和内连接的查询是 -
DECLARE @columns NVARCHAR(MAX)
DECLARE @params NVARCHAR(MAX) = '@columns NVARCHAR(MAX) OUTPUT'
DECLARE @sql NVARCHAR(MAX) = 'SELECT @columns = STUFF(
(
SELECT '',''+ [column_name] FROM information_schema.columns
WHERE (table_schema = ''dbo''
AND table_name = ''main_mps_dqs_analog'')
AND (ordinal_position <= 73) FOR XML PATH('''')),1,1,'''')'
EXEC sp_executesql @sql, @params, @columns OUTPUT
SET @sql = 'SELECT '+ @columns + ' FROM dbo.main_mps_dqs_analog WHERE logtime BETWEEN ''2014-10-10 07:17:00'' AND ''2014-10-10 08:47:00'''
EXEC(@sql)
我想将此表的内部联接应用于INDUS2_BDS.dbo.ddtable
,我希望此beam_current
数据库表的logtime
和INDUS2
以及如何应用分区BY {{1在这个查询中。
答案 0 :(得分:1)
SET @sql = 'SELECT ' + @columns + ' ,AnotherTable.beam_current, RowNumber() Over(Partition By SomeColumn Order By SomeColumn) AS Rn
FROM dbo.TableName join AnotherTable on TableName.SomeColumn = AnotherTable.SomeColumn
WHERE logtime BETWEEN ''2014-10-10 07:17:00'' AND ''2014-10-10 08:47:00'''
我在Giorgi的帮助下通过这个SQL查询解决了它