每月报告 - SQL

时间:2016-10-27 11:28:46

标签: c# sql tsql

我有下表,如何在SQL中选择每个categoryID中每个月的最后日期(从列表中)?

我希望最终得到一些结论:

CategoryID |当前|日期

1      |    5    | 2016-09-30
1      |    3    | 2016-10-30
1      |    7    | 2016-11-30
1      |    2    | 2016-12-30

等。随着历史的积累。

图片:

enter image description here

4 个答案:

答案 0 :(得分:2)

考虑到您正在使用MySQL,因为您没有提及。

假设这是您的表名: 'Dummy'

cat_id  current  date     
------  -------  --------
     1        5  2016-09-30
     1        3  2016-10-30
     1        7  2016-11-30
     1        2  2016-12-30
     2        4  2016-10-31
     2        6  2016-10-04

执行此查询:

select 
  o.cat_id,
  (SELECT DISTINCT 
    a.date 
  from
    Dummy a 
  where a.cat_id = o.cat_id 
  ORDER BY date DESC 
  LIMIT 1) as 'date' 
from
  Dummy o 
group by o.cat_id ;

为您提供每个类别的最新日期:

cat_id  date        
------  ------------
     1  2016-12-30  
     2  2016-10-31  

修改

这应该专门针对你的桌子。只需将“yourTable”替换为表格的实际名称即可。

select 
  o.CategoryID,
  o.StockCurrent
  (SELECT DISTINCT 
    a.RecordAdded 
  from
    yourTable a 
  where a.CategoryID = o.CategoryID 
  ORDER BY RecordAdded DESC 
  LIMIT 1) as 'RecordAdded' 
from
  yourTable o 
group by o.CategoryID ;

编辑2

此查询返回特定类别中每个月的最新日期。希望这是你想要的。

SELECT 
  o.CategoryID,
  o.StockCurrent,
  o.RecordAdded 
FROM
  `yourTable` o 
WHERE o.RecordAdded IN 
  (SELECT 
    MAX(i.RecordAdded) 
  FROM
    `yourTable` i 
  GROUP BY MONTH(i.RecordAdded)) 
GROUP BY o.CategoryID,
  o.RecordAdded ;

假设该表包含以下示例数据:

CategoryID  StockCurrent  RecordAdded  
----------  ------------  -------------
         1             5  2016-09-01   
         1             3  2016-09-02   
         1             7  2016-10-01   
         1             2  2016-10-02   
         2             4  2016-09-01   
         2             6  2016-09-02   
         2            66  2016-10-01   
         2            77  2016-10-02   

运行此查询将返回以下结果集:

CategoryID  StockCurrent  RecordAdded  
----------  ------------  -------------
         1             3  2016-09-02   
         1             2  2016-10-02   
         2             6  2016-09-02   
         2            77  2016-10-02   

答案 1 :(得分:2)

有几种方法可以做到这一点,其中一种方法可能是使用窗口函数rownumber。在CTE(WITH)中,您可以获得日期内的记录的本地顺序(使用隐蔽来消除此处的时间)+按日期时间DESC的分类ID分区( - >首先是最新的)。您需要这样做,因为您不能在WHERE子句中使用窗口函数。然后,在主查询中,您实际使用此CTE作为源表,并且仅获取每个分区的最新记录。

WITH LocallyOrdered AS (
  SELECT CategoryID, 
  StockCurrent,
  ROW_NUMBER() OVER (
    PARTITION BY CategoryID, CONVERT(date, RecordAdded) 
    ORDER BY RecordAdded DESC) 
  AS RowNumberOneIsLatest
  FROM OriginalTable)
SELECT CategoryID, StockCurrent FROM LocallyOrdered WHERE RowNumberOneIsLatest = 1

答案 2 :(得分:0)

试试这个:

WITH Temp As
(
select CategoryId, [Current], RecordAdded,
Dense_Rank() over( partition by CategoryId order by RecordAdded desc) as CatergoryWiseRank
from tblCategory
)
select CategoryId, [Current], RecordAdded from Temp where CatergoryWiseRank=1

答案 3 :(得分:0)

SELECT  
CASE MONTH(date_field) 
WHEN 1 THEN 'Enero'
WHEN 2 THEN 'Febrero'
WHEN 3 THEN 'Marzo'
WHEN 4 THEN 'Abril'
WHEN 5 THEN 'Mayo'
WHEN 6 THEN 'Junio'
WHEN 7 THEN 'Julio'
WHEN 8 THEN 'Agosto'
WHEN 9 THEN 'Septiembre'
WHEN 10 THEN 'Octubre'
WHEN 11 THEN 'Noviembre'
WHEN 12 THEN 'Diciembre'
END as Mes, COUNT(date_field) as cantidad FROM nacimientos 
WHERE YEAR(date_field)='1991'
GROUP BY MONTH(date_field)asc

结果 enter image description here