SQL Server一行多列求最大值
- --一行多列求最大值
- --建立臨時表
- create table #temp(id int,createdate1 varchar(17),createdate2 varchar(17),createdate3 varchar(17));
- --插入資料
- insert into #temp(id,createdate1,createdate2,createdate3)
- select 1,'2016-08-16 20:00','2016-08-16 21:00','2016-08-16 22:00'
- union all
- select 2,'2016-08-15 20:00','2016-08-15 21:00','2016-08-15 22:00'
- union all
- select 3,'2016-08-14 20:00','2016-08-14 21:00','2016-08-14 22:00';
- --求最大值
- select id,(select max(createdate) from (values(createdate1),(createdate2),(createdate3)) m(createdate)) createdate
- from #temp
- --求一行轉多列的方法有多種,比如:
- --1:構建表值函式 將多列以分隔符(比如:逗號)追加到一起,比如:price,num,total
- -- 然後通過迴圈插入到表中,再求最大值
- --2:構建標量值函式 此方法有侷限性,列較少時可以用,建議不用
- --3:上面示例
- --個人比較看好上面示例
http://blog.csdn.net/mh942408056/article/details/52225223
Solution 1
The first solution is the following:
SELECT ID, (SELECT MAX(LastUpdateDate) FROM (VALUES (UpdateByApp1Date),(UpdateByApp2Date),(UpdateByApp3Date)) AS UpdateDate(LastUpdateDate)) AS LastUpdateDate FROM ##TestTable
Solution 2
We can accomplish this task by using UNPIVOT:
SELECT ID, MAX(UpdateDate) AS LastUpdateDate FROM ##TestTable UNPIVOT ( UpdateDate FOR DateVal IN ( UpdateByApp1Date, UpdateByApp2Date, UpdateByApp3Date ) ) AS u GROUP BY ID, Name
Solution 3
This task can be solved by using UNION:
SELECT ID, MAX(UpdateDate) AS LastUpdateDate FROM ( SELECT ID, UpdateByApp1Date AS UpdateDate FROM ##TestTable UNION SELECT ID, UpdateByApp2Date AS UpdateDate FROM ##TestTable UNION SELECT ID, UpdateByApp3Date AS UpdateDate FROM ##TestTable ) ud GROUP BY ID
Solution 4
And the fourth solution also uses a UNION:
SELECT ID, ( SELECT MAX(UpdateDate) AS LastUpdateDate FROM ( SELECT tt.UpdateByApp1Date AS UpdateDate UNION SELECT tt.UpdateByApp2Date UNION SELECT tt.UpdateByApp3Date ) ud ) LastUpdateDate FROM ##TestTable tt
SELECT MostRecentDate
FROM SourceTable
CROSS APPLY (SELECT MAX(d) MostRecentDate FROM (VALUES (Date1), (Date2), (Date3)) AS a(d)) m
https://stackoverflow.com/questions/71022/sql-max-of-multiple-columnsIf you're using MySQL, you can use
注意此函式 LEAST函式?SELECT
GREATEST
(col1, col2 ...) FROM table
相關文章
- oracle 資料庫對於多列求最大值Oracle資料庫
- JavaScript 陣列排序 與 求最大值JavaScript陣列排序
- SQL SERVER 求階乘之和SQLServer
- 確定幾個SQL Server欄中的最大值SQLServer
- 求陣列之和,最小值,最大值,平均值陣列
- SQL查詢 多列合併成一行用逗號隔開stuff()SQL
- 演算法求陣列中的最大值最小值演算法陣列
- 2397 求最大值
- SQL 如何返回最大值所在的多條記錄SQL
- 求一個sql。多謝各位SQL
- JavaScript 專題之如何求陣列的最大值和最小值JavaScript陣列
- 將多列值轉換為一行顯示
- JavaScript 陣列最大值JavaScript陣列
- C 陣列最大值陣列
- 2397 求最大值 迴圈
- 解析SQL Server中行轉列問題SQLServer
- SQL SERVER 儲存過程 獲取三個數中的最大值SQLServer儲存過程
- SQL Server 函式返回多個值SQLServer函式
- 視窗最大值陣列陣列
- sql 中將多列變成行SQL
- SQL SERVER檢視列的預設值SQLServer
- SQL Server不可以增加非空列SQLServer
- Blazor Server 發起HttpPost請求,但是多引數BlazorServerHTTP
- Python求最大值的方法有哪些?Python
- 2419 求最大值和最小值
- 7-2 求最大值及其下標
- SQL Server 2008新特性稀疏列SQLServer
- SQL Server 2008 引入了“稀疏列”SQLServer
- SQL Server 2008稀疏列的使用SQLServer
- 怎樣在SQL Server中檢查 IDENTITY 列SQLServerIDE
- 騰訊面試題-求滑動視窗的最大值面試題
- python如何求最大值?常用方法有哪些?Python
- JavaScript 獲取陣列中最大值JavaScript陣列
- [待]生成視窗最大值陣列陣列
- 數列區間最大值(ST表)
- 包含列的索引:SQL Server索引進階 Level 5索引SQLServer
- SQL Server 2008稀疏列說明示例SQLServer
- Python演算法與資料結構–求所有子陣列的和的最大值Python演算法資料結構陣列