简体   繁体   English

在子查询中获取 DISTINCT 值

[英]Getting a DISTINCT value in a subquery

I have a recently been trying to convert the First() function from MS Access to T-SQL.我最近一直在尝试将 First() 函数从 MS Access 转换为 T-SQL。 Now First() does not exist in T-SQL so I've had to explore some alternatives.现在 First() 在 T-SQL 中不存在,所以我不得不探索一些替代方案。 I Found the First_Value function which seems to do the job.我找到了似乎可以完成这项工作的 First_Value 函数。

However, Take the code below:但是,请使用以下代码:

SELECT [Order Transactions Table].[Job Number],
   [Order Transactions Table].OutstandingBalance,
   [Order Transactions Table].[Accepted Quantity],
   MRPStockOut.MRPStockOutID,
   MRPStockOut.[Date],
   MRPStockOut.[Units],
   MRPHeader.MRPHeaderID,
   FIRST_VALUE(MRPSupplier.StockCode) OVER (PARTITION BY MRPSupplier.StockCode ORDER BY MRPSupplier.StockCode) AS FirstOfStockCode,
   --(SELECT TOP (1) MRPSupplier.StockCode ORDER BY [Order Transactions Table].[Job Number]) AS FirstOfStockCode,
   MRPHeader.MaterialDescription,
   MRPStockOut.StockQtyOut,
   MRPHeader.WeightPerUnit * MRPStockOut.StockQtyOut AS [Weight],
   LiveWIPStockAllocatedToJobsPrices.PurchPriceEach,
   LiveWIPStockAllocatedToJobsPrices.StockPriceEach,
   LiveWIPStockAllocatedToJobsPrices.PriceEach,
   (IIF(ISNULL(LiveWIPStockAllocatedToJobsPrices.PurchPriceEach,0) > 0,
    ISNULL(LiveWIPStockAllocatedToJobsPrices.PurchPriceEach,0),
    ISNULL(LiveWIPStockAllocatedToJobsPrices.StockPriceEach,0)) +0) * MRPStockOut.StockQtyOut AS TotalPrice

FROM [Order Transactions Table]
INNER JOIN MRPStockOut ON [Order Transactions Table].[Job Number] = MRPStockOut.JobNumber
INNER JOIN MRPHeader ON MRPStockOut.MRPHeaderID = MRPHeader.MRPHeaderID
INNER JOIN LiveWIPStockAllocatedToJobsPrices ON MRPHeader.MRPHeaderID = 
LiveWIPStockAllocatedToJobsPrices.MRPHeaderID
INNER JOIN MRPSupplier ON MRPHeader.MRPHeaderID = MRPSupplier.MRPHeaderID
GROUP BY
[Order Transactions Table].[Job Number],
[Order Transactions Table].OutstandingBalance,
[Order Transactions Table].[Accepted Quantity],
MRPStockOut.MRPStockOutID,
MRPStockOut.[Date],
MRPStockOut.[Units],
MRPHeader.MRPHeaderID,
MRPSupplier.StockCode,
MRPHeader.MaterialDescription, 
MRPStockOut.StockQtyOut,
MRPHeader.WeightPerUnit * MRPStockOut.StockQtyOut,
LiveWIPStockAllocatedToJobsPrices.PurchPriceEach,
LiveWIPStockAllocatedToJobsPrices.StockPriceEach,
LiveWIPStockAllocatedToJobsPrices.PriceEach,
(IIF(ISNULL(LiveWIPStockAllocatedToJobsPrices.PurchPriceEach,0) > 0,
ISNULL(LiveWIPStockAllocatedToJobsPrices.PurchPriceEach,0),
ISNULL(LiveWIPStockAllocatedToJobsPrices.StockPriceEach,0)) +0) * MRPStockOut.StockQtyOut
Having [Order Transactions Table].OutstandingBalance > 0

Particularly the line to select the first value in MRPSupplier.特别是在 MRPSupplier 中选择第一个值的行。 ( First(MRPSupplier.StockCode) in Access) (访问中的第一个(MRPSupplier.StockCode)

FIRST_VALUE(MRPSupplier.StockCode) OVER (PARTITION BY MRPSupplier.StockCode ORDER BY MRPSupplier.StockCode) AS FirstOfStockCode FIRST_VALUE(MRPSupplier.StockCode) OVER (PARTITION BY MRPSupplier.StockCode ORDER BY MRPSupplier.StockCode) AS FirstOfStockCode

Now this value is returning what that I am expecting, however, certain Jobs are come back with 2 records rather than one, under the same [Job Number].现在这个值正在返回我期望的值,但是,在相同的 [Job Number] 下,某些 Jobs 返回了 2 条记录而不是 1 条记录。 This is because one of the duplicate jobs comes back with FirstOfStockCode = Null and the other has the expected value.这是因为其中一个重复作业返回FirstOfStockCode = Null而另一个具有预期值。

This is a problem because this is a subquery and I have a SUM on the query above which is giving me an incorrect because TotalPrice is being Summed twice due to the duplicate record.这是一个问题,因为这是一个子查询,我在上面的查询中有一个 SUM,这给了我一个不正确的结果,因为由于重复记录,TotalPrice 被求和了两次。 How can I get it so that I only receive a Distinct value from this column?我怎样才能得到它,以便我只从该列中收到一个 Distinct 值?

Edit:编辑:

Current Results:当前结果: 当前结果

Expected Results are the only the 2nd record above is selected the First NULL record shouldn't be in the select list.预期结果是上面唯一的第 2 条记录,第一个 NULL 记录不应出现在选择列表中。

Edit 2:编辑2:

Here is another screenshot of more records (546 rows - This is two more than I get in MS Access):这是更多记录的另一个屏幕截图(546 行 - 这比我在 MS Access 中得到的多两行): 更多记录

Again, These two Null value records should not be exist as they are duplicates Edit 3: User @Isaac has managed to help me resolve this.同样,这两个空值记录不应该存在,因为它们是重复的 编辑 3:用户 @Isaac 已设法帮助我解决此问题。 See comments thread in his answer.请参阅他的回答中的评论线程。

Try replacing this line...尝试更换这一行...

FIRST_VALUE(MRPSupplier.StockCode) OVER (PARTITION BY MRPSupplier.StockCode ORDER BY MRPSupplier.StockCode) AS FirstOfStockCode,

with this...有了这个...

FIRST_VALUE(MRPSupplier.StockCode) OVER (ORDER BY MRPSupplier.StockCode) AS FirstOfStockCode,

I believe The PARTITION BY clause is causing the extra row to be returned.我相信 PARTITION BY 子句会导致返回额外的行。 If that causes the row with a NULL value to be returned add DESC like this...如果这导致返回具有 NULL 值的行,请像这样添加DESC ...

FIRST_VALUE(MRPSupplier.StockCode) OVER (ORDER BY MRPSupplier.StockCode DESC) AS FirstOfStockCode,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM