Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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,你好,大师们,我需要你们的帮助 有桌子: DataCollection ================== PK Code smallint RestaurantCode smallint Year tinyint Month money Amount money AccumulativeMonthsAmount ... 我需要每个餐厅上个月的累计金额 首先,我得到了该案例“本年度”每个餐厅的最后一个月:

你好,大师们,我需要你们的帮助

有桌子:

DataCollection
==================
PK            Code
smallint      RestaurantCode
smallint      Year
tinyint       Month
money         Amount
money         AccumulativeMonthsAmount
...
我需要每个餐厅上个月的累计金额

首先,我得到了该案例“本年度”每个餐厅的最后一个月:

SELECT RestaurantCode, MAX(Month) as Month FROM DataCollection
WHERE (Year >= 2012 AND YEAR <= 2012) GROUP BY RestaurantCode
我想得到每个餐厅最后一个月的累计金额:

RestCode Accumulative Month
123         345453.65    12
122         764545.00    12
99          2500.98      6
...
随着需求的变化:

;WITH x AS 
(
  SELECT RestCode, Accumulative, [Month],
    rn = ROW_NUMBER() OVER (PARTITION BY RestCode ORDER BY [Month] DESC)
    FROM dbo.DataCollection -- or is it dbo.PAL_Entries_Relatives?
)
SELECT RestCode, Accumulative, [Month]
  FROM x
  WHERE rn = 1
  ORDER BY [Month] DESC, RestCode DESC;
随着需求的变化:

;WITH x AS 
(
  SELECT RestCode, Accumulative, [Month],
    rn = ROW_NUMBER() OVER (PARTITION BY RestCode ORDER BY [Month] DESC)
    FROM dbo.DataCollection -- or is it dbo.PAL_Entries_Relatives?
)
SELECT RestCode, Accumulative, [Month]
  FROM x
  WHERE rn = 1
  ORDER BY [Month] DESC, RestCode DESC;

SQL Server中不允许使用该语法。您可以对EXISTS执行类似的操作:


SQL Server中不允许使用该语法。您可以对EXISTS执行类似的操作:


您使用正确连接而不是子查询。似乎我失去了秒数,提问可以回答。似乎是一个经典问题。您使用正确连接而不是子查询。似乎我失去了秒数,提问可以回答。似乎是一个经典问题。@Marlin Pierce和所有人,抱歉我犯了一个错误,表格PAL\u Entries\u Relatives是同一个表格数据集合。@zerazobz那么您能显示示例数据和所需结果吗?如果它们是同一个表,那么您的查询对我来说就没有意义了。我用样本数据和期望的结果更新了答案。@Marlin Pierce和所有人,很抱歉我犯了一个错误,表PAL_Entries_Relatives是同一个表数据集合。@zerazobz那么您能显示样本数据和期望的结果吗?如果它们是同一个表,那么您的查询对我来说没有意义。我用示例数据更新了答案,并得到了所需的结果。
SELECT dc.AccumulativeMonthsAmount 
  FROM dbo.DataCollection AS dc
  INNER JOIN 
  (
    SELECT RestaurantCode, MAX(Month)
      FROM dbo.PAL_Entries_Relatives
      WHERE [Year] = 2012
      GROUP BY RestaurantCode
  ) AS r(rc, m)
ON dc.RestaurantCode = r.rc
AND dc.[Month] = r.m;
;WITH x AS 
(
  SELECT RestCode, Accumulative, [Month],
    rn = ROW_NUMBER() OVER (PARTITION BY RestCode ORDER BY [Month] DESC)
    FROM dbo.DataCollection -- or is it dbo.PAL_Entries_Relatives?
)
SELECT RestCode, Accumulative, [Month]
  FROM x
  WHERE rn = 1
  ORDER BY [Month] DESC, RestCode DESC;
SELECT AccumulativeMonthsAmount
FROM DataCollection dc
WHERE exists (select 1
              from PAL_Entries_Relatives er
              where  (Year >= 2012 AND YEAR <= 2012)
              group by RestaurantCode
              having er.RestaurantCode = dc.RestaurantCode and
                     max(er.month) = dc.Month
             )
SELECT AccumulativeMonthsAmount
FROM DataCollection
  INNER JOIN PAL_Entries_Relatives
    ON DataCollection.RestaurantCode = PAL_Entries_Relatives.RestaurantCode
WHERE (Year >= 2012 AND YEAR <= 2012)
GROUP BY DataCollection.RestaurantCode
HAVING AccumulativeMonthsAmount.Month = MAX(PAL_Entries_Relatives.Month)