MySQL Sum and GroupBy in same query - mysql

I need some help designing a MySQL query that will return the sum of the AMT for each TYPE, grouped by YEAR. I'm sure this will be easy for someone. Just not for me! Thanks.
AMT YEAR TYPE
10 2010 A
20 2010 B
30 2010 B
20 2011 A
15 2011 A
11 2011 B
15 2011 A
13 2012 A
15 2012 A
20 2012 B
10 2012 B
10 2012 B
13 2012 B
17 2013 A
22 2013 A
24 2013 B
15 2013 B
20 2013 A
15 2013 A
11 2013 B
15 2013 A
13 2010 B
15 2010 A
20 2010 B
10 2011 A
10 2011 A
13 2011 B
17 2011 A
22 2012 A

Something like this:
SELECT SUM(Amt), Year, Type
FROM YourTable
GROUP BY Year, Type
SQL Fiddle Demo

you mean
select SUM(AMT) from table group by year, type
?
this will result in
2011 A X1
2011 B Y1
2012 A X2
2012 B Y2
etc...

Related

SQL sum and show row with non-existent sum values

i have 2 tables : dt_user and dt_invoice.
**dt_members :**
id firstname
3 Salim
5 Sara
8 Julie
**dt_invoice**
user_id amount_ht period month year
3 4950 04 2018 04 2018
3 7200 10 2018 10 2018
8 11000 10 2018 10 2018
8 5500 11 2018 11 2018
3 6750 11 2018 11 2018
3 8700 12 2018 12 2018
3 8800 01 2019 01 2019
8 7500 01 2019 01 2019
3 4950 02 2019 02 2019
3 7550 03 2019 03 2019
I want to create a query joining the two table, but i want to show each user_id for PERIOD that there is in table dt_invoice.
**Expected results :**
user_id amount_ht period month year
3 4950 04 2018 04 2018
5 0 04 2018 04 2018 //non-existent record in dt_invoice
8 0 04 2018 04 2018 //non-existent record in dt_invoice
3 7200 10 2018 10 2018
5 0 10 2018 10 2018 //non-existent record in dt_invoice
8 11000 10 2018 10 2018
8 5500 11 2018 11 2018
5 0 11 2018 11 2018 //etc ...
3 6750 11 2018 11 2018
3 8700 12 2018 12 2018
5 0 12 2018 12 2018
8 0 12 2018 12 2018
3 8800 01 2019 01 2019
5 0 01 2019 01 2019
8 7500 01 2019 01 2019
3 4950 02 2019 02 2019
5 0 02 2019 02 2018
8 0 02 2019 02 2018
3 7550 03 2019 03 2019
5 0 03 2019 03 2018
8 0 03 2019 03 2018
Thanks in advance for your help, i'm totally stuck ..
SQL datas available here : https://rextester.com/live/LBSEY76360
also in sqlfiddle : http://sqlfiddle.com/#!9/728af3/1
Use a cross join to generate the rows and left join to bring in the values:
select m.user_id, p.period, p.month, p.year,
coalesce(t.amount_ht, 0) as amount_ht
from dt_members m cross join
(select distinct period, month, year from dt_invoice) p left join
dt_invoice t
on t.user_id = m.id and t.period = p.period;
Maybe this would help.
SELECT user_id, amount_ht, period, month, year
FROM dt_invoice
LEFT JOIN dt_members ON user_id = id

MySQL query to get data dynamically based on financial year

I want to select fields dynamically based on financial year (i.e., for the year of Apr'18 to Mar'19).
I have data in a table exact as given below :
ROWNO YEAR MONTH
1 2016 1
2 2016 2
3 2016 6
4 2017 7
5 2017 5
6 2018 4
7 2018 5
8 2018 6
9 2018 7
10 2018 8
11 2018 9
12 2018 10
13 2018 11
14 2018 12
15 2019 1
16 2019 2
17 2019 3
18 2019 3
19 2017 4
20 2017 1
21 2017 2
22 2018 3
I want to get the result as shown below for the financial year 2018-19 are
ROWNO YEAR MONTH
6 2018 4
7 2018 5
8 2018 6
9 2018 7
10 2018 8
11 2018 9
12 2018 10
13 2018 11
14 2018 12
15 2019 1
16 2019 2
17 2019 3
I used Query
SELECT * FROM rup_calendar WHERE YEAR BETWEEN YEAR(CURDATE()) AND YEAR(CURDATE())+1;
but not able to get exact result.
Please give me the query to get the result as given above.
Given the data you have your approach is not far away
select rowno,year,month
from t
where year * 100 + month between
case when month(now()) < 4 then (year(now()) - 1) * 100 + 3
else year(now()) * 100 + 4
end
and
case when month(now()) < 4 then (year(now())) * 100 + 3
else (year(now()) + 1) * 100 + 4
end
Notice I have calulated a yearmonth field to ease comparison. Note I haven't fully tested this and you should do so by substituting an # variable for now() to test.

Read the penultimate value ssrs

i have a report with parameter is the year i can select a multiple value
year
Data 2010 2011 2012
hp 14 25 30
Dell 17 18 20
what i need when i create the report i want to have another column with the last value concatenation with the last previous one like this :
year
Data 2010 2011 2012 2012/2011
hp 14 25 30 3025
Dell 17 18 20 2018
i tried this :
=Last(Field!Qte.value)+Last(Field!Qte.value)-1
but i had the wrong result i got this result :
year
Data 2010 2011 2012 2012/2011
hp 14 25 30 3029
Dell 17 18 20 2019
the problem is in this function
Last(Field!Qte.value)-1
it doesn't return the last previous value

Query in a SQL table

I have the following tables:
http://sqlfiddle.com/#!2/1e991/1
But I have a problem! The output of my query is:
DATE SLOT sum(SUCCESSFUL) SUCCESSFUL PERCENTAGE OTA_NAME USERS_SI
July, 02 2013 00:00:00+0000 2 120 120 41.6667 campana 2 50
July, 02 2013 00:00:00+0000 1 200 200 25 campana 2 50
July, 02 2013 00:00:00+0000 1 150 150 53.3333 campana 3 80
July, 01 2013 00:00:00+0000 2 100 100 20 campana 1 20
July, 01 2013 00:00:00+0000 3 440 440 4.5455 campana 1 20
July, 01 2013 00:00:00+0000 1 700 700 2.8571 campana 1 20
And I need a sum of successful with the same date and campaign, for example, I have in July 1 with campaign 1 three rows, then I need sum the successful of the three rows, and with campaign 2 two rows, then I need sum the successful of the two rows, and with campaign 3 one rows, then I need sum the successful of the one row that is the same.
And finally the percentage is the division between sum(SUCCESSFUL) and USERS_SI
The output that I need is:
DATE SLOT sum(SUCCESSFUL) SUCCESSFUL PERCENTAGE OTA_NAME USERS_SI
July, 02 2013 00:00:00+0000 2 320 120 41.6667 campana 2 50
July, 02 2013 00:00:00+0000 1 320 200 25 campana 2 50
July, 02 2013 00:00:00+0000 1 150 150 53.3333 campana 3 80
July, 01 2013 00:00:00+0000 2 1240 100 20 campana 1 20
July, 01 2013 00:00:00+0000 3 1240 440 4.5455 campana 1 20
July, 01 2013 00:00:00+0000 1 1240 700 2.8571 campana 1 20
Can you help me?
sqlfiddle here
SELECT other.date AS date,
other.slot,
other.successful,
calc.sum_successful,
max((case when (rule.tree_si = dms.tree) then dms.numberResponses end))/successful AS percentage,
other.name AS ota_name,
other.successful AS successful,
max((case when (rule.tree_si = dms.tree) then dms.numberResponses end)) AS users_si
FROM aca_ota_other other
join aca_dms_rules rule on other.name = rule.ota_name
join aca_dms dms on dms.date = other.date and rule.tree_si = dms.tree
join (SELECT name, sum(successful) as sum_successful FROM aca_ota_other GROUP BY name) as calc on other.name = calc.name
GROUP BY
other.date,
other.name,
other.successful
ORDER BY other.date desc
I'm not sure but if i understood well, you need something like this:
I'm sorry, I've made some little changes in your query.
SELECT other.date date,
other.slot,
other.name ota_name,
sum(successful) successful,
dms.numberResponses users_si,
SUM(successful)/dms.numberResponses AS percentage
FROM aca_ota_other other
JOIN aca_dms_rules rule ON other.name=rule.ota_name
JOIN aca_dms dms ON dms.date=other.date AND rule.tree_si=dms.tree
GROUP BY other.date,
other.name
ORDER BY other.date DESC
http://sqlfiddle.com/#!2/1e991/22

Sorting table based on Year and Year+Months

I have a table with a column Year and ID
YEAR ID
1988 29
1989 89
1990 22
1992 9
1994 8
1998 23
1922 20
August 1990 12
September 2009 14
August 1991 11
November 2009 33
October 1990 30
January 1990 55
March 2001 24
Is there way I can sort the table in such a way that my final result is in Order.. I am looking for the result like
YEAR ID
1922 20
1988 29
1989 89
1990 22
1992 9
1994 8
1998 23
January 1990 55
August 1990 12
October 1990 30
August 1991 11
March 2001 24
September 2009 14
November 2009 33
Thank you in advance
Just replace myYear with your table name...
select year,id
from
(select year,id,
case when STR_TO_DATE(year,'%Y') is not null then STR_TO_DATE(year,'%Y') else STR_TO_DATE(year,'%M %Y') end as d,
case when STR_TO_DATE(year,'%Y') is not null then 0 else 1 end as ob
from myYear
) y
order by ob asc,d asc;