I have the following query:
WITH cteCountryLanguageMapping AS (
SELECT * FROM (
VALUES
('Spain', 'English'),
('Spain', 'Spanish'),
('Sweden', 'English'),
('Switzerland', 'English'),
('Switzerland', 'French'),
('Switzerland', 'German'),
('Switzerland', 'Italian')
) x ([Country], [Language])
)
SELECT
[Country],
CASE COUNT([Language])
WHEN 1 THEN MAX([Language])
WHEN 2 THEN STRING_AGG([Language], ' and ')
ELSE STRING_AGG([Language], ', ')
END AS [Languages],
COUNT([Language]) AS [LanguageCount]
FROM cteCountryLanguageMapping
GROUP BY [Country]
I was expecting the value inside Languages column for Switzerland to be comma separated i.e.:
| Country | Languages | LanguageCount
--+-------------+-------------------------------------------+--------------
1 | Spain | Spanish and English | 2
2 | Sweden | English | 1
3 | Switzerland | French, German, Italian, English | 4
Instead I am getting the below output (the 4 values are separated by and
):
| Country | Languages | LanguageCount
--+-------------+-------------------------------------------+--------------
1 | Spain | Spanish and English | 2
2 | Sweden | English | 1
3 | Switzerland | French and German and Italian and English | 4
What am I missing?
Here is another example:
SELECT y, STRING_AGG(z, '+') AS STRING_AGG_PLUS, STRING_AGG(z, '-') AS STRING_AGG_MINUS
FROM (
VALUES
(1, 'a'),
(1, 'b')
) x (y, z)
GROUP by y
| y | STRING_AGG_PLUS | STRING_AGG_MINUS
--+---+-----------------+-----------------
1 | 1 | a+b | a+b
Is this a bug in SQL Server?
CASE COUNT([Language]) WHEN 1234567 THEN STRING_AGG([Language], ' and ') ELSE STRING_AGG([Language], ', ') END AS [Languages]
(uses the1234567
case) andCASE COUNT([Language]) WHEN 1234567 THEN STRING_AGG([Language], ' and ') END AS [Languages]
(leave outELSE
-- now the match fails and the expression becomesNULL
). No matter what the "correct" result should be, surely that's not it. – ContemnSTRING_AGG
is entirely missing and instead the expression is rebound to the first one, as if theCASE
had saidSTRING_AGG([Language], ' and ')
twice. Any subsequentCASE
s are absorbed as well. Looks like something very weird is going on with subexpression elimination. – ContemnSTRING_AGG
. If theELSE
is changed to'blargh' + STRING_AGG(...)
, you'll get'blarghFrench and German...
, so it improperly unifies the secondSTRING_AGG
with the first. The simplest workaround is to change theELSE
expression toSTRING_AGG([Language] + '', ', ')
-- this defeats CSE, suggesting there's a bug where CSE ignores the second argument toSTRING_AGG
. – Contemn