我有两张桌子,想加入并获得具有侵略性的和字段。
我的表是:
表格中的“PriceListid”相同(加入字段)
所以,我想找到用户(“UserID”)和“Price”,但是“Type = 0(查看)或1(点击)”,并获得SUM(“ShowQuantity”),Sum(“ClickQuantity”)和“标题”, “URL”,... 结果应按“UserID”分组。
结果如下:
答案 0 :(得分:0)
根据评论中的新信息,主表可用于查找哪些用户查看/点击了哪些优惠次数:
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
然后展开此基本查询,您可以加入第二个表以获取select
pl.userid,
pl.PriceListId,
pl.offerid,
sum(case when pl.type = 0 then 1 else 0 end) as 'times_viewed',
sum(case when pl.type = 1 then 1 else 0 end) as 'times_clicked'
from PriceList pl
group by pl.userid, pl.PriceListId, pl.offerid
having sum(case when pl.type = 1 then 1 else 0 end) > 0
和title
:
url
要进一步过滤此选项,仅显示select
pl.userid,
pl.pricelistid,
pl.offerid,
sum(case when pl.type = 0 then 1 else 0 end) as 'times_viewed',
sum(case when pl.type = 1 then 1 else 0 end) as 'times_clicked',
max(pld.Title) as 'Title',
max(pld.URL) as 'URL',
max(pld.Model) as 'Name'
from PriceList pl
inner join PriceListDetails pld on pld.OfferId = pl.OfferId and pld.pricelistid = pl.pricelistid
group by pl.userid, pl.pricelistid, pl.offerid
having sum(case when pl.type = 1 then 1 else 0 end) > 0
满足评论中所述某些条件的记录,这应该有效:
PriceListId