I have a matrix which has starts out fine
| TType | Sept21 | Oct21 |
| ----- | ------ | ----- |
| DT | 50 | 29 |
| VT | 20 | 30 |
| AT | 10 | 11 |
| Total | 80 | 70 |
The DT/VT/AT is a row group, and the month columns is a column group and the values are SUM(Volume). The total is an ungrouped row that just sums it all. This all works ok.
However, what I want to add is extra rows to the same matrix with a percentage of what each TType is of the total. This would give me something that looks like this
| TType | Sept21 | Oct21 |
| ----- | ------ | ----- |
| DT | 50 | 29 |
| VT | 20 | 30 |
| AT | 10 | 11 |
| Total | 80 | 70 |
| DT | 62.5% | 41.4% |
| VT | 25% | 42.8% |
| AT | 12.5% | 15.7% |
When I try to do this, I have tried using the expression sum(volume) / sum(volume,"groupname") where groupname is my dataset, but this doesn't take account of the column group and split out by months, nor does it take account of the filter which is to ignore a TType I don't need. I have tried making a row group, but then that splits out by the TType, but still ignores the month split.
Does anyone know how I can get this to work as expected?
Related
I have a mySQL database table containing cellphones information like this:
ID Brand Model Price Type Size
==== ===== ===== ===== ====== ====
1 Apple A71 3128 A 40
2 Samsung B7C 3128 B 20
3 Apple ZX5 3128 A 30
4 Huawei Q32 2574 B 40
5 Apple A21 2574 A 25
6 Apple A71 3369 A 30
7 Samsung A71 7413 C 40
Now I want to create another table, that would contain counts for every possible combination of the parameters.
Params Count
============================================== =======
ALL 1000000
Brand(Apple) 20000
Brand(Apple,Samsung) 40000
Brand(Apple),Model(A71) 7100
Brand(Apple),Type(A) 6000
Brand(Apple),Model(A71,B7C),Type(A,B) 7
Model(A71) 12514
Model(A71,B7C) 26584
Model(A71),Type(A) 6521
Model(A71),Type(A,B) 8958
Model(A71),Type(A,B),Size(40) 85
And so on for every possible combination. I was thinking about creating a stored procedure (that i would execute periodically), that would perform queries with every existing condition like that, but I am a little stuck on how exactly should it look like. Or is there a better way how to do this?
Edit: the reason why I want to store information like this is to be able to show number of results in filter in client application, like in the picture.
I would like to create index on the Params column to be able to get the Count number for given hash instantly, improving performance.
I also tried querying and caching the values dynamically, but I want to try this approach as well, so I can compare which one is more effective.
This is how I am calculating the counts now:
SELECT COUNT(*) FROM products;
SELECT COUNT(*) FROM products WHERE Brand IN ('Apple');
SELECT COUNT(*) FROM products WHERE Brand IN ('Apple', 'Samsung');
SELECT COUNT(*) FROM products WHERE Brand IN ('Apple') AND Model IN ('A71');
etc.
You can use a ROLLUP for this.
SELECT
model, type, size, COUNT(*)
FROM mytab
GROUP BY 1, 2, 3
WITH ROLLUP
With your sample data, we get the following:
| model | type | size | COUNT(*) |
| ----- | ---- | ---- | -------- |
| A21 | A | 25 | 1 |
| A21 | A | | 1 |
| A21 | | | 1 |
| A71 | A | 30 | 1 |
| A71 | A | 40 | 1 |
| A71 | A | | 2 |
| A71 | C | 40 | 1 |
| A71 | C | | 1 |
| A71 | | | 3 |
| B7C | B | 20 | 1 |
| B7C | B | | 1 |
| B7C | | | 1 |
| Q32 | B | 40 | 1 |
| Q32 | B | | 1 |
| Q32 | | | 1 |
| ZX5 | A | 30 | 1 |
| ZX5 | A | | 1 |
| ZX5 | | | 1 |
| | | | 7 |
The subtotals are present in the rows with null values in different columns, and the total is the last row where all group by columns are null.
I'm trying to break up a SQL table that needs to take a users name and find the unique user ID's from up to 4 systems.
The data is currently like this:
| Name | User_ID |
-----------------
| A | 10 |
| A | 110 |
| A | 1500 |
| A | 4 |
| B | 20 |
| B | 100 |
| B | 2 |
| C | 10 |
I need to pivot it around the user's name to look like this (the id's don't need to be in numerical order as the SYS#_ID for each doesn't matter):
| Name | SYS1_ID | SYS2_ID | SYS3_ID | SYS4_ID |
------------------------------------------------
| A | 4 | 10 | 110 | 1500 |
| B | 2 | 20 | 100 | NULL |
| C | 10 | NULL | NULL | NULL |
This is the code I have tried on MySQL:
PIVOT(
COUNT(User_ID)
FOR Name
IN (SYS1_ID, SYS2_ID, SYS3_ID, SYS4_ID)
)
AS PivotedUsers
ORDER BY PivotedUsers.User_Name;
I'm unsure if PIVOT works on MySQL as I keep getting an error "PIVOT unknown". Is there a way to find the values that each user has and if they do not appear in the table already add them to the next column with a max of 4 values?
I have table in mysql like
| service_code | charges | caller_number | duration | minutes |
+--------------+---------+---------------+----------+---------+
| 10 | 15 | 8281490235 | 00:00:00 | 1.0000 |
| 11 | 12 | 9961621709 | 00:00:00 | 0.0000 |
| 10 | 15 | 8281490235 | 01:00:44 | 60.7333 |
| 11 | 2 | 9744944316 | 01:00:44 | 60.7333 |
+--------------+---------+---------------+----------+---------+
from this table I want to get charges*minutes for each separate caller_number.
I have done like this
SELECT sum(charges*minutes) as cost from t8_m4_bill groupby caller_number
but I am not getting expected output. Please help?
SELECT caller_number,sum(charges*minutes) as cost
from t8_m4_bill
group by caller_number
order by caller_number
In my access database, we keep track of two sets of dates. One set is for date of membership dues payments, the other set is date of other contributions (a non-membership donation.) There are multiple dates for each person depending on number of payments made for each type.
Example:
+----+---------------+---------------+
| ID | Dues_Date | Cont_Date |
+----+---------------+---------------+
| 1 | 01/01/15 | 09/12/11 |
| | 01/01/14 | |
| | 01/01/13 | |
| 2 | 07/30/14 | 06/20/13 |
| | | 11/12/11 |
+----+---------------+---------------+
First I needed to know the most recent payment for each of the two fields so I ran a query that tells me the MAX (most recent) date for each field.
Example Query:
+----+---------------+---------------+
| ID | Max Dues_Date | Max Cont_Date |
+----+---------------+---------------+
| 1 | 01/01/15 | 09/12/11 |
| 2 | 07/30/14 | 06/20/13 |
| 3 | 02/11/13 | 09/16/14 |
| 4 | 07/30/12 | 06/20/11 |
| 5 | 12/13/13 | 11/12/14 |
+----+---------------+---------------+
Now I need a third field in the same query to compare the results of the first two fields and show which is the MAX of those two.
I have column 2 and 3 in the query; how can I take that and create column 4 in the same query?
Example Query:
+----+---------------+---------------+-----------------+
| ID | Max Dues_Date | Max Cont_Date | Max Date(DD&CD) |
+----+---------------+---------------+-----------------+
| 1 | 01/01/15 | 09/12/11 | 01/01/15 |
| 2 | 07/30/14 | 06/20/13 | 07/30/14 |
| 3 | 02/11/13 | 09/16/14 | 09/16/14 |
| 4 | 07/30/12 | 06/20/11 | 07/30/12 |
| 5 | 12/13/13 | 11/12/14 | 11/12/14 |
+----+---------------+---------------+-----------------+
Try adapting this to your own scenario:
SELECT tblTest.DueDate, tblTest.ContDate, [DueDate]-[ContDate] AS Test, IIf([Test]<0,[ContDate],[DueDate]) AS MaxRes
FROM tblTest;
"Test" finds which is the later date, ContDate or Due Date. The IIf statement selects the later date.
Does this help?
I am filtering a tablix and then grouping both rows (total of 3) and columns (total of 2), with no detail section. One row only has an applicable value for one of the grouped columns. The group by places all of the values in the correct cell. However, if I add an indicator, the row without an applicable value for the column inherits data from another row. Adding the field that the indicator is based on does not make this change. This row (and others that are working correctly) has a null value for the indicator in the first column, IE it shouldn't show up.
Dataset:
+---------+---------------+-----------+---------+
| Family | Description | Value |Indicator|
+---------+---------------+-----------+---------+
| A | Something | 5 | 2 |
| A | Another | 2 | 1 |
| B | Yearly Plans | 63 | null |
| B | Weekly Plans | 4 | 2 |
| B | Yearly Qual | .4 | 1 |
| B | Weekly Qual | .2 | 1 |
| B | Purchased % | .76 | null |
+---------+---------------+-----------+---------+
Filter tablix for Family = B
Row Group: =iif(Field!Description.Value like "Plans","Plans",iif(Field!Description.Value like "Qual","Qualifying",Field!Description.Value))
Column Group: iif(Field!Description.Value like "Yearly*","YTD","Weekly")
Result without indicators:
+---------------+------------+----------+
| Description | Weekly | YTD |
+---------------+------------+----------+
| Qualifying | .2 | .4 |
| Plans | 4 | 63 |
| Purchased % | .76 | |
+---------------+------------+----------+
Result with indicators (Based on indicator field):
+---------------+--------+---+--------+---+
| Description | Weekly | | YTD | |
+---------------+--------+---+--------+---+
| Qualifying | .2 | - | .4 | - |
| Plans | 4 | + | 63 | |
| Purchased % | .76 | | .4 | - |
+---------------+------------+--------+---+
What it SHOULD be:
+---------------+--------+---+--------+---+
| Description | Weekly | | YTD | |
+---------------+--------+---+--------+---+
| Qualifying | .2 | - | .4 | - |
| Plans | 4 | + | 63 | |
| Purchased % | .76 | | | |
+---------------+------------+--------+---+
I have patched the current problem by unioning in a Yearly Purchased % row with null values, but there must be something else going one. Is there something in how indicator's behave that would add values that aren't part of a group?
I realized the same thing, could it be a bug in SSRS indicators, as workaround, you can write an expression inside the cell to check against another cell and investigate if there is data then show the value inside the cell, if not then write nothing.
like: =iif(count(Fields!Field1.Value)>1,"",Fields!Field2.Value)