如何将记录与Transact-SQL分组?

发布于 2025-01-18 12:01:29 字数 1852 浏览 1 评论 0原文

我需要在数据库中对相同的记录进行分组。我的代码:

SELECT 
    FirstName, LastName, FullDateAlternateKey
FROM 
    ((AdventureWorksDW2012..FactInternetSales 
INNER JOIN 
    AdventureWorksDW2012..DimProduct ON FactInternetSales.ProductKey = DimProduct.ProductKey)
INNER JOIN 
    AdventureWorksDW2012..DimDate ON DimDate.DateKey = FactInternetSales.DueDateKey)
INNER JOIN 
    AdventureWorksDW2012..DimCustomer ON DimCustomer.CustomerKey = FactInternetSales.CustomerKey
WHERE 
    YEAR(FullDateAlternateKey) LIKE '2013' 
    AND MONTH(FullDateAlternateKey) LIKE '12'

我得到的:

名字姓氏日期
EugeneHuang2013-12-22
EugeneHuang2013-12-22
EugeneHuang2013-12-22
EugeneHuang2013-12-22
喜欢Lal2013-12-22
喜欢Lal2013-12 -22
喜欢拉尔2013-12-11
喜欢拉尔2013-12-11
喜欢Lal2013-12-12
喜欢Lal2013-12-12
JaclynLu2013-12-01

我需要记录是这样的:

FIRSTNAMELASTNAME日期
EugeneHuang2013-12-22
喜欢Lal2013-12 -22

等。

如果我添加,

GROUP BY FirstName, LastName

我会收到此错误:

选择列表中不允许使用“AdventureWorksDW2012...DimDate.FullDateAlternateKey”列,因为它不包含在聚合函数或 GROUP BY 语句中。

I need to group the same records at the database. My code:

SELECT 
    FirstName, LastName, FullDateAlternateKey
FROM 
    ((AdventureWorksDW2012..FactInternetSales 
INNER JOIN 
    AdventureWorksDW2012..DimProduct ON FactInternetSales.ProductKey = DimProduct.ProductKey)
INNER JOIN 
    AdventureWorksDW2012..DimDate ON DimDate.DateKey = FactInternetSales.DueDateKey)
INNER JOIN 
    AdventureWorksDW2012..DimCustomer ON DimCustomer.CustomerKey = FactInternetSales.CustomerKey
WHERE 
    YEAR(FullDateAlternateKey) LIKE '2013' 
    AND MONTH(FullDateAlternateKey) LIKE '12'

What I get:

FIRSTNAMELASTNAMEDATE
EugeneHuang2013-12-22
EugeneHuang2013-12-22
EugeneHuang2013-12-22
EugeneHuang2013-12-22
LikeLal2013-12-22
LikeLal2013-12-22
LikeLal2013-12-11
LikeLal2013-12-11
LikeLal2013-12-12
LikeLal2013-12-12
JaclynLu2013-12-01

I need to records would be like this:

FIRSTNAMELASTNAMEDATE
EugeneHuang2013-12-22
LikeLal2013-12-22

and so on.

If I add

GROUP BY FirstName, LastName

I get this error:

The "AdventureWorksDW2012...DimDate.FullDateAlternateKey" column is not allowed in the selection list because it is not contained in either the aggregate function or the GROUP BY sentence.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2025-01-25 12:01:29

您需要每个名称和最后一个名称的最大日期,因此您组由FirstName,LastName和SELECT max(FulldateAteAlternateKey)。 (顺便说一句,fulldatealternatekey是日期的奇怪名称。)

加入到dimproduct似乎是多余的。可以改善日期条件。

SELECT c.firstname, c.lastname, MAX(d.fulldatealternatekey)
FROM adventureworksdw2012.factinternetsales fis
JOIN adventureworksdw2012.dimcustomer c ON c.customerkey = fis.customerkey
JOIN adventureworksdw2012.dimdate d ON d.datekey = fis.duedatekey
WHERE d.fulldatealternatekey >= '2013-12-01'
  AND d.fulldatealternatekey < '2014-01-01'
GROUP BY c.firstname, c.lastname
ORDER BY c.firstname, c.lastname;

You want the maximum date per FirstName and LastName, so you GROUP BY FirstName, LastName and select MAX(FullDateAlternateKey). (FullDateAlternateKey is a strange name for a date by the way.)

The join to DimProduct seems superfluous. The date condition can be improved.

SELECT c.firstname, c.lastname, MAX(d.fulldatealternatekey)
FROM adventureworksdw2012.factinternetsales fis
JOIN adventureworksdw2012.dimcustomer c ON c.customerkey = fis.customerkey
JOIN adventureworksdw2012.dimdate d ON d.datekey = fis.duedatekey
WHERE d.fulldatealternatekey >= '2013-12-01'
  AND d.fulldatealternatekey < '2014-01-01'
GROUP BY c.firstname, c.lastname
ORDER BY c.firstname, c.lastname;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文