使用SQL Server 2008 R2我目前有这个查询按预期工作。它返回一个简单的不同列表catalogID
和catType
。
注意 - catType
字符串未在ALM_Catalog
中进行规范化,因此内部选择
SELECT DISTINCT
catalogID, catType
FROM
ALM_Catalog cat1
WHERE
cat1.CatalogID = (SELECT max(CatalogID)
FROM ALM_Catalog cat2
WHERE cat1.catType = cat2.catType)
ORDER BY
catType;
我现在有一个ALM_CatalogAudience
表。这是一个包含两列catalogID
和businessGroupID
的链接表。它旨在仅向某些业务组显示某些目录项。
所以我将上面的查询改为:
SELECT DISTINCT
cat1.catalogID, cat1.catType
FROM
ALM_Catalog cat1
LEFT OUTER JOIN
ALM_CatalogAudience ON cat1.catalogID = ALM_CatalogAudience.catalogID
WHERE
cat1.CatalogID = (SELECT MAX(CatalogID)
FROM ALM_Catalog cat2
WHERE cat1.catType = cat2.catType)
AND ALM_CatalogAudience.businessGroupID = (SELECT md_businessGroup.businessGroupID
FROM
((directory d
LEFT OUTER JOIN
md_department ON d.departmentID = md_department.departmentID)
LEFT OUTER JOIN
md_businessGroup ON md_department.businessGroupID = md_businessGroup.businessGroupID)
WHERE directoryID = 18230)
ORDER BY
cat1.catType;
什么都不返回,我看不出原因。注 - 第二个子选择(SELECT md_businessGroup ...)返回1,当我运行它时:
select * from ALM_CatalogAudience where businessGroupID=1
它返回132条记录。我错过了什么?提前谢谢。
编辑:一些示例输出是:
1362 Accessories & Parts
1368 Audio & Video
1399 Batteries & Power
1412 Cables & Connectors
1418 Carrying Cases
1424 Docks & Port Replicators
1429 Keyboards & Mice
编辑:看来这个位置:
cat1.CatalogID = (SELECT MAX(CatalogID) MAX
是我犯的错误。 MAX catalogID不一定映射到businessGroupID 1,除非目录项只被映射到一个恰好是1的业务组。所以我相信这对我现在有用:
SELECT DISTINCT max(cat1.catalogID) , cat1.catType FROM ALM_Catalog cat1
left outer join ALM_CatalogAudience on cat1.catalogID = ALM_CatalogAudience.catalogID
WHERE
ALM_CatalogAudience.businessGroupID = (
SELECT md_businessGroup.businessGroupID
from directory d
LEFT OUTER JOIN md_department
on d.departmentID = md_department.departmentID
LEFT OUTER JOIN md_businessGroup
on md_department.businessGroupID = md_businessGroup.businessGroupID
where directoryID=18230)
group BY cat1.catType
order BY cat1.catType;
答案 0 :(得分:0)
SELECT DISTINCT cat1.catalogID, cat1.catType
FROM ALM_Catalog cat1
left outer join ALM_CatalogAudience
on cat1.catalogID = ALM_CatalogAudience.catalogID
WHERE
cat1.CatalogID=
(SELECT max(CatalogID) from ALM_Catalog cat2 WHERE cat1.catType=cat2.catType)
AND ALM_CatalogAudience.businessGroupID =
(
SELECT md_businessGroup.businessGroupID
from directory d
LEFT OUTER JOIN md_department
on d.departmentID = md_department.departmentID
LEFT OUTER JOIN md_businessGroup
on md_department.businessGroupID = md_businessGroup.businessGroupID
where directoryID=18230
)
ORDER BY cat1.catType;
我很惊讶您没有收到语法错误。我刚修好它并没有改变任何东西,如果它仍然无法工作,那么我们需要一些样本数据。
好的,因为仍然没有记录我能告诉你的是检查问题所在的方法。
运行:
SELECT md_businessGroup.businessGroupID
from directory d
LEFT OUTER JOIN md_department
on d.departmentID = md_department.departmentID
LEFT OUTER JOIN md_businessGroup
on md_department.businessGroupID = md_businessGroup.businessGroupID
where directoryID=18230
如果你把号码写下来了。如果你在这里得到0记录,你应该检查目录表并再次测试脚本。
2.再次检查ALM_CatalogAudience并保存结果
select * from ALM_CatalogAudience where businessGroupID = the number you just get;
如果不是,则ALM_CatalogAudience不具有该groupID。如果获取记录,请将其保存在临时表中。
答案 1 :(得分:0)
您可能希望修复原始查询以匹配Raffaello.D.Huke为您修复的版本。如果您的子查询和ALM_CatalogAudience.businessGroupID都返回1,则可能是您的businessGroupID不是同一类型。你可以试试下面的吗?
...
AND CAST(ALM_CatalogAudience.businessGroupID AS VARCHAR) =
(
SELECT CAST(md_businessGroup.businessGroupID AS VARCHAR)
from directory d
...