Ranking of the affiliates with most commissions from database - mysql

The table "commissions" is of this form:
affiliate referral amount date
3 2 15 2016-08-27
1 22 10 2016-08-29
4 45 5 2016-09-06
1 33 9 2016-09-08
3 17 4 2016-09-11
2 33 10 2016-09-16
1 9 7 2016-09-26
3 17 9 2016-09-26
2 69 10 2016-09-30
1 21 7 2016-10-01
4 55 2 2016-10-06
I need to find out the ranking of the affiliates with most commissions month to month.
I have tried:
SELECT affiliate, SUM(amount) as amount
FROM commissions
GROUP BY affiliate, YEAR(date), MONTH(date)
But unfortunately it is not throwing the desired results.
Please help.

Related

How can I get days between two dates in the same column?

I have table structure like where Loanid is Foreign Key
TranID LOANID TRANSDATE
2 2 2013-05-01
13 2 2013-05-10
14 2 2013-05-15
6 5 2013-05-01
7 5 2013-06-10
8 5 2013-06-14
9 5 2013-07-01
10 5 2013-07-10
i need a query to calculate Days between like below .
TranID LOANID TRANSDATE DAYS_BETWEEN
2 2 2013-05-01 9
13 2 2013-05-10 5
14 2 2013-05-15 0
6 5 2013-05-01 41
7 5 2013-06-10 4
8 5 2013-06-14 17
9 5 2013-07-01 9
10 5 2013-07-10 0
Possibly a self join, using MIN to get the next date.
SELECT t1.tranid,
t1.loanid,
t1.transdate
DATEDIFF(IFNULL(MIN(t2.transdate), t1.transdate), t1.transdate) AS days
FROM some_table t1
LEFT OUTER JOIN some_table t2
ON t1.loanid = t2.loan_id
AND t1.transdate < t2.transdate
GROUP BY t1.tranid,
t1.loanid,
t1.transdate

SQL Aggregate Function Min with Group

I have a table named Prices. Which contain ID, fkProductID (foregnkey of Products Table), PriceDate, Price, fkStore (the supplier ID who gives the prices)
Now I need to find the best price for each product for each month and the SUPPLIER name also.
Also some suppliers are giving duplicates prices. For example product id 8 got two times 1200 (ID# 8 and 3). If possible i only need to show the first supplier or a column with count values..
ID fkProductID PriceDate Price fkStore
-----------------------------------------------------
1 8 26-10-2014 1250 13
2 8 10-09-2014 1200 13
3 8 25-10-2014 1200 1
4 8 13-10-2014 1500 1
5 8 03-09-2014 1000 1
6 8 15-09-2014 1300 15
7 8 09-09-2014 950 21
8 8 10-10-2014 1200 23
9 8 09-09-2014 950 27
10 15 10-10-2014 3500 5
11 15 11-10-2014 3400 6
12 15 09-09-2014 3100 6
13 15 10-09-2014 3200 14
14 15 16-09-2014 3100 17
-----------------------------------------------------
my expected result.
-----------------------------------------------------
ID fkProductID Month Price Supplier
-----------------------------------------------------
7 8 September 950 21
2 8 October 1200 1
13 15 September 3100 13
11 15 October 3400 6
=================================================================
SCHEMA
SQL FIDDLE
You can do what you want with the substring_index()/group_concat() trick:
select substring_index(group_concat(idPrices order by Price, idPrices), ',', 1) as id,
fkProductId, monthname(PriceDate) as mon,
min(Price) as price,
substring_index(group_concat(fkStore order by Price, idPrices), ',', 1) as spec_id
from prices p
group by fkProductId, monthname(PriceDate);

MYSQL group by month but not on first day of month

My table votes contains votes that have been made by users at different times:
id item_id position user_id created_at
1 2 0 1 11/21/2013 11:27
26 1 1 1 11/21/2013 11:27
27 3 2 1 11/21/2013 11:27
42 2 2 1 12/7/2013 2:20
41 3 1 1 12/7/2013 2:20
40 1 0 1 12/7/2013 2:20
67 2 2 1 12/13/2013 1:13
68 1 1 1 12/13/2013 1:13
69 3 0 1 12/13/2013 1:13
84 2 0 1 12/28/2013 2:29
83 3 2 1 12/28/2013 2:29
82 1 1 1 12/28/2013 2:29
113 3 0 1 1/17/2014 22:08
114 1 1 1 1/17/2014 22:08
115 2 2 1 1/17/2014 22:08
138 2 0 1 1/20/2014 16:49
139 1 1 1 1/20/2014 16:49
140 3 2 1 1/20/2014 16:49
141 1 1 11 1/20/2014 16:51
142 3 2 11 1/20/2014 16:51
143 2 0 11 1/20/2014 16:51
I need to tally the results on a monthly basis but here's the tricky part: the start/end of the month does not necessarily fall on the first day of the month. So if the votes are due on the 10th day of every month, I need a vote that was cast on the 10th to be in a different group from a vote that was cast on the 11th. Using the data above, I want to get three groups:
Group 1: 6 votes (11/21 and 12/7)
Group 2: 6 votes (12/13, 12/28)
Group 3: 9 votes (1/17, 1/20)
I've tried a lot of approaches but to no avail. This is my query right now:
select created_at, ADDDATE(DATE_FORMAT(created_at, '%Y-%m-01'),interval 10 day) as duedate,count("id") from votes where list_id = 2 group by duedate
I am getting group sizes of 3, 9, and 9, not 6, 6 and 9. Any help you can provide would be much appreciated. Thanks.
Your query is close. You just need to subtract 9 days (10 - 1) from the current day to get the month:
select created_at, date_sub(created_at, interval 9 day) as duedate,
count(id)
from votes
where list_id = 2
group by duedate;
date_format() converts a date to a string. There is no need to convert a date value to a character value for this query.
EDIT:
To group by month:
select date_format(date_sub(created_at, interval 9 day), '%Y-%m') as YYYYMM,
count(id)
from votes
where list_id = 2
group by YYYYMM;

mySQL AVG a field with a range of dates on the same table

I have a table
date d_id r_id p_id q_sold onhand
2012-10-10 5 1 3025 3 10
2012-10-10 5 1 3022 12 20
2012-10-10 5 1 3023 15 33
2012-10-11 5 1 3025 3 10
2012-10-11 5 1 3022 12 20
2012-10-11 5 1 3023 15 33
2012-10-12 5 1 3025 3 10
2012-10-12 5 1 3022 12 20
2012-10-12 5 1 3023 15 33
2012-10-13 5 1 3025 3 10
2012-10-13 5 1 3022 12 20
2012-10-13 5 1 3023 15 33
2012-10-14 5 1 3025 3 10
2012-10-14 5 1 3022 12 10
2012-10-14 5 1 3023 15 33
2012-10-15 5 1 3025 3 5
2012-10-15 5 1 3022 12 5
2012-10-15 5 1 3023 15 33
I would like to get the result of the q_sold divided by average of the onhand over a 5 day period, while displaying the other data for a specific date like the 2012-10-15.
I create a query
set #stdate = '2012-10-10';
set #endate = '2012-10-15';
SELECT date, d_id,r_id,p_id,q_sold,onhand,qty_sold/AVG(qty_onhand)
FROM stp_vwsales_info_tots
WHERE date BETWEEN #stdate and #endate and d_id=5
GROUP BY d_id,r_id,p_id
But the result being showed is incorrect, it displays the data for the 2012-10-10 instead of 2010-10-15
date d_id r_id p_id q_sold onhand avg
2012-10-10 5 1 3022 12 20 0.7579
2012-10-10 5 1 3023 15 33 0.4545
2012-10-10 5 1 3025 3 10 0.3273
Can anyone help?
Use your #stdate and #endate as DATE(#stdate/#endate) or DATE_FORMAT(#stdate/#endate,'%Y-%m-%d') otherwise you have to convert #stdate/#endate from string to date through MySQL
i think what your looking for is something called a simple moving average.
to calculate this you'll need to use an inline subquery - so performance won't be the best.
assuming you want to calculate the average over the previous 5 day period, try something like this:
SELECT
date, d_id, r_id, p_id, q_sold, onhand,
( SELECT q_sold/AVG(t2.onhand)
FROM stp_vwsales_info_tots AS t2
WHERE p_id=t1.p_id AND DATEDIFF(t1.date, t2.date) BETWEEN 0 AND 4
) AS 'moving_avg'
FROM stp_vwsales_info_tots AS t1
GROUP BY t1.p_id,t1.date
order by t1.date;

count items in col as rows MYSQL

Dear developers and programmers,
I have a tabel with SHOP_ID, PROD_ID, ipadres and some more tables, but these are where its all about...
Looks like this:
id ip number timestamp shop_id prod_id
--------------------------------------------------
42 81.69.205.25 1319488326 2 3
43 81.205.141.48 1319492649 2 3
44 193.58.10.10 1319520579 14 17
45 84.28.22.226 1319529529 11 19
46 88.15.81.188 1319543745 2 1
47 178.17.241.191 1319563031 14 7
48 87.28.107.171 1319563038 2 6
49 80.156.47.144 1319572818 14 7
50 82.76.241.175 1319577506 11 1
51 82.76.241.175 1319577584 13 1
52 82.76.241.175 1319577785 14 1
53 82.76.241.175 1319577860 4 1
54 62.94.133.153 1319579221 14 1
55 62.94.133.153 1319579281 2 3
56 77.70.175.221 1319617238 11 1
57 77.70.175.221 1319621845 13 1
58 77.70.175.221 1319621848 2 1
59 77.70.175.221 1319621850 11 1
.... more
--------------------------------------------------------
Is there a way to see for each prod_id how many ip numbers there excist for each shop id?
output example
1 2 3 4 5
---------------------------------------------
1 18 5 51 8 4
2 58 5 45 3 4
3 7 6 31 9 2
where horizontal is the prod_id and vertical is the shop_id
I've don this already:
select
shop_id,
count(distinct(ipadres)) amount
from table
GROUP BY shop_id
order by amount desc
but this wil give me only the result of all prod_id's combined.
I would like to have the prod_id's seperate in columns.
I hope there is a solution!
Kind Regards
Try this
select `prod_id`,shop_id,
count(distinct(ipadres)) amount
from table
GROUP BY shop_id,prod_id
order by amount desc