2

有任何访问大师可以帮助我吗?试图弄清楚如何实现以下目标:我想要一个查询来遍历个人表中的每个人,并返回那些在圣诞节列表字段中具有真实值的人。这个数据库是旧的,是用 CompanyName 作为公司表的主键创建的(我知道 - 糟糕的设计)。为了解决拥有多个地点的公司的问题,他们创建了一个地址栏字段。这样,如果“Y 公司”在圣地亚哥开设新位置,他们会添加“Y 公司 - 圣地亚哥”条目并在地址行中输入“Y 公司”。然后对于没有公司的个人,将他们输入到“Home”的虚拟公司条目中。所以理想情况下,我希望这个查询做的是循环,抓住所有应该在圣诞节名单上的人,如果地址栏字段 ISN'T NULL 我希望 CompanyName 等于 AddressLine,但如果 CompanyName = "Home" 我希望 CompanyName 等于 ""(空字符串)。还有一些人有隔夜地址,因为他们的正常地址是邮政信箱。以下查询我主要为我工作,但是如果 CompanyName="Home",我不清楚如何在返回的结果中将公司名称设为空白。

SELECT tblIndividuals.FirstName, tblIndividuals.LastName, IIf([tblcompanies].[addressline] Is Not Null,[tblcompanies].[addressline],[tblcompanies].[companyname]) AS CompanyName, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[MailingAddress1],[tblIndividuals].[MailingAddress1]) AS MailingAddress1, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[MailingAddress2],[tblIndividuals].[MailingAddress2]) AS MailingAddress2, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[MailingAddress3],[tblIndividuals].[MailingAddress3]) AS MailingAddress3, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[MailingAddress4],[tblIndividuals].[MailingAddress4]) AS MailingAddress4, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[City],[tblIndividuals].[City]) AS City, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[State],[tblIndividuals].[State]) AS State, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[Zip],[tblIndividuals].[Zip]) AS Zip
FROM tblIndividuals INNER JOIN tblCompanies ON tblIndividuals.CompanyName = tblCompanies.CompanyName
WHERE (((tblIndividuals.ChristmasList)=True))
Order by [tblIndividuals].[companyname];

如果可能的话,我希望它按公司名称值 (AZ) 排序,然后按个人的姓氏 (AZ) 排序。我不会假装精通 SQL,所以您能给我的任何帮助将不胜感激。感谢您的时间。

4

1 回答 1

2

考虑一个 UNION 查询,例如:

SELECT i.FirstName, i.LastName, 
       IIf(c.[addressline] Is Not Null,
           c.[addressline],c.[companyname]) AS CompanyName, 
       c.[MailingAddress1] AS MailingAddress1, 
       c.[MailingAddress2] AS MailingAddress2, 
       c.[MailingAddress3] AS MailingAddress3, 
       c.[MailingAddress4] AS MailingAddress4, 
       c.[City] AS City, 
       c.[State] AS State,
       c.[Zip] AS Zip
FROM tblIndividuals i
INNER JOIN tblCompanies c ON i.CompanyName = c.CompanyName
WHERE i.ChristmasList=True
AND i.[MailingAddress1] Is Null
UNION
SELECT i.FirstName, i.LastName, 
       IIf(c.[addressline] Is Not Null,c.[addressline],
           c.[companyname]) AS CompanyName, 
       i.[MailingAddress1] AS MailingAddress1, 
       i.[MailingAddress2] AS MailingAddress2, 
       i.[MailingAddress3] AS MailingAddress3, 
       i.[MailingAddress4] AS MailingAddress4, 
       i.[City] AS City, 
       i.[State] AS State,
       i.[Zip] AS Zip
FROM tblIndividuals i
INNER JOIN tblCompanies c ON i.CompanyName = c.CompanyName
WHERE i.ChristmasList=True
AND i.[MailingAddress1] Is Not Null

您可以将查询括在括号中以创建可以使用查询设计窗口排序的派生表。

SELECT FirstName, LastName, etc 
FROM ( <union query> ) As q
Order by q.[companyname]
于 2012-06-19T22:37:38.433 回答