当前位置: 代码迷 >> ASP.NET >> sql
  详细解决方案

sql

热度:847   发布时间:2013-02-25 00:00:00.0
求一个sql?


上面的表中数据形式 下面则 是想实现的效果?指教

------解决方案--------------------------------------------------------
SQL code
--借用14楼的数据CREATE TABLE #(type VARCHAR(10),dataday  DATETIME,point    INT)INSERT INTO #SELECT  '组别一', '2012-03-02',23  UNION ALL SELECT  '组别二', '2012-03-02',23  UNION ALL SELECT  '组别三', '2012-03-02',23  UNION ALL SELECT  '组别一', '2012-03-04',23  UNION ALL SELECT  '组别二', '2012-03-05',23  UNION ALL SELECT  '组别二', '2012-03-06',23  UNION ALL SELECT  '组别三', '2012-03-07',23  UNION ALL SELECT  '组别三', '2012-03-08',23  UNION ALL SELECT  '组别二', '2012-03-09',23  UNION ALL SELECT  '组别一', '2012-03-10',23  UNION ALL SELECT  '组别一', '2012-03-11',23  UNION ALL SELECT  '组别三', '2012-03-12',23  UNION ALL SELECT  '组别一', '2012-03-13',23  UNION ALL SELECT  '组别三', '2012-03-14',23  UNION ALL SELECT  '组别一', '2012-03-15',23  UNION ALL SELECT  '组别二', '2012-03-16',23  UNION ALL SELECT  '组别三', '2012-03-16',23DECLARE @CaseSql nvarchar(4000)DECLARE @ExecSql nvarchar(4000)SET @ExecSql='SELECT type,'SELECT @CaseSql=ISNULL(@CaseSql,'')+'MAX(CASE dataday WHEN '''+CONVERT(varchar(20),dataday,120)+''' THEN point ELSE 0 END) AS ['+CONVERT(varchar(20),dataday,120)+'],' FROM # GROUP BY datadaySET @ExecSql=@ExecSql+LEFT(@CaseSql,LEN(@CaseSql)-1)+' FROM # GROUP  BY  type'EXEC(@ExecSql)/*type    2012-03-02 00:00:00    2012-03-04 00:00:00    2012-03-05 00:00:00    2012-03-06 00:00:00    2012-03-07 00:00:00    2012-03-08 00:00:00    2012-03-09 00:00:00    2012-03-10 00:00:00    2012-03-11 00:00:00    2012-03-12 00:00:00    2012-03-13 00:00:00    2012-03-14 00:00:00    2012-03-15 00:00:00    2012-03-16 00:00:00组别二    23    0    23    23    0    0    23    0    0    0    0    0    0    23组别三    23    0    0    0    23    23    0    0    0    23    0    23    0    23组别一    23    23    0    0    0    0    0    23    23    0    23    0    23    0*/
  相关解决方案