我有一个月的客户总商店访问列表。客户有一个家庭商店,但可以访问其他商店。如下表所示:
setInterval(function () { // Content value
...
}, 1);
我希望我的select语句能够针对该月的该成员的每个商店提供对本地商店的访问次数。如下所示:
MemberId | HomeStoreId | VisitedStoreId | Month | Visits
1 5 5 1 5
1 5 3 1 2
1 5 2 1 1
1 5 4 1 7
我已经在里面查看了包含CASE语句的SUM,但是在PARTITION中查看了它,但我似乎无法解决这个问题。
由于
答案 0 :(得分:1)
我会使用窗口函数:
select t.*,
sum(case when homestoreid = visitedstoreid then visits end) over
(partition by memberid, month) as homestorevisits
from t;
答案 1 :(得分:0)
SELECT MemberID,HomestoreID,visitedstoreid,Month,visits, homestorevisits
FROM Table LEFT OUTER JOIN
(SELECT MemberID, Visits homestorevisits
FROM TABLE WHERE homestoreID =VisitedStoreId
)T ON T.MemberID = Table.MemberID
答案 2 :(得分:0)
您可以使用简单的子查询来实现此目的。
SELECT MemberId, HomeStoreID, VisitedStoreID, Month, Visits,
(SELECT Visits FROM table t2
WHERE t2.MemberId = t1.MemberId
AND t2.HomeStoreId = t1.HomeStoreId
AND t2.Month = t1.Month
AND t2.VisitedStoreId = t2.HomeStoreId) AS HomeStoreVisits
FROM table t1