Filter Rows Based on String AC27 and AC28 - r

im_id
sub_status
status_date
Flag_Status
12345
PD01
01-FEB-2023
12345
PD02
02-FEB-2023
12345
PD81
02-FEB-2023
12345
PD63
02-FEB-2023
12345
AC27
02-FEB-2023
12345
AC27
03-FEB-2023
Cancel Not Available
12345
AC28
03-FEB-2023
Cancel Not Available
12345
AC01
04-FEB-2023
22233
PD01
04-FEB-2023
22233
AC27
04-FEB-2023
22233
S02
04-FEB-2023
22233
DN99
04-FEB-2023
22233
PD01
04-FEB-2023
22233
AC27
05-FEB-2023
No Infusion Only
22233
AC28
05-FEB-2023
No Infusion Only
22233
AC29
06-FEB-2023
No Infusion Only
22233
AC02
05-FEB-2023
55522
AC27
06-FEB-2023
Cancel Not Available
55522
AC28
04-FEB-2023
Cancel Not Available
55522
AC28
04-FEB-2023
55522
AC29
04-FEB-2023
55522
AC27
05-FEB-2023
77732
AC27
05-FEB-2023
Cancel Not Available
77732
AC28
06-FEB-2023
Cancel Not Available
77732
AC01
05-FEB-2023
77732
PD02
06-FEB-2023
In above data frame (Table Name-DF) "im_id", "sub_status" and "status_date" are 3 input columns.
The expected result will be shown in "Flag Status" column.
Here I want 2 results mentioned as below
1st Scenario-
Do Filter only those records for where ims_id should match with sub_status "AC27" and "AC28" and Not Matched with "CC38". In this, code should filter only those records for each ims_id where "AC28" comes after "AC27" and expected string should be visible Infront of both AC27 and "AC#*" as "Cancel Not Available"
The Output mentioned in above table, in that you can see "AC28" comes after "AC27" and expected string "Cancel Not Available" exist Infront of "AC27 and AC28".
2nd Scenario-
Do Filter only those records for where ims_id should align with sub_status "AC27", "AC28", "AC29" and Not Matched with "AC01". In this, code should filter only those records for each ims_id where "AC28" comes after "AC27" and "AC29" comes after "AC28". expected string should be visible Infront of AC27, AC28 and AC29 as "No Infusion Only"
The Output mentioned in above table, in that you can see "AC28" comes after "AC27" and "AC29" comes after "AC28" and expected string "No Infusion Only" exist Infront of "AC27, AC28 and AC29".
Note: Bothe Scenarios should not mix the results. Earlier I tried filter, subset method but it will not give expected result.
I Tried Below Code for 1st Scenario But while filtering it's selecting all records.
DF$Flag_Status <- ifelse(DF$sub_status %in% c("AC27", "AC28") &
DF$sub_status != "CC38", "Cancelled_Not_Available", DF$Flag_Status)
I Tried Below Code for 2nd Scenario But while filtering it's selecting all records.
DF$Flag_Status <- ifelse(DF$sub_status %in% c("AC27", "AC28", "AC29") &
DF$sub_status != "AC01", "No Infusion Only", DF$Flag_Status)
But in expected results after executing second chunk the earlier results overridden because AC27 and AC28 was included in Scenario 1st.

Related

Unable to Display Correct Date Format in 2nd Select list of UNION

I have a simple query UNION ALL from two tables. I am finding that several of my columns of the second table is blank where there should be data and that the dates are shown as whole numbers (where you can discern the dates, but in YYMMDD preceded by "1"). I have shortened the query for this question to six columns to start. I have also made sure the data types are correct and even change '' to NULL and vice versa just to get errors. Here is the query:
SELECT
PRIMARY_SERVICE_DATE AS "Common Date"
,'P' AS "Claim Type"
,PRI_MEMB_ID AS "Member ID"
,PRIMARY_SERVICE_DATE AS "Primary Service Date"
,NULL AS "Admit Date"
,NULL AS "Discharge Date"
FROM MAINDB.TABLECLAIMS
WHERE
PRI_MEMB_ID IN ('99999')
UNION ALL
SELECT
ADMIT_DATE AS "Common Date"
,'' AS "Claim Type"
,CAST(MEMBER_ID AS BIGINT) AS "Member ID"
,NULL AS "Primary Service Date"
,ADMIT_DATE AS "Admit Date"
,DISCHARGE_DATE AS "Discharge Date"
FROM MAINDB.TABLESCLAIMSSUMMARY
WHERE ADMIT_DATE between '2016-01-01' and '2020-12-31'
AND CAST(MEMBER_ID AS BIGINT) IN ('99999')
This image link shows the results and how I need to display.
enter image description here
In Teradata, the first query in the UNION [ALL] determines the data types of the result, and in this case NULL is being considered INTEGER so the dates from the second query are implicitly CAST to INTEGERDATE form (CYYMMDD). Whenever your first query includes constant values, you should CAST to the expected result data type. (Also when data types or lengths differ you may need to explicitly CAST the result in the first query to be sure the values are returned as intended.)
SELECT
PRIMARY_SERVICE_DATE AS "Common Date"
,CAST('P' AS CHAR(1)) AS "Claim Type"
,PRI_MEMB_ID AS "Member ID"
,PRIMARY_SERVICE_DATE AS "Primary Service Date"
,CAST(NULL AS DATE) AS "Admit Date"
,CAST(NULL AS DATE) AS "Discharge Date"
FROM MAINDB.TABLECLAIMS
WHERE
PRI_MEMB_ID IN ('99999')
…
I am assuming you want the result as a DATE, despite formatting the Excel as DateTime.
If you want TIMESTAMP(0) then you should CAST all 3 dates in the first query.

sqlite3: "IN" works, "NOT IN" doesn't

This query works:
select id from wagtailcore_pagerevision
where id in (select live_revision_id as id from wagtailcore_page)
This query produces no results:
select id from wagtailcore_pagerevision
where id not in (select live_revision_id as id from wagtailcore_page)
(The difference is in vs. not in.)
... but I can very plainly see by inspection of the data that the result of the second query should be many hundreds of rows.
An index does exist on the live_revision_id column, and id of course is a primary key.

Robot Framework - How to if statement if keyword fails

${Var_Name}= page should contain element ${ID}
run keyword if "some keyword" ${Var_Name} false
If the page doesn't contain the element the test fails, is it possible to ignore the fail and run "some keyword" as it returns false?
The keyword 'page should contain element' does not return anything, so ${Var_Name} will not contain a value apart from 'None' when the 'page should contain element' test succeeds.
You could use the keyword "Get Matching Xpath Count" from the selenium2Library and perform the required action in an if/else statement based on the value of the xpath count.
like so:
${value}= | Get Matching Xpath Count | [xpath]
Run Keyword If | ${value} > 0 | [keyword]
... | ELSE | [keyword]

How can I pass text or strings using SQLite JDBC?

I am having this problem using JDBC.
I can create a table called food.db with text fields breakfast, lunch, dinner. When I call the following....
statement.executeUpdate("create table food (breakfast string, lunch string, dinner string)");
breakfast = JOptionPane.showInputDialog("What was your breakfast?");
lunch = JOptionPane.showInputDialog("What was your lunch?");
dinner = JOptionPane.showInputDialog("What was your dinner?");
statement.executeUpdate("insert into food values(\'"+breakfast+"\', \'"+lunch+"\' ,\'"+dinner+"\')");
That last statement, however, results in an error. For whatever reason, it says that whatever I type in for "breakfast" (for example, oatmeal) is not a column, even though I know that I can use SQLite's syntax in this way to update columns.
Also I have checked the argument to executeUpdate(), and the syntax with single quotes and everything matches up...I have tried text and string column fields, get the same error for both.
Try this
change string to VARCHAR(SIZE) OR TEXT
statement.executeUpdate("CREATE TABLE food (breakfast VARCHAR(25), lunch VARCHAR(25), dinner VARCHAR(25))");
String breakfast = JOptionPane.showInputDialog("What was your breakfast?");
String lunch = JOptionPane.showInputDialog("What was your lunch?");
String dinner = JOptionPane.showInputDialog("What was your dinner?");
statement.executeUpdate("INSERT INTO food (breakfast, lunch , dinner) VALUES ('"+breakfast+"', '"+lunch+"' ,'"+dinner+"')");
Change your CREATE TABLE to
statement.executeUpdate("create table food (breakfast text, lunch text, dinner text)");
Your INSERT INTO statement is escaping single ' quotes which isn't required. Change to
statement.executeUpdate("insert into food values('"+breakfast+"', '"+lunch+"' ,'"+dinner+"')");
There's no string data type in SQLite but it doesn't give an error because SQLite's SQL isn't strongly typed. The column affinity takes over and treats any unknown data type as NUMERIC. Better switch the column data type to TEXT or VARCHAR.

Query to output content of AllXml column of ELMAH_Error sql server table

I am having trouble writing query so that I can query the content of AllXml column inside Elmah_Error table.
How can I list out all the item nodes as an output of the query.
How could I write query to only list for certain item nodes?
I would like to get follow resultset:
item value
===== =====
ALL_HTTP HTTP_CONNECTION:xxxx
ALL_RAW Connection: xxxxx
I would also like to be able to filter the query by ErrorID
Content of AllXml column may look like this.
<error
application="/"
message="hello world"
source="TestWebElmah"
detail="xxxxx">
<serverVariables>
<item
name="ALL_HTTP">
<value
string="HTTP_CONNECTION:xxxx" />
</item>
<item
name="ALL_RAW">
<value
string="Connection: xxxxx" />
</item>
</serverVariables>
</error>
Remote Addr nodes
select T.N.value('(value/#string)[1]', 'varchar(30)') as REMOTE_ADDR
from
(select cast(AllXml as xml) as AllXml from ELMAH_Error) e
cross apply AllXml.nodes('//item[#name="REMOTE_ADDR"]') as T(N)
HTTP User Agents which contain mozilla
select T.N.value('(value/#string)[1]', 'varchar(30)') as HTTP_USER_AGENT
from
(select cast(AllXml as xml) as AllXml from ELMAH_Error) e
cross apply AllXml.nodes('//item[#name="HTTP_USER_AGENT"]') as T(N)
where T.N.value('(value/#string)[1]', 'varchar(30)') like '%mozilla%'
Elmah table stores the AllXml column as nvarchar so it needs to be casted to xml
all tags + values, by error id
select T.N.value('#name', 'varchar(30)') as Name,
T.N.value('(value/#string)[1]', 'varchar(30)') as Value
from
(select cast(AllXml as xml) as AllXml from ELMAH_Error where ErrorId = 'DC82172B-F2C0-48CE-8621-A60B702ECF93') e
cross apply AllXml.nodes('/error/serverVariables/item') as T(N)
Before voting down this answer, because uses most of the part of Mikael Eriksson's answer, I let you know I'll happily accept the downvotes only for this reason, since is mainly true
This query will give you all item nodes
select T.N.value('#name', 'varchar(30)') as Name,
T.N.value('(value/#string)[1]', 'varchar(30)') as Value
from Elmah_Error
cross apply AllXml.nodes('/error/serverVariables/item') as T(N)
If you want to filter on any of the values you can put that in a sub-query apply a regular where clause.
select Name,
Value
from
(
select T.N.value('#name', 'varchar(30)') as Name,
T.N.value('(value/#string)[1]', 'varchar(30)') as Value
from Elmah_Error
cross apply AllXml.nodes('/error/serverVariables/item') as T(N)
) T
where Name = 'ALL_HTTP'

Resources