How to output table results by using cfoutput group by date? - coldfusion

I have a query that get the all the employees count in a table:
emp_namelast | emp_namefirst | employeetotal | year_cse1
--------------------------------------------------------
smith | john | 13 | 2014
smith jr | jonnny | 10 |2014
baker |jane |5 |2015
doe |john |6 |2015
I'm outputting the results in a table. I have the results in order from the query.
But with the code below it's outputting the top result from 2014 and then the top
result from 2015.
I have tried using no group , which gives me all the data from the query.
I would like it to output the data from 2014 and 2015 in two different tables.
One would contain records for 2014 and the other 2015.
Would it have to be done without using a 'group'?
<h2>employees</h2>
<table >
<thead><tr><th>Employee</th><th>Stars</th></tr></thead>
<tbody>
<cfoutput query="GetEmployeeTotals" group="year_cse1">
<tr><td>#emp_namefirst# #emp_namelast#</td>
<td>#employeetotal#</td>
</tr>
</cfoutput>
</tbody>
</table>

You are on the right track but are missing a detail. The group attribute works like this:
<cfoutput query="somequery" group="somefield">
grouped data goes here
<cfoutput>
ungrouped data goes here
</cfoutput>
grouped data can go here as well
</cfoutput>
In your code, you are not outputting any grouped data, and you are missing the extra cfoutput tags for the ungrouped data.

Related

How to setup cross-report drillthrough in power bi with same dimension with different value

In my Power BI reports,I have 3 tables
Summary Table
2 Details Tables
The Summary Table has these columns
| Day | Country |
|------| --------|
| Jan 1 | USA |
| Feb 2 | UK |
| Feb 13 | USA |
| Feb 23 | USA |
And the 2 Details table, 1 is for USA country and 1 is for UK country. The schema between 2 tables are different.
USA Detail Table
Day
Country
City
Some USA columns 1
Some USA columns 2
Some USA columns 3
UK Detail Table
Day
Country
City
Some UK columns 1
Some UK columns 2
So I build 3 tabs in my Power BI report.
1 tab has charts using Summary Table as data source
1 tab has charts using USA Detail Table as data source
1 tab has charts using UK Detail Table as data source
My question is how can I setup 'cross-report drillthrough in power bi' so that when I click the row in the Summary table with 'USA' as country , it will drill thru to the 'USA Tab'.
And when I click the row with 'UK' as country, it will dru to UK tab?
The only common columns between the USA , UK Details Tables are 'Country' and Day.
I am not sure how can I setup the drill thru so that it can go to different tab with the same column but different values?

How to output query value only once?

I have query that returns four columns. One of the columns can have the same value for multiple records. I would like to output that value only once. Here is example of the data:
Rec ID Name Color Year
45 Nick Green 2018
34 Mike Red 2018
37 Nick Blue 2019
44 John Pink 2019
23 Jimmy Orange 2019
I uses this code to output the values:
<cfoutput>
<cfloop query="myQuery">
<cfif fiscal_year gt 1991>
<tr>
<td colspan="4"><a href="new_page.cfm?year=#year#>View All</a></td>
</tr>
</cfif>
<tr>
<td>#rec_id#</td>
<td>#name#</td>
<td>#color#</td>
<td>#year#</td>
</tr>
</cfloop>
</cfoutput>
My output looks like this:
View All
45 Nick Green 2018
View All
34 Mike Red 2018
View All
37 Nick Blue 2019
View All
44 John Pink 2019
View All
23 Jimmy Orange 2019
Instead I would like my output too look like this:
View All
45 Nick Green 2018
34 Mike Red 2018
View All
37 Nick Blue 2019
44 John Pink 2019
23 Jimmy Orange 2019
What is the easiest way to achieve this?
The <cfoutput> tag has a group attribute that allows you to group your query data by a column. You can nest the grouped data in another <cfoutput> tag and even group by multiple columns. It should look something like this:
<cfoutput query="myQuery" group="year">
<cfif fiscal_year gt 1991>
<tr>
<td colspan="4">View All</td>
</tr>
</cfif>
<cfoutput>
<tr>
<td>#rec_id#</td>
<td>#name#</td>
<td>#color#</td>
<td>#year#</td>
</tr>
</cfoutput>
</cfoutput>

How to append current and previous sessions side by side filtered by two independent slicers

Objective: I would like obtain the difference between current and previous sessions based on date slicers
I want the output to be 4 columns as such:
Date
Current Sessions (see measure below)
Previous Sessions (see measure below)
Difference (no measure calculated yet).
Situation:
I currently have two measures
Current Sessions: SUM(Sales[Sessions])
Previous Sessions (thanks to #Alexis Olson):
VAR datediffs = DATEDIFF(
CALCULATE (MAX ( 'Date'[Date] ) ),
CALCULATE (MAX ('Previous Date'[Date])),
DAY
)
RETURN
CALCULATE(SUM(Sales[Sessions]),
USERELATIONSHIP('Previous Date'[Date],'Date'[Date]),
DATEADD('Date'[Date],datediffs,DAY)
)
I have three tables.
Sales
Date
Previous Date (carbon copy of Date table)
My previous date table is 1:1 inactive relationship with the Date table. Date table is 1 to many active relationship
with my Sales Table.
I have two slicers at all time comparing the same amount of days from different time periods (e.g. Jan 1th to Jan 7th 2019 vs Dec 25st to Dec 31th 2019)
If i put current sessions, previous sessions and a date column from any of the three tables
+----------+------------------+-------------------+------------+
| date | current sessions | previous sessions | difference |
+----------+------------------+-------------------+------------+
| Jan 8th | 10000 | 70000 | 3000 |
| Jan 9th | 20000 | 10000 | 10000 |
| Jan 10th | 15000 | 16000 | -1000 |
| Jan 11th | 14000 | 12000 | 2000 |
| Jan 12th | 12000 | 14000 | -2000 |
| Jan 13th | 11000 | 16000 | -5000 |
| Jan 14th | 15000 | 18000 | -3000 |
+----------+------------------+-------------------+------------+
When I put the Sessions date on the table along with sessions and previous sessions, I get the sessions amounts right for each day but the previous session amounts doesn't calculate correctly I assume because its being filtered by the date rows.
How can I override that table filter and force it to get the exact previous sessions amounts? Basically have both results appended to each other.The following shows my problem. the previous session is the same on each day and is basically the amount of dec 31st jan 2018 because the max date is different for each row but I want it to be based on the slicer.
The mistake came in the first part of the VAR Datediffs variable within the previous session formula:
CALCULATE(LASTDATE('Date'[Date]),ALLSELECTED('Date'))
This forces to always calculate the last day for each row and overrides the date value in each row.

Select most recent rows in Django ORM with grouping

We have a system written in Django to track patients recruited to clinical trials.
Spread sheets are used to record the number of patients recruited each month throughout a financial year; so the sheet only contains 12 months of data even though a study may run for years.
There is a table in a django database in to which the spread sheets are imported each month. The data includes the month/year, a count of patients, and some other fields. Each import will include all the previous months data; we need this to make sure no data has been changed on the import sheet since the last import.
For example, the import table containing two imports (the first up to January and the second up to February) would look like this:
id | study_id | data_date | patient_count | [other fields] -->
100 5456 2016-04-01 10 ...
101 5456 2016-05-01 8 ...
102 5456 2016-06-01 5 ...
... all months in between ...
109 5456 2016-01-01 12 ...
110 5456 2016-02-01 NULL ...
111 5456 2016-03-01 NULL ...
112 5456 2016-04-01 10 ...
113 5456 2016-05-01 8 ...
114 5456 2016-06-01 5 ...
... all months in between ...
121 5456 2016-01-01 12 ...
122 5456 2016-02-01 6 ...
123 5456 2016-03-01 NULL ...
The other fields includes a foreign key to another table containing the actual study identification number (iras_number), so I have to join to that to select the rows for a particular study.
I want the most recent values of data_date and patient_count for a study, which may span more than one financial year, so I tried this query (iras_number is passed to the function performing this query):
totals = ImportStudyData.objects.values('data_date', 'patient_count') \
.filter(import_study__iras_number=iras_number) \
.annotate(max_id=Max('id')).order_by()
However, this produces a SQL query which includes patient_count in the GROUP BY, resulting in duplicate rows:
data_date | patient_count | max_id
2016-04-01 10 100
2016-04-01 10 112
2016-05-01 8 101
2016-05-01 8 113
...
2016-01-01 12 109
2016-01-01 12 121
2016-02-01 NULL 110
2016-02-01 6 122
How do I select the most recent data_date and patient_count from the table using the ORM?
If I were writing the SQL I would do an inner select of the max(id) grouped by data_date and then use that to join, or use an IN query, to select the fields I require from the table; such as:
SELECT data_date, patient_count
FROM importstudydata
WHERE id IN (
SELECT MAX(id) AS "max_id"
FROM importstudydata INNER JOIN importstudy
ON importstudydata.import_study_id = importstudy.id
WHERE importstudy.iras_number = 5456
GROUP BY importstudydata.data_date
)
ORDER BY data_date ASC
I've tried to create an inner select to replicate the SQL query, however the inner select returns more than one field (column) a causes the query to fail:
totals = ImportStudyData.objects.values('data_date', 'patient_count') \
.filter(id__in=ImportStudyData.objects.values('data_date') \
.filter(import_study__iras_number=iras_number) \
.annotate(max_data_id=Max('id'))
Now I can't get the inner select to return only the max(id) grouped by `data_date' and for it to be performed in a single SQL query.
For now I'm splitting the query in to a number of steps to get the result I want.
First I query for the most recent id of all rows related to the study
id_qry = ImportStudyData.objects.values('data_date')\
.filter(import_study__iras_number=iras_number)\
.annotate(max_id=Max('id'))
To get a list of just the numbers, stripping out the date, I use list comprehension:
id_list = [x['max_id'] for x in id_qry]
This list is then used as a filter for the final query to get the number of patients
totals = ImportStudyData.objects.values('data_date', 'patient_count') \
.filter(id__in=id_list)
It hits the database twice, and is computationally more expensive, but for now it works and I need to move on.
I'll come back to this problem at a later date.
Use: distinct=True
totals = ImportStudyData.objects.values('data_date', 'patient_count').filter(import_study__iras_number=iras_number).annotate(max_id=Max('id')).order_by('data_date').distinct()

Django ORM: Joining a table to itself and aggregating

I have a table in my Django app, UserMonthScores, where every user has a "score" for every month. So, it looks like
userid | month | year | score
-------+-------+------+------
sil | 9 | 2014 | 20
sil | 8 | 2014 | 20
sil | 7 | 2014 | 20
other | 9 | 2014 | 100
other | 8 | 2014 | 1
I'd like to work out which position a specific user was in, for each month, in the ranking table. So in the above, if I ask for monthly ranking positions for user "sil", per month, I should get a response which looks like
month | year | rank
------+------+-----
9 2014 2 # in second position behind user "other" who scored 100
8 2014 1 # in first position ahead user "other" who scored 1
7 2014 1 # in first position because no-one else scored anything!
The way I'd do this in SQL is to join the table to itself on month/year, and select rows where the second table was for the specific user and the first table had a larger score than the second table, group by month/year, and select the count of rows per month/year. That is:
select u1.month,u1.year,count(*) from UserMonthScores u1
inner join UserMonthScores u2
on u1.month=u2.month and u1.year=u2.year
and u2.userid = 'sil' and u1.score >= u2.score
group by u1.year, u1.month;
That works excellently. However, I do not understand how to do this query using the Django ORM. There are other questions about joining a table to itself, but they don't seem to cover this use case.