Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.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
如何使SQLover子句在SQLServer条件下工作_Sql_Sql Server - Fatal编程技术网

如何使SQLover子句在SQLServer条件下工作

如何使SQLover子句在SQLServer条件下工作,sql,sql-server,Sql,Sql Server,我的问题是: 我需要在数据库中为每个匹配行获取第一个结果。我有很多争吵 原始查询为每个custid返回多行: select distinct b.custid, name FROM mth_Charge_Records a join mth_header b on b.id = a.headerid 所以,为了得到第一行,我想我会给它们编号,只取编号为1的。这是一个问题: select distinct b.custid, na

我的问题是:

我需要在数据库中为每个匹配行获取第一个结果。我有很多争吵

原始查询为每个custid返回多行:

   select distinct b.custid, name   
        FROM  mth_Charge_Records  a   
        join mth_header b on b.id = a.headerid    
所以,为了得到第一行,我想我会给它们编号,只取编号为1的。这是一个问题:

  select distinct b.custid, name,   
         count(custid) OVER (order by custid rows unbounded preceding)   as custcount
     FROM  mth_Charge_Records  a   
     join mth_header b on b.id = a.headerid     
它起作用,行号被计数。因此,我尝试添加custcount作为条件,但失败了:

  select distinct b.custid, name,   
         count(custid) OVER (order by custid rows unbounded preceding)   as custcount
     FROM  mth_Charge_Records  a   
     join mth_header b on b.id = a.headerid     
     where custcount = 1   <<--- Added this line
选择不同的b.custid、name、,
计数(custid)超过(按custid行排序,前面无限制)作为custcount
从mth_费用_记录a
在b.id=a.headerid上加入mth_头b
其中custcount=1请查看。您会注意到,语句的
SELECT
部分在
WHERE
之后进行计算。这意味着您不能通过别名引用
SELECT
中的列

您可能还注意到,不能在
WHERE
子句中使用窗口函数。你可以使用CTE,但我认为,也许你真正想要的是一个
HAVING
子句。因此:

SELECT mt.custid,
       [name]
FROM mth_Charge_Records mCR
     JOIN mth_header mt on mCR.headerid = mt.id
GROUP BY mt.custid,
         [name]
HAVING COUNT(mt.custid) = 1;
编辑:
COUNT
在重读后有点误导。您将需要一个CTE和
行号

WITH CTE AS(
    SELECT mt.custid,
           [name],
           ROW_NUMBER() OVER (ORDER BY mt.custid) AS RN
    FROM mth_Charge_Records mCR
     JOIN mth_header mt on mCR.headerid = mt.id)
SELECT *
FROM CTE
WHERE RN = 1;
看一看。您会注意到,语句的
SELECT
部分在
WHERE
之后进行计算。这意味着您不能通过别名引用
SELECT
中的列

您可能还注意到,不能在
WHERE
子句中使用窗口函数。你可以使用CTE,但我认为,也许你真正想要的是一个
HAVING
子句。因此:

SELECT mt.custid,
       [name]
FROM mth_Charge_Records mCR
     JOIN mth_header mt on mCR.headerid = mt.id
GROUP BY mt.custid,
         [name]
HAVING COUNT(mt.custid) = 1;
编辑:
COUNT
在重读后有点误导。您将需要一个CTE和
行号

WITH CTE AS(
    SELECT mt.custid,
           [name],
           ROW_NUMBER() OVER (ORDER BY mt.custid) AS RN
    FROM mth_Charge_Records mCR
     JOIN mth_header mt on mCR.headerid = mt.id)
SELECT *
FROM CTE
WHERE RN = 1;

您尝试
行\u number()
将返回任意行。如果这就是您想要的,为什么不使用聚合呢

select h.custid, max(crname)  
from mth_Charge_Records cr join
     mth_header h
     on h.id = cr.headerid;

您尝试
行\u number()
将返回任意行。如果这就是您想要的,为什么不使用聚合呢

select h.custid, max(crname)  
from mth_Charge_Records cr join
     mth_header h
     on h.id = cr.headerid;

请您标记您正在使用的RDMBS好吗?请使用row_number()函数而不是count。。。您可以将记录插入临时表,然后只选择行数为1的记录,或者使用CTEWhat is the first row?这就是你需要开始的地方——有明确定义的需求
custcount
不是有效的列,因为SQL Server在被要求使用该名称之前未处理该部分查询(命名列)。有几种方法可以解决这个问题,但哪种方法最好取决于这些要求。您可以标记您正在使用的RDMBS吗?使用row_number()函数而不是count。。。您可以将记录插入临时表,然后只选择行数为1的记录,或者使用CTEWhat is the first row?这就是你需要开始的地方——有明确定义的需求
custcount
不是有效的列,因为SQL Server在被要求使用该名称之前未处理该部分查询(命名列)。有几种方法可以解决这个问题,但哪种方法最好取决于这些要求。阅读OP行之间的
COUNT
有点误导。我已经更新了。在OP的两行之间阅读,
COUNT
有点误导。我已经更新了。