Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按月份对日期进行分组的SQL查询_Sql_Sql Server 2008 - Fatal编程技术网

按月份对日期进行分组的SQL查询

按月份对日期进行分组的SQL查询,sql,sql-server-2008,Sql,Sql Server 2008,我有一个包含3个表的数据库: mothlist([number],[name]) temp\u subs([sub\u id]、[date\u created]、[expiration\u date]) 临时订单([order\u id]、[sub\u id]、[order\u date]) 我正在尝试编写一个查询,以获得按月份分组的当前年度的总计数到期日期和订单 我现在的问题是: SELECT monthlist.name 'Month', count(temp_subs.e

我有一个包含3个表的数据库:

  • mothlist([number],[name])
  • temp\u subs([sub\u id]、[date\u created]、[expiration\u date])
  • 临时订单([order\u id]、[sub\u id]、[order\u date])
  • 我正在尝试编写一个查询,以获得按月份分组的当前年度的总计数到期日期和订单

    我现在的问题是:

    SELECT 
        monthlist.name 'Month',
        count(temp_subs.expiry_date) 'Expiry Date',
        count(temp_orders.order_date) 'Order Date'    
    FROM 
        monthlist  
        FULL JOIN temp_subs 
        on 
            monthlist.number = datepart(month, temp_subs.expiry_date) and 
            datepart(year, temp_subs.expiry_date) = year(getdate())
        FULL JOIN temp_orders 
        on 
            monthlist.number = datepart(month, temp_orders.order_date) and 
            datepart(year, temp_orders.order_date) = year(getdate())
    GROUP BY monthlist.number ,monthlist.name      
    ORDER BY monthlist.number
    

    如果有人能告诉我我做错了什么,我将不胜感激。

    双连接意味着每个临时订单都要重复。避免重复计算的一种方法是在唯一列上添加一个
    distinct
    。如果表中有一个名为
    id
    的主键,则可能如下所示:

    SELECT monthlist.name 'Month'
      ,count(distinct temp_subs.id) 'Expiry Date'
      ,count(distinct temp_orders.id) 'Order Date'  
    

    现在怎么了?行太多?谢谢Andromar的快速回复,但添加“distinct”不起作用。如果我做左连接而不是完全连接,并且只留下其中一个,那么总数是正确的。现在我正试图找到一种方法让两个join都连接起来,你必须在一个唯一的列上添加
    distinct
    ,比如
    id
    。那就行了,谢谢你。不同的ID字段和左连接可以完美地工作