Update Query depending on Date - sql

RN_TESTCYCL_ID
RN_CYCLE_ID
RN_TEST_ID
RN_RUN_ID
RN_RUN_NAME
RN_EXECUTION_DATE (if RN_VTS Null, compare with this Date, format: DDMMYY)
RN_EXECUTION_TIME
RN_HOST
RN_STATUS
RN_DURATION
RN_TESTER_NAME
RN_PATH
RN_USER_01
RN_USER_02
RN_USER_03
RN_USER_04
RN_USER_05
RN_USER_06
RN_USER_07
RN_USER_08
RN_USER_09
RN_USER_10
RN_USER_11
RN_USER_12
RN_TEST_VERSION
RN_ATTACHMENT
RN_RUN_VER_STAMP
RN_VTS (compare with current Date, sometimes Null, Format: YYYYMMDDHH24MISS)
RN_CYCLE
RN_TEST_INSTANCE
RN_OS_NAME
RN_OS_SP
RN_OS_BUILD
RN_VC_LOKEDBY
RN_VC_STATUS
RN_VC_VERSION
RN_OS_CONFIG
RN_ASSIGN_RCYC
RN_BPTA_CHANGE_DETECTED
RN_BPTA_CHANGE_AWARENESS
RN_VC_VERSION_NUMBER
RN_PINNED_BASELINE
RN_TEST_CONFIG_ID
RN_DRAFT
RN_ITERS_PARAMS_VALUES
RN_ITERS_SUM_STATUS
RN_BPT_STRUCTURE
RN_STATE
RN_COMMENTS
RN_SUBTYPE_ID
RN_TEXT_SYNC
RN_ENVIRONMENT
RN_BUILD_REVISION
RN_DETAIL
RN_JENKINS_URL
RN_JENKINS_JOB_NAME
RN_RESULTS_FILES_NETWORK_PATH
I need to update every RN_Tester_name to "anonymous" if RN_VTS < DateX and if RN_VTS is Null then compare RN_EXECUTION_DATE < DateX
Can someone figure out a query to update RN_Tester_Name, i'm kinda stuck?

Use coalesce SQL function to select the first non-null column. Here you see an example oraclesql update statement:
update X set
rn_tester_name = 'anonymous'
where coalesce(rn_vts, rn_execution_date) < Y
I don't know how you can use that in your vb.net code thought.

Related

Is there any option to replace NVL in where clause for parameter

I have been using NVL in my WHERE clause and it worked well till now.
But in such case where the column has NULL value and parameter was also NULL, it didnt return any query.
select * from Table
where
f_date BETWEEN NVL(:F_DATE_FROM,F_DATE) AND NVL(:F_DATE_TO,F_DATE)
AND op_code = NVL(:CODE, OP_CODE)
AND T_CBC = NVL(:TO_CBC,T_CBC)
order by fiscal_date desc
I updated the query as below, and it returns me all the records as expected. However it takes way too long to execute the query. The original query took 1.5min and the new query takes 7min. Is there any way to fine tune the below query please?
select * from Table
where
f_date BETWEEN NVL(:F_DATE_FROM,F_DATE) AND NVL(:F_DATE_TO,F_DATE)
AND (OP_CODE = :CODE or :CODE is null)
AND (T_CBC = :TO_CBC or :TO_CBC is null)
order by fiscal_date desc
Sure:
WHERE
(f_date >= :F_DATE_FROM OR :F_DATE_FROM IS NULL) AND
(f_date <= :F_DATE_TO OR :F_DATE_TO IS NULL) AND
...
though I'm not sure how much of a performance improvement it'll realize. If your query is about performance specifically, ask a question that includes a query plan

Where statement in my query is supposed to return only today's and yesterday's dates, but is still returning earlier dates

I am looking to only retrieve data from the past 24 hours. The WHERE statement I am using, in theory, should retrieve only from those productiondates. However, I am still having week-old productiondates returned. Any thoughts on how to improve this, or am I doing it wrong? I am using periscope.
select example1,
example2,
example3,
productiondate,
example4,
example5
from final
where exampleX = exampleY or exampleX is null
and productiondate > DATEADD(day,-1, GETDATE())
and example1 <> 'XXX'
and example2 <> 'YYY'
and example2 <> 'ZZZ'
order by 2
Logical operator precedence in SQL can be surprising. You need parentheses around the OR.
where (exampleX = exampleY or exampleX is null)
Alternatively, you could do this:
where coalesce(exampleX, exampleY) = exampleY

How to add conditional statement inside SQL query?

Hi I am working on one SQL query. Below is my method name which executes query.
private string GetAggregatedOptionParametersByStyleIdCommand(string styleId, bool currentStore)
{
SELECT
opts.style, opts.option::text as {OptionKey}, opts.primary_colour, opts.secondary_colour, opts.brand_description, opts.description, params.*,
CASE WHEN {currentStore} == true THEN
FROM
rex.options opts
JOIN
rex.product_atoms atoms ON atoms.option_id = opts.option
JOIN
rex.parameters params ON atoms.id = params.product_atom_id
JOIN
rex.stores stores ON params.store = stores.id
WHERE
opts.style = '{styleId}'
}
Below is the structure of stores table.
CREATE TABLE rex.stores (
id serial NOT NULL,
close_date timestamp NOT NULL,
country text NULL,
distribution_centre text NULL,
"name" text NULL,
open_date timestamp NOT NULL,
CONSTRAINT "PK_stores" PRIMARY KEY (id)
);
So what I am trying to make is, Whenever the currentStore is true then I want to return the current store. Condition to check currentStore is
store.OpenDate <= currentDate &&
store.CloseDate >= currentDate
Whenever the currentStore is false I want to return all store. To check all store condition is
store.CloseDate >= currentDate
I am trying to add these conditions inside SQL query. I have added CASE WHEN {currentStore} == true THEN I am not sure how to add my closed store condition. Can anyone help me to complete this query? Any help would be appreciated. Thank you.
If you want to check for another possibility you can either add another WHEN or ELSE condition to your CASE statement. These need to come before the END.
For example:
CASE
WHEN x < 0 THEN 'negative'
WHEN x > 0 THEN 'positive'
ELSE 'zero'
END
I am not familiar with postgres, you may not need an END, and you may also be able to say CASE x WHEN > 0 THEN... WHEN < 0 ... meaning you only need to mention x once after CASE; again, this may not be a thing in postgres.

Use of After, Before, Include in Temporal Database

First of all thank you to read me and try to help me.
I am starting to working with temporal database, exactly with bitemporal database with the next structure:
CREATE TABLE poblat (
dni VARCHAR2(9),
name VARCHAR(12),
tiv DATE,
tfv DATE,
tit TIMESTAMP,
tft TIMESTAMP,
PRIMARY KEY (dni,tiv, tit)
);
I would to know how can i do a query using a clause like after, before or include.
For example i do this:
SELECT nombre, tiv, tfv FROM poblat
WHERE (tiv, tfv) INCLUDE (to_date('31/12/2014'), to_date('31/12/2016'));
But sql developer says that im using an "invalid relational operator".
Thank you for your attention and for your help.
Presumably your WHERE clause is specifying a date range and you are looking for records which fall within that range. If so:
SELECT nombre, tiv, tfv
FROM poblat
WHERE tiv > =to_date('31/12/2014') -- start of date range
and tfv <= to_date('31/12/2016') -- end of date range
;
there is no INCLUDE is Oracle's SQL. According to bitemporal db's document http://docs.marklogic.com/guide/temporal/searching#id_78584 , there's two consecutive Allen operators seem complying with include :
aln_equals : x-start = y-start and x-end = y-end aln_contains :
x-start < y-start and x-end > y-end
( where X and Y are both periods )
may result in Oracle :
SELECT nombre, tiv, tfv FROM poblat WHERE to_date(tiv,'dd/mm/yyyy') >= to_date('31/12/2014','dd/mm/yyyy') and to_date(tif,'dd/mm/yyyy') <= to_date('31/12/2016','dd/mm/yyyy');

Getting table(records) to update properply using the MERGE Statement

Good morning everyone!
Below is a piece of code I stitched together: I used a CTE to grab the records(data) from a link table and than convert strings to dates, than use the merge statement to get the data into a local table:
I am having a problem with the column(field) LAST_RACE_DATE this field is set to NULL and is not required but it does not update with my current set up. What I am trying to accomplished is for this field to populate when data is entered but also update, meaning it should also update with NULL.
So if the field has a specific date, and a new date is entered in the remote database, this field should update as well, even if the data is deleted in the back end, it should also remove the local table data for this field.
WITH CTE AS(
SELECT MEMBER_ID
,[MEMBER_DATE] = MAX(CONVERT(DATE, MEMBER_DATE))
,RACE_DATE = MAX(CONVERT(DATE, RACE_DATE))
,LAST_RACE_DATE = MAX(CONVERT(DATE, LAST_RACE_DATE))
FROM [EXAMPLE].[dbo].[LINKED_MEMBER_DATA]
WHERE (MEMBER_DATE IS NOT NULL) AND (ISDATE(MEMBER_DATE)<> 0) AND (RACE_DATE IS NOT NULL) AND (ISDATE(RACE_DATE)<> 0)
AND (LAST_RACE_DATE IS NULL) OR (ISDATE(LAST_RACE_DATE)<> 0)
GROUP BY MEMBER_ID)
MERGE dbo.LINKED_MEMBER_DATA AS Target
USING (SELECT
MEMBER_ID, MEMBER_DATE, RACE_DATE, LAST_RACE_DATE
FROM CTE
GROUP BY MEMBER_ID, RACE_DATE, LAST_RACE_DATE)AS SOURCE ON (Target.MEMBER_ID = SOURCE.MEMBER_ID)
WHEN MATCHED AND
(Target.MEMBER_DATE) <> (SOURCE.MEMBER_DATE)
OR (Target.RACE_DATE) <> (SOURCE.RACE_DATE)
OR ISNULL(TARGET.LAST_RACE_DATE , Target.LAST_RACE_DATE) <> ISNULL(SOURCE.LAST_RACE_DATE, SOURCE.LAST_RACE_DATE)
THEN UPDATE SET
Target.MEMBER_DATE = SOURCE.MEMBER_DATE
,Target.RACE_DATE = SOURCE.RACE_DATE
,Target.LAST_RACE_DATE = SOURCE.LAST_RACE_DATE
WHEN NOT MATCHED BY TARGET THEN
INSERT(
MEMBER_ID, MEMBER_DATE, RACE_DATE, LAST_RACE_DATE)
VALUES (Source.MEMBER_ID, Source.MEMBER_DATE, Source.RACE_DATE, Source.LAST_RACE_DATE);
I also tried this:
ISNULL(Target.LAST_RACE_DATE,'N/A') <> ISNULL(SOURCE.LAST_RACE_DATE,'N/A')
But it generates the below error for dates conversion:
Conversion failed when converting date and/or time from character string.
Thanks a Million!!
Your current statement is failing because the ISNULLs that you have don't do anything (if one of the values is NULL the expression will evaluate to NULL), and NULL values don't compare. Your second attempt doesn't work because ISNULL requires the data types of the two values to be the same, so you could try eg ISNULL(Target.LAST_RACE_DATE, '1970-01-01') <> ISNULL(Source.LAST_RACE_DATE, '1970-01-01').
Another option would be to simply enumerate the different cases (eg, (((Source.LAST_RACE_DATE IS NULL AND Target.LAST_RACE_DATE IS NOT NULL) OR (Source.LAST_RACE_DATE IS NOT NULL AND Target.LAST_RACE_DATE IS NULL) OR (Source.LAST_RACE_DATE <> Target.LAST_RACE_DATE))). Enumerating the different situations makes the code a bit more verbose, but it can result in better performance (whether it is measurably better really depends on how much data you are processing).