【问题标题】:SQL Server pivot with string concatenation使用字符串连接的 SQL Server 透视
【发布时间】:2016-12-02 11:46:26
【问题描述】:

我有如下数据

| Name      | Subject   | Answer                |
|:----------|:----------|:---------------------:|
|Pranesh    |Physics    |Numerical Problems     |
|Pranesh    |Physics    |Other                  |
|Pranesh    |Chemistry  |Understanding Concepts |
|Pranesh    |Chemistry  |Organic chemistry reactions
|Pranesh    |Maths      |Lack of understanding  |
|Pranesh    |Maths      |Insufficient practice  |
|Pranesh    |Maths      |Other                  |

使用此 SQL 查询:

select * 
from 
    (select  
         l.FullName Name, sq.Title Subject, cAns.Name Answer 
     from 
         Answer as sa
     left join
         Question AS sq on sq.ID = sa.QuestionID
     left join 
         Master as cAns on cAns.ID = sa.AnswerID
     left join 
         Profile as l on l.ID = sa.ProfileID) src
pivot
    (max(Answer)
        for Subject in ([Physics], [Chemistry], [Maths], [Biology],[ComputerScience], [CommonUnderstanding])) piv;

我可以得到如下数据

如何连接同一主题的答案栏并显示与上面的屏幕截图相同?

【问题讨论】:

  • 预期输出应该是

标签: sql-server pivot concatenation


【解决方案1】:

我首先按照名称和主题连接了答案列表,然后应用了旋转 -

declare @temp table (name varchar(100), subject varchar(100), answer varchar(100))

insert into @temp
            select 'Pranesh','Physics'  ,'Numerical Problems'
union all   select 'Pranesh','Physics'  ,'Other'
union all   select 'Pranesh','Chemistry','Understanding Concepts'
union all   select 'Pranesh','Chemistry','Organic chemistry reactions'
union all   select 'Pranesh','Maths'    ,'Lack of understanding'
union all   select 'Pranesh','Maths'    ,'Insufficient practice'
union all   select 'Pranesh','Maths'    ,'Other'
union all   select 'Ramesh','Biology'   ,   'Other'
union all   select 'Ramesh','Biology'   ,   'Science'

;with cte as (select distinct name, subject from @temp)
select * from
(
    select
        c.name,
        c.subject,
        answer = stuff((select ',' + answer from @temp t where t.name=c.name and t.subject=c.subject for xml path('')), 1, 1, '')
    from cte c
) src
pivot
(
    max(answer) for subject in ([Physics], [Chemistry], [Maths], [Biology],[ComputerScience],[CommonUnderstanding])
) piv

【讨论】:

  • 我需要研究您的查询并尝试从我的代码中获取结果。给我一些时间
  • 我接受你的回答+1,你能在这里简单介绍一下@temp表的用法,执行后内存会发生什么等等。性能方面,这个查询将如何运作?
  • @temp 表用于存储与您相同的数据。请忽略临时表并使用您的永久表名代替临时表。对于这个答案,我只想在旋转之前连接字符串。
  • 为了连接字符串,我需要一组不同的名称和主题,所以我使用 CTE。
  • 我的实际代码是一个存储过程,其中包括 10 个左连接和 15 列作为输出。因此,我将您的代码用于所有这些选择列。我不知道如何在不使用临时表的情况下实现。你能帮帮我吗?
【解决方案2】:

试试这个:-

SELECT ANS, PHYSICS,CHEMISTRY,MATHS
FROM
(
SELECT  L.FULLNAME NAME, SQ.TITLE SUBJECT, CANS.NAME ANSWER FROM ANSWER AS SA
LEFT JOIN QUESTION AS SQ ON SQ.ID = SA.QUESTIONID
LEFT JOIN MASTER AS CANS ON CANS.ID = SA.ANSWERID
LEFT JOIN PROFILE AS L ON L.ID = SA.PROFILEID
) AS T
UNPIVOT
(
  COMBINE_COLUMN_VALUES FOR ANS IN (ANSWER)
) AS U
PIVOT
(
  MAX(COMBINE_COLUMN_VALUES) FOR [SUBJECT] IN (PHYSICS,CHEMISTRY,MATHS)
) AS P

【讨论】:

  • 这是我从你的查询中得到的。|ANS |物理 |化学 |数学答案 |其他 |理解概念 |其他
  • 你能解释一下COMBINE_COLUMN_VALUES ..吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
  • 2017-05-11
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
相关资源
最近更新 更多