with book_stats_sum as (
select book_id, sum(ctr) ctr
from book_stats
where book_stats.lang = 'ru' and book_stats.category = 'horror'
group by book_id
) -- подстраховаться, чтобы выбиралось только по одной записи на каждый book_id
select books.book_id, coalesce(book_stats_sum.ctr, books.ctr) ctr
from books
left join book_stats_sum on book_stats_sum.book_id = books.book_id
order by coalesce(book_stats_sum.ctr, books.ctr) desc
limit 200
SELECT calldate,
src,
dst,
dcontext,
channel,
dstchannel,
lastapp,
lastdata,
duration,
billsec,
MIN(uniqueid) as uniqueid1,
amaflags,
accountcode,
userfield
FROM cdr WHERE src='81113331110'
GROUP BY calldate,
src,
dst,
dcontext,
channel,
dstchannel,
lastapp,
lastdata,
duration,
billsec,
amaflags,
accountcode,
userfield
ORDER BY (MIN(uniqueid)) DESC
with rec as (
select "recordId" as rec_id
from catalog_49_links
where "catalogId" = 36 and "catalogRecordId" = 352
order by "createdAt" desc
limit 1
)
select "recordId" as new_rec_id from catalog_49_links where "recordId" in (select rec_id from rec) and "catalogId" = 56 or ("catalogId" = 36 and "catalogRecordId" = 352) order by "createdAt" desc limit 1;
with rec as (
select "recordId" as rec_id
from catalog_49_links
where "catalogId" = 36 and "catalogRecordId" = 352
order by "createdAt" desc
limit 1
)
select "recordId" as new_rec_id
from catalog_49_links
join rec on rec.rec_id = catalog_49_links."recordId"
where "catalogId" = 56 or ("catalogId" = 36 and "catalogRecordId" = 352)
order by "createdAt" desc limit 1;
SELECT
`campaign_post`,
`campaign_name`,
`post`,
COUNT(*) AS `leads`,
SUM(`new`) AS `new`,
SUM(`approved`) AS `approved`,
SUM(`declined`) AS `declined`,
SUM(`webmaster_profit`) AS `webmaster_profit`
FROM (
SELECT
CONCAT(w,'|',t) AS `campaign_post`,
w AS `campaign_name`,
t AS `post`,
IF(`status` = 'new', 1, 0) AS `new`,
IF(`status` = 'approved', 1, 0) AS `approved`,
IF(`status` = 'declined', 1, 0) AS `declined`,
web_total AS `webmaster_profit`
FROM `m1`
UNION ALL
SELECT
CONCAT(`subid1`, '|', `subid2`) AS `campaign_post`,
`subid1` AS `campaign_name`,
`subid2` AS `post`,
IF(`status` = 'waiting', 1, 0) AS `new`,
IF(`status` = 'approved', 1, 0) AS `approved`,
IF(`status` IN ('declined', 'trash'), 1, 0) AS `declined`,
`profit` AS `webmaster_profit`
FROM `add1`
) q1
GROUP BY `campaign_post`,
`campaign_name`,
`post`;