按sku分组,最大日期为SQL [英] Group by sku, max date SQL

查看:85
本文介绍了按sku分组,最大日期为SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这在这里被问了很多,并且我尝试使用其他示例并入我自己的示例,但是我似乎无法完成这项工作.

I know this is asked quite a bit here, and I have tried to use other examples to incorporate into my own, but I can't seem to make this work.

我有sku,日期和费用列,我想查看所有3列,但仅按最大日期(按sku分组)查看. 当前:

I have columns for sku, date, and cost, and I want to view all 3 columns, but only by max date, grouped by sku. Currently:

Sku   Date     Cost
1     06/24/15 .01
1     02/22/14 .02
2     06/24/15 .04
2     02/22/14 .05

需要:

Sku   Date     Cost
1     06/24/15 .01
2     06/24/15 .04

这是我的SQL的样子:

This is what my SQL looks like:

SELECT dbo_SKU.PROD_CODE AS Sku, dbo_LOTS.REC_DATE AS [Last Date], 
   dbo_LOT_ITEM.COST AS Cost
FROM (dbo_LOTS INNER JOIN dbo_SKU ON dbo_LOTS.SKU_ID = dbo_SKU.SKU_ID) 
INNER JOIN dbo_LOT_ITEM ON dbo_LOTS.LOT_ID = dbo_LOT_ITEM.LOT_ID;

这是设计视图的外观(我更是一个视觉化的人): 设计视图

Here is what the design view looks like (I'm more of a visual person): Design View

这是第2周的自学,它教自己如何操作Access以及它如何工作,因此,如果我们能在蜡笔上细分一下如何正确进行此工作,那就太好了.

This is week 2 of teaching myself how to operate Access and how it all works, so if we could break this down in crayon on how I make this work correctly, that would be great.

推荐答案

您可以添加其他逻辑以获取最后日期.一种方法是在WHERE子句中添加相关的子查询:

You can add additional logic to get the last date. One method is to add a correlated subquery in the WHERE clause:

SELECT s.PROD_CODE AS Sku, l.REC_DATE AS [Last Date],  li.COST AS Cost
FROM (dbo_LOTS as l INNER JOIN
      dbo_SKU as si
      ON l.SKU_ID = s.SKU_ID
     ) INNER JOIN
     dbo_LOT_ITEM as li
     ON l.LOT_ID = li.LOT_ID
WHERE l.REC_DATE = (SELECT MAX(l2.REC_DATE)
                    FROM dbo_LOTS as l2
                    WHERE l2.SKU_ID = l.SKU_ID
                   );

这篇关于按sku分组,最大日期为SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆