Sql 每月最高和最低销售额

Sql 每月最高和最低销售额,sql,sql-server,Sql,Sql Server,如果我有以下数据: sale_id sale_date 1 1/5/2010 2 1/8/2010 3 1/16/2010 4 1/28/2010 5 2/2/2010 6 2/21/2010 7 2/29/2010 8 3/3/2010 我想要每个月的第一个和最后一个(每个月2个或更少的记录): 期望输出: s

如果我有以下数据:

sale_id     sale_date
      1      1/5/2010
      2      1/8/2010
      3      1/16/2010
      4      1/28/2010
      5      2/2/2010
      6      2/21/2010
      7      2/29/2010
      8      3/3/2010
我想要每个月的第一个和最后一个(每个月2个或更少的记录):

期望输出:

sale_id     sale_date
      1      1/5/2010
      4      1/28/2010
      5      2/2/2010
      7      2/29/2010
      8      3/3/2010
我认为缓慢的一种方式是:

select * from table o
where sale_date in (select max(sale_date) from table where datepart(month+year of sale_date) = datepart(month+year of o.sale_date), select min(sale_date) from table where ... )
试一试


我将把调试留给您(因为我手边没有sql server的副本),但我想您正在寻找以下内容:

select min(_date) AS minDate, max(_date) as maxDate, _month, _year FROM (
  select 
  datepart(date,sale_date) AS _date,
  datepart(month,sale_date) AS _month,
  datepart(year,sale_date) AS _year,
  FROM sales -- or whatever you have named your table
)
GROUP BY _month, _year
ORDER BY _month,_year

将在SQLServer2000及更高版本上运行的示例

CREATE TABLE #temp(sale_id INT,    sale_date datetime)

INSERT #temp VALUES(      1 ,     '1/5/2010')
INSERT #temp VALUES(      2 ,     '1/8/2010')
INSERT #temp VALUES(      3 ,     '1/16/2010')
INSERT #temp VALUES(      4 ,     '1/28/2010')
INSERT #temp VALUES(      5 ,     '2/2/2010')
INSERT #temp VALUES(      6 ,     '2/21/2010')
 INSERT #temp VALUES(     7 ,     '2/28/2010')
 INSERT #temp VALUES(     8  ,    '3/3/2010')


 SELECT t.* FROM #temp t
 JOIN(
 SELECT MIN(sale_date) AS MinDAte, MAX(sale_date) AS MaxDate
FROM #temp
GROUP BY YEAR(sale_date), MONTH(sale_date)) x ON t.sale_date = x.MaxDate
OR t.sale_date = x.MinDAte
2005年及以后……对Martin的代码稍加修改(查看where子句)


您可以使用Union All选项或使用以下脚本来执行此操作

但当您需要ORDERBY子句来查询时,联合就不起作用了

SELECT *
FROM Sales
WHERE SalesID IN 
(
SELECT TOP 1 SalesID
FROM Sales
ORDER BY SaleslID
)
OR
SalesID IN 
(
SELECT TOP 1 SalesID
FROM Sales
ORDER BY SalesID DESC
)GO

如果你投了反对票,请说明原因:)如果你有多年的时间怎么办?输出中的ID在哪里?添加了年份以按clausesAlso、SqlNemage进行选择和分组。如果正确,则应该有一个外部选择以在输出中包括sale_ID字段。添加了外部选择。sheesh,你们太苛刻了。你们也可以做
WHERE 1 IN(RN,RN2)
作为一个快捷键这实际上比我要求的要多,而且更多的部分是非常有用的,我可以轻松地做第一个1,最后一个1或第一个n,最后一个n。!谢谢你,马丁
CREATE TABLE #temp(sale_id INT,    sale_date datetime)

INSERT #temp VALUES(      1 ,     '1/5/2010')
INSERT #temp VALUES(      2 ,     '1/8/2010')
INSERT #temp VALUES(      3 ,     '1/16/2010')
INSERT #temp VALUES(      4 ,     '1/28/2010')
INSERT #temp VALUES(      5 ,     '2/2/2010')
INSERT #temp VALUES(      6 ,     '2/21/2010')
 INSERT #temp VALUES(     7 ,     '2/28/2010')
 INSERT #temp VALUES(     8  ,    '3/3/2010')


 SELECT t.* FROM #temp t
 JOIN(
 SELECT MIN(sale_date) AS MinDAte, MAX(sale_date) AS MaxDate
FROM #temp
GROUP BY YEAR(sale_date), MONTH(sale_date)) x ON t.sale_date = x.MaxDate
OR t.sale_date = x.MinDAte
WITH T
     AS (SELECT *,
                ROW_NUMBER() OVER (PARTITION BY Year(sale_date), MONTH(sale_date)
                ORDER BY sale_date)  RN,
                ROW_NUMBER() OVER (PARTITION BY Year(sale_date), MONTH(sale_date)
                ORDER BY sale_date DESC) RN2
         FROM   #temp)
SELECT sale_id,
       sale_date
FROM   T
WHERE  1 IN(RN ,RN2)
SELECT *
FROM Sales
WHERE SalesID IN 
(
SELECT TOP 1 SalesID
FROM Sales
ORDER BY SaleslID
)
OR
SalesID IN 
(
SELECT TOP 1 SalesID
FROM Sales
ORDER BY SalesID DESC
)GO