I have the following nested iif statement that brings back an error "End of Statement expected."
Can someone please help? I know its probably very straightforward but i have been looking at this for sometime.
Thanks
=SUM(IIF(Fields!Grade.Value = "A*", 6, IIF(Fields!Grade.Value = "A", 5, IIF(Fields!Grade.Value = "B", 4, IIF(Fields!Grade.Value = "C" , 3, IIF(Fields!Grade.Value = "D" , 2 , IIF(Fields!Grade.Value = "E", 1, IIF(Fields!Grade.Value = "E/U", 0))))))) - IIF(Fields!Target_Grade.Value = "A*" , 6, IIF(Fields!Target_Grade.Value = "A", 5, IIF(Fields!Target_Grade.Value = "B", 4 , IIF(Fields!Target_Grade.Value = "C", 3, IIF(Fields!Target_Grade.Value = "D", 2, IIF(Fields!Target_Grade.Value = "E" , 1, IIF(Fields!Target_Grade.Value = "U", 0)))))))))
I needed an else 0 statement, added this to the code to give the below and all is good
=SUM(IIF(Fields!Grade.Value = "A*", 6, IIF(Fields!Grade.Value = "A", 5, IIF(Fields!Grade.Value = "B", 4, IIF(Fields!Grade.Value = "C" , 3, IIF(Fields!Grade.Value = "D" , 2 , IIF(Fields!Grade.Value = "E", 1, IIF(Fields!Grade.Value = "E/U", 0, 0))))))) - IIF(Fields!Target_Grade.Value = "A*" , 6, IIF(Fields!Target_Grade.Value = "A", 5, IIF(Fields!Target_Grade.Value = "B", 4 , IIF(Fields!Target_Grade.Value = "C", 3, IIF(Fields!Target_Grade.Value = "D", 2, IIF(Fields!Target_Grade.Value = "E" , 1, IIF(Fields!Target_Grade.Value = "U", 0, 0))))))))
I think you have one too many closing brackets- you could try removing the last bracket.
(I pasted your code into Notepad++ and put in a carriage return for each opening bracket and closing bracket. Notepad++ highlights the matching opening and closing brackets, and there was one left over))
Related
I have a Ms Sql Table. In this table, I store data in json format in my column named "DynamicData". What I want to do is to filter on this table. But to do this filtering according to different fields on the json data in my column named "dynamicData".
e.g;
Id | Title | DynamicData
---------------------------------------------------------------- ------------------------------
1 | Title1 | { "Id": 1, "Score": 5, "Message": "Msg1", Types: ["a", "b", "c"]}
2 | Title2 | { "Id": 2, "Score": 3, "Message": "Msg2", Types: ["z", "k", "c"]}
3 | Title3 | { "Id": 3, "Score": 1, "Message": "Msg3", Types: ["a", "b", "c"]}
4 | Title4 | { "Id": 4, "Score": 4, "Message": "Msg4", Types: ["h", "n", "f"]}
Exactly what I want; querying in different ways according to the fields in this json data.
For example;
Retrieve the record whose "score" field is "5".
Bring the record with "Score" field "5" and "Message" field "Msg1".
Fetch the record with the "Score" field "5" or the "Types" field "h" or "n".
There may be different and somewhat more complex inquiries similar to these. I searched but couldn't find what I wanted. Do you have a suggestion or a sample application on how I can make such inquiries?
If you are SQL Server 2016+ you can use OPENJSON()
Here's an example of how you can filter on the Score:
DECLARE #testdata TABLE
(
[Id] INT
, [Title] NVARCHAR(20)
, [DynamicData] NVARCHAR(MAX)
);
INSERT INTO #testdata (
[Id]
, [Title]
, [DynamicData]
)
VALUES ( 1, 'Title1', '{ "Id": 1, "Score": 5, "Message": "Msg1", "Types": ["a", "b", "c"]}' )
, ( 2, 'Title2', '{ "Id": 2, "Score": 3, "Message": "Msg2", "Types": ["z", "k", "c"]}' )
, ( 3, 'Title3', '{ "Id": 3, "Score": 1, "Message": "Msg3", "Types": ["a", "b", "c"]}' )
, ( 4, 'Title4', '{ "Id": 4, "Score": 4, "Message": "Msg4", "Types": ["h", "n", "f"]}' );
SELECT *
FROM #testdata [td]
CROSS APPLY OPENJSON([td].[DynamicData])
WITH (
[Id] INT
, [Score] INT
, [Message] NVARCHAR(20)
, [Types] NVARCHAR(MAX) AS JSON
) AS [dd]
WHERE [dd].[Score] = 5
Giving you the results of:
Id Title DynamicData Id Score Message Types
----------- -------------------- ------------------------------------------------------------------- ----------- ----------- ------- -----
1 Title1 { "Id": 1, "Score": 5, "Message": "Msg1", "Types": ["a", "b", "c"]} 1 5 Msg1 ["a", "b", "c"]
To get to "Types" we'll use anther CROSS APPLY since "Types" is an array:
SELECT *
FROM #testdata [td]
CROSS APPLY OPENJSON([td].[DynamicData])
WITH (
[Id] INT
, [Score] INT
, [Message] NVARCHAR(20)
, [Types] NVARCHAR(MAX) AS JSON
) AS [dd]
CROSS APPLY OPENJSON([Types]) dt --Add another cross apply for types since it is an array
WHERE [dd].[Score] = 5
OR dt.[Value] IN ('h','n') --Then we can filter on it
Giving results of:
Id Title DynamicData Id Score Message Types key value type
----------- -------------------- ---------------------------------------------------------------------- ----------- ----------- --------- ---------------- ---- ------- ----
1 Title1 { "Id": 1, "Score": 5, "Message": "Msg1", "Types": ["a", "b", "c"]} 1 5 Msg1 ["a", "b", "c"] 0 a 1
1 Title1 { "Id": 1, "Score": 5, "Message": "Msg1", "Types": ["a", "b", "c"]} 1 5 Msg1 ["a", "b", "c"] 1 b 1
1 Title1 { "Id": 1, "Score": 5, "Message": "Msg1", "Types": ["a", "b", "c"]} 1 5 Msg1 ["a", "b", "c"] 2 c 1
4 Title4 { "Id": 4, "Score": 4, "Message": "Msg4", "Types": ["h", "n", "f"]} 4 4 Msg4 ["h", "n", "f"] 0 h 1
4 Title4 { "Id": 4, "Score": 4, "Message": "Msg4", "Types": ["h", "n", "f"]} 4 4 Msg4 ["h", "n", "f"] 1 n 1
You see there are duplicates, with the CROSS APPLY on "Types", each element in that array is now it's own row, shown in the Value column.
You will have to make adjustments for your specific requirement, but this should get you moving in the right direction.
If you can't use JSON there is always Ngrams8k.
DECLARE #table TABLE (ID INT IDENTITY, Title VARCHAR(20), DynamicData VARCHAR(1000));
INSERT #table(Title,DynamicData) VALUES
('Title1', '{"Id": 1, "Score": 5, "Message": "Msg1", Types: ["a", "b", "c"]}'),
('Title2', '{"Id": 2, "Score": 3, "Message": "Msg2", Types: ["z", "k", "c"]}'),
('Title3', '{"Id": 3, "Score": 1, "Message": "Msg3", Types: ["a", "b", "c"]}'),
('Title4', '{"Id": 4, "Score": 4, "Message": "Msg4", Types: ["h", "n", "f"]}');
DECLARE #score INT = 5, #type CHAR(1) = 'b'
SELECT t.*
FROM #table AS t
CROSS APPLY PerfLib.samd.ngrams8K(t.DynamicData,7) AS ng
CROSS APPLY (VALUES(SUBSTRING(t.DynamicData,ng.Position+8,8000))) AS pfx(Txt)
CROSS APPLY (VALUES(CHARINDEX('[',t.DynamicData),
CHARINDEX(']',t.DynamicData))) AS tp(Lft,Rgt)
CROSS APPLY (VALUES(REPLACE(SUBSTRING(
t.DynamicData,tp.Lft+1,tp.Rgt-tp.Lft-1),'"',''))) AS tl(List)
CROSS APPLY (VALUES(SUBSTRING(pfx.Txt,1,CHARINDEX(',',pfx.Txt)-1))) AS f(Score)
WHERE ng.Token = '"Score"'
AND f.Score = #score
AND EXISTS (SELECT 1 FROM STRING_SPLIT(tl.List,',') AS split
WHERE LTRIM(split.[value]) = #type);
Returns:
ID Title DynamicData Position
--- ------ ---------------------------------------------------------------- --------
1 Title1 {"Id": 1, "Score": 5, "Message": "Msg1", Types: ["a", "b", "c"]} 11
I'm working with a species data in presence/absence format set where samples have been taken multiple times a day over a period of several days.
Here's a dummy version of the data:
dummy = structure(list(Sample = c("A1", "A1", "A1", "A2", "A2", "A2",
"B1", "B1", "B1", "B2", "B2", "B2"), Species = c("snuffles1",
"snuffles2", "snuffles3", "snuffles1", "snuffles2", "snuffles3",
"snuffles1", "snuffles2", "snuffles3", "snuffles1", "snuffles2",
"snuffles3"), Presence = c(1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1
), Day = c("A", "A", "A", "A", "A", "A", "B", "B", "B", "B",
"B", "B")), row.names = c(NA, -12L), class = c("tbl_df", "tbl",
"data.frame"))
ggplot(dummy[which(dummy$Presence>0),], aes(x = Day, y = Species, color = Species)) +
geom_point(alpha=0.5) +
geom_count(aes(size = sum(dummy$Presence)))
I would like to plot the data in ggplot where the size of each point is dependent on the sum of the number of observations within that group (i.e if on Day A, snuffles1 was observed 2 times, then the point should be size 2, whereas if on Day B, snuffles1 was observed once, the point would be size 1). I hope this makes sense? This counting presence/absence based on group is similar, but not quite what I need.
My guess is that I have to use some sort of function to count the number of observations for each species, depedent on which variable I'm considering, but I am not smart enough to think of how to do this.
Thanks for any and all advice.
Make an additional count by group. Then plot this data frame as an extra layer using geom_point
I am adding breaks to scale_size in order to show only the exiting sizes
library(tidyverse)
count_dum <- dummy %>% group_by(Day, Species) %>% summarise(count = sum(Presence))
ggplot(dummy[which(dummy$Presence > 0), ], aes(x = Day, y = Species, color = Species)) +
geom_point(data = count_dum, aes(size = count), alpha = 0.5) +
scale_size_continuous(breaks = unique(count_dum$count))
How can I fix to save, write command in octave?
Import_Data
error: octave_base_value::save_ascii(): wrong type argument 'object'
DATASET.TSERIES = csvread(’MR_AER_DATASET1.csv’);
DATASET.LABEL = {’DATES’,’T_PI’,’T_CI’,’m_PI’,’m_CI’,’APITR’,’ACITR’,’PITB’,’CITB’,’GOV’};
DATASET.VALUE = [ 1, 2, 3, 4, 5 , 6 , 7 , 8 , 9 , 10 ];
DATASET.UNIT = [ 0, 2, 2, 2, 2 , 2, 2 , 1 , 1 , 1 ];
save (’DATASET’, ’DATASET’);
I am using the following expression in SSRS
=IIF(ReportItems!Textbox69.Value = "~*",
"Excellent", IIF (ReportItems!Textbox69.Value = 1,
"Very Good", IIF (ReportItems!Textbox69.Value = 2,
"Good", IIF (ReportItems!Textbox69.Value = 3,
"Modest", "Cause for Concern")
)
)
)
When I run my report, all the fields that had a * originally are coming up as an error. How can I convert this character please?
Since your textbox contains strings you have to use quotes in comparisons
=IIF(ReportItems!Textbox69.Value = "*",
"Excellent", IIF (ReportItems!Textbox69.Value = "1",
"Very Good", IIF (ReportItems!Textbox69.Value = "2",
"Good", IIF (ReportItems!Textbox69.Value = "3",
"Modest", "Cause for Concern")
)
)
)
I would also suggest to consider using Switch instead of nested Iif
= Switch(
ReportItems!Textbox69.Value = "*", "Excellent",
ReportItems!Textbox69.Value = "1", "Very Good",
ReportItems!Textbox69.Value = "2", "Good",
ReportItems!Textbox69.Value = "3", "Modest",
True,"Cause for Concern"
)
I am new to the SSRS This is expression i am trying but it's showing "#Error" i tried varies things but still not any progress any guess why might it's happening.
=IIF(Fields!MailMeans.Value = "M","METER",
IIF(Fields!MailMeans.Value = "I","INDICIA",
IIF(Fields!MailMeans.Value = "S","STAMPS",
IIF(Fields!MailMeans.Value = "N","NO-MAIL","")
)
)
)
Try using Switch rather than the nested IIF
=Switch(Fields!MailMeans.Value = "M", "METER",
Fields!MailMeans.Value = "I", "INDICIA",
Fields!MailMeans.Value = "S", "STAMPS")
This should also work:
=Switch(Fields!MailMeans.Value = "M", "METER",
Fields!MailMeans.Value = "I", "INDICIA",
Fields!MailMeans.Value = "S", "STAMPS",
Fields!MailMeans.Value = "N", "NO-MAIL",
Fields!MailMeans.Value <> "M" AND Fields!MailMeans.Value <> "I" AND
Fields!MailMeans.Value <> "S" AND Fields!MailMeans.Value <> "N", "")