Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 将列中类似(但不相同)的数据合并到一行中_Sql Server_Tsql_Pivot - Fatal编程技术网

Sql server 将列中类似(但不相同)的数据合并到一行中

Sql server 将列中类似(但不相同)的数据合并到一行中,sql-server,tsql,pivot,Sql Server,Tsql,Pivot,我是SQL Server的初学者,正在努力找出如何更新查询以使其正确显示我的数据 我签出并尝试在QueryID上执行分组,但不确定在需要按文本末尾的数字和id#进行分组时如何应用此功能。我也看过很多关于pivot/unpivot的文章,它们可以执行多个pivot,但我很难应用它 下面是我当前的查询和示例结果。我想看到的是,当QueryID以相同的数字结尾,员工ID相等时,日期/级别/原因都返回到同一行,而不是3行。我感谢任何指导 With DisciplineTable as ( Sele

我是SQL Server的初学者,正在努力找出如何更新查询以使其正确显示我的数据

我签出并尝试在QueryID上执行分组,但不确定在需要按文本末尾的数字和id#进行分组时如何应用此功能。我也看过很多关于pivot/unpivot的文章,它们可以执行多个pivot,但我很难应用它

下面是我当前的查询和示例结果。我想看到的是,当QueryID以相同的数字结尾,员工ID相等时,日期/级别/原因都返回到同一行,而不是3行。我感谢任何指导

With DisciplineTable as
(
   Select HrEmployees.Number, 
     HrEmployees.FirstName + ' ' + HrEmployees.LastName as Name,    
     HrEmployeeQueries.QueryID, 
     HrEmployeeQueries.Query, 
     HrEmployeeQueries.Response
    From HrEmployeeQueries
    Left Join HrEmployees on HrEmployees.EmployeeID = HrEmployeeQueries.EmployeeID
    Where QueryID Like 'DIS%')
Select * 
From DisciplineTable
Pivot(Max(Response) for Query in ([DATE:],[LEVEL:],[REASON:])) as DisciplinaryAction
Where QueryID Like 'DIS%' 
Order by Name
我的结果如下所示:

+-----+----------+---------+----------+---------+-------------+
|  #  |   Name   | QueryID |  DATE:   | LEVEL:  |   REASON:   |
+-----+----------+---------+----------+---------+-------------+
| 123 | John Doe | Date2   | 10/16/14 | Null    | Null        |
| 123 | John Doe | Level2  | Null     | Serious | Null        |
| 123 | John Doe | Reason2 | Null     | Null    | Attendance  |
| 123 | John Doe | Date3   | 12/13/14 | Null    | Null        |
| 123 | John Doe | Level3  | Null     | Major   | Null        |
| 123 | John Doe | Reason3 | Null     | Null    | Performance |
+-----+----------+---------+----------+---------+-------------+
我想让他们看起来像这样。
Action
列应填充
QueryID
末尾的数值,该数值可以是1-10。
QueryID
开头的文本始终是
日期
原因
级别

+-----+----------+--------+----------+---------+-------------+
|  #  |   Name   | Action |  DATE:   | LEVEL:  |   REASON:   |
+-----+----------+--------+----------+---------+-------------+
| 123 | John Doe |      2 | 10/16/14 | Serious | Attendance  |
| 123 | John Doe |      3 | 12/13/14 | Major   | Performance |
+-----+----------+--------+----------+---------+-------------+

只需将
Max aggregate
groupby

WITH DisciplineTable
     AS (SELECT HrEmployees.Number,
                HrEmployees.FirstName + ' '
                + HrEmployees.LastName AS NAME,
                substring(HrEmployeeQueries.QueryID,patindex('%[0-9]%',HrEmployeeQueries.QueryID),LEN(HrEmployeeQueries.QueryID))QueryID,
                HrEmployeeQueries.Query,
                HrEmployeeQueries.Response
         FROM   HrEmployeeQueries
                LEFT JOIN HrEmployees
                       ON HrEmployees.EmployeeID = HrEmployeeQueries.EmployeeID
         WHERE  QueryID LIKE 'DIS%')
SELECT [#],
       [Name],
       [QueryID] Action,
       [DATE:],
       [LEVEL:],
       [REASON:]
FROM   DisciplineTable
       PIVOT(Max(Response)
            FOR Query IN ([DATE:],
                          [LEVEL:],
                          [REASON:])) AS DisciplinaryAction
ORDER  BY NAME 

解决这个问题的一些建议。问题的一部分在于
QueryID
列中有多个值,因此PIVOT函数在生成不同行的每个值上分组

首先,要获取
Action
值,需要去掉
QueryID
列中的字母字符。考虑到你将得到1-10之间的值,你可以利用它来得到它。注意:有几种方法可以做到这一点,包括使用
替换
,等等。一旦有了
操作
值,就可以应用枢轴。以下是一个示例版本:

select Number, Name, Action, [DATE:],[LEVEL:],[REASON:]
from
(
  select number, name, reason, query,
    Action = stuff(QueryID, 1, patindex('%[0-9]%', QueryID)-1, '')
  from yourdata
) d
pivot
(
  max(reason)
  for query in ([DATE:],[LEVEL:],[REASON:])
) p;
看。然后将其添加到当前查询中,将是:

With DisciplineTable as
(
    Select 
        HrEmployees.Number, 
        HrEmployees.FirstName + ' ' + HrEmployees.LastName as Name, 
        Action = stuff(HrEmployeeQueries.QueryID, 1, patindex('%[0-9]%', HrEmployeeQueries.QueryID)-1, '') , 
        HrEmployeeQueries.Query, 
        HrEmployeeQueries.Response
    From HrEmployeeQueries
    Left Join HrEmployees on HrEmployees.EmployeeID = HrEmployeeQueries.EmployeeID
    Where QueryID Like 'DIS%'
)
Select Number, Name, Action, [DATE:],[LEVEL:],[REASON:]
From DisciplineTable
Pivot
(
    Max(Response) for Query in ([DATE:],[LEVEL:],[REASON:])
) as DisciplinaryAction
Order by Name

CTE内部查询的输出是什么
操作是什么
?看起来您需要从子查询中删除
QueryID
列,并将其替换为
Action
列。@BlueFoots Action只是QueryID结尾的数字-so Date2,Level2,原因2将在操作列中显示为2。@HRIS仅是一个数值,如
2
3
?或者你会有
10
100
?@NoDisplayName-在pivot之前,有规律的结果基本上是这样的:123 | John Doe | DATE 2 | DATE:| 10/16/14,因此pivot将日期、级别和原因作为列标题移动到了“响应”上。首先,不是我的DV,而是一些注释,您可能不需要在外部查询中使用
WHERE
,所有内容都应该在内部查询中过滤。其次,为什么不修剪内部查询中的
QueryID
,这样就不必两次应用聚合函数了?第三,他们说,
QueryID
可以增加到10,所以获取最后一个字符是不起作用的。@bluefeet-是的,我在外部查询中没有看到where子句!!谢谢你指出问题所在mistakes@NoDisplayName在更新之前,查询处理queryid(可能是2位数字),唯一的问题是10显示为0。更新后,我收到以下错误:Msg 102,级别15,状态1,第6行“HrEmployeeQueries”附近的语法不正确。@NoDisplayName discore-添加了一个逗号,错误消失了这似乎工作得很好!谢谢你们两位的帮助!!