Hope youre doing well, need help with creating a SQL query (or multiple) to update the records in a table that its updated daily. The table has information about visits to stores from different users from the company that have to check if the store is in good conditions or not, it has 5 columns:
Key: String
Role: takes 3 values "rol1", "rol2" and "rol3" String
Date
User: String
Status: Boolean default is 1
The table is updated daily and need to check if there are duplicate values of the "key" attribute but with different roles values and change the "Status" to 0 for the old record and 1 (default) for the new record with duplicated key value based on the role, if the record has role value = "rol1" and a new record with "rol2" or "rol3" is added, need to keep both registers but change the status value to 0 to the previous record in the table with "rol1", thats in case the new record is "rol2" or "rol3". When the old record and the new record are both "rol1", need to change the status value based on the date, keep todays record with 1 and the older record to 0 (case that both records are "rol1").
When the existing record is "rol2" or "rol3" and the new record is "rol1", need to keep the old record with status 1 and the new to 0.
There can only be one true value for the status attribute with multiple keys.
12 FEB
KEY
ROLE
Col DATE
Col USER
STATUS
C KEY1
ROL1
DATE 1
TEXT
1
C KEY2
ROL2
DATE 3
TEXT
1
C KEY3
ROL1
DATE 3
TEXT
1
13 FEB
KEY
ROLE
Col DATE
Col USER
STATUS
C KEY1
ROL1
DATE 1
TEXT
0
C KEY2
ROL2
DATE 3
TEXT
1
C KEY3
ROL1
DATE 3
TEXT
0
C KEY1
ROL2
DATE 4
TEXT
1
C KEY3
ROL1
DATE 4
TEXT
1
C KEY2
ROL1
DATE 4
TEXT
0
It can be either a SQL query or maybe a Pandas script.
Thanks in advance guys!!
Was working on this query but soon realised it had to be different.
UPDATE table t
SET t.STATUS =
CASE WHEN t.ROLE = 'ROL2' THEN 1
WHEN EXISTS (
SELECT 1
FROM table t2
WHERE t.KEY = t2.KEY
AND t.ROLE = t2.ROLE
AND t2.ROLE = 'ROL1')
THEN 0
ELSE 1
END
Related
I'm writing a sql constraint about values on columns based on some conditions in Oracle database. My table is like below.(assume id is auto increment, also 'alpha' and 'beta' columns are numbers)
id alpha beta
--------------------------
1 1 0
2 1 1
3 0 0
4 0 0
5 2 3
6 4 1
If alpha value in two rows are same, only one row can be inserted with beta value of 1. In other words, i shouldn't insert a row with (1,1) values because there is already a row with beta value of 1.(look at the row with id=2). Any value besides 1 can be inserted freely. I need a useful control about that situation.
You can use a function-based index:
create unique index xxx on t(a, case when b = 1 then -1 else id end)
Here is a db<>fiddle.
You can use unique index with condition-based column as follows:
create unique index u123
on your_table (alpha, case when beta = 1 then beta else id end)
I am trying to get one row returned for each store number and per date that includes all values from the RecordTypeA column for that date.
The table I am using is created with a column named "RecordTypeA", it is a bit data type with (1 and 0) entries. 1 equals Type A and 0 equals Type B.
What I am trying to do is show the value of the RecordTypeA column for the store if there are entries of 1 and / or 0 on the same date on the same row.
Scenario 1 (One row returns for the store for the date): RecordTypeA column value = '1'
There is one row in the table for the store and date and the RecordTypeA column = '1' :
Scenario 2 (Two Rows return for the store for the same date):
Row One - RecordTypeA = '1'
Row Two - RecordTypeA = '0' (The column is still named RecordTypeA, but value '0' means something different so I want to create a column name?)
Scenario 3 (One row returns for the store for the date):
RecordTypeA column value = '0'
There is one row in the table for the store and date and the RecordTypeA column = '0' :
My issue is that I am getting multiple rows returned when the store has a RecordtypeA = 0 and a RecordtypeA = 1 row. Which I need to return on the same row. (Create columns that hold both 1 and 0 or Null.
What I am getting is
StoreID Date RecordTypeA
1234 2020-01-04 0
1234 2020-01-05 0
1234 2020-01-05 1
Needed:
StoreID Date RecordTypeA RecordTypeB
1234 2020-01-04 0 NULL
1234 2020-01-05 0 1
I have tried adding in case statements but I have not been able to get the one row as needed. Also, searched and tried PIVOT statements (I don't truly understand PIVOTs) but I get an error on the RecordTypeA Bit type.
Case when s.RecordTypeA = '1' Then 'TypeA' Else 'Null' End as Type
Case when s.RecordTypeA = '0' Then 'TypeB' Else 'Null' End as Type
SELECT r.StoreID,
r.CreatedDate,
s.RecordTypeA
From Request r
Inner Join Stores s on r.id = s.id
Group by r.StoreID,
r.CreatedDate,
s.RecordTypeA
Welcome to stack overflow community!
Have you tried to construct 3 queries and use UNION ALL statement?
You can create a query for StoreID, Date, RecordTypeA, TypeB and Typec.
For example:
SELECT CONCAT(r.StoreID, r.CreatedDate, s.RecordTypeA) AS DATA, 'A' AS TYPE
FROM YOURDATABASE.YOURTABLE
WHERE (A CRITERIA FOR TYPE A)
UNION ALL
SELECT CONCAT(r.StoreID, r.CreatedDate, s.RecordTypeA) AS DATA, 'B' AS TYPE
FROM YOURDATABASE.YOURTABLE
WHERE (A CRITERIA FOR TYPE B)
UNION ALL
...
I use the same alias on the example because with union all, all the queries must have the same columns, so you can use a CONCAT to put all your data from the different queries in one column, the column "TYPE" is for the difference the queries at the result.
Even if you not use concat you can return the columns different but all the queries must have the same column count on the select.
With multiple queries, you can define de criteria you want for type A, B, C through Z but have all of them at one result.
Important: Union all statements are somehow heavy for performance, so have it in mind.
So I found these 2 articles but they don't quite answer my question...
Find max value and show corresponding value from different field in SQL server
Find max value and show corresponding value from different field in MS Access
I have a table like this...
ID Type Date
1 Initial 1/5/15
1 Periodic 3/5/15
2 Initial 2/5/15
3 Initial 1/10/15
3 Periodic 3/6/15
4
5 Initial 3/8/15
I need to get all of the ID numbers that are "Periodic" or NULL and corresponding date. So I want a to get query results that looks like this...
ID Type Date
1 Periodic 3/5/15
3 Periodic 3/6/15
4
I've tried
select id, type, date1
from Table1 as t
where type in (select type
from Table1 as t2
where ((t2.type) Is Null) or "" or ("periodic"));
But this doesn't work... From what I've read about NULL you can't compare null values...
Why in SQL NULL can't match with NULL?
So I tried
SELECT id, type, date1
FROM Table1 AS t
WHERE type in (select type
from Table1 as t2
where ((t.Type)<>"Initial"));
But this doesn't give me the ID of 4...
Any suggestions?
Unless I'm missing something, you just want:
select id, type, date1
from Table1 as t
where (t.type Is Null) or (t.type = "") or (t.type = "periodic");
The or applies to boolean expressions, not to values being compared.
Hoping someone can help out here, I have the following data
Field 1 Field 2 Date Data
1 1 12/09/14 1
2 2 12/09/14 1
3 1 11/09/14 1
4 3 11/09/14 1
I need to write an sql query that sums all "Data" based on a date range and then anything that matches in Field 2. So if a line is out of the date range but the value in Field 2 matches another line that is within the date range, it should be included
For example, if I was to query everything for the 12/09/14, I want to see the sum of line 1, 2 and 3.... as line 3 is outside of the date range but it matches line 1 in the "Field 2" column. Line 4 should not be included as it is outside the range and does not have a matching value in "Field 2"
Any ideas?
I've been playing around with variations of queries but it either selects only the date range values or everything :(
EDIT:
Ok I've given Rajesh answer a try and it doesn't seem to include the data outside the date range. I was expecting the final sum in this example to equal 3 but it's only showing 2
select sum(a) from (
select sum(batch_m2_nett) as a
from batch_inf
where batch_date = to_date('30/09/15','DD/MM/RR')
union
select sum(f2.batch_m2_nett) as a
from batch_inf f1
inner join batch_inf f2
on f1.batch_date = to_date('30/09/15','DD/MM/RR')
and f1.batch_opt_start_batch = f2.batch_opt_start_batch
and f2.batch_date != to_date('30/09/15','DD/MM/RR')
);
SUM(A)
------
2
SQL> select batch_no, batch_opt_start_batch, batch_date, batch_m2_nett from batch_inf where batch_no in (8811,8812,8814);
BATCH_NO BATCH_OPT_START_BATCH BATCH_DATE BATCH_M2_NETT
-------- --------------------- --------------- -------------
8811 8814 30-SEP-15 1
8812 8814 30-SEP-15 1
8814 8814 01-OCT-15 1
the first statement gets sum of data values where date matches
the second statement gets sum of data values where field2 of matched date row is matching with other rows using self join
select SUM(s)
from
(
select SUM(data) as s
from fields
where date ='12/09/14'
union
select sum(f2.data) as s
from fields f1
inner join fields f2
on f1.date ='12/09/14'
and f1.field2 = f2.field2
and f2.date != '12/09/14'
) T
I need a SQL query which allow me to extract the current value of a column if this column has not be modified since the add of the object.
Or the last before the current value if the column was modified.
Note that the current value is saved in a table A and the history of modification in table B
Table A
ID
STATUS
other columns ....
Table B
ID
items_ID (which is the ID of element in table A)
modif_ID (indicate which column was modified)
old_value
new_value
other columns..
Thanks in advance,
Imene.
For exemple, For table A
id STATUS Title Description User
1 Closed Help Need help for configuring Apache IBH
2 Solved DSL DSL is down IBH
3 Assign Hardware Need new network cards IBH
For table B
id Items_id Modif_id Date_mod Old_value New value
1 1 1 12-02-2014 Assign solved
2 1 1 15-02-2014 solved closed
3 3 1 20-02-2014 New Assign
3 3 4 21-02-2014 hard Hardware
All modif where the modif_id!=1 are ignored.
For each element id (Table A), we extract the current status if the status dosen’t change or the last before the current value if the status is changed one or more time. The expected result is:
id STATUS
1 solved
2 solved
3 New