简体   繁体   English

SQL查询以获取不同年份但具有一致数据的已售零件总数

[英]SQL query to get totals of parts sold for different years but with aligned data

I have a sales order database that contains entries on items sold. 我有一个销售订单数据库,其中包含有关已售商品的条目。 I want to display the totals for each part sold for both the current year and previous two years. 我想显示当年和前两年售出的每个零件的总计。 My current query is shown below. 我当前的查询如下所示。 (It is searching for part numbers ending in a 'k' which is a special type of part in our database.) (它正在搜索以“ k”结尾的零件号,这是我们数据库中特殊的零件类型。)

SELECT DISTINCT PRTNUM_28, sum(SHPQTY_28) AS TOTAL, PM.PMDES1_01
FROM   SO_Detail SOD
           JOIN PART_MASTER PM ON SOD.PRTNUM_28 = PM.PRTNUM_01
WHERE PRTNUM_28 LIKE '%k'
AND   SHPDTE_28 > '2011'
AND   SHPDTE_28 < '2012'
GROUP BY PRTNUM_28, PM.PMDES1_01
ORDER BY TOTAL DESC, PRTNUM_28

The above query works fine to display the totals for each part number for 2012, but how do I also include the data from the previous two years in the same result? 上面的查询可以很好地显示2012年每个零件的总数,但是如何将前两年的数据也包含在同一结果中?

Note that for each year, part numbers may or may not exist - ie 2011 may contain a part that wasn't sold in 2012 or 2010 and similarly 2011 may not contain a part that was sold in 2012 and/or 2010. I'm not sure how to achieve this query. 请注意,每年零件编号可能存在​​或不存在-即2011年可能包含2012年或2010年未售出的零件,同样,2011年可能不包含2012年和/或2010年售出的零件。不确定如何实现此查询。

Assuming that you also want to break it out by year, then its probably something like this: 假设您还想按年份细分,则可能是这样的:

SELECT LEFT(SHPDTE_28,4) As YR, PRTNUM_28, sum(SHPQTY_28) AS TOTAL, PM.PMDES1_01
FROM SO_Detail SOD
JOIN PART_MASTER PM ON SOD.PRTNUM_28 = PM.PRTNUM_01
WHERE PRTNUM_28 LIKE '%k'
AND SHPDTE_28 > '2009'
AND SHPDTE_28 < '2012'
GROUP BY LEFT(SHPDTE_28,4), PRTNUM_28, PM.PMDES1_01
ORDER BY TOTAL DESC, PRTNUM_28, YR

I cannot be sure though without the table definitions. 我不能确定是否没有表定义。

(Note also that you do not normally use DISTINCT with GROUP BY as its redundant.) (还请注意,通常不要将DISTINCT与GROUP BY用作冗余。)

I am not sure what RDBMS you are using but you can alter this for any. 我不确定您使用的是哪种RDBMS,但是您可以更改任何关系。 The DatePart function is specific for SQL Server. DatePart函数特定于SQL Server。

But, if you want to display the data for each year in a separate column, then you can use something like this, which basically pivots the data for each year: 但是,如果要在单独的列中显示每年的数据,则可以使用如下所示的方法,这基本上是对每年的数据进行透视:

SELECT 
  PRTNUM_28, 
  sum(case when DatePart(yy, SHPDTE_28) = 2009 then SHPQTY_28 end) AS Total_2009, 
  sum(case when DatePart(yy, SHPDTE_28) = 2010 then SHPQTY_28 end) AS Total_2010, 
  sum(case when DatePart(yy, SHPDTE_28) = 2011 then SHPQTY_28 end) AS Total_2011, 
  sum(case when DatePart(yy, SHPDTE_28) = 2012 then SHPQTY_28 end) AS Total_2012,
  PM.PMDES1_01
FROM SO_Detail SOD
LEFT JOIN PART_MASTER PM 
  ON SOD.PRTNUM_28 = PM.PRTNUM_01
WHERE PRTNUM_28 LIKE '%k'
  AND SHPDTE_28 > '2009'
  AND SHPDTE_28 < '2012'
GROUP BY DatePart(yy, SHPDTE_28), PRTNUM_28, PM.PMDES1_01;

Or if you have access to a PIVOT function, you can also use the following: 或者,如果您有权使用PIVOT功能,则还可以使用以下功能:

select *
from
(
  SELECT DatePart(yy, SHPDTE_28) As YR, 
    PRTNUM_28, 
    SHPQTY_28,
    PM.PMDES1_01
  FROM SO_Detail SOD
  LEFT JOIN PART_MASTER PM 
    ON SOD.PRTNUM_28 = PM.PRTNUM_01
  WHERE PRTNUM_28 LIKE '%k'
    AND SHPDTE_28 > '2009'
    AND SHPDTE_28 < '2012'
) src
pivot
(
  sum(SHPQTY_28)
  for yr in ([2009], [2010], [2011], [2012])
) piv

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

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