我从3个不同的表格中获取了数据,如下所示。在这里我获取数据,但在与where Convert(Varchar(10),DWA.Date,111)<convert(Varchar(10),DFD.Date_of_Filling,111)
比较后只获得最小数据。
我需要表MD中提供的完整数据,但由于条件的原因而无法获取。请帮助。
不仅我需要AVG(cast(DWA.Airtel as int)) as Avg_DG_Run
平均可用的最后3个日期。
请帮助
SELECT md.IndusID,
md.Site_Name,
md.ANCHOR_OPCO,
md.DISTRICT,
md.TECH_NAME,
md.TECH_No,
md.OME_Technical_Manager_Name,
md.DIESEL_FILLER,
md.DIESEL_FILLER_MOB,
md.EB_DG,
md.Min_Stock_Req,
md.Actual_CPH,
Sum(Cast(dfd.Qty_Filled AS INT)) AS This_Month_Filling
,
Max(CONVERT(VARCHAR(10), DFD.Date_of_Filling, 111)) AS
Last_Filling_Date,
Avg(Cast(DWA.Airtel AS INT)) AS Avg_DG_Run,
Sum(Cast(DWA.DG_RUN AS INT)) AS DGRun
FROM MasterData MD
LEFT JOIN DieselFillingData DFD
ON md.IndusID = dfd.Site_ID
LEFT JOIN DayWiseAlarm DWA
ON md.IndusID = dwa.Indus_ID
WHERE CONVERT(VARCHAR(10), DWA.Date, 111) <
CONVERT(VARCHAR(10), DFD.Date_of_Filling, 111)
GROUP BY md.IndusID, md.Site_Name, md.ANCHOR_OPCO, md.DISTRICT, md.TECH_NAME,
md.TECH_No, md.OME_Technical_Manager_Name, md.DIESEL_FILLER,
md.DIESEL_FILLER_MOB, md.EB_DG, md.Min_Stock_Req, md.Actual_CPH
ORDER BY md.OME_Technical_Manager_Name,
md.TECH_NAME
答案 0 :(得分:0)
我明白了。 where
条件将外连接转换为内连接。将条件移动到on
子句:
FROM MasterData MD LEFT JOIN
DieselFillingData DFD
ON md.IndusID = dfd.Site_ID LEFT JOIN
DayWiseAlarm DWA
ON md.IndusID = dwa.Indus_ID AND
CONVERT(VARCHAR(10), DWA.Date, 111) < CONVERT(VARCHAR(10), DFD.Date_of_Filling, 111)
将日期转换为字符串进行比较似乎是一个坏主意。这应该做你想要的:
FROM MasterData MD LEFT JOIN
DieselFillingData DFD
ON md.IndusID = dfd.Site_ID LEFT JOIN
DayWiseAlarm DWA
ON md.IndusID = dwa.Indus_ID AND
DWA.Date < DFD.Date_of_Filling