对于SQL而言,我是新手,我需要一些帮助,这对我来说有点太复杂,无法消化并使其有效。我这里有一个存储过程,它将在输入搜索参数时为我的客户显示一个报告。
我的表格是:
TXN_RMReport - RMRID(pk),rmrPlant(fk,来自FO_Property),rmrCID(fk,来自RM_Category),rmrBusinessArea,rmrCostCenter,rmrWCCode,rmrReportedBy
FO_Property - PID(pk),pCode,pMappingCode(包含RM_BusinessArea的baBusinessArea)
RM_Category - CID(pk)
RM_BusinessArea - BAID(pk),baBusinessArea(代码,非fk),baDescription
RM_CostCentre - CCID(pk),ccCostCenter,ccCompCode(代码,基于FO_Property的pMappingCode,而不是fk)ccBusinessArea(代码,基于baBusinessArea,而不是fk),ccDescription。
SEC_User - UID(pk),uName,uReportedBy
这里的frankenstein问题是RM_CostCentre的CCID不用于任何事情。为了使CostCentre的数据能够唯一地识别报告,它需要参考ccCompCode,ccCostCenter和ccBusinessArea。
例如,我有两个名为“RWB”和“RWMS”的pCode值,ccCostCenter“200”和“201”,两者都映射到ccBusinessArea“FB”,但有不同的描述“FOOD FACTORY”和“COFFEE TERRACE”分别为“201”的“200”和“TASTE OF INDIA”和“HAINAN EXPRESS”,共有4种独特的组合。
用户可以从数据库中选择数据来填写表格。然后,他们可以将报告插入TXN_RMReport。当他们想要在Property上使用空参数查询他们的现有条目时会出现问题,导致从未添加的错误和重复行,因为“200”或任何其他数字被映射到2个不同的代码“RWB”和“RWMS”。
SET @SELECT_BASE =
' FROM TXN_RMReport
INNER JOIN RM_BusinessArea (nolock) on rmrBusinessArea = baBusinessArea
INNER JOIN RM_CostCentre (nolock) on rmrCostCenter = ccCostCenter
LEFT JOIN SEC_User (nolock) on rmrRequestedBy = SEC_User.suUserID
LEFT JOIN (Select WCID,wcCode,wclDesc from RM_WorkCentre (nolock) inner join RM_WorkCentre_Locale (nolock) on WCID = wclWCID) as A on rmrWCCode=A.wcCode
' + CASE WHEN @WithTypeOfWorks = 1 THEN '
INNER JOIN TXN_RMDetail ON rmdRMRID = RMRID' ELSE '' END + '
WHERE 0 = 0' + CASE WHEN @RMRID IS NOT NULL THEN
' AND RMRID = @RMRID' ELSE '' END + CASE WHEN @rmrSTAYID IS NOT NULL THEN
' AND rmrSTAYID = @rmrSTAYID' ELSE '' END + CASE WHEN @PID IS NOT NULL THEN
' AND rmrPID = @PID' ELSE '' END + CASE WHEN @rmrCID IS NOT NULL THEN
' AND rmrCID = @rmrCID' ELSE '' END + CASE WHEN @rmrStatusSTID IS NOT NULL THEN
' AND rmrStatusSTID = @rmrStatusSTID' ELSE '' END + CASE WHEN @rmrJobTicketNo IS NOT NULL THEN
' AND rmrJobTicketNo LIKE REPLACE(@rmrJobTicketNo,''*'',''%'')' ELSE '' END + CASE WHEN @rmrContactNo IS NOT NULL THEN
' AND rmrContactNo LIKE REPLACE(@rmrContactNo,''*'',''%'')' ELSE '' END + CASE WHEN @rmrAppointmentDate IS NOT NULL THEN
' AND rmrAppointmentDate = @rmrAppointmentDate' ELSE '' END + CASE WHEN @AppointmentDateFrom IS NOT NULL THEN
' AND rmrAppointmentDate >= @AppointmentDateFrom' ELSE '' END + CASE WHEN @AppointmentDateTo IS NOT NULL THEN
' AND rmrAppointmentDate <= @AppointmentDateTo' ELSE '' END
(在这种情况下其他表格不相关且不影响搜索条件)这里,“1000”或“RWB”不应存在第二行,因为“成本中心”列映射为“1001”,它应该属于“RWMS”。同样的情况也是如此。
即使使用搜索条件,重复的行仍然会发生,我必须用这两行“作弊”:
+ CASE @PID WHEN 39 THEN ' AND RM_CostCentre.ccCompCode = ''RWB''' ELSE '' END
+ CASE @PID WHEN 40 THEN ' AND RM_CostCentre.ccCompCode = ''RWMS''' ELSE '' END
如何重写这个以便我不会得到错误的行?
表格不正确:
+------+------+---------+-------------+------------+--------------+-------------+
| Code | Area | CCenter | DESCRIPTION | ReportedBy | Remarks | CreatedDate |
+------+------+---------+-------------+------------+--------------+-------------+
| RWB | FB | 200 | CTERRACE | TANTAN | NO CAKE | 20/01/2017 |
| RWB | FB | 200 | FAVENUE | TANTAN | NO CAKE | 20/01/2017 |
| RWMS | CS | 501 | BACCARAT | JIM | SCRATCHED | 20/01/2017 |
| RWB | ADMC | 700 | CAFETERIA | JIM | BROKEN TILES | 21/01/2017 |
| RWB | ADMC | 700 | HRESOURCE | JIM | BROKEN TILES | 21/01/2017 |
| RWMS | FB | 200 | CTERRACE | ELSA | LEAKING PIPE | 20/01/2017 |
| RWMS | FB | 200 | FAVENUE | ELSA | LEAKING PIPE | 20/01/2017 |
+------+------+---------+-------------+------------+--------------+-------------+
预期结果:
+------+------+---------+-------------+------------+--------------+-------------+
| Code | Area | CCenter | DESCRIPTION | ReportedBy | Remarks | CreatedDate |
+------+------+---------+-------------+------------+--------------+-------------+
| RWB | FB | 200 | CTERRACE | TANTAN | NO CAKE | 20/01/2017 |
| RWMS | CS | 501 | BACCARAT | JIM | SCRATCHED | 20/01/2017 |
| RWB | ADMC | 700 | CAFETERIA | JIM | BROKEN TILES | 21/01/2017 |
| RWMS | FB | 200 | FAVENUE | ELSA | LEAKING PIPE | 20/01/2017 |
+------+------+---------+-------------+------------+--------------+-------------+