我继承了一个转换为数据库的excel文件,我试图找出去过多个地点的人。
| Customer | email | ZIP | shop |
| John Smith | js@mail.com | 75016 | 1 |
| Mary King | mary@ymail.com | 97430 | 2 |
| John Smith | js@mail.com | 75016 | 3 |
| Ivan Turtle | ivan@mail.com | 56266 | 5 |
| Mary King | mary@ymail.com | 97430 | 5 |
| John Smith | js@mail.com | 75016 | 5 |
例如:John Smith去过1,3,5 玛丽金到2,3,
我尝试使用电子邮件作为密钥,但无法弄清楚如何解决这个问题
答案 0 :(得分:0)
假设每行指定一个单独的位置,
select customer, count(*) as countOfLocation
from TABLE
group by customer
having count(*) > 1
order by countOfLocation desc
是最简单的答案
答案 1 :(得分:0)
我相信这样的事情会对你有用。
查找前往多家商店的所有客户,然后加入以获取每位客户的详细信息。
这将为每个人提供多行,每个商店都有一行。如果你关心的只是客户的名字,你可以简单地使用第二个SELECT
如果您想要每个客户一行和商店的CSV,您将需要进行一些加入。
SELECT a.*
FROM Customers a
INNER JOIN
-- Find all customers who have been to more than one shop.
(SELECT email
FROM Customers
GROUP BY email
HAVING COUNT(shop) > 1) b
ON a.email = b.email
答案 2 :(得分:0)
尝试使用这个,如果是Sql server:
select customer, count(distinct(shop))
from table
group by customer, shop
having count(distinct(shop)) > 1
或者,您只想知道他们访问了多少个地方:
select customer, count(distinct(shop))
from table
group by customer, shop
答案 3 :(得分:0)
如果您想显示用户名并在一行中用逗号分隔,请使用此
SELECT Customer
,STUFF((SELECT ', ' + CAST(shop AS VARCHAR(10)) [text()]
FROM 'ur table'
WHERE customer = t.customer
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,2,' ') List_Output
FROM 'ur table' t
GROUP BY customer