SQL查询:将不同的行放入同一行

时间:2015-05-13 08:11:18

标签: sql sql-server left-join

我是初学者,我正在使用SQL Server进行此特定查询。我必须将两行数据合并为一行。不同的是进行检查的人。获取所有数据我没有问题。但我不能将两行数据放入单行。 Conductor 1Conductor 2不应该是同一个人。每次检查可以超过2人进行检查。所以,现在我正在尝试按Items对每一行进行分组。每个项目可以超过2人审查。现在我的conductor2及以后是NULL。我怎样才能解决这个问题? conductor2及之后的值转到下一行,而不是填充val1列之后的列。

SELECT Z.* , 
(Z.val1 - z.val2 ) AS Variance FROM(

SELECT 
(SELECT C.desc WHERE C.id = B.inspectionItem_id ) AS Items,
(SELECT C.itemType) AS Type,
(SELECT D.card WHERE D.id = A.CardId) AS CardNo,
(SELECT E.desc WHERE E.id = D.taskId) AS Task,
(SELECT D.No WHERE D.id = A.CardId) AS blockNo,
A.inspectionDate AS Idate, 
(SELECT F.fName WHERE F.id = A.conductor_id) AS conductor1,
(SELECT H.desc WHERE H.id = G.position_id AND A.conductor_id = G.emp_id) AS Position,
(SELECT J.fName) AS Supervisor,
(SELECT (CONVERT(INTEGER,(B.vals1))) + (CONVERT(INTEGER,(B.vals2))) + (CONVERT(INTEGER,(B.vals3))) WHERE B.inspection_id = A.id AND B.inspectionItem_id = C.id AND C.itemType = 'MULTIPLE') AS val1,
(SELECT F.fName WHERE K.inspection_id != B.inspection_id ) conductor2,
(SELECT H.desc WHERE H.id = G.position_id AND A.conductor_id = G.emp_id AND A.id != B.inspection_id) AS Position2,
(SELECT F.fName WHERE F.id = A.emp_id AND A.id != B.inspection_id) AS Staff2,
(SELECT (CONVERT(INTEGER,(B.vals1))) + (CONVERT(INTEGER,(B.vals2))) + (CONVERT(INTEGER,(B.vals3))) WHERE B.inspection_id != A.id AND A.CardId = D.id AND A.conductor_id != F.id AND C.itemType = 'MULTIPLE') AS val2


FROM Inspection A

LEFT JOIN Details B ON B.inspection_id = A.id
LEFT JOIN inspectionItem C ON C.id = B.inspectionItem_id
LEFT JOIN Card D ON D.id = A.CardId
LEFT JOIN Master E ON E.id = D.taskId
LEFT JOIN Employee F ON F.id = A.conductor_id
LEFT JOIN Employee J ON J.id = A.emp_id
LEFT JOIN EmployeePosition G ON G.emp_id = F.id
LEFT JOIN Position H ON H.id = G.Position_id
LEFT JOIN Details K ON K.inspection_id = A.id

)Z

WHERE Z.itemType = 'MULTIPLE'

我无法发布确切的表格及其样本。所以我准备了几张表,以及预期的结果。

表员工

   Id | fName | lName 
   ---| ----- | ----- 
    1 |Michael| John
    2 | Angie | Kors
    3 | Jesse | Cottrell
    4 | James | McFadden
    5 | Flynn | Gabriel
    6 | Cath  | Tan

表格提示

Id | positionId | empId
---| ---------- | ---
1  |    2       | 1
2  |    3       | 2
3  |    4       | 3
4  |    5       | 4
5  |    2       | 5
6  |    3       | 6

表格位置

   Id | desc 
  --- | --- 
    1 | Admin
    2 | Intern
    3 | Assistant Manager
    4 | Manager
    5 | General Manager 

表mainInspection

   Id | cardNo | conductedById | supervisedById | inspectionDate
  --- | ------ | ------------- | -------------- | -------------------------
    1 | 001    |      1        |       2        | 2015-03-11 10:40:00.000
    2 | 001    |      3        |       4        | 2015-03-11 11:40:18.000
    3 | 001    |      6        |       5        | 2015-03-11 12:00:31.317
    4 | 002    |      1        |       2        | 2015-03-11 13:50:10.000
    5 | 002    |      3        |       4        | 2015-03-11 14:20:51.424
    6 | 002    |      6        |       5        | 2015-03-11 15:26:15.507

表格注册

   Id | cardNo | colorCardId | taskId | roomNo
  --- | ------ | ----------- | --- | ---
    1 |   001  |      1      | 1   | 10
    2 |   002  |      1      | 2   | 8

表项描述

   Id | itemDescription
    1 | Purchase Record Files
    2 | Sales Record Files

表格任务

Id| taskDesc
1 | Annual Report
2 | Monthly Report

表项目详细信息

Id | mainInspectionId | itemDescriptionId | completeQuantity | notCompleteQuantity | totalFiles
1  |         1        |         1         |        10        |         15          |     25
2  |         2        |         1         |         8        |          4          |     12
3  |         3        |         1         |         1        |          1          |     2
4  |         4        |         2         |        16        |          5          |     21
5  |         5        |         2         |         8        |          0          |     8
6  |         6        |         2         |         8        |          2          |     10

表格颜色

   Id | colorCard | desc
  ----| ----------| ---------------
    1 |    Red    | lastYearRecord
    2 |   Yellow  | latestRecord

我的查询结果应该是这样的:

    itemDescription      | cardNo | colorCard |   taskDesc     | roomNo | inspectionDate | conductor 1  | Position | totalFiles1 |   conductor 2  | Position | totalFiles2 |  conductor 3  | Position | totalFiles3 | Variance (totalFiles1 -totalFiles2 – totalFiles3)


   --------------------- | ------ | ----------| -------------- | ------ | -------------- | -------------| -------- | ----------- | -------------- | ---------| ----------- | ------------- | -------- | ----------- | ------------------------------------------------ 
   Purchase Record Files |   001  |    Red    | lastYearRecord |   10   |   2015-03-11   | Michael John |  Intern  |      25     | Jesse Cottrell | Manager  |     12      | Flynn Gabriel |  Intern  |      2      |                       11
   Sales Record Files    |   002  |  Yellow   | latestRecord   |    8   |   2015-03-11   | Micheal John |  Intern  |      21     | Jesse Cottrell | Manager  |      8      | Flynn Gabriel |  Intern  |      10     |                        3

在我的情况下,我仍然无法获取指挥2及其后的结果。

1 个答案:

答案 0 :(得分:1)

我查看了您的表格,表格名称和列名称与原始查询不匹配。

无论如何,我无法提供复制/粘贴&运行查询,但我会尝试让你走上正确的轨道。

第一步是让查询更容易阅读:

  • 您无需为每列选择创建subqueries
  • 您应该使用相关的aliases名称,即使您想使用 单个字符alias(例如T代表TaskP代表Position,...)

解决您的问题:

  • 您可以使用PIVOTAPPLY运算符在同一行显示导体1,2,3。

  • 另一种方法是在EmployeeEmpPoisition上加入3次, PositionitemDetails但是很难维护,所以你 不应该使用这个解决方案。

超出范围建议:

  • 你应该使用一个独特的命名约定/ case并保留它 整个数据库架构。

  • 如果导体不限于3(每张卡N个导体),    设计这样的查询不是一个好主意,你应该展示一下    不同行上的不同导体或使用XML datatype。我们不知道这是什么    将使用查询,但您可以将其拆分为2    查询:

    • 显示商品详情的查询(itemDescriptioncardNocolorCardtaskDescroomNoinspectionDate
    • 对指挥的查询(cardNoconductorPositiontotalFiles