I have records like these:
GR_ID, “debit”, “credit”, “balance”
Example:
1, 100.00, 0.00, 100.00
1, 200.00, 50.00, 150.00
1, 0.00, 50.00, -50.00
2, 300.00, 0.00, 300.00
2, 0.00, 300.00, -300.00
3, 200.00, 0.00, 200.00
3, 100.00, 200.00, -100.00
I need to print only group totals but only if group total for “balance”<>0.00 and I need to count them.
The group with GR_ID=2 should not be visible because the total balance=0 and result look like this:
1, 300.00, 100.00, 200.00
3, 300.00, 200.00, 100.00
Count: 2 : Total Debit: 600.00, Total Credit: 300.00; Total Balance: 300.00
I cannot find a way to get that COUNT in SSRS.
I know I can do it by filtering records using SQL and using SSRS CountDistinct() function, but I’m interested in an SSRS solution.
Finally found the solution! The "Count" field in the "TOTALS" row should be calculated using: =CountDistinct(iif(Sum(Fields!Balance.Value, "Gr_ID")=0,nothing,Fields!Gr_ID.Value))
I tried various methods and the only way I was able to get it to work was with a Count of Report Items in the Header for the GR_ID cell in the table.
=COUNT(ReportItems!GR_ID.Value)
I thought I could get it in the table by grouping and using the same grouping filter but I was still getting 3 instead of two. Apparently it counts BEFORE the filter is applied.
Related
If there is a data shown as below;
id cnt_stamp
1 999
2 3
3 9
4 3
5 1000
6 30
If an input is (4, 1, 2, 3) in this order, I would like to get only (3, 999, 3, 9).
To achieve this, I created a SQL
SELECT `cnt_stamp`
FROM `stm_events`
ORDER BY FIELD(`id`, 4, 1, 2, 3);
But it returns (1000, 30, 3, 999, 3, 9) instead. How should I fix my SQL to achieve my goal? Thank you for taking your time.
FIELD will assign NULL to any non matching id, and nulls sort first by default in MySQL. If you don't want to see non matching items at all you may just add a WHERE clause:
SELECT cnt_stamp
FROM stm_events
WHERE id IN (1, 2, 3, 4)
ORDER BY FIELD(id, 4, 1, 2, 3);
If you want to see all your data, with non matching id values at the end, then reverse the order of the field list and sort descending:
SELECT cnt_stamp
FROM stm_events
ORDER BY FIELD(id, 3, 2, 1, 4) DESC;
Demo
Use COALESCE function :
SELECT `cnt_stamp`
FROM `stm_events`
ORDER BY COALESCE(`id`,FIELD(`id`,4,1,2,3));
SQL Fiddle Demo
I have details, subtotals and totals.
When I put avg function in totals line I have avg of every row.
I need avg of subtotals
How to do it?
week1
day1..... 2
day3..... 3
day4..... 4
day6..... 2
total.... 11 sum()
week2
day1..... 3
day2..... 2
total..... 5 sum()
Total
........... 16 sum() OK
............ 2,66666 avg() here should be (11+5)/2 =8
Result after implementing solution
I created a dataset to replicate your sample data as follows:
DECLARE #t TABLE (week int, day int, amount int)
INSERT INTO #t VALUES
(1, 1, 2),
(1, 3, 3),
(1, 4, 4),
(1, 6, 2),
(2, 1, 3),
(2, 2, 2)
SELECT * FROM #t
I then built a simple tablix as you had done (more or less)
I included the incorrect results you had for illustration and then added a new expression to calculate this correctly.
The result looks like this
You can ignore the other datasets, this is just a report I use for testing. Only dataset3 is used here.
The expression used was this
=Sum(Fields!amount.Value) / CountDistinct(Fields!week.Value)
You'll just need to edit this to match your field names. It basically just sums all the detail amounts then divides by the number of distinct weeks in the dataset.
I'm trying to count all instances where a group of data has one or more fail.
I'm also Finding it very difficult to build this question so I'm hoping that showing an example will do the trick in explaining what I'm trying to achieve.
Sample data:
INSERT INTO test.answers (id, result_id, fail_all, fail_group) VALUES
(1,1,0,1), (2,1,0,1), (3,1,0,1), (4,1,0,0),
(5,2,1,0), (6,2,0,0), (7,2,1,0), (8,2,1,0), (9,2,1,0),
(10,3,0,1), (11,3,1,1), (12,3,0,1), (13,3,0,1), (14,3,0,1),
(15,4,0,0), (16,4,0,0), (17,4,0,1), (18,4,0,1), (19,4,0,0), (20,4,0,1),
(21,5,1,0), (22,5,0,1), (23,5,1,1), (24,5,0,1), (25,5,1,0), (26,5,0,1);
INSERT INTO test.results (id,team_id) VALUES
(1,1), (2,1), (3,1), (4,2), (5,2);
I then run the following query:
SELECT
COUNT(IF(a.fail_all = 1,1,NULL)) AS count_fail_all,
COUNT(IF(a.fail_group = 1,1,NULL)) AS count_fail_group,
a.result_id
FROM test.answers AS a
GROUP BY a.result_id
Result:
count_fail_all, count_fail_group, result_id
0, 3, 1
4, 0, 2
1, 5, 3
0, 3, 4
3, 4, 5
I need to create a query that groups by team_id and counts how many fails there are per result. If a result has more than one fail, then that overall result is a fail. thereby if the count of results in the above query is 3 (example: first result) then it should only be counted as one. The fail_group can be ignored for now as I believe that the same solution for fail_all will work for fail_group.
The result I hope for is:
team_id, amount_of_fails, amount_of_fails_per_group
1, 2, 2
2, 1, 2
I hope someone might be able to help me create the query that I need? I'm not even sure how to start.
Let me know if there is anything that I can do to adjust the query as I know it's not very well asked?
Thanks!
If you want that output. Maybe something like this:
SELECT
r.team_id,
SUM(IF(a.fail_all = 1,1,0)) AS amount_fail_all,
SUM(IF(a.fail_group = 1,1,0)) AS amount_fail_group
FROM answers AS a
JOIN results AS r on r.id=a.result_id
GROUP BY r.team_id
This will get you this output:
team_id amount_fail_all amount_fail_group
1 2 2
2 1 2
I have the following table:
OrderHasItem
OrderTable_idOrder
Item_idItem
quantity
SELECT Item_idItem, COUNT(Item_idItem)+quantity
FROM OrderHasItem
GROUP BY Item_idItem
I want to count how many of each item, but also take into account the quantity of each entry.
The problem is when I tried it with this data I got really strange results:
Data:
OrderTable_idOrder, Item_idItem, quantity
1, 1, 1
2, 1, 2
34, 4, 2
43, 4, 1
77, 2, 1
Result:
Item_idItem, COUNT(Item_idItem)+quantity
1, 3
2, 2
4, 4
I don't really understand how it got what it gave me so I'm not quite sure how to proceed. Thank you for the help.
Here is the target Data:
Item_idItem, quantity
1, 3
4, 3
2, 1
SELECT Item_idItem, COUNT(Item_idItem), COUNT(Item_idItem)+SUM(quantity)
FROM OrderHasItem
GROUP BY Item_idItem
I think it should work, if sum of quantity and number of items is required.
Or
SELECT Item_idItem, SUM(quantity)
FROM OrderHasItem
GROUP BY Item_idItem
if only sum of quantity is required.
Because quantity is not part of the GROUP BY clause it can not be selected reliably. So MySQL is returning more or less arbitrary data.
Zohaib has a query that you should use. I wanted to provide the explanation.
I have the following query
SELECT * FROM invoice WHERE invoice_id IN (13, 15, 9, 27)
My result is:
invoice_id | invoice_number | ...
------------------------------------
9 | 201006003 |
13 | 201006020 |
15 | 201006022 |
27 | 201006035 |
which is the result set I want except that is ordered by the invoice_id (which is an autoincrement value).
Now I want the result in the order I specified in my query (13, 15, ...). Is there a way to achive that?
The background is that I have a DataTable bound to a DataGridView. The user can filter and sort the result but if he want's to print the result I don't use the DataTable for printing because it only contains the most important columns and instead I pull the whole records from the database and pass it to my printing control.
I also tried to extend the existing DataTable with the missing results but that seems to slower than using the IN (...) query.
It's ugly, but you could do:
ORDER BY CASE invoice_id WHEN 13 THEN 0 WHEN 15 THEN 1 WHEN 9 THEN 2 WHEN 27 THEN 3 ELSE 4 END
Actually, there's the FIELD function:
ORDER BY FIELD(invoice_id, 13, 15, 9, 27)
The FIELD function returns the position of the first argument in the list of the rest.
Or, if you're generating it dynamically, you could do:
WHERE invoice_id IN ({list}) ORDER BY FIND_IN_SET(invoice_id, '{list}')
You want the FIELD order by parameter.
SELECT * FROM invoice WHERE invoice_id IN (13, 15, 9, 27) ORDER BY FIELD (invoice_id, 13, 15, 9, 27)