0
我已經編寫了此查詢以從整個表中動態查找數值的最大值或最小值。我的查詢是這樣的:列的類型「」與UNPIVOT列表中指定的其他列的類型衝突
declare @col varchar(2000)
set @col = STUFF((select ',' +' [' + c.name + ']' FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
WHERE
c.object_id = OBJECT_ID('JRR_20170301_Stg')
and c.system_type_id = 108 for XML path('')),1,1, '')
select @col -- This returns the column name and good.
--However, the problem starts from here below
declare @sql varchar(max)
set @sql='SELECT max(col)
FROM JRR_20170301_Stg unPIVOT (col for ListofColumns in ('[email protected]+')) as unpivat'
exec (@sql)
這裏是我的表結構:
CREATE TABLE [dbo].[JRR_20170301_Stg](
[col1] [numeric](7, 0) NULL,
[col2] [numeric](7, 0) NULL,
[col3] [varchar](30) NULL,
[TIMESTAMP1] [varchar](8) NULL,
[BRANCH] [varchar](2) NULL,
[COE] [varchar](8) NULL,
[SCHEME] [varchar](3) NULL,
[NO] [varchar](7) NULL,
[PLAN] [varchar](5) NULL,
[ACODE] [varchar](8) NULL,
[ACTNAME] [varchar](30) NULL,
[SRNAME] [varchar](30) NULL,
[OTHERNAME] [varchar](30) NULL,
[ACTWTCH] [varchar](1) NULL,
[EMPLC] [varchar](5) NULL,
[EMPLNO] [varchar](13) NULL,
[LADATE] [numeric](7, 0) NULL,
[EARN] [numeric](7, 2) NULL,
[REGPA] [numeric](7, 2) NULL,
[OTAMT] [numeric](7, 2) NULL,
[EMPSTAT] [varchar](1) NULL,
[RESVE5] [varchar](100) NULL
) ON [PRIMARY]
有什麼辦法來糾正這個錯誤,並得到想要的結果?我可以使用交叉應用或其他?
我剛剛在這個鏈接中回答了你的問題,它會工作100%。 http://stackoverflow.com/questions/43585733/column-having-maximum-integer-value-from-table-sql-server/43585985?noredirect=1#comment74252698_43585985 – KumarHarsh