将SQL结果返回到新表并重新排序

时间:2018-05-06 22:20:59

标签: sql sql-server

我创建了一个SQL语句(对于Microsoft SQL Server),它返回结果:

  • 第1列=号码有效用户= [ActiveUser]
  • 第2列=月份为数字= [InMonth]
  • 第3列=航空公司ID = [航空公司代码]

我现在要做的是创建一个新表并以这样的方式重新排列结果:

  • 行= [航空公司代码]
  • 列=月
  • Cells =活跃用户数= [ActiveUser]

到目前为止的查询:

SELECT 
    COUNT (DISTINCT AppUser.Id) as ActiveUser, 
    InMonth = DATEPART(month, ImportDate), 
    AirlineCode   
FROM 
    [db].[dbo].[AppUser] 
JOIN 
    [db].[dbo].[PlanDataVersion] ON AppUser.Id = PlanDataVersion.UserId
WHERE 
    ImportDate BETWEEN '2017-01-01' AND '2017-12-31' 
GROUP BY  
    AirlineCode, DATEPART(month, ImportDate)
ORDER BY 
    InMonth, ActiveUser;

非常感谢任何帮助,谢谢!

安德烈亚斯

2 个答案:

答案 0 :(得分:1)

您需要TSQL PIVOT查询:

SELECT AirlineCode, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]
FROM  
(
    SELECT
        COUNT (DISTINCT AppUser.Id) AS ActiveUser,
        DATEPART(month, ImportDate) AS InMonth,
        AirlineCode
    FROM
        [db].[dbo].[AppUser]
        JOIN [db].[dbo].[PlanDataVersion]
            ON AppUser.Id = PlanDataVersion.UserId
    WHERE ImportDate BETWEEN '2017-01-01' AND '2017-12-31' 
    GROUP BY AirlineCode, DATEPART(month, ImportDate)
) AS Source
PIVOT  
(  
    SUM(ActiveUser)
    FOR InMonth IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
) AS PivotTable
ORDER BY AirlineCode;

如果日期与时间部分一起存储,请注意日期范围测试,因为'2017-12-31'表示'2017-12-31 12:00:00.000'!因此比较

更安全
ImportDate >= '2017-01-01 00:00' AND ImportDate < '2018-01-01 00:00' 

包括“2017-12-31 23:59:59.500”等日期,即年底前的半秒。否则12月31日上午12点之后的日期将不包括在内!请参阅:BETWEEN (Transact-SQL)

答案 1 :(得分:0)

您需要使用PIVOT功能。

SQL看起来像这样:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
ServerName example.com
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias www.example.com
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>