X
X
xjunkiex2014-05-08 13:36:00
PostgreSQL
xjunkiex, 2014-05-08 13:36:00

Select rows in the first table by frequency condition in the second

there is postgresql, you need to pull data matched by condition from two tables:
loy_transaction (with columns id , purchase_number, cash_number, shift_number, shop_number, sale_time, transaction_time, operation_type, status, discountvalue)
and
loy_bonus_transactions ( id, bonus_account_type, bonus_amount, bonus_period_finish, bonus_period_start, discount_card , advert_act_guid, transaction_id )
columns with the same value in bold can be matched rows.
a query is required that will display the card number (discount_card) if the card participated in the same shift (shift_number) more than 3 times.
I settled on this one:
48dfed3fa95e459ebf727f4a53a1c832.png
but, apparently, it gives everything, since rows with the same shift are not tied to transactions with accruals from the loy_bonus_transactions table

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Kalenich, 2014-05-08
@xjunkiex

Probably something like this
SELECT lbt.`discount_card`, COUNT(1) as num
FROM `loy_transaction` as lt
LEFT JOIN `loy_bonus_transactions` as lbt
ON lt.`id` = lbt.`transaction_id`
GROUP BY lt.`shift_number` , lbt.`discount_card`
HAVING COUNT(1) > 3;

X
xjunkiex, 2014-05-08
@xjunkiex

img.leprosorium.com/2193396 my request is fullsize

W
whats, 2014-05-08
@whats

Select 
    * 
FROM 
    loy_transaction lt 
JOIN 
    loy_bonus_transactions lbt 
ON 
    lt.id = lbt.transaction_id 
group by 
    lbt.shift_number , lbt.discount_card 
HAVING count(*) > 3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question