IIF功能(访问)

时间:2012-06-19 22:13:16

标签: ms-access iif

任何Access大师都可以帮助我吗?试图弄清楚如何实现以下目标:我希望查询遍历个人表中的每个人,并返回那些在圣诞节列表字段中具有真实价值的人。这个数据库是旧的,是用CompanyName作为公司表的主键创建的(我知道 - 糟糕的设计)。为了让公司拥有多个地点,他们创建了一个地址线字段。这样,如果“公司y”在圣地亚哥开设新地点,他们会添加“公司Y - 圣地亚哥”的条目,并在地址栏中输入“公司Y”。然后,对于没有公司的个人,他们进入“家庭”的虚拟公司条目。所以理想情况下,我希望这个查询要做的是循环,抓住所有应该在圣诞节列表上的个人,如果地址线字段是非NULL我想要CompanyName等于AddressLine,但是如果CompanyName =“首页“我希望CompanyName等于”“(emptry string)。还有一些人有隔夜地址,因为他们的正常地址是P.O Box。以下查询我主要为我工作,但是如果CompanyName =“Home”,我不清楚如何在返回的结果中将companyname设为空白。

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];

我希望按照公司名称值(A-Z)排序,然后按个人姓氏(A-Z)排序,如果可能的话。我不会假装擅长SQL,所以你能给我的任何帮助都会非常感激。感谢您的时间。

1 个答案:

答案 0 :(得分: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]