Im working towards creating a calculated column that should contain an integer value specified by me if the criteria is met. So, from a single table, I want to check values of few columns, if the values that i specify are present in those column then the custom column should return an integer value specified by me. For example
Column A, Column B, Column C
arizona, 3, 3109
colorado,4, 2353
.
.
.
california,23, 6978
I want to create a custom column in such a way that
if column a='california' && column b='3' && column c= '3109' then 7 elseif
column a='california' && column b='5' && column c='3109' then 8 elseif and so on.
I have tried all the possible functions in PowerBi but it is not giving the desired output.
any kind of lead will be appreciated.
Thanks
Best way to do this is in the query editor. Go to Edit Queries > Add Column > Custom Column and use following code:
= if [Column A] = "california" and [Column B] = "3" and [Column C] = "3109" then 7
else if [Column A] = "california" and [Column B] = "5" and [Column C] = "3109" then 8
else if ...more conditions...
else 0 ' <- This one is for the case that no if condition is met and closes the if
Note that if Column B and Column C of type numberic you have to write it without the quotes. Like this:
= if [Column A] = "california" and [Column B] = 3 and [Column C] = 3109 ...
Related
Insert into D.d
Select * from A.a join B.b on
A.a.a1=B.b.b1
Join C.c on C.c.c1=B.b.b1
I have complex statements for which i need to extract source db name ( in above statement source DB are A,B,C and source tables are a,b,c &Target Db is D and target table is d)
Need output like
SourceDB SourceTbl TargetDB Targettbl
A,B,C a,b,c D d
Or we can get values in json format as well for each field.. Also this needs to accomodate for update and delete statements as well. Please assist
Thanks
You can use the SQLPARSE to parse the statement. I am providing a code below which is not optimally and efficiently written, but it has the logic to get the information
import sqlparse
raw = 'Insert into D.d ' \
'Select * from A.a join B.b on ' \
'A.a.a1=B.b.b1 Join C.c on C.c.c1=B.b.b1;'
parsed = sqlparse.parse(raw)[0]
tgt_switch = "N"
src_switch = "N"
src_table=[]
tgt_table= ""
for items in parsed.tokens:
#print(items,items.ttype)
if str(items) == "into":
tgt_switch ="Y"
if tgt_switch == "Y" and items.ttype is None:
tgt_switch = "N"
tgt_table = items
if str(items).lower() == "from" or str(items).lower() == "join":
src_switch = "Y"
if src_switch == "Y" and items.ttype is None:
src_switch = "N"
src_table.append(str(items))
target_db = str(tgt_table).split(".")[0]
target_tbl = str(tgt_table).split(".")[1]
print("Target DB is {} and Target table is {}".format(target_db,target_tbl))
for obj in src_table:
src_db = str(obj).split(".")[0]
src_tbl = str(obj).split(".")[1]
print("Source DB is {} and Source table is {}".format(src_db, src_tbl))
Snowflake does not offer any SQL statement parsing support. You can hack at it with regex'es, of course, or use any of the tools on the market.
If this query ran, and ran successfully, you can use ACCESS_HISTORY view https://docs.snowflake.com/en/sql-reference/account-usage/access_history.html to see which tables (A.a, B.b, C.c, D.d) and columns (A.a.a1, B.b.b1, C.c.c1, D.d.d1) it accessed and how (read or write).
I have a report which creates a spreadsheet. one cell could have data from one of three fields depending on a fourth field status or field two being blank..
I was thinking of writing as CASE statement but that does not work in ssrs.
The IIF statement works great for 2 values but how do you write for three values?
In common language it would be "If secondary value ="Yes", use secondary name, If account field value is Null, use Contact field value, otherwise use account field value"
Thanks
You can use nested IIF statements but they quickly get messy. You should use SWITCH which acts a bit like a case statement.
A simple example would be
= SWITCH(
Fields!myFirstField.Value = <10 , "Small",
Fields!myFirstField.Value = <30 , "Medium",
Fields!myFirstField.Value = <80 , "Large",
True , "HUGE",
)
As switch stops at the first true expression there is not need to check ranges of values if you get them in the correct order.
The final "True" expression just acts like an ELSE
If you need to check multiple criteria per condition then you can do that too like this..
= SWITCH(
Fields!myFirstField.Value = <10 AND Fields!mySecondField.Value = 1 , "Small Cat",
Fields!myFirstField.Value = <10 AND Fields!mySecondField.Value = 2 , "Small Dog",
Fields!myFirstField.Value = <30 , "Medium Animal",
Fields!myFirstField.Value = <80 , "Large Animal",
True , "HUGE Animal",
)
There must be a way in SSRS to have multiple conditionals for separate data filters? I have an input report level filter #reportparameter, and data item "Checknum" I need to do something resembling the following:
if #reportparameter = "C" and Left(Fields!Checknum,2) = "NC", filter
otherwise
if #reportparameter = "E" and Left(Fields!Checknum,2) = "VR", then filter
two separate conditionals, both compound statements.
What does the SSRS Dataset look like, as far as syntax?
If you want this as part of the query, you could add it to the WHERE clause:
WHERE (#reportparameter = 'C' and Left(Fields!Checknum,2) = 'NC')
OR (#reportparameter = 'E' and Left(Fields!Checknum,2) = 'VR')
But if you want to do it in the dataset filter, your filter Expression would be like
=IIF((Parameters!reportparameter.Value= "C" AND LEFT(Fields!Checknum.Value, 2) = "NC")
OR (Parameters!reportparameter.Value= "E" AND LEFT(Fields!Checknum.Value, 2) = "VR")
, 1, 0)
The type would be Integer and the Value would be 1.
I would like compare the value of two colums from a single table, and UPDATE another colums in function of results, in MySql. Values are on the same row.
Example:
If value COL A > value Col B ==> Col C = "player1"
If value COL A < value Col B ==> Col C = "player2"
How could i do that ?
Something like :
UPDATE table SET col C = "player1" WHERE ...
Edit :
So i just try with CASE...
UPDATE partie SET col C =
CASE
WHEN Col A > Col B THEN 'player1'
WHEN Col A < Col B THEN 'player2'
ELSE 'deuce'
END
WHERE .... ;
Is it correct in Mysql ?
Many thanks
Either do so in two different queries:
UPDATE table SET C = 'player1' WHERE a > b;
UPDATE table SET C = 'player2' WHERE a < b;
Or try this one liner:
UPDATE table SET C = IF(a > b, 'player1', 'player2')
Do note:
What do you want to happen when a = b?
Without further filtering all of the above will make for full table scans, so very heavyweight for large tables.
Something like this
<?php
if ($Cola > $Colb){
UPDATE $tableName SET ColC = "player1";
}
else{
UPDATE $tableName SET ColC = "Player 2";
}
?>
seems like a stupid question...
I have a mysql table where I want to modify column A to a number 0 or 1 depending on the condition of another column B
So: if( B > 500 ) A = 1 ELSE A = 0
Column A = INT
Column B = DOUBLE
How do you do something like this in sql?
Thanks,
Erik
Try the following statement,
UPDATE tableName
SET A = (B > 500)
SQLFiddle Demo
(B > 500) is a boolean arithmetic in mysql which returns 1 and 0 for true and false , respectively.
You can also use CASE for much more RDBMS friendly,
UPDATE tableName
SET A = CASE WHEN B > 500 THEN 1 ELSE 0 END