简体   繁体   English

了解TSQL和COALESCE在PIVOT中具有动态列

[英]Understanding TSQL and COALESCE to have dynamic columns in a PIVOT

I had asked a similar question yesterday but still don't have a basic understanding of why this logic works. 昨天我曾问过类似的问题,但对于这种逻辑为何有效,仍然没有基本的了解。 I'm getting the right outupt and I'm happy with that, but why does it work the way it does? 我得到了正确的结果,对此我感到满意,但是为什么它会像以前那样运作呢?

Say for instance we're using this simple query: 假设我们正在使用以下简单查询:

    create table #TestTable (FakeColumn varchar(50))
    INSERT INTO #TestTable (FakeColumn) VALUES ('ABC'),('DEF'),('GHI'),('JKL')

    DECLARE @columns VARCHAR(8000)

  SELECT @columns = COALESCE(@columns + ',[' + cast(FakeColumn as varchar) + ']',
  '[' + cast(FakeColumn as varchar)+ ']')
  FROM #TestTable
  GROUP BY FakeColumn

      select @columns

      drop table #TestTable

The output of the following query is: [ABC],[DEF],[GHI],[JKL] which happens to be exactly what I need... but lets say we modified the query to read: 以下查询的输出是: [ABC],[DEF],[GHI],[JKL]恰好正是我所需要的...但是可以说我们将查询修改为:

 SELECT @columns = '[' + cast(FakeColumn as varchar)+ ']'
 FROM #TestTable
 GROUP BY FakeColumn

 select @columns
  1. Why is my output now: [JKL] ? 为什么现在我的输出是: [JKL]

And now if we modify the COALESCE to only include the first argument [with the @columns appended to the front] 现在,如果我们将COALESCE修改为仅包含第一个参数[将@columns附加在最前面]

  SELECT @columns = @columns + ',[' + cast(FakeColumn as varchar) + ']'
        FROM #TestTable
  GROUP BY FakeColumn
  1. Why is my output now: NULL ? 为什么现在输出: NULL

It looks as if my first value in that COALESCE statement returns NULL, therefore it should go to my second statement but that's only returning [JKL] ... however, with both of them combined I get the string that I need... I'm not sure how this works, or why it does. 似乎我在该COALESCE语句中的第一个值返回NULL,因此它应该转到我的第二条语句,但这仅返回[JKL] ...但是,将它们两个结合在一起,我得到了我需要的字符串...我我不确定这是如何工作的,或者为什么会这样。 Can anyone help explain this to a rookie? 任何人都可以帮助向菜鸟解释吗?

1.Why is my output now: [JKL]? 1.为什么现在我的输出是:[JKL]?

This is easy. 这很容易。 You have a group by in the query, so it is returning multiple rows. 您在查询中有一个group by ,因此它返回多行。 Only one row provides the assignment to the variable. 仅一行提供了对该变量的分配。 Which row is arbitrary. 哪一行是任意的。

1.Why is my output now: NULL? 1.为什么现在我的输出为:NULL?

This is easy. 这很容易。 The variable @columns is initialized to NULL . 变量@columns初始化为NULL Concatenating anything to NULL produces NULL . 将任何内容连接为NULL产生NULL

 SELECT @columns = @columns + ',[' + cast(FakeColumn as varchar) + ']'
       FROM #TestTable
 GROUP BY FakeColumn

Your value here is null because you declared @column without any value before the select, so the initial value is NULL. 您此处的值为空,因为您在选择之前声明了@column而不包含任何值,因此初始值为NULL。 Everything added to a NULL varchar, will result in a NULL result 所有添加到NULL varchar的内容都将导致NULL结果

COALESCE returns the first not NULL value. COALESCE返回第一个非NULL值。

The first argument in the COALESCE below will return a NULL, because anything added to a NULL value, is by default NULL (your @columns variable is NULL). 下面的COALESCE中的第一个参数将返回NULL,因为默认情况下,添加到NULL值的任何内容均为NULL(您的@columns变量为NULL)。 So first it will return the second part, and there after it will add the first part in the COALESCE when @columns have a value 因此,首先返回第二部分,然后在@columns具有值的情况下将其添加到COALESCE中

 SELECT @columns = COALESCE(@columns + ',[' + cast(FakeColumn as varchar) + ']',
 '[' + cast(FakeColumn as varchar)+ ']')
 FROM #TestTable
 GROUP BY FakeColumn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM