Access SQL: order by customized cases and pivot by month - sql

I wrote a crosstab "A" in Access which selects 2 text columns and 2 number columns, month and posts from another table "B".
An excerpt of the code is like this...
Select B.xx...
From B
Group by B.xx...
Pivot B.Month;
...which works fine if the months belong to the same year, but since the months are on a rolling basis, when the year carries over, for e.g. month are {October, November, December, January, February}, the order just stays as B.Month ASC {01, 02, 10, 11, 12} and I want it to be pivoted in order like {10 11 12 01 02}
UPDATE: I fixed the main problem, but now I want to
1. change the order to 10/2013 to 3/2014
2. change the headings to 10 to 3
TOPIC SUB-TOPIC 1/2014 10/2013 11/2013 12/2013 2/2014 3/2014
language english 86 88 90 82 88 90
language french 70 77 75 79 82 80
mathematics geometry xx xx xx xx xx xx
mathematics calculus xx xx xx xx xx xx

Create an expression that includes both the year and the month, and use it in the pivot.
something like Pivot (year & format(month,"00"))
or, if you have the complete date in a field, something like `Pivot Format (Date,"yyyy/mm/dd")
as a reminder, Year, Date and Month may be reserved words and you may need different field names - not sure, but likely

Related

SSRS: Horizontal alignment on a group

On my dataset I select information from four different years sorted by date and how many subscriptions I had on said date, which looks something like this:
Date Year Subs Day
15/09/2014 2015 57 1
16/09/2014 2015 18 2
17/09/2014 2015 16 3
14/09/2015 2016 10 1
15/09/2015 2016 45 2
16/09/2015 2016 28 3
12/09/2016 2017 32 1
13/09/2016 2017 11 2
14/09/2016 2017 68 3
24/08/2017 2018 23 1
25/08/2017 2018 53 2
26/08/2017 2018 13 3
What I'm trying to do is create an 'Year' Column Group to align them horizontally, but when I do that, this is the result:
result
Expected result:
expected result
Is this achievable in SSRS? I've tried removing the group =(Details), which gives me the desired result, except it only returns one line of information.
Any insight aprreciated.
By default, the Details group causes you to get one row per row in the dataset. In your case, I would suggest grouping the Rows by the Day column and create a column group by Year.
First, create the two groups and add columns inside the column group.
Then, add a row outside and above the Day row group. Place the headings here and then delete the top row. It should look like this:
Now these 4 columns will repeat to the right for each year and you will get rows based on the number of days in your dataset.

How to perform multiple table calculation with joins and group by

I have two tables client and grouping. They look like this:
Client
C_id
C_grouping_id
Month
Profit
Grouping
Grouping_id
Month
Profit
The client table contains monthly profit for every client and every client belongs to a specific grouping scheme specified by C_grouping_id.
The grouping table contains all the groups and their monthly profits.
I'm struggling with a query that essentially calculates the monthly residual for every subscriber:
Residual= (Subscriber Monthly Profit - Grouping monthly Profit)*(average subscriber monthly profits for all months / average profits for all months for the grouping subscriber belongs to)
I have come up with the following query so far but the results seem to be incorrect:
SELECT client.C_id, client.C_grouping_Id, client.Month,
((client.Profit - grouping.profit) * (avg(client.Profit)/avg(grouping.profit))) as "residual"
FROM client
INNER JOIN grouping
ON "C_grouping_id"="Grouping_id"
group by client.C_id, client.C_grouping_Id,client.Month, grouping.profit
I would appreciate it if someone can shed some light on what I'm doing wrong and how to correct it.
EDIT: Adding sample data and desired results
Client
C_id C_grouping_id Month Profit
001 aaa jul 10$
001 aaa aug 12$
001 aaa sep 8$
016 abc jan 25$
016 abc feb 21$
Grouping
Grouping_id Month Profit
aaa Jul 30$
aaa aug 50$
aaa Sep 15$
abc Jan 21$
abc Feb 27$
Query Result:
C_ID C_grouping_id Month Residual
001 aaa Jul (10-30)*(10/31.3)=-6.38
... and so on for every month for avery client.
This can be done in a pretty straight forward way.
The main difficulty is obviously that you try to deal with different levels of aggregation at once (average of the group and the client as well as the current record).
This is rather difficult/clumsy with simple SELECT FROM GROUP BY-SQL.
But with analytical functions aka Window functions this is very easy.
Start with combining the tables and calculating the base numbers:
select c.c_id as client_id,
c.c_grouping_id as grouping_id,
c.month,
c.profit as client_profit,
g.profit as group_profit,
avg (c.profit) over (partition by c.c_id) as avg_client_profit,
avg (g.profit) over (partition by g.grouping_id) as avg_group_profit
from client c inner join grouping g
on c."C_GROUPING_ID"=g."GROUPING_ID"
and c. "MONTH" = g. "MONTH";
With this you already get the average profits by client and by grouping_id.
Be aware that I changed the data type of the currency column to DECIMAL (10,3) as a VARCHAR with a $ sign in it is just hard to convert.
I also fixed the data for MONTHS as the test data contained different upper/lower case spellings which prevented the join to work.
Finally I turned all column names into upper case to, in order to make typing easier.
Anyhow, running this provides you with the following result set:
CLIENT_ID GROUPING_ID MONTH CLIENT_PROFIT GROUP_PROFIT AVG_CLIENT_PROFIT AVG_GROUP_PROFIT
16 abc JAN 25 21 23 24
16 abc FEB 21 27 23 24
1 aaa JUL 10 30 10 31.666
1 aaa AUG 12 50 10 31.666
1 aaa SEP 8 15 10 31.666
From here it's only one step further to the residual calculation.
You can either put this current SQL into a view to make it reusable for other queries or use it as a inline view.
I chose to use it as a common table expression (CTE) aka WITH clause because it's nice and easy to read:
with p as
(select c.c_id as client_id,
c.c_grouping_id as grouping_id,
c.month,
c.profit as client_profit,
g.profit as group_profit,
avg (c.profit) over (partition by c.c_id) as avg_client_profit,
avg (g.profit) over (partition by g.grouping_id) as avg_group_profit
from client c inner join grouping g
on c."C_GROUPING_ID"=g."GROUPING_ID"
and c. "MONTH" = g. "MONTH")
select client_id, grouping_id, month,
client_profit, group_profit,
avg_client_profit, avg_group_profit,
round( (client_profit - group_profit)
* (avg_client_profit/avg_group_profit), 2) as residual
from p
order by grouping_id, month, client_id;
Notice how easy to read the whole statement is and how straight forward the residual calculation is done.
The result is then this:
CLIENT_ID GROUPING_ID MONTH CLIENT_PROFIT GROUP_PROFIT AVG_CLIENT_PROFIT AVG_GROUP_PROFIT RESIDUAL
1 aaa AUG 12 50 10 31.666 -12
1 aaa JUL 10 30 10 31.666 -6.32
1 aaa SEP 8 15 10 31.666 -2.21
16 abc FEB 21 27 23 24 -5.75
16 abc JAN 25 21 23 24 3.83
Cheers,
Lars

SQL count ocurrences within time periods

I recently started up working with Access database in a project at my work (hospital) and got lots of useful tips from this site. However, I have now a problem that I can't figure out how to solve.
I have a table containing treatment dates (as well as other data) for lots of patients. My task is to count the number of treatments given within each week (and possibly month/quarter/year as well). The treatment dates are located in the column 'TreatDate' in the table 'Data'.
I have used DatePart to separate out year and week number as:
SELECT
DatePart('yyyy',[TreatStart]) AS Year, DatePart('ww',[TreatStart]) AS Week
FROM Data
ORDER BY DatePart('yyyy',[TreatStart]),DatePart('ww',[TreatStart]);
Which gives me:
Year Week
2006 16
2006 16
2006 16
2006 17
2006 17
2006 18
2006 19
2006 19
2006 19
... ...
How do I count the number of occurances in each week in order to get something like:
Year Week N
2006 16 3
2006 17 2
2006 18 1
2006 19 3
... ... ...
Best regards,
Mattias
Just add the group by on the same values by which you want count and add the count aggregate function to get the total count of the appointments at that given group.
SELECT DatePart('yyyy',[TreatStart]) AS Year, DatePart('ww',[TreatStart]) AS Week , COUNT(*) As N
FROM Data
GROUP BY DatePart('yyyy',[TreatStart]),DatePart('ww',[TreatStart])
ORDER BY DatePart('yyyy',[TreatStart]),DatePart('ww',[TreatStart]);
Use GROUP BY and COUNT:
SELECT DatePart('yyyy',[TreatStart]) AS Year,
DatePart('ww',[TreatStart]) AS Week,
COUNT(*) as N
FROM Data
GROUP BY DatePart('yyyy',[TreatStart]),DatePart('ww',[TreatStart])
ORDER BY DatePart('yyyy',[TreatStart]),DatePart('ww',[TreatStart]);
The output will be:
Year Week N
2006 16 3
2006 17 2
2006 18 1
2006 19 3

How to fetch data according to date in sql

I have a table truck_data with columns truck_no, diesel_filled, source, destination, amount etc
I want to fetch only truck_no and diesel_filled in such way that it will show the details of diesel_filled in each truck through out the month..
Please tell me the SQL query for that - I had tried this query but it's not working
SELECT
truck_no, diesel_filled, date
FROM
truck-data
ORDER BY
date
Please help me out
Thanks in advance
I want output like this
truck_no 1 2 3 4 5 6 7 8 9 etc(date from 1 to 31))
---------------------------------------------------------------------------
xyz 25 22 33 33 22 22 22 0 0 (diesel filled in truck order by date)
pqr 25 25 22 11 22 00 22 55 22
abc 21 15 12 14 13 00 22 00 00
It's still quite unclear what you want. But I think you are mixing presentation and data
SELECT truck_no,diesel_filled,date
FROM truck-data
ORDER BY date
Will give you all rows ordered by date. Presumably you whant some filter on that to only show a specific month.
SELECT truck_no,diesel_filled,date
FROM truck-data
WHERE date >= 2014-10-05 AND date < 2014-11-01 ORDER BY date
To get all the rows for october this year. If date here is a DateTime column.
Then you could change it to:
SELECT truck_no,sum(diesel_filled),Day(date)
FROM truck-data
WHERE date >= 2014-10-05 AND date < 2014-11-01
GROUP BY truck_no
ORDER BY date"
That would give you only one row per day and truck. If disel_filled is a numeric value of some kind.
Then in your UI you would have to do the presentation in any way you prefer. For example in some kind of pivot table like your description above.
You could of course do that in SQL as well, but usually that is a job for the presentation layer.
If you really want to do in SQL you could look into: http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
If you'r using MsSql.

Select Max Date in Year Month Day Format

I have a table that I drew the following sample from:
Item <other columns> year month day
---- -- ------------ ---- ----- ---
VX4O GL 630.5938 2012 7 20
BX2T GL 0 2012 7 13
MWB806I GL 92004.72 2012 6 15
4XU GL 17.125 2012 7 20
VL4O GL 130.5 2012 7 20
MWB806I GL 92004 2012 10 26
MWB806I GL 92005 2012 11 30
3PU GL 25 2012 7 20
VC4O GL 630.6094 2012 7 20
MWB806I GL 92005 2012 11 2
The first column is Item, the last three columns are year, month, day.
How do I select the max date per item?
SELECT Item, MAX(CONVERT(DATETIME, RTRIM([year])
+ RIGHT('0'+RTRIM([month]), 2)
+ RIGHT('0'+RTRIM([day], 2)
)
FROM dbo.table
GROUP BY Item;
Now you really should consider fixing this schema. Why are you storing year/month/day separately? All it does is make calculations like this one much more difficult, and prevents any proper validation (you can have check constraints for basic stuff, but these are much more complex for things like leap years). And it doesn't save you any space (in fact you lose space if month/day are int, or if you can use smalldatetime).