SSRS: Horizontal alignment on a group - sql

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.

Related

Total of Various Columns as a Separate Column in Oracle

Could you help me out with an issue I have in Oracle?
Let's say I have a table that tells me about how many items were sold in each month, and looks like so:
Item
January
February
March
April
Computer
3
5
2
9
TV
10
12
16
14
Camera
22
25
20
27
What I need in the output is a table that would count the total number of items sold over the period, and would look like this:
Item
January
February
March
April
Total
Computer
3
5
2
9
19
TV
10
12
16
14
52
Camera
22
25
20
27
94
I am honestly not sure how to do that. Should I use grouping()?
Thank you in advance.
You don't need to use grouping at all just try to plus all columns as a new column Total.
SELECT T.*,
(January + February + March + April) Total
FROM T

MS Access - Dynamic Crosstab Query - VBA for Totals Row that provides Column totals

I have built a dynamic crosstab query where the user selects options from multiple combo boxes and then I use VBA to update the SQL for the cross tab query. It is then, in turn, displayed on a subform. My question is basically how can I programmatically (VBA or SQL, don't really care either way) add a totals row that calculates the average of each column. I know how to do this manually, but since the number of columns and column headings are not set, this is impractical. I would basically like to automatically set a totals row at the bottom for the average of each column regardless of the number of columns or column headers. An example of a potential case is shown below with the user inputs on top.
Group: A
Team: All
Start Date: 1/1/2014
End Date: 5/31/2014
Query Type: Monthly
Name 2014-Jan 2014-Feb 2014-Mar 2014-Apr 2014-May
John Doe1 1 2 3 4 5
John Doe2 2 2 2 2 2
John Doe3 1 5 9 13 17
John Doe4 2 10 18 26 34
John Doe5 1 8 15 22 29
John Doe6 2 12 22 32 42
John Doe7 1 15 29 43 57
John Doe8 2 14 26 38 50
John Doe9 1 12 23 34 45
Total (Avg): 1.44 8.89 16.33 23.78 31.22
I am trying the generate the bottom "Totals" row programmatically without knowing the column count ahead of time and without manually inserting totals via the ribbon.
You can use a pivot table on a simple select query.
Doesn't the crosstab query do that for you?
https://support.office.com/en-us/article/Make-summary-data-easier-to-read-by-using-a-crosstab-query-8465B89C-2FF2-4CC8-BA60-2CD8484667E8
http://www.fmsinc.com/microsoftAccess/query/crosstab-report/index.html
Although crosstab queries are somewhat limited in their power, you can certainly create another query to do a basic average of the crosstab query you just created.

sql running total math current quarter

Im trying to figure out the total for the quarter when the only data shown is a running total for the year:
Id Amount Periods Year Type Date
-------------------------------------------------------------
1 65 2 2014 G 4-1-12
2 75 3 2014 G 7-1-12
3 25 1 2014 G 1-1-12
4 60 1 2014 H 1-1-12
5 75 1 2014 Y 1-1-12
6 120 3 2014 I 7-1-12
7 30 1 2014 I 1-1-12
8 90 2 2014 I 4-1-12
In the data shown above. The items in type G and I are running totals for the period (in qtrs). If my query returns period 3, is there a sql way to get the data for the qtr? The math would involve retrieving the data for the 3rd period - 2nd period.
Right now my sql is something like:
SELECT * FROM data WHERE Date='4-1-12';
In this query, it will return row #1, which is a total for 2 periods. I would like it to return just the total for the 2nd period. Im looking to make this happen with SQLite.
Any help would be appreciated.
Thank alot
You want to subtract the running total of the previous quarter:
SELECT Id,
Year,
Type,
Date,
Amount - IFNULL((SELECT Amount
FROM data AS previousQuarter
WHERE previousQuarter.Year = data.year
AND previousQuarter.Type = data.Type
AND previousQuarter.Periods = data.Periods - 1
), 0) AS Amount
FROM data
The IFNULL is needed to handle a quarter that has no previous quarter.

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.