如何仅在 SQL 的第一列上将此视图设为 SELECT DISTINCT
How to get this view to SELECT DISTINCT on first column only in SQL
我不习惯使用 SQL 创建视图,这迫使我使用 HAVING 和 GROUP BY,在标准的 SQL 语句中我会使用 WHERE,但显然这是不允许的。我想在 dbo.[ShowTex Belgie NV$Item].No_ only 上 SELECT DISTINCT。但它似乎也结合了我在 GROUP BY 中添加的任何内容。我不能在列上使用 HAVING,除非我将它们包含在 GROUP BY 中。走到这一步已经花了我很长时间。 :/ 我只是想创建一个简单的 WHERE 语句 ;(
SELECT DISTINCT TOP (100) PERCENT dbo.[ShowTex Belgie NV$Item].No_, SUM(dbo.[ShowTex Belgie NV$Warehouse Entry].Quantity) AS [Quantity Sum]
FROM dbo.[ShowTex Belgie NV$Item]
LEFT OUTER JOIN dbo.[ShowTex Belgie NV$Warehouse Entry] ON dbo.[ShowTex Belgie NV$Item].No_ = dbo.[ShowTex Belgie NV$Warehouse Entry].[Item No_]
GROUP BY dbo.[ShowTex Belgie NV$Item].No_, dbo.[ShowTex Belgie NV$Warehouse Entry].[Bin Code]
HAVING (dbo.[ShowTex Belgie NV$Warehouse Entry].[Bin Code] <> 'SHIPPING') OR
(dbo.[ShowTex Belgie NV$Warehouse Entry].[Bin Code] <> 'VERZEND') OR
(dbo.[ShowTex Belgie NV$Warehouse Entry].[Bin Code] <> 'WORKSHOP')
ORDER BY dbo.[ShowTex Belgie NV$Item].No_
使用 Server Studio Managment 2012,但似乎没有相关标签。
根据 Gordon Linoff 的回答,我的 SQL 查询变成了
SELECT TOP (100) PERCENT i.No_, SUM(we.Quantity) AS [Quantity Sum]
FROM dbo.[ShowTex Belgie NV$Item] AS i LEFT OUTER JOIN
dbo.[ShowTex Belgie NV$Warehouse Entry] AS we ON i.No_ = we.[Item No_]
WHERE (we.[Bin Code] NOT IN ('SHIPPING', 'VERZEND', 'WORKSHOP'))
GROUP BY i.No_
ORDER BY i.No_
首先,为什么要使用 distinct
和 group by
?二、这个版本有什么问题:
SELECT i.No_, SUM(dbo.we.Quantity) AS [Quantity Sum]
FROM dbo.[ShowTex Belgie NV$Item] i LEFT OUTER JOIN
dbo.[ShowTex Belgie NV$Warehouse Entry] we
ON i.No_ = we.[Item No_]
WHERE we.[Bin Code] NOT IN ('SHIPPING', 'VERZEND', 'WORKSHOP')
GROUP BY i.No_, we.[Bin Code]
ORDER BY i.No_;
Table 别名也使查询更具可读性。
我不习惯使用 SQL 创建视图,这迫使我使用 HAVING 和 GROUP BY,在标准的 SQL 语句中我会使用 WHERE,但显然这是不允许的。我想在 dbo.[ShowTex Belgie NV$Item].No_ only 上 SELECT DISTINCT。但它似乎也结合了我在 GROUP BY 中添加的任何内容。我不能在列上使用 HAVING,除非我将它们包含在 GROUP BY 中。走到这一步已经花了我很长时间。 :/ 我只是想创建一个简单的 WHERE 语句 ;(
SELECT DISTINCT TOP (100) PERCENT dbo.[ShowTex Belgie NV$Item].No_, SUM(dbo.[ShowTex Belgie NV$Warehouse Entry].Quantity) AS [Quantity Sum]
FROM dbo.[ShowTex Belgie NV$Item]
LEFT OUTER JOIN dbo.[ShowTex Belgie NV$Warehouse Entry] ON dbo.[ShowTex Belgie NV$Item].No_ = dbo.[ShowTex Belgie NV$Warehouse Entry].[Item No_]
GROUP BY dbo.[ShowTex Belgie NV$Item].No_, dbo.[ShowTex Belgie NV$Warehouse Entry].[Bin Code]
HAVING (dbo.[ShowTex Belgie NV$Warehouse Entry].[Bin Code] <> 'SHIPPING') OR
(dbo.[ShowTex Belgie NV$Warehouse Entry].[Bin Code] <> 'VERZEND') OR
(dbo.[ShowTex Belgie NV$Warehouse Entry].[Bin Code] <> 'WORKSHOP')
ORDER BY dbo.[ShowTex Belgie NV$Item].No_
使用 Server Studio Managment 2012,但似乎没有相关标签。
根据 Gordon Linoff 的回答,我的 SQL 查询变成了
SELECT TOP (100) PERCENT i.No_, SUM(we.Quantity) AS [Quantity Sum]
FROM dbo.[ShowTex Belgie NV$Item] AS i LEFT OUTER JOIN
dbo.[ShowTex Belgie NV$Warehouse Entry] AS we ON i.No_ = we.[Item No_]
WHERE (we.[Bin Code] NOT IN ('SHIPPING', 'VERZEND', 'WORKSHOP'))
GROUP BY i.No_
ORDER BY i.No_
首先,为什么要使用 distinct
和 group by
?二、这个版本有什么问题:
SELECT i.No_, SUM(dbo.we.Quantity) AS [Quantity Sum]
FROM dbo.[ShowTex Belgie NV$Item] i LEFT OUTER JOIN
dbo.[ShowTex Belgie NV$Warehouse Entry] we
ON i.No_ = we.[Item No_]
WHERE we.[Bin Code] NOT IN ('SHIPPING', 'VERZEND', 'WORKSHOP')
GROUP BY i.No_, we.[Bin Code]
ORDER BY i.No_;
Table 别名也使查询更具可读性。