SSAS: change boolean to 'yes' or 'no' - cube

i'm new to Analysis Services. I have created a dimension on a boolean column. Now users want to have 'yes' and 'no' instead of 'true' and 'false' as result.
Thanks.

Create a calculated column in your DSV which provides the label you want.
case when column = 0 then 'No' else 'Yes' end

Related

create a new column in the table depending from other column

in my table, I got a column where is written the company code
example
DE06 or DE07
now I need to add a new column to this table where will have as value 'YES' for DE06 and 'NO' for DE07
I try to add a condition just for De06 but I cant make it work
ALTER Table dbo.DE06_PROJECT$
add [PROJECT_COMPANY_CODE2] nvarchar(50) null
GO
UPDATE dbo.DE06_PROJECT$ SET [PROJECT_COMPANY_CODE] ='YES' where([COMPANY_CODE]='DE06')
GO
I need a new column [PROJECT_COMPANY_CODE2] with result 'YES' or 'NO' based on the value of the column [COMPANY_CODE]
I forget to mention that in the future I could need 3 possibilities 'YES' for DE06 and 'NO' for DE07 and 'MAYBE" for DE08
If you have SQL Server then you can go for CASE
UPDATE dbo.DE06_PROJECT$
SET [PROJECT_COMPANY_CODE] = CASE WHEN [COMPANY_CODE]='DE06' THEN 'YES' ELSE 'NO' END
Use a computed column to ensure data consistency:
ALTER TABLE dbo.DE06_PROJECT$
ADD [PROJECT_COMPANY_CODE] AS CASE [COMPANY_CODE] WHEN 'DE06' THEN 'YES'
WHEN 'DE07' THEN 'NO'
WHEN 'DE08' THEN 'MAYBE'
ELSE 'whatever' END

How to update sql table column value dynamicllay based on another value?

I have one table with the following structure and some values :
PK_ID Webinar_Name Start_Date Is_Active
1 w1 3/1/2016 True
2 w2 1/7/2016 True
3 w3 4/9/2016 True
Now i want the Is_Active column value to be updated dynamically (i.e : NOT after update,insert and delete) based on the date.. if its matches the current date so the Is_Active column value will set to be false.
I've tried to use triggers but it should be after some action like insert and update which is conflicts with my requirements.
I appreciate any help . thanks
It looks like you should be able to use a computed column for this, though I've never tried it.
https://msdn.microsoft.com/en-GB/library/ms188300.aspx
I guess it'd look something like this:
ALTER TABLE webinars ADD ActiveFlag AS
CASE WHEN Start_Date = CAST(GetDate() AS date) THEN 'True'
ELSE 'False' END
Use a computed column:
alter table t add Is_Active as (case when cast(start_date as date) = cast(getdate() as date) then 0 else 1 end)
Of course, you might need to remove the column first.
Note: this uses 1 for true and 0 for false. These constants are not defined in SQL Server. You can use whatever values you like.

Selecting Data from a column where NULLs and strings exist

Hi i have a query that appears to be pulling in 'Not Set' twice:
Now after a few checks i know it is because the currentstage column has NULLS and the string 'Not Set'stored in the table. So below yields 3 strings of 'Not set' and 195 NULLS forced to 'Not Set'.
But what i actually want to see is 198 x 'Not Sets' in my #TempCircCount. How can this be done please?
My Failing code is here:
IF OBJECT_ID('tempdb..#TempCircCount') is not null
DROP TABLE #TempCircCount
SELECT
ISNULL(cirRep.CurrentStage, 'Not Set') AS CurrentStage,
COUNT(ISNULL(cirRep.CurrentStage, 'Not Set')) AS Circuits
INTO #TempCircCount
FROM
[QuoteBase].[dbo].[CircuitReports] cirRep
RIGHT JOIN
Quotebase.dbo.Circuits cir ON cir.[PW Number] = CirRep.[PWNumber]
WHERE
Cir.Status='New Circuit Order'
GROUP BY CurrentStage
ORDER BY CurrentStage
SELECT
ISNULL(CurrentStage, 'Not Set') AS [CurrentStage],
Circuits AS Circuits
FROM #TempCircCount
GROUP BY CurrentStage, Circuits
ORDER BY CurrentStage
I believe simply changing
GROUP BY CurrentStage
to
GROUP BY ISNULL(cirRep.CurrentStage, 'Not Set')
would work.
The GROUP BY uses CurrentStage from one of your table fields (i.e. cirRep.CurrentStage) instead of the field in the select. SQL server disallows grouping by a field in the select.
I also recommend not using the same names for your output fields as already existing fields for exactly this reason.

How to change flag value in single query using sql server?

I'm having one table, it's containing the column name like flag, that data's are looks like 'Y' or 'N'.
When I'll take a data from that table using "Select Flag from tablename" query means it shows only 'Y' or 'N'.
But what I need is, if it's 'Y' means 'Yes' or 'N' means 'No.
If we will use Stored procedure means we will do this concept easily using 'If' or 'Case' condition.
But I want to take that data using single query.
How can I do this?
Please help me friends...
One way is with a case statement:
select (case when flag = 'Y' then 'Yes'
when flag = 'N' then 'No'
else 'Oops!'
end) as FullFlag
from table t
You can also do this with a calculated column. You could define the table and store the flag as a bit (which is as efficient as possible) and then use a computed column to retrieve another value:
create table . . .
FlagBit bit,
Flag as (case when FlagBit = 1 then 'Yes' else 'No' end)
. . .

How to select true/false based on column value?

I have a table with the following columns:
EntityId, EntityName, EntityProfile, .................
I want to select the Id and Name and true/false column based on the value of entity profile,
for example a returned result set like below, would mean that entities 1&2 have profiles while 3 not.
1 Name1 True
2 Name2 True
3 Name3 False
etc.....
I know I can do it using a function that return true/false based on the profile value like this:
SELECT EntityId, EntityName, dbo.EntityHasProfile(EntityId) AS HasProfile FROM Entities
but I'm returning a large no. of records and with this function call for each record, the query is very slow, and when I remove the function call the query execution time drops significantly.
So is there another way of doing this?
Thanks
Use a CASE. I would post the specific code, but need more information than is supplied in the post - such as the data type of EntityProfile and what is usually stored in it. Something like:
CASE WHEN EntityProfile IS NULL THEN 'False' ELSE 'True' END
Edit - the entire SELECT statement, as per the info in the comments:
SELECT EntityID, EntityName,
CASE WHEN EntityProfile IS NULL THEN 'False' ELSE 'True' END AS HasProfile
FROM Entity
No LEFT JOIN necessary in this case...
You can try something like
SELECT e.EntityId,
e.EntityName,
CASE
WHEN ep.EntityId IS NULL THEN 'False'
ELSE 'TRUE'
END AS HasProfile
FROM Entities e LEFT JOIN
EntityProfiles ep ON e.EntityID = ep.EntityID
Or
SELECT e.EntityId,
e.EntityName,
CASE
WHEN e.EntityProfile IS NULL THEN 'False'
ELSE 'TRUE'
END AS HasProfile
FROM Entities e
Maybe too late, but I'd cast 0/1 as bit to make the datatype eventually becomes True/False when consumed by .NET framework:
SELECT EntityId,
EntityName,
CASE
WHEN EntityProfileIs IS NULL
THEN CAST(0 as bit)
ELSE CAST(1 as bit) END AS HasProfile
FROM Entities
LEFT JOIN EntityProfiles ON EntityProfiles.EntityId = Entities.EntityId`
If the way you determine whether or not an entity has a profile is a deterministic function, and doesn't require any access to another table, you could write a stored function and define a computed, persisted field which would store that value for you and not have to re-compute it over and over again.
If you need to query a separate table (to e.g. check the existance of a row), you could still make this "HasProfile" a column in your entity table and just compute that field on a regular basis, e.g. every night or so. If you have the value stored as an atomic value, you don't need the computation every time. This works as long as that fact - has a profile or not - doesn't change too frequently.
To add a column to check whether or not EntityProfile is empty, do something like this:
CREATE FUNCTION CheckHasProfile(#Field VARCHAR(MAX))
RETURNS BIT
WITH SCHEMABINDING
AS BEGIN
DECLARE #Result BIT
IF #Field IS NULL OR LEN(#Field) <= 0
SET #Result = 0
ELSE
SET #Result = 1
RETURN #Result
END
and then add a new computed column to your table Entity:
ALTER TABLE dbo.Entity
ADD HasProfile AS dbo.CheckHasProfile(EntityProfile) PERSISTED
Now you have a BIT column and it's persisted, e.g. doesn't get computed every time to access the row, and should perform just fine!
At least in Postgres you can use the following statement:
SELECT EntityID, EntityName, EntityProfile IS NOT NULL AS HasProfile FROM Entity
What does the UDF EntityHasProfile() do?
Typically you could do something like this with a LEFT JOIN:
SELECT EntityId, EntityName, CASE WHEN EntityProfileIs IS NULL THEN 0 ELSE 1 END AS Has Profile
FROM Entities
LEFT JOIN EntityProfiles
ON EntityProfiles.EntityId = Entities.EntityId
This should eliminate a need for a costly scalar UDF call - in my experience, scalar UDFs should be a last resort for most database design problems in SQL Server - they are simply not good performers.

Resources