I would like to create something like this
Input Data table
customer_id fee_name amount
1 a 1
1 b 3.25
1 c 1.75
1 d 2
1 e 0
2 a 1
2 b 3.25
2 c 1.75
2 d 2
2 e 0
3 a 1
3 b 3.25
3 c 1.75
3 d 3.5
3 e 0
4 a 1
4 b 3.25
4 c 1.75
4 d 3.5
4 f 1
5 a 1
5 b 3.25
5 c 1.75
5 d 3.5
5 f 1
See, you have to create groups for all fee_name and amount. So there would be 3 groups here. 1 resembling the schedule for customer 1 and 2, second group resembling customer 3 and the third group resembling schedule for customers 4 and 5. name this groups also
Output 1 should look like
group a b c d e f
1 1 3.25 1.75 2 0 0
2 1 3.25 1.75 3.5 0 0
3 1 3.25 1.75 3.5 0 1
Second output table should map these groups to customer id.
So second output should look like
customer id group
1 1
2 1
3 2
4 3
5 3
Any help is highly appreciated.
Thanks
SELECT x.*
, y.customer_id
, CASE WHEN #prev = x.fee_schedule THEN #i:=#i ELSE #i:=#i+1 END i
, #prev:=x.fee_schedule
FROM
( SELECT DISTINCT GROUP_CONCAT(amount ORDER BY fee_name) fee_schedule FROM my_table GROUP BY customer_id ) x
JOIN
( SELECT customer_id, GROUP_CONCAT(amount ORDER BY fee_name) fee_schedule FROM my_table GROUP BY customer_id ) y
ON y.fee_schedule = x.fee_schedule
JOIN
( SELECT #prev:=null, #i:=0 ) vars
ORDER
BY y.fee_schedule
, y.customer_id;
Related
Let's say I have a table:
ID A B
10 0 0
11 0 0
12 0 1
13 0 1
14 1 1
15 1 1
16 1 1
And I want my table output to be:
ID A B A_B_COUNT
10 0 0 2
11 0 0 2
12 0 1 2
13 0 1 2
14 1 1 3
15 1 1 3
16 1 1 3
but with the code I have here my output looks like this
SELECT ID, COUNT(*) AS A_B_COUNT
FROM table
GROUP BY A, B
ID A B A_B_COUNT
10 0 0 2
12 0 1 2
14 1 1 3
Any way I can create an sql query to is like my top table vs the one I make currently
Using: 10.5.5-MariaDB
Use window functions:
select t.*, count(*) over (partition by a, b) as a_b_count
from t;
You can do:
select e.A, e.B, c.cnt as A_B_COUNT
from entries e
join (
select A, B, count(*) as cnt
from entries
group by A, B
) as c
where e.A=c.A and e.B=c.B
See db-fiddle.
I have 2 tables.
MARKET TABLE
ID main_key sub_key name created_at
------------------------------------------------------
1 1 1 bottle 1606636000
2 2 1 flask 1606642546
3 2 2 flask 1606650045
4 3 1 can 1606650445
5 3 2 can 1606651546
6 4 1 glass 1606652545
MARKET_UPDATES TABLE
ID main_key sub_key price update
------------------------------------------------------
1 1 1 100 1606665555
2 2 1 120 1606665555
3 2 2 150 1606665555
4 3 1 500 1606665555
5 3 2 550 1606665555
6 4 1 25 1606665555
7 1 1 110 1606665666
8 2 1 135 1606665666
9 2 2 145 1606665666
10 3 1 490 1606665666
11 3 2 440 1606665666
12 4 1 29 1606665666
I've tried this.
SELECT *
FROM market m
JOIN (
SELECT MAX(id) max_id, fk_main_key
FROM market_update
GROUP BY fk_main_key, sub_key
) m_max ON (m_max.fk_main_key = m.main_key)
JOIN market_update mu ON (mu.id = m_max.max_id)
But it multiples it a lot of times and I end up with a lot of the same lines. I think it multiples main_key amount and sub_key amount.
I am trying to JOIN MARKET_UPDATES into MARKET so I can get the latest prices from MARKET_UPDATES, but my issue is that I have 2 id's that I have to check on, main_key & sub_key.
So I have to merge the MARKET table with id(7-12) in MARKET_UPDATES.
here is one way based on your query :
SELECT *
FROM market m
JOIN (
SELECT MAX(id) max_id , fk_main_key, sub_key
FROM market_update
GROUP BY fk_main_key, sub_key
) m_max
ON (m_max.fk_main_key = m.main_key
and m_max.sub_key = m.sub_key)
JOIN market_update mu ON (mu.id = m_max.max_id)
and here is another way using window function RANK() :
SELECT *
FROM market m
JOIN (
SELECT id
, fk_main_key
, sub_key
, rank() over (partition by fk_main_key, sub_key order by id desc ) rnk
FROM market_updat
) mu
ON mu.fk_main_key = m.main_key
and mu.sub_key = m.sub_key
and rnk = 1
I have table p proxy bids and table b bids:
Table p (Proxy):
id user_id auction_id max_amount
1 2 1 4
2 4 3 5
3 2 1 2
4 5 2 3
5 2 5 3
6 2 5 5
Table b (Bids):
id user_id auction_id bid_amount is_automatic
1 2 1 0.1 1
2 4 1 0.1 1
3 2 1 0.2 1
4 2 4 0.1 0
5 4 3 0.3 1
6 2 1 0.1 1
What I want to get is the sum of proxy bids from table p AND the total sum of automatic bids from table b PER user_id and auction_id:
So for user 2, auction 1, result would be something like this:
user_id auction_id proxy_sum bids_sum
2 1 6 0.4
Same way, for user 4, auction 3, result would be something like this:
user_id auction_id proxy_sum bids_sum
4 3 5 0.3
Same way, for user 5, auction 2, result would be something like this:
user_id auction_id proxy_sum bids_sum
5 2 3 0
So far I have this query:
SELECT
COALESCE( SUM( p.max_amount ), 0 ) AS proxy_max,
COALESCE( SUM( b.bid_amount ), 0 ) AS proxy_used
FROM
p
LEFT JOIN
b
ON
p.auction_id = b.auction_id
WHERE
b.member_id = "2"
AND
b.auction_id = "5"
AND
b.is_automatic = "1"
The problem is that if there is not row in table b for a given user_id and auction_id, it does not return the sum of the proxy, and I need the sum of the proxy regardless. For example in the query above for user 5, auction 2, it returns:
user_id auction_id proxy_sum bids_sum
2 5 0 0
I need it to return the sum of the proxy regardless like this:
user_id auction_id proxy_sum bids_sum
2 5 8 0
good evening,
i have a table:
A B C
45 1 1
22 2 1
40 3 1
43 1 2
21 2 2
61 3 2
49 4 2
60 5 2
76 1 3
41 2 3
57 3 3
i find max(A) from max(B) group by C. The result should be 60 - max number in A from last row in B from each group (C)
Thank you for your help
If i understand correctly your question you could use an inner join on select max(b):
select max(A)
from my_table m
inner join (
select C,
max(B) act_B
from my_table
group by C
) t on t.act_B = m.B and t.c = m.c
This is my table name :- xyztable and in this table i have 3 column
A:- id
B:- Name
c:- Order_number
i have a sample data like
Id Name order_nmber
1 A 1
2 K 0
3 B 6
4 c 3
5 P 0
6 d 5
7 e 2
8 O 0
I wrote the query is
select * from xyztable order by order_number ASC;
that give me the result is
Id Name order_nmber
2 K 0
5 P 0
8 O 0
1 A 1
7 e 2
4 c 3
6 d 5
3 B 6
But i want the result like :-
Id Name order_nmber
1 A 1
7 e 2
4 c 3
6 d 5
3 B 6
2 K 0
5 P 0
8 O 0
So how can i get this in single query....Please help!!!!
You could do like below:
select * from xyztable order by order_number = 0, order_number ASC;
select * from xyztable
order by case when order_number=0 then 1 else 0 end ASC, order_number ASC;
Try something like this:
SELECT * FROM xyztable ORDER BY order_number = 0, order_number;