简体   繁体   English

Microsoft Access sql 查询以查找列中所有值都匹配的记录

[英]Microsoft Access sql query to find records where all values in a column match

I have a similar to below:我有一个类似于下面的:

ProductID产品编号 Division分配 OrdNum序号
1 1 011 011 123 123
1 1 705 705 123 123
1 1 660 660 123 123
2 2 011 011 511 511
2 2 705 705 412 412
2 2 660 660 216 216

I am trying to write a query that will return each product ID and if the OrdNum for all Divisions is the same it will return the OrdNum, and if the OrdNums are different it will return "Varies"我正在尝试编写一个返回每个产品 ID 的查询,如果所有部门的 OrdNum 相同,它将返回 OrdNum,如果 OrdNum 不同,它将返回“Varies”

The results should look like this:结果应如下所示:

ProductID产品编号 OrdNum序号
1 1 123 123
2 2 Varies变化

Consider:考虑:

Query1:查询1:

SELECT DISTINCT Table1.ProductID, Table1.OrdNum FROM Table1;

Query2:查询2:

SELECT ProductID, Count(OrdNum) AS CountOfOrdNum
FROM Query1
GROUP BY ProductID;

Query3:查询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");

Below query may work for you.以下查询可能对您有用。

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

在此处输入图像描述

Just compare the min and max values:只需比较最小值和最大值:

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.

相关问题 访问查询查找所有链接匹配的所有记录 - Access Query Find All records Where all Linked Records Match SQL查询列出所有记录,其中一列的所有值不在另一列中 - SQL Query for listing all records where all values for one column are not in another column SQL查询连接中的所有记录是否与条件匹配? - SQL query where ALL records in a join match a condition? SQL:如何查找列中所有记录具有相同值的值总和? - SQL: How to find the sum of values where all the records has the same value in a column? Microsoft SQL查找行,其中列包含数组中的所有项目 - Microsoft SQL find rows where column contains all items in an array Microsoft Access SQL 仅返回一组分组记录中的最后一条记录等于另一列中的记录 - Microsoft Access SQL to return only records where the last record in a set of grouped records equals something in another column SQL查询仅在所有列值匹配时才更新表 - SQL query to update table only if all values for column match SQL-是否可以从一列中选择所有不同的值,而另一列中的所有值都匹配? - SQL - Is it possible to select all Distinct Values from one column where all values from another column match? SQL查询查找具有第二列的所有值的值 - SQL Query find values that have all values of second column SQL查询以查找第一列中的值相同的两列中的重复项 - Sql Query to find duplicates in 2 columns where the values in first column are same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM