zoukankan      html  css  js  c++  java
  • SQL 动态PIVOT查询

    DECLARE @sql_str VARCHAR(8000)
    DECLARE @sql_col VARCHAR(8000)

    SELECT @sql_col = ISNULL(@sql_col + ',','') + QUOTENAME([indicator]) FROM [sizelist] GROUP BY [Indicator]

    SET @sql_str = '
    SELECT * FROM (
    SELECT [Country],[gender],sku,[indicator],[sizename] FROM [sizelist]) p PIVOT
    (max([sizename]) FOR [indicator] IN ( '+ @sql_col +') ) AS pvt
    ORDER BY pvt.[Country]'
    PRINT (@sql_str)
    EXEC (@sql_str)

    另外使用SQL方式

    DECLARE @sql VARCHAR(8000)
    SET @sql = 'SELECT [Name],'
    SELECT @sql = @sql + 'SUM(CASE [Subject] WHEN '''+[Subject]+''' THEN [score] ELSE 0 END) AS '''+QUOTENAME([Subject])+''','
    FROM (SELECT DISTINCT [Subject] FROM [student2]) AS a



    SELECT @sql = LEFT(@sql,LEN(@sql)-1) + ' FROM student2 GROUP BY Name'
    PRINT(@sql)
    EXEC(@sql)
    GO

  • 相关阅读:
    Apollo与ROS
    QT windeployqt
    自定义QGraphicsItem
    ROS与C++
    aptitude与apt-get
    解决tcp粘包问题
    网络中两台主机通信
    I/O多路复用之select、poll、epoll
    Nginx命令行控制
    C++11
  • 原文地址:https://www.cnblogs.com/fklin/p/6595697.html
Copyright © 2011-2022 走看看