1. <legend id='6go6n'><style id='6go6n'><dir id='6go6n'><q id='6go6n'></q></dir></style></legend>

      <small id='6go6n'></small><noframes id='6go6n'>

        <bdo id='6go6n'></bdo><ul id='6go6n'></ul>

        <tfoot id='6go6n'></tfoot>
        <i id='6go6n'><tr id='6go6n'><dt id='6go6n'><q id='6go6n'><span id='6go6n'><b id='6go6n'><form id='6go6n'><ins id='6go6n'></ins><ul id='6go6n'></ul><sub id='6go6n'></sub></form><legend id='6go6n'></legend><bdo id='6go6n'><pre id='6go6n'><center id='6go6n'></center></pre></bdo></b><th id='6go6n'></th></span></q></dt></tr></i><div id='6go6n'><tfoot id='6go6n'></tfoot><dl id='6go6n'><fieldset id='6go6n'></fieldset></dl></div>

        将 Access TRANSFORM/PIVOT 查询转换为 SQL Server

        Convert Access TRANSFORM/PIVOT query to SQL Server(将 Access TRANSFORM/PIVOT 查询转换为 SQL Server)
        <tfoot id='KS3MO'></tfoot>
            <bdo id='KS3MO'></bdo><ul id='KS3MO'></ul>
              <i id='KS3MO'><tr id='KS3MO'><dt id='KS3MO'><q id='KS3MO'><span id='KS3MO'><b id='KS3MO'><form id='KS3MO'><ins id='KS3MO'></ins><ul id='KS3MO'></ul><sub id='KS3MO'></sub></form><legend id='KS3MO'></legend><bdo id='KS3MO'><pre id='KS3MO'><center id='KS3MO'></center></pre></bdo></b><th id='KS3MO'></th></span></q></dt></tr></i><div id='KS3MO'><tfoot id='KS3MO'></tfoot><dl id='KS3MO'><fieldset id='KS3MO'></fieldset></dl></div>
                <tbody id='KS3MO'></tbody>
            1. <small id='KS3MO'></small><noframes id='KS3MO'>

              <legend id='KS3MO'><style id='KS3MO'><dir id='KS3MO'><q id='KS3MO'></q></dir></style></legend>
                  本文介绍了将 Access TRANSFORM/PIVOT 查询转换为 SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  TRANSFORM Avg(CASE WHEN [temp].[sumUnits] > 0 
                                     THEN [temp].[SumAvgRent] / [temp].[sumUnits] 
                                     ELSE 0 
                                END) AS Expr1
                  SELECT [temp].[Description]
                    FROM [temp] 
                  GROUP BY [temp].[Description]
                  PIVOT [temp].[Period];
                  

                  需要将此查询转换为 sql server

                  Need to convert this query for sql server

                  我已阅读所有其他帖子,但无法将其转换为相同内容

                  I have read all other posts but unable to convert this into the same

                  推荐答案

                  这是使用 PIVOT 表操作符:

                  Here is the equivalent version using the PIVOT table operator:

                  SELECT *
                  FROM
                  (
                    SELECT 
                      CASE 
                        WHEN sumUnits > 0 
                        THEN SumAvgRent / sumUnits ELSE 0 
                    END AS Expr1,
                    Description,
                    Period
                    FROM temp
                  ) t
                  PIVOT
                  (
                    AVG(Expr1)
                    FOR Period IN(Period1, Period2, Period3)
                  ) p;
                  

                  SQL Fiddle 演示

                  例如,这会给你:

                  SQL Fiddle Demo

                  For instance, this will give you:

                  | DESCRIPTION | PERIOD1 | PERIOD2 | PERIOD3 |
                  ---------------------------------------------
                  |          D1 |      10 |       0 |      20 |
                  |          D2 |     100 |    1000 |       0 |
                  |          D3 |      50 |      10 |       2 |
                  

                  <小时>

                  注意 使用 MS SQL Server PIVOT 表运算符时,您必须输入透视列的值.但是,在 MS Access 中,这是 TRANSFORMPIVOT 所做的工作,即动态获取透视列的值.在这种情况下,您必须使用 PIVOT 运算符动态执行此操作,如下所示:


                  Note that When using the MS SQL Server PIVOT table operator, you have to enter the values for the pivoted column. However, IN MS Access, This was the work that TRANSFORM with PIVOT do, which is getting the values of the pivoted column dynamically. In this case you have to do this dynamically with the PIVOT operator, like so:

                  DECLARE @cols AS NVARCHAR(MAX);
                  DECLARE @query AS NVARCHAR(MAX);
                  
                  SELECT @cols = STUFF((SELECT distinct 
                                          ',' +
                                          QUOTENAME(Period)
                                  FROM temp
                              FOR XML PATH(''), TYPE
                              ).value('.', 'NVARCHAR(MAX)') 
                          ,1,1,'');
                  
                  
                  
                  SET @query =  ' SELECT Description, ' + @cols + '
                      FROM 
                      (
                        SELECT 
                          CASE 
                            WHEN sumUnits > 0 
                            THEN SumAvgRent / sumUnits ELSE 0 
                        END AS Expr1,
                        Description,
                        Period
                        FROM temp
                      ) t
                      PIVOT
                      (
                        AVG(Expr1)
                        FOR Period IN( ' + @cols + ') 
                      ) p ';
                  
                  
                  
                  Execute(@query);
                  

                  更新的 SQL Fiddle 演示

                  这应该给你相同的结果:

                  Updated SQL Fiddle Demo

                  This should give you the same result:

                  | DESCRIPTION | PERIOD1 | PERIOD2 | PERIOD3 |
                  ---------------------------------------------
                  |          D1 |      10 |       0 |      20 |
                  |          D2 |     100 |    1000 |       0 |
                  |          D3 |      50 |      10 |       2 |
                  

                  这篇关于将 Access TRANSFORM/PIVOT 查询转换为 SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)

                    <legend id='0N4Jx'><style id='0N4Jx'><dir id='0N4Jx'><q id='0N4Jx'></q></dir></style></legend>
                  • <small id='0N4Jx'></small><noframes id='0N4Jx'>

                      <tfoot id='0N4Jx'></tfoot>

                        <bdo id='0N4Jx'></bdo><ul id='0N4Jx'></ul>
                        <i id='0N4Jx'><tr id='0N4Jx'><dt id='0N4Jx'><q id='0N4Jx'><span id='0N4Jx'><b id='0N4Jx'><form id='0N4Jx'><ins id='0N4Jx'></ins><ul id='0N4Jx'></ul><sub id='0N4Jx'></sub></form><legend id='0N4Jx'></legend><bdo id='0N4Jx'><pre id='0N4Jx'><center id='0N4Jx'></center></pre></bdo></b><th id='0N4Jx'></th></span></q></dt></tr></i><div id='0N4Jx'><tfoot id='0N4Jx'></tfoot><dl id='0N4Jx'><fieldset id='0N4Jx'></fieldset></dl></div>
                          <tbody id='0N4Jx'></tbody>