SELECT DISTINCT 和 GROUP BY 可以一起使用吗?
Is it possible to use SELECT DISTINCT and GROUP BY together?
是否可以同时使用 SELECT DISTINCT
和 GROUP BY
子句?
我需要 DISTINCT
来避免重复值并输入记录的单个条目,然后获取这些重复值的总数。
例如,我有像项目名称和数量这样的列,它们的记录是(产品 A,5),(产品 A,7).
由于产品相同,我希望将其作为单个条目然后合计其数量。所以,我报告的输出是这样的:(product A,12)
DISTINCT
和 GROUP BY
子句可以一起解决这个问题吗?
GROUP BY
会产生不同的行,它会自动执行此操作,您不需要 SELECT DISTINCT
来减少行数。
CREATE TABLE mytable(
product VARCHAR(1) NOT NULL
,quantity INTEGER NOT NULL
);
INSERT INTO mytable(product,quantity) VALUES ('a',7);
INSERT INTO mytable(product,quantity) VALUES ('a',5);
INSERT INTO mytable(product,quantity) VALUES ('b',17);
INSERT INTO mytable(product,quantity) VALUES ('b',15);
select product, sum(quantity) as qty
from mytable
group by product
product | qty
:------ | --:
a | 12
b | 32
db<>fiddle here
请注意,该结果中只有一行,即名为 product
的列中的每个不同值只有一行,因为我们在 GROUP BY
子句中指定了该列。
db<>fiddle here
根据您对 Used_By_Already 的回答的评论,我认为您需要使用 sum() over (...) 构造,例如以下...
create table [dbo].[Purchases_Supplier] (
[Purchased From] nvarchar(50),
Address nvarchar(50),
[Transaction Month] nvarchar(6),
Category nvarchar(50),
Articles nvarchar(50),
Unit int,
[Unit Price] money,
Qty int,
Tin nvarchar(50),
Cashier nvarchar(50)
);
insert [dbo].[Purchases_Supplier] values
('Acme', '123 Street', '202003', 'Baubles', 'Product A', 1, 1.1, 5, 'Nickel', 'John'),
('Acme', '123 Street', '202003', 'Baubles', 'Product A', 1, 1.1, 7, 'Nickel', 'John'),
('Acme', '123 Street', '202003', 'Baubles', 'Product B', 1, 1.1, 9, 'Silver', 'Maria'),
('Acme', '123 Street', '202003', 'Baubles', 'Product B', 1, 1.1, 11, 'Silver', 'Maria');
declare @Supplier_Name nvarchar(50) = N'Acme',
@Month_Purc nvarchar(6) = '202003',
@Cat nvarchar(50) = 'Baubles';
select
Articles,
Qty,
Unit,
[Unit Price],
[Purchased From],
Address,
Tin,
Cashier,
-- NOTE: partition by has everything in the GROUP BY except [Qty]...
sum(Qty) over (partition by Articles, Unit, [Unit Price], [Purchased From], Address, Tin, Cashier) as Total
from dbo.Purchases_Supplier
where [Purchased From] = @Supplier_Name
and [Transaction Month] = @Month_Purc
and Category = @Cat
group by Articles, Qty, Unit, [Unit Price], [Purchased From], Address, Tin, Cashier;
产生结果:
Articles Qty Unit Unit Price Purchased From Address Tin Cashier Total
Product A 5 1 1.1000 Acme 123 Street Nickel John 12
Product A 7 1 1.1000 Acme 123 Street Nickel John 12
Product B 9 1 1.1000 Acme 123 Street Silver Maria 20
Product B 11 1 1.1000 Acme 123 Street Silver Maria 20
是否可以同时使用 SELECT DISTINCT
和 GROUP BY
子句?
我需要 DISTINCT
来避免重复值并输入记录的单个条目,然后获取这些重复值的总数。
例如,我有像项目名称和数量这样的列,它们的记录是(产品 A,5),(产品 A,7).
由于产品相同,我希望将其作为单个条目然后合计其数量。所以,我报告的输出是这样的:(product A,12)
DISTINCT
和 GROUP BY
子句可以一起解决这个问题吗?
GROUP BY
会产生不同的行,它会自动执行此操作,您不需要 SELECT DISTINCT
来减少行数。
CREATE TABLE mytable( product VARCHAR(1) NOT NULL ,quantity INTEGER NOT NULL ); INSERT INTO mytable(product,quantity) VALUES ('a',7); INSERT INTO mytable(product,quantity) VALUES ('a',5); INSERT INTO mytable(product,quantity) VALUES ('b',17); INSERT INTO mytable(product,quantity) VALUES ('b',15);
select product, sum(quantity) as qty from mytable group by product product | qty :------ | --: a | 12 b | 32
db<>fiddle here
请注意,该结果中只有一行,即名为 product
的列中的每个不同值只有一行,因为我们在 GROUP BY
子句中指定了该列。
db<>fiddle here
根据您对 Used_By_Already 的回答的评论,我认为您需要使用 sum() over (...) 构造,例如以下...
create table [dbo].[Purchases_Supplier] (
[Purchased From] nvarchar(50),
Address nvarchar(50),
[Transaction Month] nvarchar(6),
Category nvarchar(50),
Articles nvarchar(50),
Unit int,
[Unit Price] money,
Qty int,
Tin nvarchar(50),
Cashier nvarchar(50)
);
insert [dbo].[Purchases_Supplier] values
('Acme', '123 Street', '202003', 'Baubles', 'Product A', 1, 1.1, 5, 'Nickel', 'John'),
('Acme', '123 Street', '202003', 'Baubles', 'Product A', 1, 1.1, 7, 'Nickel', 'John'),
('Acme', '123 Street', '202003', 'Baubles', 'Product B', 1, 1.1, 9, 'Silver', 'Maria'),
('Acme', '123 Street', '202003', 'Baubles', 'Product B', 1, 1.1, 11, 'Silver', 'Maria');
declare @Supplier_Name nvarchar(50) = N'Acme',
@Month_Purc nvarchar(6) = '202003',
@Cat nvarchar(50) = 'Baubles';
select
Articles,
Qty,
Unit,
[Unit Price],
[Purchased From],
Address,
Tin,
Cashier,
-- NOTE: partition by has everything in the GROUP BY except [Qty]...
sum(Qty) over (partition by Articles, Unit, [Unit Price], [Purchased From], Address, Tin, Cashier) as Total
from dbo.Purchases_Supplier
where [Purchased From] = @Supplier_Name
and [Transaction Month] = @Month_Purc
and Category = @Cat
group by Articles, Qty, Unit, [Unit Price], [Purchased From], Address, Tin, Cashier;
产生结果:
Articles Qty Unit Unit Price Purchased From Address Tin Cashier Total
Product A 5 1 1.1000 Acme 123 Street Nickel John 12
Product A 7 1 1.1000 Acme 123 Street Nickel John 12
Product B 9 1 1.1000 Acme 123 Street Silver Maria 20
Product B 11 1 1.1000 Acme 123 Street Silver Maria 20