MSSQL Running IN OUT count Record - sql-server-2008-r2

I'm trying to create a report that counts how many people are signed into a section or signed out of a section each day.
select inout.name_id, nametable.name, inout.datetime, inout.status, inout.section
from inout
join nametable on inout.name_id = nametable.name_id
where inout.date between '1/1/2016' and '1/3/2016'
order by inout.date
Sample Data:
Datetime Name Section Status
1/1/2016 1:34:56 John A IN
1/1/2016 4:11:11 Steve A OUT
1/1/2016 18:20:20 Bill A IN
1/2/2016 13:13:13 John A OUT
1/2/2016 12:12:12 Ben A IN
1/3/2016 1:01:01 JIM A IN
Then final result:
Date # of people in section
1/1/2016 2
1/2/2016 2
1/3/2016 3
EDIT: Was thinking IN status would give +1 to a running count and OUT would give -1, so if a person came into work or left work for the day i could see how many people are working in a specific section each day, I'm not quite sure how to show for the people that were there before that date range either though.

This is not an answer, but may help others to visualize and may inspire you (but i think you did try it):
Use the query you have posted.
In Crystal designer, create a group by the column 'Datetime'.
Create a formula: if {inout.status} = "IN" then 1 else -1. Let's say it's name is #Quantifier.
Create a running total field: field to summarize = #Quantifier, type of summary = sum, evaluate for each record, reset never. Let's call it #NumberOfPeopleInSection_CloseButNoCigar.
Put the running total field in the group footer.
The result, as you said you expected, is not really what you want, because there is this "Steve out" without a previous "Steve in". So it still needs a trick.
To turn it in a solution, you may try another query to get the initial count. Try something to:
select sum(case when status='IN' then 1 else -1 end)
from inout
join nametable on inout.name_id = nametable.name_id
where inout.datetime < '2016-1-1'
Then consider this initial value in the final result.

Related

Count records as proportion of total in Tableau

I have data with sales, details including gender, location, date, etc. There's one row for each sale, so the total number of sales is a count of the rows.
customerid sale_date regionid studentid sex
18761372 01/09/2016 AFB07458 C2F815C6 1
18761372 01/09/2016 AFB07459 206AA234 0
07189635 01/09/2016 AFB07460 F218C8F1 1
07189635 01/09/2016 AFB07461 F021CD27 0
07189635 01/09/2016 AFB07462 E6145555 1
I'm trying to produce a line graph that shows number of sales by month, split by male and female. However male sales are ~5million for the year, and female are ~13 million. So it's hard to compare whether there's any difference in how the sales vary by month (i.e. whether males are proportionately more likely to buy in september).
I manage to get this to work by creating the following calculated field:
If [sex] = 0
THEN 1/5000000
ELSE 1/13000000
END
This does what I need, but it's not a great solution as it involves me manually checking what each total is and writing it down (admittedly not much of an issue with sex, but if I'm doing it on something with multiple fields then it would become very time consuming.
Is there a way of doing this more elegantly? I tried to use sums and case statements in there but I got "cannot mix aggregate and non-aggregate arguments".
I would approach this with a calculated field that shows a percent of total, by month.
Right click on your pill, select 'Quick Table Calculation' and choose 'Percent of Total'.
That way, instead of looking at a count of orders where one group always appears greater, you are looking at their share which will be more uniform.
Maybe I'm understanding this wrong but it seems fairly straight forward. Using the sample data provided I just:
Add the date field pill "sale date" to the column shelf(set it to pull back the month)
Drag the "number of records" to the rows shelf(automatically sum the results)
In the "Show Me" section in the top right select line chart.
Drag the sex pill onto the color card, this will break the line into to two, one for male the other for female(you may have to convert the sex pill to a dimension, to do this right click the pill and select "convert to dimension")
Let me know if this solves it for you?

Distinct count not giving expected result

I have fields for customer names, customer account numbers, date of previous orders, and order values. I want an alert to show which customers have ordered at least three times over the reporting period. We can have more than one invoice for a given delivery. The main report looks like:
Account Number Acct Name Order Date Order Total
1001 Fred Smith 1/2/2016 £1.06
1001 Fred Smith 1/2/2016 £2.34
1001 Fred Smith 8/2/2016 £5.42
2001 Aled Jones 1/2/2016 £2.90
2001 Aled Jones 8/2/2016 £3.45
I've tried concatenating the account number and order date in a column via function and running an alert based on its distinct count. (After converting the date to a string.) But it still doesn't generate a list of distinct items.
What I currently have in the final column is a function that concatenates the date and account number to give a unique field with {ORDR.CardCode} + Cstr({ORDR.DocDate}) - Which returns:
1001 1/2/2016
1001 1/2/2016
1001 8/2/2016
2001 1/2/2016
2001 8/2/2016
I want to generate alert based on this field via the distinct count function and a report of any customer who ordered at least twice in the report period.
DistinctCount ({#Concat code and date}) >2
Even after this I still don't get what I need. How can I get a list of every customer who has ordered in the report period for at least two times?
You have travelled half way... to achive continue from here.
USe the created formula {ORDR.CardCode} + Cstr({ORDR.DocDate}) to create a group.
place same formula in details and take count of the records in group footer
Now create a alert on group footer summation to get the desired result
Edit........
Since you need customer then add customer in concat fornula and then create group.
acctnamr+{ORDR.CardCode} + Cstr({ORDR.DocDate})
Now place the customer name in group header and follow process as explained above

Crystal Reports 2013: Filter shown records

I have a report with the following structure:
<sales person>
<appointment date>
<appointment status>
Now, the appointment status can be either 'kept', 'cancelled by customer', or 'cancelled by sales person' (for simplicity). I want to calculate the percentage that were cancelled. To calculate that, I will need to select all of the appointments, and divide the number that was not kept by the total number. Okay, I get that.
Now, the trouble is that I also want to show the appointments that were cancelled in my report, and exclude the appointments that were kept. In other words, I pretty much just want to skip printing these irrelevant records. How can I accomplish this?
formula1
If {<appointment status>} = "kept" then 1 else 0
That will give you a count which you can use to get your percentage for sales person.
if sum({#formula1},{<sales person>}) > 0
then sum({#formula1},{<sales person>}) % count({<appointment status>} ,{<sales person>})
else 0
Then suppress the details with a formula like this
{#formula1} = 1

Return 0 value if Details duplicated

I need your help in creating crystal report.
I have a formula in details section that computes working time.
How do I make the value return 0 if it is duplicated?
Here's the scenario
Name Time (Hours:Minutes)
John 1:20
........ 3:30
........ 3:30
Total Hours -> ?
My problem is I dont want to use the duplicated values (3:30) like shown above. I want a total hours for 4:50.
you have two options:
check the option in Database tab.. Select Distinct Records so that duplicate records will be eliminated.
If you don't want to use the first option then to calculate use Running Total so that you sum only those that are distinct...
Create running total something like Do sum only after change of time value
You can use the function "previous" to compare the current value with the previous value, but it works only with fields.
But i am not sure if i understood, you may be more precise about your question.
1) make a formula called "hours" or some other name
if not isnull(previous({Result.Time}) and {Result.Time} = previous({Result.Time}
then 0
else {Result.Time} /* you have to assure the same return type */
2) let the "total hours" be a sum of the formula "hours"
Note that it will work only if the rows are ordered by hours.
The result is the same of using a running total fields as purposed by Siva.

how do i subtotal on groups based on a condition

My report is grouped on clinic, staffname with subtotals by clinic. I need to count patients by staff where they had more than 1 admit date. I can get the correct grand total, but on the detail and subtotals, it is a progressive number.
Here's what I want
clinic1
staffname1 10
staffname2 95
subtotal 105
clinic2
staffname3 6
subtotal 6
grand total 111
Here is what I get:
clinic1
staffname1 10
staffname2 105
subtotal 105
clinic2
staffname3 111
subtotal 111
grand total 111
A lot of this may depend on the structure of your data, e.g., what is in your "detail" level. I am also assuming that you want to count of how many patients had more than one admit date, not the total number of admits for patients with multiple admits. Given that, and assuming a patient will only appear once per admit date, then this should work:
Group by patient also, so it's clinic -> staff -> patient, but suppress that group.
Create a formula to count if the number of records in each patient group is more than 1, something like this: if count({patient},{patient}) > 1 then 1 else 0
Take the formula you just created, and use it to make a summary field wherever you want a total, e.g., in the staff head it will give you a count or that staff member, in clinic it will do so for clinic, etc.
Something else to consider: I'm guessing this could be intended to gauge staff on their quality by seeing how many patients had to seek additional treatment. Even if that's not the full intent, whatever this is being used for could be skewed by staff encountering more/fewer patients. For example, staff that have 10 readmits out of 100 visits would look worse than someone who only had 5 readmits, but had also only seen 20 patients.
So: Along with the metric on which you are requesting information, I would also add a ratio metric. In the staff header, this would be straightforward: count({patients}) / distinctcount({patients}) which will give you the ratio of distinct to repeat visits. Keep in mind also that this could skew high for a staff member that had, for example, 50 patients, but one of them came back a dozen times.
To get the count of the fields that has Greater than 1.
Assuming the count field is a database field and value coming directly from database
Create a formula #Count and write below code. Place the forumla in details.
:
if(databaswefield.count > 1)
then 1
else 0
Now take the summary of the #count formula in required group footers.
Let me know if you are looking for something different.
Edit..............................................................
If I got your comment correctly.
Though you take distinct count you can use that in your calculations by storing the value in a shared variable. Something like below and you can retrive value from that variable
Shared Numbervar count;
count:=distinctcount(patientid)