I have to write a query to exclude records using DAX. Now I am not sure how would I exclude records based on a particular condition.
For example, I have to filter data and display employee data for a company in all states except New York. How would I achieve that?
It seems like I can only apply a filter to just display specific data and not for exclusion as you would do in SQL. In SQL we can just use a NOT IN (...) clause to do that. Is there something similar in DAX?
Any help would be greatly appreciated. Thanks!!
EVALUATE
CALCULATETABLE(
<table expression>
,<table>[State] <> "New York"
)
The first argument need not be a table literal, but could be a function that returns a table.
The second argument should be on the table which contains the [State] field, and we simply exclude "New York". CALCULATETABLE() takes 1-N arguments. Arguments 2-N are all filters, which can be tables or simple predicates like in the example above. All filter arguments are evaluated in a logical and.
It looks like you only need a filter if the state is New York, but if you need something equivalent of the SQL NOT IN you can use nested AND functions. For example
EVALUATE
CALCULATETABLE(
'EMPLOYEE',
AND('EMPLOYEE'[STATE] <> "New York", AND('EMPLOYEE'[STATE] <>
"VIRGINIA", 'EMPLOYEE'[STATE] <> "MARYLAND"))
)
Related
I'll try my best to explain the situation here. We are using Maria DB.
In certain case we are looping over all the records/rows of a REQUESTS table.
and for each object/record we are checking few conditions and if condition passes we are performing an action. All the said things here are being done using ruby. Say I want to check the same on pure SQL, I wanted to see how.
Sample Object a Request table:
[{"ot_rqst_id":27460354,"rqst_type_cd":"NONTP","svc_type_cd":"TD","sl_ttl_type_cd":"","lot_num":41843022, ttl_stg_cd: 'CMPLT'}]
What I want to check on sql:
To see if the ttl_stg_cd of this object is equal to 'CMPLT'. If it is then update it to NULL else "something_else".
Let me remind you again that, I am already doing this in ruby language, but I am not an expert on sql so help would be appreciated.
The following query should do what you want.
I have taken the id from your query string. You need to put in the real table name and check the WHERE clause. If you remove the WHERE clause it will update all records in the table, you may wish the change it to something like
WHERE column_X = value_Y
UPDATE table_name
SET ttl_stg_cd = CASE ttl_stg_cd
WHEN 'CMPLT' THEN null
ELSE 'something_else' END
WHERE ot_rqst_id = 27460354;
I have a table I wish to query. It has a string variable called comment which contains an ID along with other things. (i.e. "123456;varA;varB")
rowNo
comment
1
"123456;varA;varB"
2
"987654;varA;varB"
I want to filter based on the first substring in the comment variable.
That is, I want to filter the table on rows where the first substring of comment is "123456" (which in the example would return the first row)
How do I do this?
I was thinking something along the lines of the code below, using the "string_split" function, but it doesn't work.
SELECT *,
FROM table
WHERE (SELECT value FROM STRING_SPLIT(comment,';',1)="123456")
Does anyone have any ideas?
Note, I am querying in SQL in SAS, and this is on a large dataset, so I don't want to create a new table with a new column to then query on instead. Ideally I'd want to query on the existing table directly.
You can use the SCAN() function to parse a string.
WHERE '123456'=scan(comment,1,';')
I have used before LIKE command to match patterns to a specific SQL table column. For example need all the rows which all have name started with "A". But this case I am trying to solve things reversed , have a column "Description" where all the regular expressions row wise. And need to return specific rows which all match with a input text.
Table A
Description
============
\b[0-9A-Z ]*WT[A-Z0-9 ]*BALL\b
\b[0-9A-Z ]*WG[A-Z0-9 ]*BAT\b
\b[0-9A-Z ]*AX[A-Z0-9 ]*BAR\b
So Description column has these regular expressions and the input text "BKP 200 WT STAR BALL" So need to return the first row after select query since that the only match. If any one can help with the select query or idea, would be very helpful. If more details required please mention also.
Cross join you regex table to one that you searching within. Then just match two columns against each other.
Here's the example how you can match any of your expressions.
Here how you can match all of them
I would like to ask if it is possible to do this:
For example the search string is '009' -> (consider the digits as string)
is it possible to have a query that will return any occurrences of this on the database not considering the order.
for this example it will return
'009'
'090'
'900'
given these exists on the database. thanks!!!!
Use the Like operator.
For Example :-
SELECT Marks FROM Report WHERE Marks LIKE '%009%' OR '%090%' OR '%900%'
Split the string into individual characters, select all rows containing the first character and put them in a temporary table, then select all rows from the temporary table that contain the second character and put these in a temporary table, then select all rows from that temporary table that contain the third character.
Of course, there are probably many ways to optimize this, but I see no reason why it would not be possible to make a query like that work.
It can not be achieved in a straight forward way as there is no sort() function for a particular value like there is lower(), upper() functions.
But there is some workarounds like -
Suppose you are running query for COL A, maintain another column SORTED_A where from application level you keep the sorted value of COL A
Then when you execute query - sort the searchToken and run select query with matching sorted searchToken with the SORTED_A column
I'm working on a database query via a search bar and would like it to sometimes yield all results (depending on what is inputted)
I know that for SELECT you can use * in order to select all columns. Is there similar SQL syntax: i.e. WHERE name IS * to essentially always be true?
Edit to clarify:
The nature of the clause is that a variable is used to set the name (I'm actually not able to change the clause, that was made clear). i.e. WHERE name IS [[inputName]] (inputName is the decided by the search bar)
WHERE ISNULL(name, '') = ISNULL(name, '')
(assuming that 'name' is of a string type)
Just make the column reference itself. However, if this is the only goal of your query, why are you against omitting the WHERE clause?
If you want to return all results in a SQL statement, you can simply omit the WHERE clause:
SELECT <* or field names> FROM <table>;
You should use WHERE only when you want to filter your data on a certain field. In your case you just don't want to filter at all.
Actually you don't need WHERE clause at all in this situation. But if you insist then you should write your predicate so it always returns true. This can be done many ways:
Any predicate like:
WHERE 1=1
With column:
WHERE name = name OR name is null
With LIKE:
WHERE name LIKE '%' OR name is null
With passed parameter:
WHERE name = #name OR #name is null
You can think of more of course. But I think you need the last one. Pass NULL from app layer if you want all rows.