【问题标题】:Is it possible to use SELECT DISTINCT and GROUP BY together?是否可以同时使用 SELECT DISTINCT 和 GROUP BY ?
【发布时间】:2020-07-16 07:44:06
【问题描述】:

可以同时使用SELECT DISTINCTGROUP BY 子句吗? 我需要DISTINCT 来避免重复值并输入一个记录,然后获取这些重复值的总数。

例如,我有 Item NameQuantity 之类的列,它们的记录是 (product A,5), (产品 A,7)

由于产品相同,我希望将其作为单个条目,然后合计其数量。所以,我的报告的输出是这样的:(product A,12)

DISTINCTGROUP BY 子句可以一起解决吗?

【问题讨论】:

  • 您可以嵌套查询,每个嵌套级别都有自己的 DISTINCT 和 GROUP BY。您问题中的示例可以简单地使用select Name, sum(Quantity) as 'Total' from Product group by Name 解决,尚不清楚您为什么要在那里使用 DISTINCT。你能扩展你的例子吗?
  • 感谢您的回复。因为我想在我的水晶报告中汇总记录,这就是我使用 DISTINCT 的原因。我不想在我的报告中重复记录,因为它们是相同的产品名称
  • 然后添加他们的数量以使其成为汇总报告
  • GROUP BY 将产生不同的行,它会自动执行此操作,您不需要 distinct

标签: sql sql-server group-by distinct


【解决方案1】:

根据您对 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

【讨论】:

  • 基于您的示例,我希望它只有两行产品 A 和产品 B
【解决方案2】:

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);

选择产品,总和(数量)作为数量 来自我的表 按产品分组 产品 |数量 :-------- | --: 一个 | 12 乙 | 32

db小提琴here 请注意,该结果中有一行,即 product 列中的每个不同值只有一行,因为我们在 GROUP BY 子句中指定了该列。

db小提琴here

【讨论】:

  • 选择文章,单位,[单位价格],总和(数量)* [单位价格] 作为总金额,[购买自],[地址],锡,收银员,总和(数量)作为总自PURCHASES_SUPPLIER WHERE [PURCHASED FROM]=@supplier_name AND [TRANSACTION MONTH]=@month_purc AND CATEGORY=@cat GROUP BY ARTICLES,QTY,UNIT,[UNIT PRICE],AMOUNT,[PURCHASED From],[ADDRESS],TIN,CASHIER
  • 这是我的问题,这可能吗?
  • 当有人回答我的问题时我该怎么办?请阅读:stackoverflow.com/help/someone-answers
猜你喜欢
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 2020-02-02
  • 1970-01-01
  • 2020-11-05
  • 2018-06-28
  • 2023-03-10
  • 2016-08-14
相关资源
最近更新 更多