如何使用基于外部查询的count在sql中创建子查询

时间:2012-10-17 22:53:41

标签: sql-server sql-server-2008

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

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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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]'))
例如

答案 2 :(得分: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列没有我所拥有的条件,我不能在这种类型的子查询中使用多个。 我需要在第4行添加“WHERE QC_Status_Code,如'R%'和mmm_Facility_Code ='MC'和Active_Status_Ind ='A',但它不会让我。任何想法?

答案 3 :(得分:-1)

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

让我们考虑一下像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可能会产生太多结果的服务器。你是杀手吗?

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