I'm trying to return and change all entries in column login for each entry that the value = "Change this text" to "Requester". The below works but is only returns 2 values, "User" and "Requester" although there are 7 other entries where login = "Change this text"
declare #MainHospital varchar(50)='Hospital1';
SELECT CASE login
WHEN 'Change this text' THEN 'Requester'
ELSE 'User'
END
FROM Survey
WHERE MainHospital = #MainHospital
GROUP BY MainHospital, login
You are grouping by MainHospital & Login, so per hospital you only handle 2 cases. By grouping all of the results together, you are removing duplicate rows.
If you remove the grouping and it will return 7 rows:
SELECT CASE login
WHEN 'Change this text' THEN 'Requester'
ELSE 'User'
END
FROM Survey
WHERE MainHospital = #MainHospital
Otherwise include another unique column that will prevent the grouping.
Related
I have an SSRS report with a field called SetGroup. Each row has a SetGroup value of either 'Group 1' or 'Group 2'. I have a parameter called Term. If Term = 'All' then only show Group 1 records, otherwise show Group 2. I am trying to set up a filter to do this, but it seems I need it to be like IF-THEN-ELSE. I have the filter here, but how do I now tell it to only show the Group 1 records ELSE Group 2?
I have two ideas and I'm not sure if either are possible or how to do them. Maybe one of you has an idea.
1.) Could I put a CASE in my WHERE clause? Something along the lines of this? SQL Server says this is bad syntax because of the extra = operators.
WHERE CASE WHEN #Term = 'All' THEN j.SetGroup = 'Group 1' ELSE j.SetGroup = 'Group 2' END
2.) Could I put IIF formulas in the Expression and in the Value boxes of my filter with the bottom playing off the top one?
I knew it there was a simple solution in there somewhere.
WHERE j.SetGroup = CASE
WHEN #Term = 'All' THEN 'Group 1'
ELSE 'Group 2'
END
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
I have to update a table (say table name is Venues in SQL Server conditionally i.e. if a column (say column name is Venue_Name) in Venues CONTAINS sub-string 'C_' then SET a column (say column name is Venue_Type) in Venues = Conference.
I have tried this query as:
IF Venue_Name CONTAINS 'C_'
UPDATE Venues SET Venue_Type = 'Conference'
ELSE IF Venue_Name CONTAINS 'J_'
UPDATE Venues SET Venue_Type = 'Journal'
ELSE
UPDATE Venues SET Venue_Type = 'Other'
But found error at using the keyword CONTAINS.
What you need is a CASE expression:
UPDATE Venues
SET Venue_Type = CASE
WHEN Venue_Name LIKE '%C[_]%' THEN 'Conference'
WHEN Venue_Name LIKE '%J[_]%' THEN 'Journal'
ELSE 'Other'
END
I am trying to make a report, where I can select a multiple values from a parameter - and then make a query, where i state "where in (#partner)"
However, my problems occours, when I want to make a value, that will not do the "where in (#partner)", but will simply select all from the same query, without the where in clause - as not all rows in my db table, have the specific values set, so I want to catch them too.
All the values for the parameter is selected from a query, and I have a union on it, to create a value for the one, where I need to select all - not using the where in.
It actually works, if only one value from the parameter list is chosen - but as soon as I try to select 2 or more, I get the error "query execution failed for dataset1......"
The value of the parameter value, that is used for selecting all is 0, as you see in the code - and it does work as I can execute both select statements below, but not when I select more then 1 value from the parameter list.
IF #partner = 0
BEGIN
SELECT
thedata
FROM
thetable
WHERE
thedata = 3
END
ELSE
SELECT
thedata
FROM
thetable
WHERE
thedata = 3
and partner in (#partner)
END
ELSE
Hope there is some clever guys out there:) Thanks
Just do:
SELECT thedata
FROM thetable
WHERE thedata = 3
AND (partner IN (#partner) or #partner = 0)
I select my records in SQL Server Query as you seen below:
SELECT ProductName 'Some Name1' ,
Payment 'Some Name2'
FROM dbo.PrefactorProduct
Is there a way to select records like that in linq queries?
You can project result from your query to an Anonymous type and use different alias/field names for your fields.
var query = db.PrefactorProduct
.Select(r=> new
{
SomeName1 = r.ProductName,
SomeName2 = r.Payment
});
But if you are trying to format your result set for displaying purpose then you should look in to assigning column names for your grid/ data container.