Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.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
MS SQL最后记录_Sql_Sql Server - Fatal编程技术网

MS SQL最后记录

MS SQL最后记录,sql,sql-server,Sql,Sql Server,我只是尝试编写MS SQL语句来从数据库中获取最后一个数据,不幸的是,当我添加there DESC LIMIT 1时,它说的语法是错误的,尽管事实上它看起来还可以 能不能找个更有技术的人看看 Select sum(spareparts), month(calculationdate) from cz_axnmrs_calculations where CASE_ID in (select case_id from cz_axnmrs_cases

我只是尝试编写MS SQL语句来从数据库中获取最后一个数据,不幸的是,当我添加there DESC LIMIT 1时,它说的语法是错误的,尽管事实上它看起来还可以

能不能找个更有技术的人看看

Select
  sum(spareparts), 
  month(calculationdate) 
from cz_axnmrs_calculations 
where CASE_ID in (select case_id 
                  from cz_axnmrs_cases 
                  where insurer_memberid = 'MM-O-5B57274F') 
and YEAR(calculationdate)='2014'  
group by month(calculationdate) DESC LIMIT 1
像这样它工作:

Select 
  sum(spareparts), 
  month(calculationdate) 
from cz_axnmrs_calculations 
where CASE_ID in (select case_id 
                  from cz_axnmrs_cases 
                  where insurer_memberid = 'MM-O-5B57274F') 
and YEAR(calculationdate)='2014'  
group by month(calculationdate)
SQL server使用TOP而不是LIMIT来限制记录数

您的查询变成:

Select top 1 
  sum(spareparts), 
  month(calculationdate) 
from cz_axnmrs_calculations 
where CASE_ID in (select case_id 
                  from cz_axnmrs_cases 
                  where insurer_memberid = 'MM-O-5B57274F') 
and YEAR(calculationdate)='2014'  
group by month(calculationdate) DESC
SQL server使用TOP而不是LIMIT来限制记录数

您的查询变成:

Select top 1 
  sum(spareparts), 
  month(calculationdate) 
from cz_axnmrs_calculations 
where CASE_ID in (select case_id 
                  from cz_axnmrs_cases 
                  where insurer_memberid = 'MM-O-5B57274F') 
and YEAR(calculationdate)='2014'  
group by month(calculationdate) DESC

在SQL Server 2012+中,Microsoft支持ANSI标准偏移量子句。您可以这样写:

Select sum(spareparts), month(calculationdate)
from cz_axnmrs_calculations
where CASE_ID in (select case_id from cz_axnmrs_cases where insurer_memberid = 'MM-O-5B57274F') and
      YEAR(calculationdate)='2014'
group by month(calculationdate) DESC
fetch first 1 row only;

在SQL Server 2012+中,Microsoft支持ANSI标准偏移量子句。您可以这样写:

Select sum(spareparts), month(calculationdate)
from cz_axnmrs_calculations
where CASE_ID in (select case_id from cz_axnmrs_cases where insurer_memberid = 'MM-O-5B57274F') and
      YEAR(calculationdate)='2014'
group by month(calculationdate) DESC
fetch first 1 row only;

@马特:如果你编辑一篇文章,一定要把所有的东西都修好。让它值得审稿人花时间。在我看来,似乎完全忽略了任何你从未费心查找的语法?@juergend Yeh抱歉,错过了一般文本,我想清理代码并回答问题first@Matt:如果编辑帖子,请确保修复了所有内容。让它值得审稿人花时间。在我看来,似乎完全忽略了任何你从未费心查找的语法?@juergend Yeh抱歉,错过了一般文本,我想清理代码并首先回答问题如果你有第二个问题,我可能还有一个问题。现在我从所有案例中选择上一个金额和月份。但如果我只想从所有案例的最后一次计算中求和呢?所以结果应该和之前一样,加上顶部,但数字要小一些,因为你没有计算所有的计算。我不太明白你说的所有情况下的最后一次计算是什么意思。把一个例子和数据放在一起,用你想要的结果发布一个问题。Fiddle暂时不可用。如果我们删除TOP1,这段代码会做什么?它会占用所有的sparepart并计算它们的总和,但我只想得到最后一个sparepart,而不是count all。如果你有第二个问题,也许我还有一个问题。现在我从所有案例中选择上一个金额和月份。但如果我只想从所有案例的最后一次计算中求和呢?所以结果应该和之前一样,加上顶部,但数字要小一些,因为你没有计算所有的计算。我不太明白你说的所有情况下的最后一次计算是什么意思。把一个例子和数据放在一起,用你想要的结果发布一个问题。Fiddle暂时不可用。如果我们删除TOP1,这段代码会做什么?它会占用所有sparepart并计算它们的总和,但我只想得到最后一个sparepart,而不是countall。