我有两个查询返回以下输出:T1和T2
现在我想在T1.CMonth和T2.DueMonth字段上加入两个表。
最终输出应如png。
所示T1的查询:
SELECT WorkManagementAsset.AssetCategory,CONVERT(CHAR(4),WorkItem.CompleteDate,100)+ CONVERT(CHAR(4),WorkItem.CompleteDate,120)AS CMonth, COUNT(CONVERT(CHAR(4),WorkItem.CompleteDate,100)+ CONVERT(CHAR(4),WorkItem.CompleteDate,120))AS Total_Complete 来自WorkManagementAsset INNER JOIN WorkOrder On WorkManagementAsset.Oid = WorkOrder.Asset INNER JOIN WorkItem ON WorkOrder.Oid = WorkItem.WorkOrder INNER JOIN InspectionDefectItem ON WorkItem.DefectItem = InspectionDefectItem.Oid AND WorkItem.Oid = InspectionDefectItem.WorkItem WHERE(NOT(InspectionDefectItem.DefectGroupName LIKE N' Re%')) GROUP BY WorkManagementAsset.AssetCategory,CONVERT(CHAR(4),WorkItem.CompleteDate,100)+ CONVERT(CHAR(4),WorkItem.CompleteDate,120) HAVING(WorkManagementAsset.AssetCategory = N' sealedroads')AND(NOT(CONVERT(CHAR(4),WorkItem.CompleteDate,100)+ CONVERT(CHAR(4), WorkItem.CompleteDate,120)IS NULL))
T2的查询:
SELECT WorkManagementAsset.AssetCategory,CONVERT(CHAR(4), WorkItem.DueDate,100)+ CONVERT(CHAR(4),WorkItem.DueDate,120)AS DueMonth, COUNT(CONVERT(CHAR(4),WorkItem.DueDate,100)+ CONVERT(CHAR(4),WorkItem.DueDate,120))AS Total_DUE FROM WorkManagementAsset INNER JOIN WorkOrder On WorkManagementAsset.Oid = WorkOrder.Asset INNER JOIN WorkItem ON WorkOrder.Oid = WorkItem.WorkOrder INNER JOIN InspectionDefectItem ON WorkItem.DefectItem = InspectionDefectItem.Oid AND WorkItem.Oid = InspectionDefectItem.WorkItem WHERE(不是 (InspectionDefectItem.DefectGroupName LIKE N' Re%'))GROUP BY WorkManagementAsset.AssetCategory,CONVERT(CHAR(4),WorkItem.DueDate, 100)+ CONVERT(CHAR(4),WorkItem.DueDate,120)具有
(WorkManagementAsset.AssetCategory = N' sealedroads')AND (CONVERT(CHAR(4),WorkItem.DueDate,100)+ CONVERT(CHAR(4), WorkItem.DueDate,120)<> ' 0&#39)
任何帮助都将受到高度赞赏。 感谢
答案 0 :(得分:0)
此
WITH T1 AS
(
SELECT
WorkManagementAsset.AssetCategory,
CONVERT(CHAR(4), WorkItem.CompleteDate, 100) + CONVERT(CHAR(4), WorkItem.CompleteDate, 120) AS CMonth,
COUNT(CONVERT(CHAR(4), WorkItem.CompleteDate, 100) + CONVERT(CHAR(4), WorkItem.CompleteDate, 120)) AS Total_Complete
FROM WorkManagementAsset
INNER JOIN WorkOrder
ON WorkManagementAsset.Oid = WorkOrder.Asset
INNER JOIN WorkItem
ON WorkOrder.Oid = WorkItem.WorkOrder
INNER JOIN InspectionDefectItem
ON WorkItem.DefectItem = InspectionDefectItem.Oid AND WorkItem.Oid = InspectionDefectItem.WorkItem
WHERE (NOT (InspectionDefectItem.DefectGroupName LIKE N'Re%'))
GROUP BY
WorkManagementAsset.AssetCategory,
CONVERT(CHAR(4), WorkItem.CompleteDate, 100) + CONVERT(CHAR(4), WorkItem.CompleteDate, 120)
HAVING (WorkManagementAsset.AssetCategory = N'sealedroads')
AND (NOT (CONVERT(CHAR(4), WorkItem.CompleteDate, 100) + CONVERT(CHAR(4), WorkItem.CompleteDate, 120) IS NULL))
),
T2 AS
(
SELECT
WorkManagementAsset.AssetCategory,
CONVERT(CHAR(4), WorkItem.DueDate, 100) + CONVERT(CHAR(4), WorkItem.DueDate, 120) AS DueMonth,
COUNT(CONVERT(CHAR(4), WorkItem.DueDate, 100) + CONVERT(CHAR(4), WorkItem.DueDate, 120)) AS Total_DUE
FROM WorkManagementAsset
INNER JOIN WorkOrder
ON WorkManagementAsset.Oid = WorkOrder.Asset
INNER JOIN WorkItem
ON WorkOrder.Oid = WorkItem.WorkOrder
INNER JOIN InspectionDefectItem
ON WorkItem.DefectItem = InspectionDefectItem.Oid
AND WorkItem.Oid = InspectionDefectItem.WorkItem
WHERE (NOT (InspectionDefectItem.DefectGroupName LIKE N'Re%'))
GROUP BY WorkManagementAsset.AssetCategory,
CONVERT(CHAR(4), WorkItem.DueDate, 100) + CONVERT(CHAR(4), WorkItem.DueDate, 120)
HAVING (WorkManagementAsset.AssetCategory = N'sealedroads')
AND (CONVERT(CHAR(4), WorkItem.DueDate, 100) + CONVERT(CHAR(4), WorkItem.DueDate, 120) <> '0')
)
SELECT *
FROM T1
INNER JOIN T2 ON T1.CMonth = T2.DueMonth;
请注意,出于多种原因,这是一种非常低效的查询。