Cognos Analytics Detail Filters - filter from another query - analytics

I am trying to create a new report with a filter based on another query.
For instance I have a bunch of fields and want to add this type of filter:
if place code = '22' then provider state = 'PA' else no filter.
I have tried writing to separate queries; I've tried some case if, case when scenarios that all fail when testing the code.
case when ([place_code] = '22' then [provider_state] = 'PA' ) end
There is just too many results if I don't put in this filter.
I ultimately need all place_code's and the respective provider_states but when it's place_code 22 I only want to see provider_state's of 'PA'

Another approach is to use master detail
Have 2 queries
Query 1 has the main content
Query 2 has the [provider_state] data and is joined to Query 1
If there is a provider state, then the detail query will show the results, otherwise it will be blank

Try something like
Create the Data Item [State Selected]
Note: the XX value is an impossible scenario on purpose
case
when ([place_code]='')Then('XX')
when ([place_code]='22')Then('PA')
when ([place_code]='21')Then('TN')
..
END
Have a filter with an "OR" condition
([State Selected] <> 'XX' AND [provider_state] = [State Selected])
OR
([State Selected] = 'XX' AND [provider_state] <> [State Selected])
This way if there is a valid filter, it is applied
If there is no filter, then the other part of the OR statement takes place making the filter behave as if disabled

Related

Conditional Update Statement returning incorrect results on SQL Server 2012

I'm trying to update a table in SQL Server 2012 Management Studio. Rather than writing four separate Update statements, I'm attempting to see if it is possible to write one query that will update the same table for one column in four different ways depending on what the value of a column stores.
Assume I have a table called table_food with the following structure
|Customer|Preference|
+--------+----------+
|John |McDs |
|Div |KFC |
|Paul |KFC |
|Pablo |Wasabi |
My idea is to update the Preference column to new values and the query I had written was:
UPDATE table_food
SET Preference =
CASE WHEN Preference = 'McDs' Then 'Burger'
WHEN Preference = 'KFC' Then 'KingsMeal'
END
Now on my actual table, there are only 8 different options selected for preference and I just need two update 4. (I've just done two as an example above but I have four when statements...so just another two lines)
When I run the query it shows far more rows being affected and checking the results after I notice now there's only one option shown "Burger" with a count of 8 and all the other rows have been set to null. Is there something I'm missing?
That query will update every row on your table since it lacks a WHERE clause. And a CASE WHEN expression returns NULL if none of the WHEN conditions are true. You might want a query like:
UPDATE table_food
SET Preference =
CASE WHEN Preference = 'McDs' Then 'Burger'
WHEN Preference = 'KFC' Then 'KingsMeal'
END
WHERE Preference in ('McDs','KFC')
add else on your case statement.
UPDATE table_food
SET Preference =
CASE WHEN Preference = 'McDs' Then 'Burger'
WHEN Preference = 'KFC' Then 'KingsMeal'
ELSE Preference
END
or if you only want to update affected rows, you do update from
UPDATE table_food
SET Preference = t2.newPreference
FROM
(SELECT CASE WHEN Preference = 'McDs' Then 'Burger'
WHEN Preference = 'KFC' Then 'KingsMeal'
WHEN Preference = 'KFC1' Then 'KingsMeal1'
WHEN Preference = 'KFC'2 Then 'KingsMeal2'
END as newPreference, Preference
FROM table_food) t2
WHERE t2.Preference = table_food.Preference and coalesce(t2.newPreference, '') != ''

SQL Server - add to this query to first check for existence of a string

I have an nvarchar field in my database called CatCustom which contains comma-separated 5-character codes. It can contain as little as one code, or as many as 20 codes, separated by commas.
Right now, I use this query to add a new 5-character code to the field in given records (in this case the new code is LRR01):
UPDATE dbo.Sources
SET CatCustom = CONCAT_WS(', ', RTRIM(CatCustom), 'LRR01')
WHERE SourceID IN (1,2,3,4,5,8,9,44,63,45,101,102,222,344)
I need to add to this though: I need the record to be updated only if that 5-character code doesn't already exist somewhere in the CatCustom field, to ensure that code is not in there more than once.
How would I accomplish this?
EDIT: I really don't understand how this can be considered a duplicate of the suggested thread. This is a VERY specific case and has nothing to do with creating stored procedures and or variables. The alleged duplicated thread does not really help me - sorry.
Use STRING_SPLIT function to split the comma separated list and then add Not Exist condition in the WHERE clause like below
UPDATE dbo.Sources
SET CatCustom = CONCAT_WS(', ', RTRIM(CatCustom), 'LRR01')
WHERE SourceID IN (1,2,3,4,5,8,9,44,63,45,101,102,222,344)
AND NOT EXISTS (SELECT 1 FROM STRING_SPLIT(CatCustom, ',') where value = 'LRR01')
UPDATE dbo.Sources
SET
CatCustom = CONCAT_WS(', ', RTRIM(CatCustom), 'LRR01')
WHERE
SourceID IN (1,2,3,4,5,8,9,44,63,45,101,102,222,344)
AND CatCustom NOT LIKE '%LRR01%';

How to merge multiple records with strings and nulls in a Stream Analytics Group By

I am trying to pull some logged events from Application Insights into our SQL database. I have no control over the format of the inputs which are json files composed of multiple json arrays within the file. In each record, 5 pieces of information are in a json array at [context].[custom].[dimensions] in the file and using an OUTER APPLY flattens these values. The problem is it returns results not as one row per record but as though you had joined one row with 5 (which is indeed what it has done) and the values of the 5 pieces of data are NULL in 4 cases and the actual value in the other. I only need 2 of the 5 values - PageType and UserId - and given this in my GROUP BY it returns 3 records, one with each value and one with both of them null.
In normal SQL you would simply use a MAX expression to get the real values for each but in Stream Analytics you can't use MAX on strings. You also can't use COALESCE and a number of other methods I tried to resolve this with. Any ideas how the results can be changed from:
EventDateTime Event PageType UserId AppVersion CountA
2017-05-24 Nav Show NULL NULL 2.0.1293 1
2017-05-24 Nav Show NULL SIRTSW 2.0.1293 1
2017-05-24 Nav Show Trade NULL 2.0.1293 1
to
2017-05-24 Nav Show Trade SIRTSW 2.0.1293 1 ?
The code that returns three rows for each is as follows (note that e.event is an array of one item so it does not cause the same issue):
SELECT flatEvent.ArrayValue.name as Event,
e.context.data.eventTime as EventDateTime,
e.context.application.version as AppVersion
,flatCustom.ArrayValue.UserId as UserId
,flatCustom.ArrayValue.PageType as PageType,
SUM(flatEvent.ArrayValue.count) as CountA
INTO
[insights]
FROM [ios] e
CROSS APPLY GetArrayElements(e.[event]) as flatEvent
OUTER APPLY GetArrayElements(e.[context].[custom].[dimensions]) as flatCustom
GROUP BY SlidingWindow(minute, 1),
flatEvent.ArrayValue.name,
e.context.data.eventTime,
e.context.application.version,
flatCustom.ArrayValue.UserId,
flatCustom.ArrayValue.PageType
Thanks in advance,
Rob
According to your scenario, I assumed that you could use JavaScript user-defined functions for Azure Stream Analytics to coalesce the multiple dimensions into a single record. Here are my test for this issue, you could refer to them.
JSON file
{
"context":{
"data":{"eventTime":"2017-05-24"},
"application":{"version":"2.0.1293"},
"custom":{
"dimensions":[
{"PageType":null,"UserId":"SIRTSW"},
{"PageType":"Trade","UserId":null},
{"PageType":null,"UserId":null}
]
}
},
"event":[
{"name":"Nav Show","count":1}
]
}
javascript UDF, UDF.coalesce
function main(items) {
var result=[];
var UserIdStr="",PageTypeStr="";
for(var i=0;i<items.length;i++){
if(items[i].UserId!=null && items[i].UserId!=undefined)
UserIdStr+=items[i].UserId;
if(items[i].PageType!=null && items[i].PageType!=undefined)
PageTypeStr+=items[i].PageType;
}
result.push({UserId:UserIdStr,PageType:PageTypeStr});
return result;
}
Query
--first query
WITH f AS (
SELECT
e.context.data.eventTime as EventDateTime,
e.context.application.version as AppVersion,
e.event as flatEvent,
UDF.coalesce(e.[context].[custom].[dimensions]) as flatDimensions
FROM [ios] e
)
--second query
SELECT flatEvent.ArrayValue.name as Event,
f.EventDateTime,
f.AppVersion,
flatDimension.ArrayValue.UserId,
flatDimension.ArrayValue.PageType,
SUM(flatEvent.ArrayValue.count) as CountA
FROM f
CROSS APPLY GetArrayElements(f.[flatEvent]) as flatEvent
OUTER APPLY GetArrayElements(f.[flatDimensions]) as flatDimension
GROUP BY SlidingWindow(minute, 1),
flatEvent.ArrayValue.name,
f.EventDateTime,
f.AppVersion,
flatDimension.ArrayValue.UserId,
flatDimension.ArrayValue.PageType
TEST RESULT

ERROR: more than one row returned by a subquery used as an expression

I'm developing an asp.net a data and I would insert a data in one column. but he send it in the another line what I can do to correct this problem? it send me: ERROR: more than one row returned by a subquery used as an expression
Error
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
UPDATE accident_ma
SET geom_acc = (
SELECT ST_Line_Interpolate_Point (route.geom, (
select (pk_accident)/(pk_fin-pk_debut)
from route, accident_ma
where route.num_route = accident_ma.num_route
order by route.num_route limit 1))
from route, accident_ma
where route.num_route = accident_ma.num_route
order by route.num_route)
from route
where route.gid = accident_ma.gid;
It seems to me that you are complicating matters tremendously. Even with multiple events from accident_ma on a single route.geom the below should work just fine:
UPDATE accident_ma a
SET geom_acc = ST_Line_Interpolate_Point(r.geom, a.pk_accident / (a.pk_fin-a.pk_debut))
FROM route r
WHERE a.num_route = r.num_route
AND a.gid = r.gid;
In your code the correlated sub-query would return multiple rows when there are multiple accidents on a single route. In this solution that won't happen: every UPDATE operates on a single accident.

NVarchar Prefix causes wrong index to be selected

I have an entity framework query that has this at the heart of it:
SELECT 1 AS dummy
FROM [dbo].[WidgetOrder] AS widgets
WHERE widgets.[SomeOtherOrderId] = N'SOME VALUE HERE'
The execution plan for this chooses an index that is a composite of three columns. This takes 10 to 12 seconds.
However, there is an index that is just [SomeOtherOrderId] with a few other columns in the "include". That is the index that should be used. And when I run the following queries it is used:
SELECT 1 AS dummy
FROM [dbo].[WidgetOrder] AS widgets
WHERE widgets.[SomeOtherOrderId] = CAST(N'SOME VALUE HERE' AS VARCHAR(200))
SELECT 1 AS dummy
FROM [dbo].[WidgetOrder] AS widgets
WHERE widgets.[SomeOtherOrderId] = 'SOME VALUE HERE'
This returns instantly. And it uses the index that is just SomeOtherOrderId
So, my problem is that I can't really change how Entity Framework makes the query.
Is there something I can do from an indexing point of view that could cause the correct index to be selected?
As far as I know, since version 4.0, EF doesn't generate unicode parameters for non-unicode columns. But you can always force non-unicode parameters by DbFunctions.AsNonUnicode (prior to EF6, DbFunctions is EntityFunctions):
from o in db.WidgetOrder
where o.SomeOtherOrderId == DbFunctions.AsNonUnicode(param)
select o
Try something like ....
SELECT 1 AS dummy
FROM [dbo].[WidgetOrder] AS widgets WITH (INDEX(Target_Index_Name))
WHERE widgets.[SomeOtherOrderId] = N'SOME VALUE HERE'
This query hint sql server explicitly what index to use to get resutls.

Resources