SQL按月分组在日期字段上

时间:2015-08-24 18:01:52

标签: mysql sql sql-server tsql date

我需要赚钱的员工总数,但需要分为两个日期之间的月份,但是我有一个日期字段,并希望将月份和年份的名称作为两个单独的字段。

此查询显示未分组的信息 - 减去所需格式的年份和月份。

一旦我有员工在那个月赚到的钱,我需要按姓名(已经完成)和每个员工的月份排序

public class SlidingLayout extends RelativeLayout {
/* ........... */
@Override
    public void computeScroll() { // needed for automatic settling.
        if (mDragHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
            preventPositionReset = false;
        }else{
            preventPositionReset = true;
        }
    }

    public boolean isMoving() {
        return (/*mDraggingState == ViewDragHelper.STATE_IDLE ||*/
                preventPositionReset ||
                mDraggingState == ViewDragHelper.STATE_DRAGGING ||
                mDraggingState == ViewDragHelper.STATE_SETTLING);
    }
/* ......... */
}

由于

4 个答案:

答案 0 :(得分:0)

您可以使用Month()Year个功能并进行分组。将这样的内容包含在您的查询中:

SELECT Employee.Name, 
       Employee.ID, 
       MONTH(Sales.Date) as `month`, 
       YEAR(Sales.Date) as    `year`,
       SUM(Sales.Money)       
 FROM Database.Sales.Sales        
   INNER JOIN Database.Employee.Employee ON Sales.ID=Employee.ID        
 WHERE Sales.Date BETWEEN '2000-01-01' AND '2001-01-01'  
   GROUP by Employee.Name , Employee.ID, `month` , `year`        
 ORDER BY Employee.Name   

答案 1 :(得分:0)

试试这种方式

SELECT   Employee.Name      
, Employee.ID       
, Datepart(year,sales.Date),Datepart(month,sales.Date)        
, sum(Sales.Money)
FROM Database.Sales.Sales       
INNER JOIN Database.Employee.Employee        
ON Sales.ID=Employee.ID        
WHERE Sales.Date BETWEEN '2000-01-01' AND '2001-01-01' 
Group by Datepart(year,sales.Date),Datepart(month,sales.Date),Employee.Name,Employee.id           
ORDER BY Employee.Name  

答案 2 :(得分:0)

日期分为2个费用

SELECT   Employee.Name      
    , Employee.ID       
    , YEAR(Sales.Date) as sales_year
    , MONTH(Sales.Date) as sales_month
    , Sales.Money            
    FROM Database.Sales.Sales       
    INNER JOIN Database.Employee.Employee        
    ON Sales.ID=Employee.ID        
    WHERE Sales.Date BETWEEN '2000-01-01' AND '2001-01-01'            
    ORDER BY Employee.Name  

答案 3 :(得分:0)

这是一个查询:

SELECT   Employee.Name      
, Employee.ID       
, Sum(Sales.Money)
, Year(Sales.Date)           
, Month(Sales.Date)
FROM Database.Sales.Sales       
INNER JOIN Database.Employee.Employee        
ON Sales.ID=Employee.ID        
WHERE Sales.Date BETWEEN '2000-01-01' AND '2001-01-01'            
group by 
    Employee.Name      
    , Employee.ID
    , Year(Sales.Date)           
    , Month(Sales.Date)
ORDER BY Employee.Name,  Year(Sales.Date), Month(Sales.Date)