SQL Aggregate Function Min with Group - mysql

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);

Related

MS Access - Add most recent price for each new purchase

I work with this data in table Purchases:
Mat_ID Date Price
11 5.1.2018 10
11 7.1.2018 12
11 9.1.2018 14
12 5.1.2018 10
12 7.1.2018 12
13 9.1.2018 14
13 5.1.2018 10
My desired output query is to have another column with last purchase Price:
Mat_ID Date Price PrevPrice
11 5.1.2018 10 Null
11 7.1.2018 12 10
11 9.1.2018 14 12
12 5.1.2018 10 Null
12 7.1.2018 12 10
13 9.1.2018 14 Null
13 5.1.2018 10 14
Can you recommend something, please?
Thank you!
Try this:
Select
*,
(Select Top 1 Price
From YourTable As T
Where T.Mat_ID = YourTable.Mat_ID And T.[Date] < YourTable.[Date]
Order By T.Mat_ID, T.[Date] Desc) As PrevPrice
From
YourTable

Ranking of the affiliates with most commissions from database

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.

Find lowest value from the rows which contains similar foreign key value

Table:
pk fk price
1 1 23
2 1 12
3 1 3
4 2 53
5 2 75
6 3 95
7 3 113
8 3 63
9 3 73
10 3 93
11 4 113
11 4 150
11 4 105
In the above table:
How to find out the lowest price based on it's common fk value.For example: lowest price for fk=1 is 3, for fk=2 is 53, for fk=3 is 63 and for fk=4 is 105.
I want a single SQL statement which could find lowest price for each common fk value.
You just want a basic aggregate per group.
select fk, min(price)
from your_table
group by fk;

aggregate on amount column by month - sql

I can't seem to figure out how to aggregate this table. I need to figure out from this table what is the amount aggregated by a month. So I have some projects and a duration per project, average income per month (for each project). For example, I would like to see what is the total amount for all project May 2011. Here is the original table:
Year Month Amount Duration (month) average per month
2012 1 7 4 1.75
2012 2 6 5 1.2
2012 3 5 6 0.833333333
2012 4 4 6 0.666666667
2012 5 9 5 1.8
2012 6 10 4 2.5
2012 7 20 3 6.666666667
2011 4 13 2 6.5
2011 3 3 10 0.3
2011 12 4 11 0.363636364
2011 2 5 12 0.416666667
2011 3 7 3 2.333333333
2010 5 8 4 2
2010 7 3 6 0.5
2010 9 4 7 0.571428571
2010 11 5 8 0.625
2010 1 6 8 0.75
2010 2 7 8 0.875
2010 3 8 9 0.888888889
2010 4 9 1 9
I would appreciate any help. Thanks.
Assuming year is varchar and month/duration int (if not, you may need to convert them as applicable) you can do something like this:
select sum(amount) from yourtable
where YearMonth between
period_add(year&'01',month-1) and period_add(year&'01',month+duration-2)
being YearMonth a string with the year/month to be queried, in your example would be '201105'
for a number of year/months you can create a one-column table:
create table yearmonths(yearmonth varchar(6));
insert into yearmonths values
('201101'),('201102'),(201103),
('201104'),('201105'),(201106)
and join it to your table:
Select yearmonth,sum(amount)
from yearmonths y
left join yourtable t
on(y.yearmonth between period_add(year&'01',month-1)and period_add(year&'01',month+duration-2)
group by yearmonth

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