[英]Microsoft Access sql query to find records where all values in a column match
我有一個類似於下面的:
產品編號 | 分配 | 序號 |
---|---|---|
1 | 011 | 123 |
1 | 705 | 123 |
1 | 660 | 123 |
2 | 011 | 511 |
2 | 705 | 412 |
2 | 660 | 216 |
我正在嘗試編寫一個返回每個產品 ID 的查詢,如果所有部門的 OrdNum 相同,它將返回 OrdNum,如果 OrdNum 不同,它將返回“Varies”
結果應如下所示:
產品編號 | 序號 |
---|---|
1 | 123 |
2 | 變化 |
考慮:
查詢1:
SELECT DISTINCT Table1.ProductID, Table1.OrdNum FROM Table1;
查詢2:
SELECT ProductID, Count(OrdNum) AS CountOfOrdNum
FROM Query1
GROUP BY ProductID;
查詢3:
SELECT Table1.ProductID, IIf([CountOfOrdNum]=1,[OrdNum],"Varies") AS Ord
FROM Query2 INNER JOIN Table1 ON Query2.ProductID = Table1.ProductID
GROUP BY Table1.ProductID, IIf([CountOfOrdNum]=1,[OrdNum],"Varies");
以下查詢可能對您有用。
select t2.ProductID,iif(t2.OrdCount>1,"Varies",DLookup("[OrdNum]","[Table1]","[ProductID]="&t2.ProductID)) as OrdNum
from (select t1.ProductID, count(t1.ProductID) as OrdCount
from (select ProductID, OrdNum, Count(Division) AS CountOfDivision from Table1 group by ProductID, OrdNum) as t1 group by t1.ProductID) as t2
只需比較最小值和最大值:
Select
ProductID,
IIf(Min(OrdNum) = Max(OrdNum), First(OrdNum), "Varies") As OrderNumber
From
YourTable
Group By
ProductID
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.