Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.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 Server中为特定输出执行分组方式_Sql_Sql Server - Fatal编程技术网

如何在SQL Server中为特定输出执行分组方式

如何在SQL Server中为特定输出执行分组方式,sql,sql-server,Sql,Sql Server,我有一个几乎没有记录的表,我想在其中一列上获得月份数据和计数。输出应包含Isregistered标志的月份和计数 表结构 | Inserted On | IsRegistered | +-------------+--------------+ | 10-01-2020 | 1 | | 15-01-2020 | 1 | | 17-01-2020 | null | | 17-02-2020 | 1 | | 2

我有一个几乎没有记录的表,我想在其中一列上获得月份数据和计数。输出应包含Isregistered标志的月份和计数

表结构

| Inserted On | IsRegistered |
+-------------+--------------+
| 10-01-2020  | 1            |
| 15-01-2020  | 1            |
| 17-01-2020  | null         |
| 17-02-2020  | 1            |
| 21-02-2020  | null         |   
| 04-04-2020  | null         |
| 18-04-2020  | null         |
| 19-04-2020  | 1            |
例外输出

| Inserted On | Registered | Not Registered
+-------------+------------+---------------
| Jan         |     2      |    1
| Feb         |     1      |    1
| Apr         |     1      |    2
我尝试执行正常的分组,但没有得到期望的输出

SELECT
    DATENAME(MONTH, dateinserted) AS [MonthName], COUNT(ISRegistered)
FROM
    tablename 
GROUP BY
    (DATENAME(MONTH, dateinserted))

注意:此处null被视为未注册

您可以使用聚合。我会包括年份,并使用月号而不是名称,因此:

select year(inserted_on), month(inserted_on),
       coalesce(sum(is_registered), 0) as num_registered,
       sum(case when is_registered is null then 1 else 0 end) as num_not_registered
from tablename
group by year(inserted_on), month(inserted_on)
order by year(inserted_on), month(inserted_on);
注意:如果您确实需要monthname,并且希望合并不同年份的数据(这似乎不太可能,但是…),那么您可以使用:

select datename(month, inserted_on),
       coalesce(sum(is_registered), 0) as num_registered,
       sum(case when is_registered is null then 1 else 0 end) as num_not_registered
from tablename
group by datename(month, inserted_on)
order by month(min(inserted_on));

GROUPBY应该包括年和月(因此没有重叠)以及日期名(用于显示)。像这样的

drop table if exists #tablename;
go
create table #tablename(dateinserted date, ISRegistered int);
insert #tablename values 
('2020-12-01', 0),
('2020-11-02', 1),
('2020-11-03', 1),
('2020-12-01', 1),
('2020-12-03', 1),
('2020-11-02', 0);

select year(dateinserted) yr,
       datename(month, dateinserted) AS [MonthName], 
       sum(ISRegistered) Registered ,
       sum(1-ISRegistered) [Not Registered]
from #tablename 
group by year(dateinserted), month(dateinserted), datename(month, dateinserted)
order by year(dateinserted), month(dateinserted);

你说它没有给出期望的输出;怎么了?预期结果有3列,但在尝试中只定义了2列。未注册的
发生了什么事?
?您选择了不起作用的代码。至少要把它修好,让它能在下一次使用person@SteveCGordon Linoff已经提供了fix@GordonLinoff尝试了上述查询,但得到一个错误-操作数数据类型位对于求和运算符无效。我需要施展智力吗?@VishalDhasal。是的,您需要将()转换为
int
@Larnu。它只提取月份进行排序。
yr      MonthName   Registered  Not Registered
2020    November    2           1
2020    December    2           1