如何显示数据库中有多个日期的唯一用户的最早日期?

How to display the oldest date for a unique user who has multiple dates in a database?

假设我的输出看起来像这样(简化示例):

UserName ProfileCreation PurchasePrice PurchaseDate
Alice Dec 21 2019 6:00AM 120.00 Dec 21 2019 8:00AM
Alice Dec 21 2019 6:00AM 90.00 Dec 25 2019 9:00AM
Alice Dec 21 2019 6:00AM 150.00 Jan 02 2020 10:00AM
Bob Jan 01 2020 9:00PM 50.00 Jan 03 2020 11:00PM
Bob Jan 01 2020 9:00PM 70.00 Jan 07 2020 11:00PM

我猜这个输出的代码应该是这样的(没那么重要):

SELECT
UserName, ProfileCreation, PurchasePrice, PurchaseDate
FROM Some_Random_Database

但我想要的输出应该是这样的:

UserName ProfileCreation PurchasePrice FirstPurchaseDate NumberOfPurchases AvgOfPurchasePrice
Alice Dec 21 2019 120.00 Dec 21 2019 3 120.00
Bob Jan 01 2020 50.00 Jan 03 2020 2 60.00

希望我的目标是可以理解的 - 让唯一用户拥有 his/her 最早购买的日期,并为所有购买提供一些计算指标。首购价可以留,但没必要。

我正在用 SOQL 方言写作 - Salesforce Marketing Cloud。

显然,我对如何在我的代码中进行一些预期的调整有一些想法,但我希望看到任何愿意向我展示最佳方法的专家的解决方案。我真的只是个菜鸟:-)

感谢大家的帮助!

SELECT 用户名、配置文件创建、购买价格、购买日期 从 Some_Random_Database 在哪里 (用户名,购买日期)IN (SELECT 用户名,最大(购买日期)来自 Some_Random_Database 按用户名分组);

注意:我对 Salesforce Marketing Cloud 一无所知,但是...

有几种方法可以实现:

#1 - 标准 sql

SELECT UserName, ProfileCreation
  , MIN(PurchaseDate) FirstPurchaseDate
  , COUNT(PurchasePrice) NoOfPurchases
  , AVG(PurchasePrice) AvgPurchasePrice
FROM Foo
GROUP BY UserName, ProfileCreation;

#2 - window 函数

SELECT DISTINCT UserName, ProfileCreation
  , MIN(PurchaseDate) OVER(PARTITION BY UserName ORDER BY UserName) FirstPurchaseDate
  , COUNT(PurchasePrice) OVER(PARTITION BY UserName ORDER BY UserName) NoOfPurchases
  , AVG(PurchasePrice) OVER(PARTITION BY UserName ORDER BY UserName) AvgPurchasePrice
FROM Foo;