Access Crosstab subtotal columns - ms-access

I have a following question regarding crosstabs in Access:
How do I create a subtotal columns?
What I want to see as a result of the query is this:
Nov 2010 Dec 2010 2010 Total Jan 2011 Feb 2011
Row1 2 4 17 3 2
Row2 8 6 35 7 5
How do I create these subtotals for the year? (It's ok, if the year data will be in the end, after all months)
The problem is that I need to do this without hardcoding each year, the query should work with any dataset
Thanks in advance!

Say we have raw [SalesData]
SalesYear SalesMonth Region SalesTotal
--------- ---------- ------ ----------
2010 11 East 45
2010 11 West 58
2010 12 East 55
2010 12 West 63
2011 1 East 51
2011 1 West 54
2011 2 East 55
2011 2 West 61
We can create a [SalesTotals] query to combine the monthly sales totals with the yearly totals...
SELECT SalesYear & "-" & Format(SalesMonth, "00") AS SalesPeriod, Region, SalesTotal FROM SalesData
UNION ALL
SELECT SalesYear & "-Total", Region, SUM(SalesTotal) FROM SalesData GROUP BY SalesYear, Region;
...which produces
SalesPeriod Region SalesTotal
----------- ------ ----------
2010-11 East 45
2010-11 West 58
2010-12 East 55
2010-12 West 63
2011-01 East 51
2011-01 West 54
2011-02 East 55
2011-02 West 61
2010-Total East 100
2010-Total West 121
2011-Total East 106
2011-Total West 115
Then we can do our crosstab query on the [SalesTotals] query...
TRANSFORM Sum(SalesTotals.[SalesTotal]) AS SumOfSalesTotal
SELECT SalesTotals.[Region]
FROM SalesTotals
GROUP BY SalesTotals.[Region]
PIVOT SalesTotals.[SalesPeriod];
...which produces
Region 2010-11 2010-12 2010-Total 2011-01 2011-02 2011-Total
------ ------- ------- ---------- ------- ------- ----------
East 45 55 100 51 55 106
West 58 63 121 54 61 115

Related

Retrieve the first and second best win results per age_group column per a region column in mysql table

MySql issue: I want to extract the two best age_groups per region based on their wins. I haven't had much luck on this, having browsed similar issues. It's probably straightforward but mysql isn't playing nice for me this evening.
region
age_group
wins
london
35
52
paris
10
54
dublin
15
57
london
40
65
paris
20
68
dublin
35
73
paris
5
75
london
5
79
dublin
25
81
paris
15
81
london
30
82
dublin
20
83
london
20
85
london
10
87
london
25
87
paris
30
91
paris
25
91
dublin
40
94
dublin
30
96
dublin
5
96
london
15
99
dublin
10
100
Results should like something like this:
region
best_age_category
second_best_age_category
dublin
10
5
london
15
25
paris
25
30
select region
,group_concat(case when dns_rnk = 1 then age_group end) as best_age_category
,group_concat(case when dns_rnk = 2 then age_group end) as second_best_age_category
from (
select *
,dense_rank() over(partition by region order by wins desc) as dns_rnk
from t
) t
group by region
region
best_age_category
second_best_age_category
dublin
10
5,30
london
15
25,10
paris
30,25
15
Use ROW_NUMBER() OVER (<partition_definition> <order_definition>) to assign row numbers to your records and then filter where the row number is 1 or 2

select top 5 group by and order by

I have a table like this:
CITY QNT EXP RATE
LONDON 60 6 900
LONDON 35 8 337
LONDON 24 6 300
LONDON 22 6 266
BIRMINGHAM 22 6 266
NEWYORK 69 19 263
LONDON 21 6 250
ROME 24 7 242
BIRMINGHAM 24 7 242
BIRMINGHAM 24 7 242
LONDON 20 6 233
BIRMINGHAM 23 7 228
STUTTGART 29 9 222
LONDON 19 6 216
STUTTGART 25 8 212
PARIS 31 10 210
STUTTGART 34 11 209
STUTTGART 34 11 209
BIRMINGHAM 18 6 200
BIRMINGHAM 18 6 200
NEWYORK 18 6 200
BIRMINGHAM 17 6 183
LONDON 19 7 171
MUNICH 16 6 166
PARIS 21 8 162
STUTTGART 39 15 160
BARCELONA 18 7 157
LONDON 18 7 157
ROME 33 13 153
BARCELONA 15 6 150
PARIS 25 10 150
ROME 20 8 150
PARIS 25 10 150
ROME 20 8 150
LONDON 15 6 150
MUNICH 15 6 150
BIRMINGHAM 15 6 150
NEWYORK 15 6 150
LONDON 17 7 142
MUNICH 17 7 142
Here is my sql command:
select CITY, QNT, EXP, (QNT-EXP)*100/EXP as RATE
from tbl_city
order by RATE desc
I want to group by city these results. But I couldn't do it. I want the top5 line to change most.
Result should be like that:
LONDON 60 6 900
BIRMINGHAM 22 6 266
NEWYORK 69 19 263
ROME 24 7 242
STUTTGART 29 9 222
This is from MySQL 5.6.
select CITY, QNT, EXP, (QNT-EXP)*100/EXP as RATE
from tbl_city GROUP BY CITY
order by RATE desc LIMIT 5
Tested in this link: http://www.sqlfiddle.com/#!9/3f1ea1/1
This is from MS SQL Server 2017
select TOP 5 f.CITY, f.QNT, f.EXP, x.RATE
from (
select CITY, MAX((QNT-EXP)*100/EXP) as RATE
from tbl_city GROUP BY CITY
) as x inner join tbl_city as f on f.CITY = x.CITY
and ((f.QNT-f.EXP)*100/f.EXP) = x.RATE
ORDER BY RATE DESC;
Tested in this link: http://www.sqlfiddle.com/#!18/7b8da/61
Maybe you want something like this?
select top 5 CITY, QNT, EXP, RATE
from (
select *, row_number() over (partition by CITY order by RATE desc) AS RN
from (
select CITY, QNT, EXP, (QNT-EXP)*100/EXP as RATE
from tbl_city
) X
) Y
where RN = 1
order by RATE desc
I didn't test this, but it should take first the row for the city with biggest rate, and then take top 5 rows so that that the same city is not duplicated
I don't know what is logic behind the specified formula but this does what you want
select top 5 * from (
select top(1) with ties city, qnt, exp, (qnt-exp)*100/exp as rate
from tbl_city
order by row_number() over (partition by city order by (qnt-exp)*100/exp desc)
)t
order by rate desc
However, top(1) with ties with analytical functions are available in SQL Server.

sql query: which students have repeated subject?

table structure is like :
student area yearlevel code year sem result
123010 INFO 9 0002 2015 1 77
123011 INFO 9 0002 2015 1 70
123012 INFO 9 0002 2015 1 55
123037 INFO 9 0002 2016 2 49
123037 INFO 9 0002 2017 1 NULL
123010 COMP 9 0007 2016 1 82
123010 ISYS 9 0026 2015 2 82
123011 ISYS 9 0026 2015 2 88
123012 ISYS 9 0026 2015 2 66
123010 COMP 9 0038 2016 2 77
123010 COMP 9 0041 2016 1 45
123010 COMP 9 0041 2017 1 NULL
123010 ISYS 9 0049 2016 1 88
So student 101 has a repeated subject 000002
Use GROUP BY and HAVING function :
SELECT student,subject
FROM your_table
GROUP BY student,subject
HAVING COUNT (*) > 1
This should work:
select Student,subject from table group by student, subject having count(*)>1

Mysql: Calculating running average for each Employee by day Monthly

I have this table that has the name of the employee and their phone time duration in mysql. The table looks like this:
Caller Emplid Calldate Call_Duration
Jack 333 1/1/2016 43
Jack 333 1/2/2016 45
Jack 333 1/3/2016 87
Jack 333 2/4/2016 44
Jack 333 2/5/2016 234
jack 333 2/6/2016 431
Jeff 111 1/1/2016 23
Jeff 111 1/2/2016 54
Jeff 111 1/3/2016 67 48
I am trying to calculate the running Daily average of each employee total_Duration by day each month. Suppose I have daily running average for the month of April, then the running average for the May should start from 1st of may and end on 31st of that month. I have tried doing many ways and mysql does not have pivot and partition function like sql server. The total employee who made the call changes daily, I need something that dynamically takes care of no of employees that makes call.
The output should look like this:
Caller Emplid Calldate Call_Duration Running_avg
Jack 333 1/1/2016 43 43
Jack 333 1/2/2016 45 44
Jack 333 1/3/2016 87 58.33333333
Jack 333 2/4/2016 44 44
Jack 333 2/5/2016 234 139
Jack 333 2/6/2016 431 236.3333333
Jeff 111 1/1/2016 23 23
Jeff 111 1/2/2016 54 38.5
Jeff 111 1/3/2016 67 48
This is the query that I started below:
SELECT row_number,Month_Year,Callername,Calldate,Caller_Emplid,`Sum of Call`,`Sum of Call`/row_number as AvgCall,
#`sum of call`:=#`sum of call`+ `sum of call` overallCall,
#row_number:=row_number overallrow_number,
#RunningTotal:=#`sum of call`/#row_number runningTotal
FROM
(SELECT
#row_number:=
CASE WHEN #CallerName=CallerName and date_format(calldate,'%d') = date_format(calldate,'%d') and
date_format(calldate,'%m') = date_format(calldate,'%m')
THEN #row_number+1
ELSE 1 END AS row_number,#CallerName:=CallerName AS Callername,Calldate,Caller_Emplid,Month_Year,`Sum of Call`
FROM transform_data_2, (SELECT #row_number:=0,#CallerName:='') AS t
ORDER BY callername) a
JOIN (SELECT #`Sum of call`:= 0) t

Filtering Multiple Data

I have two tables, first table is called 'submissions' and the second table is called 'area'
******** SUBMISSIONS TABLE ********
userid statusid no name area month year dateupdated
62 2 763 ABCD Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 7 2012 2013-11-26 15:10
62 2 869 ABC Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 6 2013 2013-11-26 14:50
61 1 763 ABCD Brazil 6 2013 2013-11-26 14:50
54 1 200 ABCDE US 12 2013 2013-11-26 21:02
32 2 200 ABCDE US 12 2013 2013-11-26 21:03
******* AREA TABLE ********
no name area
763 ABCD Brazil
869 ABC Brazil
869 ABC Brazil
869 ABC Brazil
763 ABCD Brazil
200 ABCDE US
200 ABCDE US
I am trying to achieve the following: A user selects an Area, month and year via a dropdown (SELECT). Once they have selected the 3 fields, the filtered data is displayed.
I want to the display the selection as per the fields that are filtering, but also display all other records in that particular area where is no data. Almost like an outstanding Report.. Example Below:
userid statusid no name area month year dateupdated
62 2 763 ABCD Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 7 2012 2013-11-26 15:10
62 2 869 ABC Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil
61 1 763 ABCD Brazil
54 1 200 ABCDE US
32 2 200 ABCDE US
Does anyone know how i can achieve this? Please assist.
Try somthing like this
SELECT userid , statusid, a.no , a.name , a.area , month , year , dateupdated
FROM submission s
LEFT OUTER JOIN area a
ON s.no = a.no
WHERE month = 12 AND
year = 2013 AND
a.area = 'US'
SQLFIDDLE