簡體   English   中英

SQL基於每行一個字段在另一列中計算重復項

[英]SQL count duplicates in another column based on one field per row

我正在建立客戶保留報告。 我們通過他們的電子郵件識別客戶。 這是我們表中的一些示例數據:

+----------------------------+------------------+-------------------+---------------------+------------+-------------+--------------+---------------+------------------+---------------+----------------+--------------+------------------+--+--+--+--+--+
|           Email            | BrandNewCustomer | RecurringCustomer | ReactivatedCustomer | OrderCount | TotalOrders | Date_Created | Customer_Name | Customer_Address | Customer_City | Customer_State | Customer_Zip | Customer_Country |  |  |  |  |  |
+----------------------------+------------------+-------------------+---------------------+------------+-------------+--------------+---------------+------------------+---------------+----------------+--------------+------------------+--+--+--+--+--+
| zyw@marketplace.amazon.com |                1 |                 0 |                   0 |          1 |           1 | 41:50.0      | Sha           |              990 | BRO           | NY             |          112 | US               |  |  |  |  |  |
| zyu@gmail.com              |                1 |                 0 |                   0 |          1 |           1 | 57:25.0      | Zyu           |              181 | Mia           | FL             |          330 | US               |  |  |  |  |  |
| ZyR@aol.com                |                1 |                 0 |                   0 |          1 |           1 | 10:19.0      | Day           |              581 | Myr           | SC             |          295 | US               |  |  |  |  |  |
| zyr@gmail.com              |                1 |                 0 |                   0 |          1 |           1 | 25:19.0      | Nic           |              173 | Was           | DC             |          200 | US               |  |  |  |  |  |
| zy@gmail.com               |                1 |                 0 |                   0 |          1 |           1 | 19:18.0      | Kim           |              675 | MIA           | FL             |          331 | US               |  |  |  |  |  |
| zyou@gmail.com             |                1 |                 0 |                   0 |          1 |           1 | 40:29.0      | zoe           |              160 | Mob           | AL             |          366 | US               |  |  |  |  |  |
| zyon@yahoo.com             |                1 |                 0 |                   0 |          1 |           1 | 17:21.0      | Zyo           |              84  | Sta           | CT             |          690 | US               |  |  |  |  |  |
| zyo@gmail.com              |                1 |                 0 |                   0 |          2 |           2 | 02:03.0      | Zyo           |              432 | Ell           | GA             |          302 | US               |  |  |  |  |  |
| zyo@gmail.com              |                1 |                 0 |                   0 |          1 |           2 | 12:54.0      | Zyo           |              432 | Ell           | GA             |          302 | US               |  |  |  |  |  |
| zyn@icloud.com             |                1 |                 0 |                   0 |          1 |           1 | 54:56.0      | Zyn           |              916 | Nor           | CA             |          913 | US               |  |  |  |  |  |
| zyl@gmail.com              |                0 |                 1 |                   0 |          3 |           3 | 31:27.0      | Ser           |              123 | Mia           | FL             |          331 | US               |  |  |  |  |  |
| zyk@marketplace.amazon.com |                1 |                 0 |                   0 |          1 |           1 | 44:00.0      | Myr           |              101 | MIA           | FL             |          331 | US               |  |  |  |  |  |
+----------------------------+------------------+-------------------+---------------------+------------+-------------+--------------+---------------+------------------+---------------+----------------+--------------+------------------+--+--+--+--+--+

我們通過電子郵件定義客戶。 因此,所有具有相同電子郵件的訂單都標記為在一個客戶下,然后我們在此基礎上進行計算。

現在,我試圖找出有關電子郵件已更改的客戶的信息。 因此,為此,我們將嘗試按客戶的地址排隊。

因此,對於每一行(因此當用電子郵件分隔時),我希望有另一列稱為“ Orders_With_Same_Address_Different_Email”。 我該怎么做?

我已經嘗試過使用Dense Rank做一些事情,但是似乎沒有用:

SELECT DISTINCT
Email
,BrandNewCustomer
,RecurringCustomer
,ReactivatedCustomer
,OrderCount
,TotalOrders
,Date_Created
,Customer_Name
,Customer_Address
,Customer_City
,Customer_State
,Customer_Zip
,Customer_Country
,(DENSE_RANK() over (partition by Email order by (case when email <> email then Customer_Address end)  asc) 
+DENSE_RANK() over ( partition by Email order by (case when email <> email then Customer_Address end)  desc) 
- 1) as Orders_With_Same_Name_Different_Email
--*
FROM Customers

嘗試計算按地址而不是按電子郵件划分的電子郵件:

select   Email,
         -- ...

         Orders_With_Same_Name_Different_Email = iif(
             (count(email) over (partition by Customer_Address) > 1, 
         1, 0)

from     Customers;

但這是為什么您不將電子郵件用作客戶端標識符的一課。 地址也是一個壞主意。 使用不會改變的東西。 這通常意味着制作一個內部標識符,例如自動遞增的標識符:

alter table #customers
add customerId int identity(1,1) primary key not null

現在,customerId = 1將始終引用該特定客戶。

您可以按customer_address分組並檢查計數。 假設每個客戶都有一個地址。

   Select * from table where 
  customer_address IN (
  Select customer_address
  From table group by customer_address
  having count(distinct customer_email) 
   >1) 

如果我了解您想做什么,這就是我的解決方法:

請注意,您不需要CTE中的hading子句,但是根據您的數據,它可能會使它更快。 (也就是說,如果您的數據集很大。)

WITH email2addr
(
  select email, count(distinct customer_address) as addr_cnt
  from customers
  group by email
  having count(distinct customer_address) > 1
)

SELECT 
    Email
    ,BrandNewCustomer
    ,RecurringCustomer
    ,ReactivatedCustomer
    ,OrderCount
    ,TotalOrders
    ,Date_Created
    ,Customer_Name
    ,Customer_Address
    ,Customer_City
    ,Customer_State
    ,Customer_Zip
    ,Customer_Country
    CASE when coalese(email2addr.addr_cnt,1) > 1 then 'Y' ELSE 'N' END as has_more_than_1_email 
from customers
left join email2addr on customers.email = email2addr.email

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM