我绝不是 SQL 专家,只是知道我的方法。
我需要展平一些数据以便为 SQL 报告目的生成它。
因此我的查询不是最佳的,因为它很慢,我相信有 10 种更好的方法可以做到这一点:
这里的核心列是 InspectionID,它是 Inspections 表的主键
有什么建议吗?
SELECT
INSP.HouseSiteNo,
(SELECT TOP 1 WORKFLOWSTATES.Name FROM INSPECTIONWORKFLOWITEM WFITEM
INNER JOIN WORKFLOWSTATES ON WFITEM.WorkflowStateID = WORKFLOWSTATES.WorkflowStateID
WHERE WFITEM.InspectionID = INSP.InspectionID AND WORKFLOWSTATES.StateNumber = 1) as StateName
FROM INSPECTIONS INSP
ORDER BY HouseSiteNo
表结构如下:
检查:
HouseSiteNo HouseType ProjectID InspectionID
1 A 543C6381-8704-4129-B484-05B02927F690 E061F76F-8998-450B-B08C-DA2817B310A4
2 A 543C6381-8704-4129-B484-05B02927F690 E0D1F3DB-51D4-465A-A0B4-ACFFF181DC56
检查工作流程
InspectionID WorkflowStateID DateStarted StateCompleted ID
FAA4E366-1D57-4699-982C-001FF351D717 AB2B7DA0-A90A-46FB-8F4B-8E662191E380 2012-09-09 00:00:00.000 0 D77332CB-D026-409A-814D-5632C109850A
FAA4E366-1D57-4699-982C-001FF351D717 E2AC895A-0986-4A03-B104-A727D41405E3 2012-09-09 00:00:00.000 0 F118FD02-20C4-4B45-8539-6F8BDC6B3868
FAA4E366-1D57-4699-982C-001FF351D717 F0469593-1B6B-4F98-92AE-DF50E76B9ED8 2012-09-09 00:00:00.000 0 8FBEE61B-D7B3-4D13-9FC8-730EB0FE7C95
FAA4E366-1D57-4699-982C-001FF351D717 5929D0CA-E8B0-42EE-9CF4-E3FBC9973AA6 2012-09-09 00:00:00.000 0 02646FE5-B338-4D6F-B5F5-7984B0EF7BF5
FAA4E366-1D57-4699-982C-001FF351D717 ECDDB790-88A4-4654-889D-BCA577C35CA8 2012-09-09 00:00:00.000 0 0B9C26FA-8D9D-4552-A3FE-84D4D21F5791
FAA4E366-1D57-4699-982C-001FF351D717 099D66A4-16F2-4E72-A42A-06B4D6DC0102 2012-09-09 00:00:00.000 0 3B90F113-5194-4ED2-B920-8DA004C9EA5C
FAA4E366-1D57-4699-982C-001FF351D717 66132F53-0A0E-431A-AD84-ADE77656CDF0 2012-09-09 00:00:00.000 0 05223484-8BB3-43A8-A9A8-C8FBC6270186
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 F0469593-1B6B-4F98-92AE-DF50E76B9ED8 2012-09-09 00:00:00.000 0 A1471AF4-B733-488C-86B4-FB29970E536E
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 ECDDB790-88A4-4654-889D-BCA577C35CA8 2012-09-09 00:00:00.000 0 30F3A1F9-6E51-4504-A9FD-B32FD2B35B39
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 5929D0CA-E8B0-42EE-9CF4-E3FBC9973AA6 2012-09-09 00:00:00.000 0 760D4907-ED6C-4D84-AED9-775FBBAE5123
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 AB2B7DA0-A90A-46FB-8F4B-8E662191E380 2012-09-09 00:00:00.000 0 49D632C7-31D0-4818-AC36-564F157D1959
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 66132F53-0A0E-431A-AD84-ADE77656CDF0 2012-09-09 00:00:00.000 0 C6771081-198D-475B-BA09-14BB696567DA
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 099D66A4-16F2-4E72-A42A-06B4D6DC0102 2012-09-09 00:00:00.000 0 F265B140-25C5-46CA-B060-2A27CD9F9D00
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 E2AC895A-0986-4A03-B104-A727D41405E3 2012-09-09 00:00:00.000 0 E56AEC15-4BA1-4089-B1A7-3BF054568929
工作流状态
WorkflowStateID Name StateNumber
099D66A4-16F2-4E72-A42A-06B4D6DC0102 State 3 3
AB2B7DA0-A90A-46FB-8F4B-8E662191E380 State 6 6
E2AC895A-0986-4A03-B104-A727D41405E3 State 1 1
66132F53-0A0E-431A-AD84-ADE77656CDF0 State 2 2
ECDDB790-88A4-4654-889D-BCA577C35CA8 State 5 5
F0469593-1B6B-4F98-92AE-DF50E76B9ED8 State 7 7
5929D0CA-E8B0-42EE-9CF4-E3FBC9973AA6 State 4 4