使用 SELECT 和显示条件

Using SELECT with a display condition

SELECT DISTINCT Invoice.InvNo, Invoice.OrderNo, Part.PartNo,
  orders.orddate AS Order_Date, Invoice.InvDate AS Bill_Date,
  MiscChg.Descr, MiscChg.RegFee, Invoice.InvAmt, 
  Orders.ClaimNo, Firm.FirmName AS Ordering_Firm, 
**oppatty.attyid(WHERE oppatty.attyfor = 13)**, Location.Name1 AS Location

粗体部分是我遇到问题的部分。我知道我所拥有的是不对的,但它证明了我想要完成的事情。在 oppatty table 中,可能会列出多个项目。我希望它只显示 "AttyID for the entry that has an ATTYFOR = 13".

希望这是有道理的,谢谢

杰克

你的查询没有 from 或 where 子句,你的问题有点混乱,但即便如此,我想我明白你想做什么。假设用 null 填充 "AttyID" 值是可以接受的,其中 "AttyFor" 不等于 13,那么您可以只使用 case 语句。尝试这样的事情

select
    stuff.things,
    case 
        where oppatty.attyfor <> 13 then null
        else oppatty.attyid
    end as attyid,
    stuff.others
from
    oppatty
    join stuff on oppatty.ID = stuff.ID

如果这不是您想要的结果,并且您宁愿完全排除 "AttyFor" 不等于 13 的行,那么只需使用 where 子句即可。

select
    stuff.things,
    oppatty.attyid,
    stuff.others
from
    oppatty
    join stuff on oppatty.ID = stuff.ID
where
    oppatty.attyfor = 13

您需要在 select 语句中添加一个 CASE WHEN

SELECT DISTINCT 
    Invoice.InvNo, 
    Invoice.OrderNo, 
    Part.PartNo, 
    orders.orddate AS Order_Date, 
    Invoice.InvDate AS Bill_Date, 
    MiscChg.Descr, 
    MiscChg.RegFee, 
    Invoice.InvAmt, 
    Orders.ClaimNo, 
    Firm.FirmName AS Ordering_Firm, 
    CASE WHEN oppatty.AttyFor = 13
         THEN oppatty.AttyId
         ELSE '' END AS attyfor, 
    Location.Name1 AS Location
FROM
    .........

这将在行的 AttyFor 字段等于 13 时显示 AttyId 字段,否则显示空字符串。