MSSQL 在 GROUP BY 中检索值 1 次
MSSQL retrieve a value 1 time in a GROUP BY
我有一个 table 包含以下数据:
CREATE TABLE [dbo].[Products](
[ProductNR] [varchar](14) NULL,
[Location] [int] NULL,
[Date] [datetime] NULL
);
INSERT INTO Products (ProductNR, Location, Date)
VALUES
('1234' ,1, '2016-01-17 12:30:50.010'),
('4567' ,1, '2016-03-17 12:30:50.010'),
('8978' ,1, '2016-04-17 12:30:50.010'),
('2578' ,1, '2016-05-17 12:30:50.010'),
('1234' ,2, '2016-06-17 12:30:50.010'),
('1234' ,3, '2016-07-17 12:30:50.010');
select count (distinct ProductNR)
from Products
结果:
|Count|
| 4 |
但是当我像下面这样使用 GROUP BY 语句时:
select count (distinct ProductNR) AS Count
from Products
group by MONTH(date)
结果:
|Count|
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
总共 6,但我想检索的是整个 table/GROUP BY 语句中的 ID,这意味着我只想返回 4 行回到计算每个 ID 的第一个注册日期的地方。
期望的结果,其中 ProductNR 1234 仅检索一次:
|Count|
| 1 |
| 1 |
| 1 |
| 1 |
首先获取每个 ProductNR 的最短日期,然后按月分组
select count (distinct ProductNR) AS Count
from
(
select ProductNR, Date = min(Date)
from Products
group by ProductNR
) d
group by MONTH(Date)
我想你想要达到的是这个
select count (distinct ProductNR) AS Count,min(date)
from Products
group by ProductNR
您可以添加子 select 并仅使用日期 = 产品最小日期的行
select count (distinct ProductNR) AS Count
from Products as Products
where date =(select min(date)
from Products as MinProd
where MinProd.ProductNR =Products.ProductNR
)
group by MONTH(date)
这只有在没有相同的最短日期时才有效
beater 将在子查询中使用标识列和 select 前 1(此 return 始终只有一行)
CREATE TABLE [dbo].[Products2](
[PRD_ID] int identity,
[ProductNR] [varchar](14) NULL,
[Location] [int] NULL,
[Date] [datetime] NULL
);
INSERT INTO Products2 (ProductNR, Location, Date)
VALUES
('1234' ,1, '2016-01-17 12:30:50.010'),
('4567' ,1, '2016-03-17 12:30:50.010'),
('8978' ,1, '2016-04-17 12:30:50.010'),
('2578' ,1, '2016-05-17 12:30:50.010'),
('1234' ,2, '2016-06-17 12:30:50.010'),
('1234' ,3, '2016-07-17 12:30:50.010');
select count (distinct ProductNR) AS Count
from Products as Products
where date =(select min(date)
from Products as MinProd
where MinProd.ProductNR =Products.ProductNR
)
group by MONTH(date)
我有一个 table 包含以下数据:
CREATE TABLE [dbo].[Products](
[ProductNR] [varchar](14) NULL,
[Location] [int] NULL,
[Date] [datetime] NULL
);
INSERT INTO Products (ProductNR, Location, Date)
VALUES
('1234' ,1, '2016-01-17 12:30:50.010'),
('4567' ,1, '2016-03-17 12:30:50.010'),
('8978' ,1, '2016-04-17 12:30:50.010'),
('2578' ,1, '2016-05-17 12:30:50.010'),
('1234' ,2, '2016-06-17 12:30:50.010'),
('1234' ,3, '2016-07-17 12:30:50.010');
select count (distinct ProductNR)
from Products
结果:
|Count|
| 4 |
但是当我像下面这样使用 GROUP BY 语句时:
select count (distinct ProductNR) AS Count
from Products
group by MONTH(date)
结果:
|Count|
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
总共 6,但我想检索的是整个 table/GROUP BY 语句中的 ID,这意味着我只想返回 4 行回到计算每个 ID 的第一个注册日期的地方。
期望的结果,其中 ProductNR 1234 仅检索一次:
|Count|
| 1 |
| 1 |
| 1 |
| 1 |
首先获取每个 ProductNR 的最短日期,然后按月分组
select count (distinct ProductNR) AS Count
from
(
select ProductNR, Date = min(Date)
from Products
group by ProductNR
) d
group by MONTH(Date)
我想你想要达到的是这个
select count (distinct ProductNR) AS Count,min(date)
from Products
group by ProductNR
您可以添加子 select 并仅使用日期 = 产品最小日期的行
select count (distinct ProductNR) AS Count
from Products as Products
where date =(select min(date)
from Products as MinProd
where MinProd.ProductNR =Products.ProductNR
)
group by MONTH(date)
这只有在没有相同的最短日期时才有效 beater 将在子查询中使用标识列和 select 前 1(此 return 始终只有一行)
CREATE TABLE [dbo].[Products2](
[PRD_ID] int identity,
[ProductNR] [varchar](14) NULL,
[Location] [int] NULL,
[Date] [datetime] NULL
);
INSERT INTO Products2 (ProductNR, Location, Date)
VALUES
('1234' ,1, '2016-01-17 12:30:50.010'),
('4567' ,1, '2016-03-17 12:30:50.010'),
('8978' ,1, '2016-04-17 12:30:50.010'),
('2578' ,1, '2016-05-17 12:30:50.010'),
('1234' ,2, '2016-06-17 12:30:50.010'),
('1234' ,3, '2016-07-17 12:30:50.010');
select count (distinct ProductNR) AS Count
from Products as Products
where date =(select min(date)
from Products as MinProd
where MinProd.ProductNR =Products.ProductNR
)
group by MONTH(date)