I have an Oracle table which is being loaded by a function - whenever it finds "LOW_MEMORY" in best_status, it will add the systimestamp in low_mem_timestamp column.
+----------+-------------------+-------+-------------------------------+
| device_id| best_status | job_id| low_mem_timestamp |
+----------+-------------------+-------+-------------------------------+
| 715016 | OPERATION_FAILURE | 511008|(null) |
| 715009 | LOW_MEMORY | 511008|10-MAY-17 11.13.22.143122000 AM|
| 715014 | DOWNLOAD_COMPLETE | 740004|(null) |
| 941015 | LOW_MEMORY | 740004|10-MAY-17 11.13.22.143122000 AM|
+----------+-------------------+-------+-------------------------------+
After this I have another table where i want to record the changes from above table
Whenever low_mem_timestamp changes for any device_id like:
if it had timestamp and now it got updated to "null" then it should add "1"
if it had null value and got updated to timestamp then "0"
Output table:
Condition:
device_id='715009' BEST STATUS moved from "LOW_MEMORY" to "UPDATE_DEFERRED" then low_mem_timstamp got updated to "null" then low_mem_timstamp should be "1"
device_id='715014' BEST STATUS moved from " DOWNLOAD_COMPLETE" to "LOW_MEMORY" then low_mem_timestamp got updated to some timestamp "any timestamp" then low_mem_timstamp should be "0"
device_id='941015' BEST STATUS remains same, it is not updated then low_mem_timstamp should be "NA"
Then in my final table output should be like
+----------+-------------------+-------+---------------+
| device_id| best_status | job_id| low_mem_toggle|
+----------+-------------------+-------+---------------+
| 715009 | UPDATE_DEFERRED | 511008|1 |
| 715014 | LOW_MEMORY | 740004|0 |
| 941015 | LOW_MEMORY | 740004|NA |
+----------+-------------------+-------+---------------+
Please suggest a sql query to implement this functionality.
Thanks in advance.
Related
I have a one tables in Postgresql and cannot find how to build a query.
The table contains columns nr_serii and deleteing_time. I trying to count nr_serii and substract from this positions with deleting_time.
My query:
select nr_serii , count(nr_serii ) as ilosc,count(deleting_time) as ilosc_delete
from MyTable
group by nr_serii, deleting_time
output is:
+--------------------+
| "666666";1;1 |
| "456456";1;0 |
| "333333";3;0 |
| "333333";1;1 |
| "111111";1;1 |
| "111111";3;0 |
+--------------------+
The part of table with raw data:
+--------------------------------+
| "666666";"2020-11-20 14:08:13" |
| "456456";"" |
| "333333";"" |
| "333333";"" |
| "333333";"" |
| "333333";"2020-11-20 14:02:23" |
| "111111";"" |
| "111111";"" |
| "111111";"2020-11-20 14:08:04" |
| "111111";"" |
+--------------------------------+
And i need substract column ilosc and column ilosc_delete
example:
nr_serii:333333 ilosc:3-1=2
Expected output:
+-------------+
| "666666";-1 |
| "456456";1 |
| "333333";2 |
| "111111";2 |
| ... |
+-------------+
I think this is very simple solution for this but i have empty in my head.
I see what you want now. You want to subtract the number where deleting_time is not null from the ones where it is null:
select nr_serii,
count(*) filter (where deleting_time is null) - count(deleting_time) as ilosc_delete
from MyTable
group by nr_serii;
Here is a db<>fiddle.
I need to create a query to determine if any records in a table contain NULLS in the EXPORTED column. If so then it will execute a stored procedure. I tried searching and I could not find anything that queries just one specific column. Below is an example of my table.
+--------+-----------+----------+
| SAMPLE | DATE | EXPORTED |
+--------+-----------+----------+
| S1234 | 9/17/2019 | NULL |
| S1435 | 9/17/2019 | NULL |
| S1536 | 9/17/2019 | YES |
+--------+-----------+----------+
I have a table that has a date column. For example:
| User | Date |
| 1 | 05-16-2016 |
| 1 | 07-28-2016 |
| ....
Notice how the format of the date is month-day-year. Basically I want to reformat it so that it does: day-month-year. So the resulting table would look like:
| User | Date |
| 1 | 16-05-2016 |
| 1 | 28-07-2016 |
| ....
Thanks alot in advance!
So i figured it out. Basically all you do is use format like this:
FORMAT(Date, "dd-mm-yyyy")
And I created a new column for the formatted date and used that column to carry out other computations
I have to update a value in one field of a table (t1).
Current table t1 records :
| POLNAME | VALUE |
|-------------------|
| TEST_01 | Normal |
| TEST_02 | High |
| TEST_03 | Normal |
| TEST_04 | Low |
| TEST_05 | Low** |
New table t1 records expected after the update :
| POLNAME | VALUE |
|-------------------|
| REST_01 | Normal |
| REST_02 | High |
| REST_03 | Normal |
| REST_04 | Low |
| REST_05 | Low** |
I need to replace, in t1-POLNAME field, the fixed value 'TEST_' by 'REST_' for all records of table t1.
I can do one by one using an UPDATE SQL command, but my goal is to replace all records using one SQL command.
You can use the REPLACE SQL FUNCTION.
UPDATE t1 SET POLNAME=REPLACE(POLNAME, 'TEST','REST');
You can use the REPLACE Syntax
UPDATE t1 SET POLNAME=REPLACE(POLNAME, 'TEST','REST');
I have data on approx 1000 individuals, where each individual can have multiple rows, with multiple dates and where the columns indicate the program admitted to and a code number.
I need each row to contain a distinct date, so I need to delete the rows of duplicate dates from my table. Where there are multiple rows with the same date, I need to keep the row that has the lowest code number. In the case of more than one row having both the same date and the same lowest code, then I need to keep the row that also has been in program (prog) B. For example;
| ID | DATE | CODE | PROG|
--------------------------------
| 1 | 1996-08-16 | 24 | A |
| 1 | 1997-06-02 | 123 | A |
| 1 | 1997-06-02 | 123 | B |
| 1 | 1997-06-02 | 211 | B |
| 1 | 1997-08-19 | 67 | A |
| 1 | 1997-08-19 | 23 | A |
So my desired output would look like this;
| ID | DATE | CODE | PROG|
--------------------------------
| 1 | 1996-08-16 | 24 | A |
| 1 | 1997-06-02 | 123 | B |
| 1 | 1997-08-19 | 23 | A |
I'm struggling to come up with a solution to this, so any help greatly appreciated!
Microsoft SQL Server 2012 (X64)
The following works with your test data
SELECT ID, date, MIN(code), MAX(prog) FROM table
GROUP BY date
You can then use the results of this query to create a new table or populate a new table. Or to delete all records not returned by this query.
SQLFiddle http://sqlfiddle.com/#!9/0ebb5/5
You can use min() function: (See the details here)
select ID, DATE, min(CODE), max(PROG)
from table
group by DATE
I assume that your table has a valid primary key. However i would recommend you to take IDas Primary key. Hope this would help you.