Count number of groups that match SQL condition - sql

I have a Crystal Report with all my records grouped by customer. I also have an SQL command that checks whether the customer has had services, if so then 1 else 0.
When I place this into my report in the group header/footer then as expected I get a 1 or 0 depending on whether the customer has had a service in the time frame or not. All good so far..
Now the problem is here, I need to count the number of groups that have a 1 and place this total in the report header. Whenever I use a sum or a count though I get really high numbers, because even though its showing a 1 or 0, it's instead counting the number of details in the group.
Any thoughts on how to best approach this?

Create a Running total to SUM that formula.
Set the evaluation to once for each group change.

Related

Summing the value of the aggregate function Last()

enter image description hereI am trying to design a report in ssrs that returns the last opening stock for each product in the dataset. To achieve this I used
=Last(Fields!CustProdAdj_new_openingstockValue.Value).
This works fine. But where I encountered a problem is in getting the sum of all the opening stock for each product. I tried using
=sum(Last(Fields!CustProdAdj_new_openingstockValue.Value))
but I got the error message
[Error on Preview]
Please is there another way to go about this
I have tried using aggregate(), runningValue(), to no avail
This is the dataset
This is the report layout
On previewing having used max()
This is probably easier to do directly in the dataset query but assuming you cannot change that, then this should work...
This assumes your data is ordered by the CustProdAdj_createdon column and that this is a date, datetime or some other ordered value, change this bit if required.
=SUM(
IIF(Fields!CustProdAdj_createdon.Value = Max(Fields!CustProdAdj_createdon.Value, "MyRowGroupNameHere"),
CustProdAdj_new_openingstockValue,
0)
)
Change the MyRowGroupNameHere to be the name of the rowgroup spelled exactly as it is in the rowgroup panel below the main design panel. Case sensitive and include quotes.
What this does is, for each row within the rowgroup "MyRowGroupNameHere", compare the CustProdAdj_createdon date to the max CustProdAdj_createdon across the rowgroup. . If it is the same then return the CustProdAdj_new_openingstockValue else return 0.
This will return the value required on only 1 record within the group.
For example, if you had 1 row per day then only on the last day of the month would a value be returned other than 0 because only the date of the last record would match the maximum date within the group.
Then it simply sums the results of this up.

SSRS Group Subtotal and Total

I'm trying to get the subtotal for WO Total Cost but where that WO Number appears multiple time in the Division group I don't want to add them together. I only want the cost for WO Total to appear once in the report for each division. As it is now the work order number 40321 has a WO Total Cost of $362.24 and because it appears for each laborer it is added in the total represented in the green total line. Can anyone tell me how to prevent summing WO Total Cost where the WO Number appears more than once in a Division group?
Thanks for the reply Alan. Here is a screenshot of the design view in Report Builder. So, what I'd like to see is a summing of only the WO Total cost once for each report for each WO Number. The WO Total cost represents all costs (Equipment, Labor, and Material) for a given work order. So, at it stands now, the report is summing work order 40321 three times and giving us an incorrect total.
I've grouped the report by Division so we can see costs for a particular division for a given time period. I've also grouped the report by Person Labor so we can see how much a particular laborer is costing us for a given period of time.
What I don't know is how to do is prevent the report from summing the WO Total cost where the WO Number appears multiple times in the results for a given division.
I'm not sure you will be able to do this directly in the report design without some convoluted custom code. Other people maybe will have more inspiration but I can't see how to do it. You cannot even look for unique values to sum as it's possible that 2 WO numbers could have the same cost.
Without knowing what your dataset/database tables look like I can't offer much more but here are some approaches I might consider if I was doing this, both mean doing the work in SQL.
**Option 1 **
I would consider calculating this in your query in a new column which should be fairly simple. Each row would end up with the total amount for the AssetGroup. So, if you had 20 records for AssetGroup A and 10 records for AssetGroup B than all the 'A' records would have the same 'AssetGroupTotalWOAmount' value and all the 'B' records would have the same 'AssetGroupTotalWOAmount' value.
Then in your report you can simply use =FIRST(Fields!AssetGroupTotalWOAmount.Value) to get the correct number. This will get the first value within the scope of the expression, so your case within the ASSETGROUP row group.
**Option 2 **
Create a separate dataset with just the amounts you need, with a single record per AssetGroup . So probably just two columns, AssetGroup and AssetGroupTotalWOAmount
In the report you could then use a LOOKUP to get the correct value.

Microsoft Access query to retrieve random transactions with seemingly impossible criteria

I was asked to assist with developing a report to retrieve a 25% sample of random transactions within a specific date range. I am not a programmer but I was able to devise the following fairly quickly:
SELECT TOP 25 PERCENT account.CID, account.ACCT, account.NAME, log.DATE, log.action_txt, log.field_nm, log.from_data, log.to_data, log.tran_id, log.init
FROM account INNER JOIN log ON account.ACCT = log.ACCT
GROUP BY account.CID, account.ACCT, account.NAME, log.DATE, log.action_txt, log.field_nm, log.from_data, log.to_data, log.tran_id, log.init
HAVING (((log.DATE) Between #2/7/2018# And #6/15/2018#) AND ((log.action_txt)="mod" Or (log.action_txt)="del") AND ((log.init)="J1X"
ORDER BY log.tran_dt
This returns 25% of the records within the date range. Each record row is unique but each account number potentially has multiple records on each day. In some cases the records have the same date and tran_id as well.
Upon further discussion with the requester, he actually wants to see all of the transactions for 25% of the accounts that have activity on each day within the date range. Thus if there were 100 accounts on 3/1/2018 with records in this table, he wants to see all of the transactions for 25 of those accounts; if there were 60 accounts on 3/2/2018 with records in this table, he wants to see all of the transactions for 15 of those accounts; and so on.
I was thinking that an Access module would work best in this scenario as I believe there are multiple parts to this. I figured that I need a function to loop through the date range and for each day:
1. Count the account numbers only one time
2. Return all of the transactions for 25% of the total accounts
But as I mentioned, I am not a programmer and I am exhausted from searching possible solutions for the many parts.
I think the key to your question is that you only really need a pseudo random selection of results for your report. So you can force the Random number generator to reorder your results based on a value in the record and the current time.
Something like this should work - I assume your actiontxt field is a text field and pull out the length of each field and apply that to current date/time to create a pseudo random number that can be sorted.
All I really do is change your ORDER BY line
See if this works for you
SELECT TOP 25 PERCENT
account.CID, account.ACCT, account.NAME, log.DATE, log.action_txt, log.field_nm, log.from_data,
log.to_data, log.tran_id, log.init
FROM account
INNER JOIN log ON account.ACCT = log.ACCT
GROUP BY account.CID, account.ACCT, account.NAME, log.DATE, log.action_txt, log.field_nm, log.from_data, log.to_data, log.tran_id, log.init
HAVING (((log.DATE) Between #2/7/2018# And #6/15/2018#) AND ((log.action_txt)="mod" Or (log.action_txt)="del") AND ((log.init)="J1X"
ORDER BY Rnd(CLng(Now()*Len(log.action_txt))-(Now()*Len(log.action_txt)));
Modified similar idea from other StackOverflow question and response

How to display n number of rows between two groups in SSRS 2010

Using SSRS 2010
I have Two groups YearMonth and Insured. I need to display only 50 records per page based on a group "Insured". So I have created parent group "GroupPageBreakOnly" and used this expression =CEILING(RowNumber(Nothing)/50).
I ensured that the Page Break at end is checked so that individual groups appear in individual page.
As a result the first page displays 31 rows, the second one 50 rows, and the third one 9 rows.
I tried to specify data region "Insured"
=CEILING(RowNumber("Insured")/50),
but it gives me an error:
...the value of the scope parameter of RowNumber must equal the name of the group directly containing the current group.
What am I missing here?
Unless you need this report to do other things, I would apply the grouping and aggregation in the Dataset itself which is generally a lot more efficient anyway.
Have you tried using ROW_NUMBER() OVER (PARTITION BY YearMonth, Insured ORDER BY YearMonth, Insured) to give the number of rows, perhaps even throwing in a % 50 at the end to see which group of 50 it fell into?
This can then be grouped on in your report.

Ms Access : Query to work out percentage

I have a database which currently records the amount of times someone does a certain procedure and they scores they have received. The scoring is done by select a value of either N, B or C.
I currently have written a query which will count the total number of times a procedure is done and the amount of times each score is received.
Here is the result of the query (original: http://www.flickr.com/photos/mattcripps/6673555339/)
and here is the code
TRANSFORM Count(ed.[Entry ID]) AS [CountOfEntry ID]
SELECT ap.AdultProcedureName, ap.Target, Count(ed.[Entry ID]) AS [Total Of Entry ID]
FROM tblAdultProcedures AS ap LEFT JOIN tblEntryData AS ed ON ap.AdultProcedureName = ed.[Adult Procedure]
GROUP BY ap.AdultProcedureName, ap.Target
PIVOT ed.Grade;
If a score of N or B is given that is deemed below standard and C is deemed at standard. Is there a way I can add something to my query which will show me in percentage how many of the procedures we at standard and how many below?
I really cant get my head round this so any help would be great.
Thanks in advance
UPDATE TabProd
SET PrecProd = (PrecProd * 1.1)
WHERE Código IN (1,2,3,4)
I did something very similar to this on a pretty large scale.
My issue was the need to be able to run queries over specific (but user variable) timeframes and output similar percentage of total results in a report.
I won't get into the date issue but my solution was to run the "sum" function on the total line on my specific reject criteria to get totals of the rejects then use a divide expression to create a new column element (defined expression) in the same query pulling from the joined table of "Total net production" - joined by a common reference - job ID.
For your case it sounds like you want to sum the two failure types - which you would simply add defined expressions dividing your total instances into your various failure modes and formatting in your output report as percents. To finish the data portion of your report you then need a third expression defining your "non-fail percent" - which would be 1.0 - N/total - B/total - both of which you will have previously defined in the query to determine the N and B failure rates.
Then its a matter of pulling that information into your report and formatting. It definitely CAN be done.
Hope this helps.