Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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,我有25644行3列的表b,客户ID,部门和月份。此表有重复的客户ID、部门和月份行,并且没有缺少的值 我还有一个独特的表a,共有63077行和3列,客户ID,段和月。在段列中,263行的值为#N/a,而90行的值为空 我正在尝试将表a连接到表b,但是我总共得到了33225行 这是我的密码: select * from table_b as a left join table_a as b on a.Customer_ID = b.Customer_ID 这将返回33225行。它是否应该返回表

我有25644行3列的
表b
客户ID
部门
月份
。此表有重复的
客户ID
部门
月份
行,并且没有缺少的值

我还有一个独特的
表a
,共有63077行和3列,
客户ID
。在
列中,263行的值为
#N/a
,而90行的值为

我正在尝试将
表a
连接到
表b
,但是我总共得到了33225行

这是我的密码:

select *
from table_b as a
left join table_a as b on a.Customer_ID = b.Customer_ID
这将返回33225行。它是否应该返回
表b
中的行数,即25644

我还尝试:

select * 
from table_b as a
left join table_a as b on a.Customer_ID = b.Customer_ID
where b.Segment is not null and b.Segment ! = '#N/A`
但是,这将返回总共33127行

谁能解释一下原因吗

最后,我尝试连接两个表,然后计算
表a
上属于特定
部门
表b
不同段的数量

最后,我尝试合并两个表,然后计算一月份不同段的数量

我不明白为什么有必要加入
join
。查询所需的所有信息都出现在
表a
中:

select a.month, count(distinct a.seqment)
from table_a a
where a.Segment is not null and a.Segment <> '#N/A'
group by a.month;

您不小心用了一个记号来结束N/a字符串。@MichaelZ。非常感谢。我想这是我从问题中抄来的。事实上,你做到了,我现在明白了。容易错过。我看到过关于使用的引号字符串不正确的查询的问题。似乎时常发生。@Gordon.Linoff在我努力做到简洁的过程中,我没有准确地描述我的问题。是的,“段”在表a上,但是假设我在表b上有一个附加列,称之为“部门”。所以表a上的每个段对应表b上的一个部门。因此,我需要计算段(在表_a上)属于特定部门(在表_b上)的行数。这有意义吗?@TheRookie请编辑您的问题,然后让Gordon知道您添加了更多上下文。没有理由在评论中重新解释。
where a.month = 'January'