SSRS MDX query doesn't work with muti-value parameter - sql-server-2008

I need your your help!!
I want to create a report with multi-value parameters with SSRS.
So, I wrote this mdx script:
WITH
-- Geography metadata
MEMBER [Measures].[Geographie]
AS StrToValue ( #SelectionGeographie + ".Hierarchy.Currentmember.Uniquename" )
MEMBER [Measures].[Geographie_Label]
AS StrToValue( #SelectionGeographie + ".Hierarchy.CurrentMember.Member_Caption" )
-- Activity metadata
MEMBER [Measures].[Activite]
AS StrToValue( #SelectionActivite + ".Hierarchy.Currentmember.Uniquename" )
MEMBER [Measures].[Activite_Label]
AS StrToValue( #SelectionActivite + ".Hierarchy.CurrentMember.Member_Caption" )
-- Date metadata
MEMBER [Measures].[Temps]
AS StrToValue( #Annee + ".Hierarchy.Currentmember.Uniquename" )
MEMBER [Measures].[Temps_Label]
AS StrToValue( #Annee + ".Hierarchy.CurrentMember.Member_Caption" )
-- Perimetre metadata
MEMBER [Measures].[Perimetre]
AS StrToValue( #Perimetre + ".Hierarchy.Currentmember.Uniquename" )
MEMBER [Measures].[Perimetre_Label]
AS StrToValue( #Perimetre + ".Hierarchy.CurrentMember.Member_Caption" )
SELECT NON EMPTY {
-- display the parameters attributes on columns
[Measures].[Geographie],
[Measures].[Geographie_Label],
[Measures].[Activite],
[Measures].[Activite_Label],
[Measures].[Temps],
[Measures].[Temps_Label],
[Measures].[Perimetre],
[Measures].[Perimetre_Label],
[Measures].[11 VA]
} ON COLUMNS,
( STRTOSET ( "{" + #SelectionGeographie + "}") ,
STRTOSET ("{" + #SelectionActivite + "}" ))
ON ROWS
FROM [MyCube]
WHERE STRTOTUPLE ( "(" +#Annee + "," + #Perimetre + ")" )
But It works with one value in parameter and not with muti-value parameters.
I have null result in my metadata members when I have multi value.
Any idea ?
Thank U

WITH
-- Geography metadata
MEMBER [Measures].[Geographie] AS "[Geographie]."+ #NiveauGeographie +".Currentmember.Uniquename"
MEMBER [Measures].[Geographie_Label] AS "[Geographie]."+ #NiveauGeographie
+".CurrentMember.Member_Caption"
SELECT NON EMPTY { [Measures].[Geographie],
[Measures].[Geographie_Label], [Measures].[11 VA]
} ON COLUMNS,
( STRTOSET ( "{" + #SelectionGeographie + "}") ,
STRTOSET ("{" + #SelectionActivite + "}" ))
ON ROWS
FROM [MyCube]
WHERE STRTOTUPLE ( "(" +#Annee + "," + #Perimetre + ")"

Related

Use a column created using AS to do aggregate functions

Can MySQL detect the columns created using AS?
Example;
SUM( Mark1 + Mark2) AS '2022_SUM'
SUM( Mark3 + Mark4) AS '2021_SUM'
(2022_SUM + 2021_SUM) AS 'SUM1'
It hits unknown column in field list.
Just write:
SELECT
SUM(Mark1 + Mark2) as '2022_SUM',
SUM(Mark3 + Mark4) as '2021_SUM',
SUM(Mark1 + Mark2) + SUM(Mark3 + Mark4) as 'SUM1'
FROM someTableName
OR
SELECT
2022_SUM,
2021_SUM,
2022_SUM + 2021_SUM as 'SUM1'
FROM (
SELECT
SUM(Mark1 + Mark2) as '2022_SUM',
SUM(Mark3 + Mark4) as '2021_SUM'
FROM someTableName
) x

Why am I getting a "No value specified for parameter 2" error in Spring boot jdbc template

My sql query looks like:
select T.Col1
from (
select Past_Diagnoses_1 as Col1
from patienthistory
union
select Past_Diagnoses_2 as Col1
from patienthistory
union
select Past_Diagnoses_3 as Col1
from patienthistory
union
select Past_Diagnoses_4 as Col1
from patienthistory
) as T;
I keep getting an error on Spring which says No value specified for parameter 2
I am trying to union 4 columns together into 1 super long column.
This is a simplified code. I have 32 columns but for question I have shown 4
Java code :
public UserTemp findHistoryByID(Integer Patient_Number) {
String sql = "select T.Col1\n" +
"from (\n" +
" select Past_Diagnoses_1 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_2 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_3 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_4 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_5 as Col1\n" +
" from patienthistory\n"
" where Patient_Number = ?\n" +
" ) as T";
return jdbcTemplate.queryForObject(sql, new Object[]{Patient_Number}, (rs, rowNum) ->
new UserTemp(
rs.getString("T.Col1")
));
}

SSRS - trying to get total rows by using CountRows function

I am trying to get the total rows returned by each tablix. Each row returned is an error and I need to get the total errors. Each tablix has it own data set name so I am trying to use CountRows(data set) and add them together to get the total rows returned or total errors.
Code:
="Number of Errors: " & CountRows("Reader_Check")'' + '' & CountRows("Access_Panel_Check")'' + '' & CountRows("Reader_Check")'' + '' & CountRows("Alarm_Panel_Check")'' + '' & CountRows("Alarm_Input_Check")'' + '' & CountRows("Alarm_Output_Check")'' + '' & CountRows("Segment_Check")'' + '' & CountRows("Access_Level_Check")'' + '' & CountRows("Area_Check")'' + '' & CountRows("Timezone_Check")'' + '' & CountRows("Alarm_Check")
Any help would be great. Thanks
It looks like you are appending the + as a string?
I think you just need something like
="Number of Errors: " &
(CountRows("Reader_Check")
+ CountRows("Access_Panel_Check")
+ CountRows("Reader_Check")
+ CountRows("Alarm_Panel_Check")
+ CountRows("Alarm_Input_Check")
+ CountRows("Alarm_Output_Check")
+ CountRows("Segment_Check")
+ CountRows("Access_Level_Check")
+ CountRows("Area_Check")
+ CountRows("Timezone_Check")
+ CountRows("Alarm_Check")
)
Note: you have Reader_Check specified twice which I assume is incorrect

How to re-use result from a SELECT statement?

My goal is to re-use the result from a SELECT statement to be used in SQL EXISTS statement.
The general idea looks like this:
SELECT *
FROM table
WHERE col1=1
OR EXISTS (
SELECT 1 // this is an exact copy of the SELECT statement above.
FROM table
WHERE col=1
)
The actual SQL statement I am trying to reduce:
"SELECT user_detail.user, user_detail.name, channel_member.role " +
"FROM user_detail " +
"JOIN channel_member ON user_detail.user=channel_member.user " +
"AND channel_member.uuid=#{channelUuid} " +
"WHERE user_detail.user=#{username} " +
"OR EXISTS ( " +
" SELECT 1" +
" FROM user_detail " +
" JOIN channel_member ON user_detail.user=channel_member.user " +
" AND channel_member.uuid=#{channelUuid} " +
" WHERE user_detail.user=#{username} " +
")"
You can only do this if your version of MySQL supports window functions, ie. version >= 8.0
You can use conditional window aggregation, like this:
SELECT *
FROM (
SELECT *, COUNT(CASE WHEN col1 = 1 THEN 1 END) OVER () AS CountMatches
FROM table
) t
WHERE CountMatches > 0;
Depending on the number of rows matching to non-matching, this may be more or less performant. You need to test.
This query:
SELECT *
FROM table
WHERE col1 = 1 OR
EXISTS (SELECT 1 // this is an exact copy of the SELECT statement above.
FROM table
WHERE col=1
)
Doesn't really make sense. It is saying to return all rows if col = 1 is in the table -- but then it filters to check if any row has col = 1. So it is equivalent to:
SELECT *
FROM table
WHERE (SELECT 1 FROM table t2 WHERE t2.col = 1);
I strongly suspect that you intend NOT EXISTS -- so get everything with 1. If there is no 1 then return everything:
SELECT *
FROM table
WHERE col1 = 1 OR
NOT EXISTS (SELECT 1 // this is an exact copy of the SELECT statement above.
FROM table
WHERE col = 1
);
This should work fine with tables -- and is in fact probably optimal with the right indexes.
If "table" is really a complex query, then you might consider window functions:
select t.*
from (select t.*,
sum( col = 1 ) as num_1s
from t
) t
where col = 1 or num_1s = 0;
If you want to use any conditional statement on the query you are running you will need to wrap the query and put it in a FROM statement and then run the conditional outside of the query, like so....
SELECT aliasName.*
FROM
(SELECT *
FROM table
WHERE col1=1) aliasName
WHERE EXISTS aliasName //this is the conditionl statement OUTSIDE of the query you built.
Let me know how you do...
For Mr./Ms. Barmar:
The idea
// Idea:
// If one of the row have col1 with value 1.
// Then return all of the row, or return empty []
// SELECT *
// FROM table
// WHERE col1=1
// OR EXISTS (
// SELECT 1
// FROM table
// WHERE col=1
// )
The solution
"WITH temp AS (" +
" SELECT user_detail.user, user_detail.name, channel_member.role " +
" FROM user_detail " +
" JOIN channel_member ON user_detail.user=channel_member.user " +
" AND channel_member.uuid=#{channelUuid} " +
") " +
"SELECT * " +
"FROM temp " +
"WHERE user=#{username} " +
" OR EXISTS ( " +
" SELECT 1" +
" FROM temp " +
" WHERE user=#{username} " +
" )"
The solution above use WITH clause as recommended by Mr./Ms. Barmar, I am posting this, so you can inspect whether this is logical or not.

Conversion from type 'DBNull' to type 'Double' is not valid

In my code I understand query getting null values and it throws this error. But since my query is little complex I don't understand how do I check for null values and avoid this error. Please help me to correct this query.
SELECT (SUM(charges) + SUM(behaviour) + SUM(admission) + SUM(properInformation) + SUM(hygine) + SUM(treatment))/(count(doctorID) * 6) AverageRating, COUNT(ID) RatingCount from ratings where doctorID = '" + doctorID + "'
If you want the query to not return NULL, you can just surround the expression with IFNULL to convert a possible NULL to 0, something like;
SELECT IFNULL((SUM(charges) + SUM(behaviour) + SUM(admission) +
SUM(properInformation) + SUM(hygine) + SUM(treatment))
/(count(doctorID) * 6), 0) AverageRating,
COUNT(ID) RatingCount
FROM ratings
WHERE doctorID = '" + doctorID + "'
If you definitely know your query returning null value correctly, then you can use try-catch block as below:
Try
Dim dt As DataTable = Me.GetData("SELECT (SUM(charges) + SUM(behaviour) + SUM(admission) + SUM(properInformation)
Catch ex As Exception
MsgBox("Error while fetching data" & vbCrLf & ex.Message)
End Try