0

我希望有人可以帮助我解决这个问题。基本上我有两个要“合并”的查询。我希望第二个查询与第一个查询一起作为额外的列。第一个是这样的:

SELECT t_Item_Storage_Location.Storage_Loc_Nbr, 
t_Storage_Location.Storage_Loc_Type_Code, 
Count(t_Load.Load_Id) AS CurrentLoadCount,
t_load.MMM_Id_Nbr

FROM t_Load INNER JOIN (t_Storage_Location INNER JOIN t_Item_Storage_Location ON 
t_Storage_Location.Storage_Loc_Nbr = t_Item_Storage_Location.Storage_Loc_Nbr) ON 
(t_Load.Storage_Loc_Nbr = t_Item_Storage_Location.Storage_Loc_Nbr) 
AND (t_Load.MMM_Id_Nbr = t_Item_Storage_Location.MMM_Id_Nbr)


where ((((t_Item_Storage_Location.MMM_Id_Nbr) Between '702004%' And '702011%') 
AND ((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%A') 
AND ((t_Storage_Location.Storage_Loc_Type_Code)='CD') 
AND ((t_Load.Active_Status_Ind)='A') 
AND ((t_Load.QC_Status_Code) Like 'R%') 
AND ((t_Load.MMM_Facility_Code)='MC')) 
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%B'))
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%C')) 
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%D')) 
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%E'))

)
GROUP BY t_Item_Storage_Location.MMM_Id_Nbr,
t_Item_Storage_Location.Storage_Loc_Nbr, 
t_Storage_Location.Storage_Loc_Type_Code,
t_Load.MMM_Facility_Code,
t_load.MMM_Id_Nbr

HAVING
Count(t_Load.Load_Id)<4

第二个基于第一个的 t_load.MMM_Id_Nbr。基本上我想用那个 mmm_id_nbr 计算所有负载。

SELECT count(Load_ID) as LoadCount, MMM_Id_Nbr, storage_Loc_Nbr
FROM t_load 
WHERE QC_Status_Code like 'R%' and mmm_Facility_Code ='MC' and Active_Status_Ind='A'
GROUP by MMM_Id_Nbr, storage_loc_Nbr
4

4 回答 4

0

将此作为建议而不是帮助,查询过于未优化,也许您应该重新考虑您的数据库结构

于 2012-10-17T22:57:59.723 回答
0

好吧我可以回答你的问题

您可以使用类似这样的方式加入另一个查询

Select KeyColumn,Column1 From Table1
left Join ( Select ForeignKeyColumn, Count(*) From Table2 Group By ForeignKeyColumn) dummyTableName
On Table1.KeyColumn = dummyTableName.ForeignKeyColumn

我正要弄清楚如何将这种技术应用于您的查询,但我的手指发生了叛变。

快速提示

OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%B'))
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%C')) 
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%D')) 
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%E'))

可以替换为

OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%[BCDE]'))

例如

于 2012-10-17T23:09:02.357 回答
0

我现在做了一个这样的子查询:

Select b.Storage_Loc_Nbr, b.CurrentLoadCount,
(SELECT count(Load_ID) as LoadCount 
FROM t_load c
WHERE b.MMM_Id_Nbr=c.MMM_Id_Nbr

)as CountOtherLoc

FROM
(

SELECT * FROM

(

SELECT t_Item_Storage_Location.Storage_Loc_Nbr, 
t_Storage_Location.Storage_Loc_Type_Code, 
Count(t_Load.Load_Id) AS CurrentLoadCount,
t_load.MMM_Id_Nbr

FROM t_Load INNER JOIN (t_Storage_Location INNER JOIN t_Item_Storage_Location ON 
t_Storage_Location.Storage_Loc_Nbr = t_Item_Storage_Location.Storage_Loc_Nbr) ON 
(t_Load.Storage_Loc_Nbr = t_Item_Storage_Location.Storage_Loc_Nbr) AND      (t_Load.MMM_Id_Nbr = t_Item_Storage_Location.MMM_Id_Nbr)


where ((((t_Item_Storage_Location.MMM_Id_Nbr) Between '702004%' And '702011%') 
AND ((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%A') 
AND ((t_Storage_Location.Storage_Loc_Type_Code)='CD') 
AND ((t_Load.Active_Status_Ind)='A') 
AND ((t_Load.QC_Status_Code) Like 'R%') 
AND ((t_Load.MMM_Facility_Code)='MC')) 
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%B'))
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%C')) 
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%D')) 
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%E'))

 ...

as a

)  
as b

它给了我想要的东西,除了 countOtherLoc 列没有我拥有的 where 条件,我不能在这种类型的子查询中使用多个。我需要在第 4 行下添加“WHERE QC_Status_Code like 'R%' and mmm_Facility_Code ='MC' and Active_Status_Ind='A' 但它不会让我。有什么想法吗?

于 2012-10-18T21:55:44.140 回答
-1

你在查询中有一些错误吗?为什么在 LIKE 查询的 AND 之后使用一些 OR 和而不是。一个查询完全是白痴。

让我们考虑一下,例如 LIKE 查询:

where [...]
AND ((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%A') 

[....]

# what about above on %A ?
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%B'))
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%C')) 
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%D')) 
OR (((t_Item_Storage_Location.Storage_Loc_Nbr) Like '%E'))

您想准确搜索以 A 结尾的字符串,%A但在 OR 子句中没有导入其他查询搜索?这不是很好的查询,也没有正确设置。

GROUP BY可能会为您的服务器生成太多结果。你是杀手吗?

如果你想使用 Like 你应该使用 REGEXP 而不是 Like 太多选项。您错过了正确 SQL 优化的一些基础知识。

于 2012-10-17T23:04:15.323 回答