<bdo id='Wjv2A'></bdo><ul id='Wjv2A'></ul>

  • <legend id='Wjv2A'><style id='Wjv2A'><dir id='Wjv2A'><q id='Wjv2A'></q></dir></style></legend>

      <tfoot id='Wjv2A'></tfoot>

        <small id='Wjv2A'></small><noframes id='Wjv2A'>

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

        SQL Server 动态 PIVOT 查询?

        SQL Server dynamic PIVOT query?(SQL Server 动态 PIVOT 查询?)

        • <bdo id='coDpK'></bdo><ul id='coDpK'></ul>

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

              <tfoot id='coDpK'></tfoot>

                <small id='coDpK'></small><noframes id='coDpK'>

                • <legend id='coDpK'><style id='coDpK'><dir id='coDpK'><q id='coDpK'></q></dir></style></legend>

                • 本文介绍了SQL Server 动态 PIVOT 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的任务是想出一种翻译以下数据的方法:

                  I've been tasked with coming up with a means of translating the following data:

                  date        category        amount
                  1/1/2012    ABC             1000.00
                  2/1/2012    DEF             500.00
                  2/1/2012    GHI             800.00
                  2/10/2012   DEF             700.00
                  3/1/2012    ABC             1100.00
                  

                  进入以下内容:

                  date        ABC             DEF             GHI
                  1/1/2012    1000.00
                  2/1/2012                    500.00
                  2/1/2012                                    800.00
                  2/10/2012                   700.00
                  3/1/2012    1100.00
                  

                  空白点可以是 NULL 或空白,两者都可以,并且类别需要是动态的.对此的另一个可能的警告是,我们将在有限的容量下运行查询,这意味着临时表已用完.我已经尝试研究并使用了 PIVOT,但因为我从未使用过它,所以我真的不理解它,尽管我尽了最大的努力来弄清楚它.有人能指出我正确的方向吗?

                  The blank spots can be NULLs or blanks, either is fine, and the categories would need to be dynamic. Another possible caveat to this is that we'll be running the query in a limited capacity, which means temp tables are out. I've tried to research and have landed on PIVOT but as I've never used that before I really don't understand it, despite my best efforts to figure it out. Can anyone point me in the right direction?

                  推荐答案

                  Dynamic SQL PIVOT:

                  Dynamic SQL PIVOT:

                  create table temp
                  (
                      date datetime,
                      category varchar(3),
                      amount money
                  )
                  
                  insert into temp values ('1/1/2012', 'ABC', 1000.00)
                  insert into temp values ('2/1/2012', 'DEF', 500.00)
                  insert into temp values ('2/1/2012', 'GHI', 800.00)
                  insert into temp values ('2/10/2012', 'DEF', 700.00)
                  insert into temp values ('3/1/2012', 'ABC', 1100.00)
                  
                  
                  DECLARE @cols AS NVARCHAR(MAX),
                      @query  AS NVARCHAR(MAX);
                  
                  SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.category) 
                              FROM temp c
                              FOR XML PATH(''), TYPE
                              ).value('.', 'NVARCHAR(MAX)') 
                          ,1,1,'')
                  
                  set @query = 'SELECT date, ' + @cols + ' from 
                              (
                                  select date
                                      , amount
                                      , category
                                  from temp
                             ) x
                              pivot 
                              (
                                   max(amount)
                                  for category in (' + @cols + ')
                              ) p '
                  
                  
                  execute(@query)
                  
                  drop table temp
                  

                  结果:

                  Date                        ABC         DEF    GHI
                  2012-01-01 00:00:00.000     1000.00     NULL    NULL
                  2012-02-01 00:00:00.000     NULL        500.00  800.00
                  2012-02-10 00:00:00.000     NULL        700.00  NULL
                  2012-03-01 00:00:00.000     1100.00     NULL    NULL
                  

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

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

                  相关文档推荐

                  Select n random rows from SQL Server table(从 SQL Server 表中随机选择 n 行)
                  SQL query to select dates between two dates(用于选择两个日期之间的日期的 SQL 查询)
                  How can I delete using INNER JOIN with SQL Server?(如何在 SQL Server 中使用 INNER JOIN 进行删除?)
                  Table Naming Dilemma: Singular vs. Plural Names(表命名困境:单数与复数名称)
                  INSERT statement conflicted with the FOREIGN KEY constraint - SQL Server(INSERT 语句与 FOREIGN KEY 约束冲突 - SQL Server)
                  How ROWNUM works in pagination query?(ROWNUM 如何在分页查询中工作?)

                      <tbody id='UKgmF'></tbody>

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

                          <small id='UKgmF'></small><noframes id='UKgmF'>

                          <legend id='UKgmF'><style id='UKgmF'><dir id='UKgmF'><q id='UKgmF'></q></dir></style></legend><tfoot id='UKgmF'></tfoot>
                            <bdo id='UKgmF'></bdo><ul id='UKgmF'></ul>