Sum Over unique identifier in Spotfire - bar-chart

I am trying to create a bar chart that sums up values in a field, but only for each unique identifier. For instance, for my data:
Condition CT_ID Enrollment Company
I 5127 24 H
J 5127 24 H
P 5127 24 H
I 5127 24 O
J 5127 24 O
P 5127 24 O
L 27668 387 C
R 27668 387 C
D 38190 650 D
Q 38190 650 D
F 38785 30 A
E 39682 30 B
M 41818 17 I
O 44093 188 G
A 54850 18 K
G 59183 F
C 59891 266 J
G 61142 48 F
H 61425 28 L
K 61449 N
A 61793 12 E
N 61793 12 E
B 61910 120 M
B 61917 120 M
B 61961 130 M
Or, since I really want to eventually summarize these data by Condition, let me just show the above data resorted by Condition instead of [CT_ID].
Condition CT_ID Enrollment Company
A 54850 18 K
A 61793 12 E
B 61910 120 M
B 61917 120 M
B 61961 130 M
C 59891 266 J
D 38190 650 D
E 39682 30 B
F 38785 30 A
G 59183 F
G 61142 48 F
H 61425 28 L
I 5127 24 H
I 5127 24 O
J 5127 24 H
J 5127 24 O
K 61449 N
L 27668 387 C
M 41818 17 I
N 61793 12 E
O 44093 188 G
P 5127 24 H
P 5127 24 O
Q 38190 650 D
R 27668 387 C
The rows are duplicated by different values in Condition and Company. CT_ID is the identifier for the record.
I want to sum up my Enrollment column, but I will be overcounting. So I thought I could create a custom expression like :
Sum(Avg([Enrollment]) OVER ([CT_ID]))
But this is somehow not a valid expression. Where am I going wrong?
For my bar chart, I'd like to have the Condition as the category axis and the Enrollment as the value axis. The below table shows how the Enrollment value should be calculated. Focus on Conditions P, I, and J.
Condition Enrollment
A 30
B 370
C 266
D 650
E 30
F 30
G 48
H 28
I 24
J 24
K
L 387
M 17
N 12
O 188
P 24
Q 650
R 387
My current solution uses a rank function and only puts the enrollment info in the first row for each unique CT_ID, but that is just plain wrong when I start filtering data. For instance, imagine in my dataset above I only had an Enrollment value for the first row of CT_ID 5127. If I filter out Condition "I" (the one in the first row), now the Enrollment value is zero!
Any help you can provide would be greatly appreciated! I'm no expert on OVER expressions, so hopefully there is an easy solution!

This is too long to comment...
Can you provide some expected results? It looks as though each CT_ID can only have one value, so SUM of them distinctively would just be any one of the values... right? If not, what determines a duplicate?
Here is how you can accomplish what you were attempting in your code above--but from your test data isn't just going to give you the value of Enrollment since there each CT_ID seemingly only has one value (duplicated).
Sum([Enrollment]) over ([CT_ID]) / Count() OVER ([CT_ID])
You could also just use First()
First([Enrollment]) OVER ([CT_ID])
EDIT
Since you have some duplicates and some not... let's just split out the duplicates into the average of the number of duplicates there are. Insert this calculated column:
Max([Enrollment]) over (Intersect([CT_ID],[Condition])) / Count([Enrollment]) over (Intersect([CT_ID],[Condition]))
Then use this column in place of Enrollment in what ever expression you want to ignore duplicates, where a duplicate is the same Condition, CT_ID, and Enrollment value.
For example... the rows for Condition = P and CT_ID = 5127 will have 12 instead of 24.

Related

Get Min and Max after summation in MySql

I have a dataset as follows,
Table_Date Description Amount
4/17/2022 A 10
4/17/2022 B 45
4/17/2022 C 34
4/17/2022 D 23
4/17/2022 E 76
4/17/2022 F 45
4/18/2022 A 23
4/18/2022 B 45
4/18/2022 C 67
4/18/2022 D 78
4/18/2022 E 98
4/18/2022 F 54
First I need to get sum of Amount for each day for last 8 days.
So I used following query.
Select Table_Date,sum(Amount) as Total_Amount from usertable where Table_Date>=DATE(NOW()-INTERVAL 8 DAY) group by Table_Date;
Result
Table_Date Total Amount
4/17/2022 233
4/18/2022 365
Now I need to get the maximum and the minimum after creating this. So I tried as follows,
select max(Total_Amount) from
(
select Table_Date,sum(Amount) as Total_Amount from usertable where Table_Date>=DATE(NOW()-INTERVAL 8 DAY) group by Table_Date
) group by Table_Date;
Seems this is not correct. Can someone show me how to get the output as follows ,
Min =233
Max=365
Note : My server do not support window functions
If I understand correctly, you might need to use aggregate function without group by
SELECT MIN(Total_Amount),
MAX(Total_Amount)
FROM (
Select Table_Date,sum(Amount) as Total_Amount
from usertable
where Table_Date>=DATE(NOW()-INTERVAL 8 DAY)
group by Table_Date
) t1

separating a table into subtables based on a value in ssrs 2008

I have a matrix in ssrs 2008 like:
id type1 type2 type3 Score
1 N H H 2
2 H N N 1
3 N L H 2
4 L L H 3
5 N L N 1
6 N N N 0
Note that the data with N are normal, all others (L and H) are anormal for me. The score is calculated by #L + #H.
What I want is to groupby this table and show like:
count:3
id type1 type2 type3
4 L L H
count:2
id type1 type2 type3
1 N H H
3 N L H
count:1
id type1 type2 type3
2 H N N
5 N L N
count:0
id type1 type2 type3
6 N N N
So there needs to be 4 different table like that in a single page. What is the best way to provide this?
Edit: It is also OK to show them in one single matrix like grouping by the mentioned way if possible.
I would advise creating a list. Then, within this list create the table you want to display. You can then group the list on Score Column (Row Group -> Group Properties -> General -> Group Expressions -> Add), which will display a separate table for each Score in your system.
The design would look like this
And then when it executes like this
You can play with look and feel, order etc, once you've got the basic structure working. Hopefully this is helpful, let me know if you have further questions

MYSQL: sum mysql rows by specific character and add the results into an equation

Please I have a single table where it has 3 columns like the below:
id value type
-----------------
1001 -10 L
1002 20 W
1003 -21 L
1004 -29 L
1005 12 D
1006 19 W
1007 18 W
1008 15 W
1009 88 O
1010 11 N
1011 44 S
I want to count values of each type sum them up after that in this way:
L + W + D + O = TOTAL (To have a clear total of all the specified types.)
Answer:
SELECT SUM(total) from (
select sum(value) as total from table where type = 'L'
union all
select sum(value) as total from table where type = 'W'
union all
select sum(value) as total from table where type = 'D'
union all
select sum(value) as total from table where type = 'O'
)a
Thanks for all who took time to review my question. I figured it out my way and posted the answer to help anyone who had the same question or would have the same in the future. Thanks for all the answers as well :)).

SELECT N rows before and after the row matching the condition? - MS Access

tbl_A
uqAID A
1 CC
2 AA
3 SS
4 QQ
5 RR
6 UU
7 LL
8 WW
tbl_B
uqBID uqAID B
1 4 NNN
2 4 MMM
3 3 OOO
4 1 PPP
5 6 QQQ
6 4 RRR
7 8 SSS
8 2 TTT
With this query
SELECT X.B,
Y.A
FROM tbl_B AS X,
tbl_A AS Y
WHERE X.uqAID = Y.uqBID
AND X.A = 'UU'
AND Y.B = 'QQQ'
I am getting the result as " UU QQQ"
but I need to get the result like
SS OOO
CC PPP
UU QQQ
QQ RRR
WW SSS
above and below of two record of UU and QQQ in MS Access
First, it's going to depend on the order. So you are going to need an order by on your query otherwise there's no way to predict the results you will get.
Assuming it's srder on the Y.B column, you could do something like this to get the QQQ and the two following it:
SELECT Top 3 X.B,
Y.A
FROM tbl_B AS X,
tbl_A AS Y
WHERE X.uqAID = Y.uqBID
AND X.A >= 'UU'
AND Y.B >= 'QQQ'
order by y.a, y.b
Then the following to get the two prior to QQQ
SELECT Top 2 X.B,
Y.A
FROM tbl_B AS X,
tbl_A AS Y
WHERE X.uqAID = Y.uqBID
AND X.A < 'UU'
AND Y.B < 'QQQ'
order by y.a desc, y.b desc
From there you can union the two result sets together (which is left an exercise for the student). That should get you the two proceeding and following the trigger row.

MySQL query the latest duplicate

EDIT:
I'm trying to do a MySQL query which will give me the latest entry for duplicates together with those without any duplicates.
This is what my table look like:
mentor table:
mentor_id applicant_id mentor_count mento_email mentor_name mentor_pass
193 92 1 test#yahoo.com test 1234
194 92 2 table#yahoo.com table 4567
195 92 3 lamp#yahoo.com lamp 7890
196 92 1 test#yahoo.com test 1234
197 92 2 table#yahoo.com table 4567
198 92 3 lamp#yahoo.com lamp 7890
mf table:
mf_id mentor_id dept contact orgname yrs length sak social char goal resp emomat res others impact evaluation
43 193 math dept 9111111 etc 1 1 e e e e e e e e e good
114 196 math dept 9111111 etc 1 1 e e e e e e e e e good
193 197 sci dept 9222222 org 2 2 n n n n n n n n n medium
194 194 sci dept 9222222 org 2 2 n n n n n n n n n medium
220 195 eng dept 9333333 hello 3 3 q q q q q q q q q bad
I tried using this query:
SELECT *
FROM mentor m1
LEFT JOIN (
SELECT mentor_name, max( mentor_id ) AS maxid
FROM mentor m
GROUP BY m.mentor_id
)m2 ON m1.mentor_name = m2.mentor_name
AND m1.mentor_id < m2.maxid
LEFT JOIN mf ON m1.mentor_id = mf.mentor_id
WHERE m1.applicant_id =833
AND m2.maxid IS NULL
ORDER BY m1.mentor_id ASC
LIMIT 0 , 30
but this is what happens:
mentor_id applicant_id mentor_count mentor_email mentor_name mentor_pass mentor_name maxid mf_id mentor_id dept contact orgname yrs length sak social char goal resp emomat res others spirit concept comm impact evaluation
/*there is data here but the column for mentor_name onwards is null*/
How can I make it so that the columns for mentor_name onwards is not null, but still displays the latest duplicates as well as those without any duplicates?
try
select * from mentor
where mentor_id in
(
SELECT max(mentor_id) from mf
where applicant_id = 92
group by mentor_id
)
I guess you want to add mentor.application_id = mf.application_id to the JOIN condition
select *
from mentor
inner join
(
SELECT *, max(mentor_id) as maxid
from mf
group by mentor_id
) mf on mentor.mentor_id = mf.maxid AND mentor.application_id = mf.application_id
where applicant_id = 92
Typically you will need an extra condition to get the duplicates. WHERE applicant_id = 92 won't be a duplicate unless there were others with the same applicant_id in the same table.