An SQL statement into SSRS - sql

I tried to incorporate the following into SSRS but failed.
If XXX = “A” then display “AT”
Else if XXX = “B” then display “BEE”
Else if XXX = “C” then display “CAR”
Else display “Other”
I tried
=Switch(
Fields!XXX.Value = "A", "AT",
Fields!XXX.Value = "B", "BEE",
Fields!XXX.Value = "C", "CAR", "Other")

You almost had it. For every output in the Switch function must be paired with a condition. Just make your last condition evaluate to True.
=Switch(
Fields!XXX.Value = "A", "AT",
Fields!XXX.Value = "B", "BEE",
Fields!XXX.Value = "C", "CAR",
True, "Other"
)

You want something like this:
=iif(Fields!XXX.Value = "A", "AT", iif(Fields!XXX.Value = "B", "BEE", iif(Fields!XXX.Value = "C", "CAR", "Other")))
[check the parens in the expression builder]

Related

R function within SQL stored procedure issue

I'm attempting to set up a simple R function using SQL stored procedure but I can't get it to work without errors.
Here is my script
CREATE PROCEDURE sp_aggregate (#paramGroup nvarchar(40))
AS
EXECUTE sp_execute_external_script
#language =N'R',
#script=N'
library(dplyr)
data <- data.frame(x = c(1,2,3,4,5,6,7,8,9,10), Country = c("A", "A","A","A","A",
"B", "B", "B", "B", "B"), Class = c("C", "C", "C", "C", "C", "D", "D", "D", "D", "D"))
agg_function <- function(df, column) {
enq_column <- enquo(column)
output <- df %>% group_by(!!enq_column) %>% summarize(total = sum(x))
return(output)
}
OutputDataSet <- as.data.frame(agg_function(df = data, column = paramgroup_var))
'
, #input_data_1 = N''
-- , #input_data_1_name = N'SQL_input'
, #output_data_1_name = N'OutputDataSet'
, #params = N'#paramgroup_var nvarchar(40)'
, #paramgroup_var = #paramGroup;
GO
Execute sp_aggregate #paramGroup = Country
This is the error I'm running into:
Error in grouped_df_impl(data, unname(vars), drop) :
Column `paramgroup_var` is unknown
Calls: source ... group_by.data.frame -> grouped_df -> grouped_df_impl
Error in execution. Check the output for more information.
Error in eval(ei, envir) :
Error in execution. Check the output for more information.
Calls: runScriptFile -> source -> withVisible -> eval -> eval -> .Call
Execution halted
The error is from missing a ) at the end of the data.frame()
CREATE PROCEDURE sp_aggregate (#paramGroup nvarchar(40))
AS
EXECUTE sp_execute_external_script
#language =N'R',
#script=N'
library(dplyr)
data <- data.frame(x = c(1,2,3,4,5,6,7,8,9,10),
Country = c("A", "A","A","A","A", "B", "B", "B", "B", "B"),
Class = c("C", "C", "C", "C", "C", "D", "D", "D", "D", "D"))
agg_function <- function(df, column) {
enq_column <- enquo(column)
output <- df %>% group_by(!!enq_column) %>% summarize(total = sum(x))
return(output)
}
OutputDataSet <- as.data.frame(agg_function(df = data, column = paramgroup_var))
'
, #input_data_1 = N''
-- , #input_data_1_name = N'SQL_input'
, #output_data_1_name = N'OutputDataSet'
, #params = N'#paramgroup_var nvarchar(40)'
, #paramgroup_var = #paramGroup;
GO

Python - find cell reference within merged_cells collection in openpyxl

I wish to identify if a cell in a worksheet is found within the merged_cells collection returned by openpyxl.
The merged_cells range looks like this (VSCode debugger):
I have the cell reference J31 - which is found in this collection. How would I write a function that returns true if that cell is found in the merged_cells.ranges collection?
for cell in ^^merged_range^^:
if cell==your_special_cell:
return True
^^merged_range^^ must be of type openpyxl.worksheet.cell_range
Further to D.Banakh's answer (+1), try something like this (building upon a previous example I wrote for someone else, since there is little context to your question):
for cell in ws.merged_cells.ranges:
#print(cellRef +' ==> '+ str(cell.min_row) +'/'+ str(cell.max_row) +'/'+ str(cell.min_col) +'/'+ str(cell.max_col))
if (int(cell.min_row) <= int(row) <= int(cell.max_row)) and (int(cell.min_col) <= int(col) <= int(cell.max_col)):
print('Cell ' +cellRef+ ' is a merged cell')
Example within a context:
import re
cellBorders = fnGetCellBorders(ws, cellRef)
if ('T' in cellBorders) or ('L' in cellBorders) or ('R' in cellBorders) or ('B' in cellBorders) or ('M' in cellBorders):
print('Cell has border *OR* is a merged cell and borders cannot be checked')
def getCellBorders(ws, cellRef):
tmp = ws[cellRef].border
brdrs = ''
if tmp.top.style is not None: brdrs += 'T'
if tmp.left.style is not None: brdrs += 'L'
if tmp.right.style is not None: brdrs += 'R'
if tmp.bottom.style is not None: brdrs += 'B'
if (brdrs == '') and ('condTableTopLeftCell' in refs):
if fnIsCellWithinMergedRange(ws, cellRef): brdrs = 'M'
return brdrs
def fnIsCellWithinMergedRange(ws, cellRef):
ans = False
col = fnAlphaToNum(re.sub('[^A-Z]', '', cellRef))
row = re.sub('[^0-9]', '', cellRef)
for cell in ws.merged_cells.ranges:
if (int(cell.min_row) <= int(row) <= int(cell.max_row)) and (int(cell.min_col) <= int(col) <= int(cell.max_col)):
ans = True
return ans
def fnAlphaToNum(ltr):
ab = ["MT", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
return ab.index(ltr)
References:
OpenPyXL - How to query cell borders?
How to detect merged cells in excel with openpyxl
https://bitbucket.org/openpyxl/openpyxl/issues/911/borders-on-merged-cells-are-not-preserved

How do I IF with 3 conditions in VBA

I have a field in a report with 4 potential values, "A", "B", "C" & "D".
The following code makes the font color "red" for "C" and "D" and "black" for all other values. How do I add another statement to make the color "green" for value "A"?
<Color>=IIF((Fields!DFEE_condition.Value = "C") or (Fields!DFEE_condition.Value = "D"),"Red","Black")</Color>
Thanks
Have you tried to reuse IIF statement like this :
<Color>=IIF((Fields!DFEE_condition.Value = "C") or (Fields!DFEE_condition.Value = "D"),"Red",IIF((Fields!DFEE_condition.Value = "A"),"Green","Black"))</Color>
You could convert [A..D] to [1..4] and Choose():
color = choose(asc(Fields!DFEE_condition.Value)-64, "Green", "Black", "Red", "Red")
(If you can use statements, you would Select Case)
You need to write nested iif's ;)
result = IIF(condition1, value_if_true, IIF(condition2, value_if_true, value_if_false))
Try something like this:
<Color>
=IIF((Fields!DFEE_condition.Value = "A"), "Green", IIF(Fields!DFEE_condition.Value = "C") or (Fields!DFEE_condition.Value = "D"), "Red", "Black"))
</Color>

linq concat column by val

I just looking how to use LINQ for grouping a list.
Class Item
Public col1 As String
Public col2 As String
Public col3 As String
Public description As String
Public Sub New(ByVal col1 As String, ByVal col2 As String,
ByVal col3 As String, ByVal description As String)
Me.col1 = col1
Me.col2 = col2
Me.col3 = col3
Me.description = description
End Sub
End Class
Dim ItemList As New List(Of Item)
ItemList.Add(New Item("A", "A", "A", "1"))
ItemList.Add(New Item("A", "A", "A", "2"))
ItemList.Add(New Item("A", "B", "A", "3"))
ItemList.Add(New Item("A", "B", "A", "4"))
ItemList.Add(New Item("A", "B", "B", "5"))
ItemList.Add(New Item("A", "B", "C", "6"))
Result should be list of 4 items:
'[0] = "A", "A", "A", "1 2"
'[1] = "A", "B", "A", "3 4"
'[2] = "A", "B", "B", "5"
'[3] = "A", "B", "C", "6"
If I understand your requirement to group by the 3 columns and join their descriptions, the following LINQ statement should work:
var query = from item in ItemList
group item by
new { Col1 = item.col1, Col2 = item.col2, Col3 = item.col3 }
into g
select new
{
Col1 = g.Key.Col1,
Col2 = g.Key.Col2,
Col3 = g.Key.Col3,
Description = String.Join(" ", g.Select(xx => xx.description))
};
Class Item
...
Public ReadOnly Property id As String
Get
Return col1 & "_" & col2 & "_" & col3
End Get
End Property
End Class
Dim groups = ItemList.GroupBy(Function(item) item.id)
Dim result = From g in groups
Select New Item(g(0).col1, g(0).col2, g(0).col3,
String.Join(" "c, g.Select(Function(i) i.description)))

Referencing Matrix (VB)

I have a matrix (5x5) with values in them for example:
Matrix (1,1) Value: 'a'
Matrix (1, 2) Value: 'b'
Matrix (2, 1) Value: 'c'
how would I be able to find the letter 'a' in that matrix and have it output the coordinates?
ie
user inputs 'b'
[searches for 'b' in table]
output (1,2)
thanks in advance
It's as simple as:
For i As Integer = 0 To LengthOfMatrix - 1
For y As Integer = 0 To HeightOfMatrix - 1
If Matrix(i, y) = "a" Then Console.Write(i & " " & y & vbCrLf)
Next
Next
Asuming that you declared Matrix as:
Dim Matrix As Char(,) = {{"a", "b", "c", "d", "e"}, {"a", "b", "c", "d", "e"}, {"a", "b", "c", "d", "e"}, {"a", "b", "c", "d", "e"}, {"a", "b", "c", "d", "e"}}
And LengthOfMatrix And HeightOfMatrix should be the dimentions of your matrix. They could be switched to something more dynamic like:
For i As Integer = 0 To Matrix.GetLength(0) - 1 'Get's the length of the first dimension
For y As Integer = 0 To Matrix.GetLength(1) - 1 'Get's the length of the second dimension
If Matrix(i, y) = "a" Then Console.Write(i & " " & y & vbCrLf)
Next
Next
In a short description, all that this loop does is it goes through all of the elements of the matrix and outputs the coordinates of every element that matches a certain criteria (In this case - equals to 'a').
Note: In most programming languages array's indexes begin from 0, so the first element in your matrix will be at coords (0,0).