2017-07-21 129 views
0

我有一個客戶交易的數據庫,這樣每個交易都有一個特定的日期。我只需要計算每個客戶在過去兩個月內進行的交易次數,只要客戶今天進行了交易即可。SQL - 如果至少有一個交易是今天的交易數量

我一直在想,它需要我使用WHERE來設置兩個月的完整範圍和另一個HAVING語句以確保最新日期(客戶交易的MAX)等於今天的日期,但我似乎無法讓它工作。這聽起來像是解決這個問題的正確方法,還是有更好的方法?

謝謝!

+1

也許,也許....爲了得到更好的答案,你將不得不添加更多關於你嘗試過的表和查詢的信息,但他們沒有工作 – user3012759

回答

0

你能折騰一個子查詢中你WHERE子句發現有今天的銷售客戶:

SELECT count(*) /*count of transactions*/ 
FROM transactions 
WHERE 
    /*Transactions in the last two months*/ 
    transaction_date > DATEADD(mm, -2, GETDATE()) 
    /*For customers that have had a sale today*/ 
    customer_number in (SELECT customer_number FROM transactions WHERE transaction_date = GETDATE()); 

在你的表結構,表名和字段名完全猜測,但是這應該讓你關閉。

0

或者,你可以嘗試做一個內部聯接:

SELECT t2.CustomerID,count(*) as TransactionsCount 
FROM [Tansaction] t1 INNER JOIN [Tansaction] t2 
ON t1.CustomerID= t2.CustomerID 
WHERE CONVERT(date,t1.TransactionDateTime) = CONVERT(date,GETDATE()) 
AND t2.TransactionDateTime>= DATEADD(mm, -2, GETDATE()) 
GROUP BY t2.CustomerID 
0

首先,你需要得到了今天進行的交易客戶名單。我假設你有一個包含交易日期和客戶詳細信息的'交易表'。

使用下面的方法做這從一個transactiontable選擇:

Select count of distinct(transactiondate), Customer 
from Transactiontable 
where transactiondate > dateadd(months,-2, getdate()) 
and customer in (select customer from transactiontable 
`where cast(transactiondate as date) = cast(getdate() as date)) 
0

你不提供有關你的模式是如何的任何信息,但我假設你有一個客戶表和事務表。考慮這個例子有4個客戶和12個交易。

客戶

| id |  name | 
|----|----------| 
| 1 | Google | 
| 2 | Facebook | 
| 3 | Hooli | 
| 4 | Yahoo! | 

交易

| id | transaction_date | customer_id | 
|----|------------------|-------------| 
| 1 |  2017-04-15 |   1 | 
| 2 |  2017-06-24 |   1 | 
| 3 |  2017-07-09 |   1 | 
| 4 |  2017-07-24 |   1 | 
| 5 |  2017-07-23 |   2 | 
| 6 |  2017-07-22 |   2 | 
| 7 |  2017-07-21 |   2 | 
| 8 |  2017-07-24 |   2 | 
| 9 |  2017-07-24 |   3 | 
| 10 |  2017-07-23 |   4 | 
| 11 |  2017-07-22 |   4 | 
| 12 |  2017-07-21 |   4 | 

計算交易的數量在過去兩個月由每個客戶一個簡單的GROUP BY將做的工作:

select name, count(*) as number_of_transactions 
from transactions t 
    inner join customers c on c.id = t.customer_id 
where t.transaction_date > dateadd(month, -2, getdate()) 
group by c.name 

這就產生

|  name | number_of_transactions | 
|----------|------------------------| 
| Facebook |      4 | 
| Google |      3 | 
| Hooli |      1 | 
| Yahoo! |      3 | 

只提取有交易等於今天TRANSACTION_DATE我們可以使用一個存在,以檢查是否這樣的行存在的客戶。

select name, count(*) as number_of_transactions 
from transactions t 
    inner join customers c on c.id = t.customer_id 
where t.transaction_date > dateadd(month, -2, getdate()) 
    and exists(select * 
      from transactions 
      where customer_id = t.customer_id 
       and transaction_date = convert(date, getdate())) 
group by c.name 

所以,如果在具有等於今天和CUSTOMER_ID一個TRANSACTION_DATE交易表中的行等於從主查詢的CUSTOMER_ID包括它的結果。運行的查詢(假設今天7月24日是)爲我們提供了這樣的結果:

|  name | number_of_transactions | 
|----------|------------------------| 
| Facebook |      4 | 
| Google |      3 | 
| Hooli |      1 | 

Check out this sql fiddle http://sqlfiddle.com/#!6/710c94/13