Oracle SQL : Updating a column with count query of another table - sql

I have two tables.
The first one "DISORDERS" with the fields: "ID" number, "Description" varchar2, "MINIMUM_SYMPTOMS" number
with these data:
The second table is "SYMPTOMS" with the fields: "ID" number, "SYMPTOM" varchar2, "DISORDER_ID" number (FK_DISORDERS_ID)
In my second table i have these data:
With this query i can find the minimum symptoms (records from the second table) in order for my patient to have the disorder:
select DISORDERS.DISORDER as DISORDER, ROUND((COUNT(SYMPTOMS.ID) / 2) ,0) as MINIMUM_SYMPTOMS
from SYMPTOMS SYMPTOMS,
DISORDERS DISORDERS
where SYMPTOMS.DISORDER_ID=DISORDERS.ID
group by DISORDERS.DISORDER
and i am getting correctly this result:
I want to update in my first table the field "MINIMUM_SYMPTOMS" and put the MINIMUM_SYMPTOMS value from the last query.

I didn't feel like typing that much so I created smaller sample data set.
SQL> select * from disorders order by id;
ID DISO MINIMUM_SYMPTOMS
---------- ---- ----------------
64 OCRD 0
65 OCD 0
248 GPD 0
SQL> select * from symptoms order by disorder_id, id;
ID SYMPTO DISORDER_ID
---------- ------ -----------
2 test 3 64
3 test 1 64
4 test 5 64
5 test 2 64
7 test 4 64
3 test 1 65
5 test 2 65
7 test 4 65
3 test 1 248
4 test 3 248
5 test 2 248
11 rows selected.
Query - similar to yours - but based only on the symptoms table. Why? You don't need disorders now; this query will be used a little bit later, in merges using clause:
SQL> select s.disorder_id,
2 round((count(s.id) / 2), 0) minsym
3 from symptoms s
4 group by s.disorder_id;
DISORDER_ID MINSYM
----------- ----------
248 2
64 3
65 2
SQL>
OK, let's now merge those results with the disorders table:
SQL> merge into disorders d
2 using (select s.disorder_id,
3 round((count(s.id) / 2), 0) minsym
4 from symptoms s
5 group by s.disorder_id
6 ) x
7 on (d.id = x.disorder_id)
8 when matched then update set
9 d.minimum_symptoms = x.minsym;
3 rows merged.
Result:
SQL> select * from disorders order by id;
ID DISO MINIMUM_SYMPTOMS
---------- ---- ----------------
64 OCRD 3
65 OCD 2
248 GPD 2
SQL>
So, yes - that's "how" you'll do that (at least, one option).

You can use update with a correlated subquery:
update disorders d
set minimum_symptoms = (select round(count(*) / 2)
from symptoms s
where s.disorder_id = d.id
);
Your example is updating all rows in disorders. If you only wanted to update rows that have a match in symptoms:
update disorders d
set minimum_symptoms = (select round(count(*) / 2)
from symptoms s
where s.disorder_id = d.id
)
where exists (select 1
from symptoms s
where s.disorder_id = d.id
);

Related

How to get the difference between two rows in a table

I have a query which returns the below data set.
Sample:
SELECT *
FROM table_name
WHERE rank_code IN (15, 22);
The output of the above query gives the below result.
RANK
RANK_CODE
RANK_DESC
3
15
XYZ
5
22
ABC
Now, I would like to get the difference of Rank i.e., 5-3 = 2 or 3-5 = 2 in a single query. Only the difference. Is there a way to achieve the same using a SQL Query so that I may pass the rank codes as input parameter.
And if any row is not found or cannot be fetched the final result will be 0. Can we put this little check as well?
Any help will be appreciated.
SELECT coalesce(max(rank) - min(rank), 0)
FROM table_name
WHERE rank_code IN (15, 22);
If - in real life - that table contains many rows and you actually want to find difference between every two consecutive rows, consider one of analytic functions (LAG or LEAD). For example:
SQL> WITH
2 test (RANK, rank_code, rank_desc)
3 AS
4 (SELECT 3, 15, 'XYZ' FROM DUAL
5 UNION ALL
6 SELECT 5, 22, 'ABC' FROM DUAL)
7 SELECT RANK,
8 LEAD (RANK) OVER (ORDER BY RANK) next_rank,
9 RANK - LEAD (RANK) OVER (ORDER BY RANK) diff,
10 --
11 rank_code,
12 rank_desc
13 FROM test;
RANK NEXT_RANK DIFF RANK_CODE RAN
---------- ---------- ---------- ---------- ---
3 5 -2 15 XYZ
5 22 ABC
SQL>

SQL Server : create batch of Employees and prevent one emp going to multiple batches

My table contains data about Employee. However it is a temporary table and EmployeeID here isn't the primary key. The table may contain a given EmployeeID multiple times.
Now, I have to select batch of records of batchSize, let's consider 200 for now. I'll send these batches to multiple threads.
I have written this query:
WITH SingleBatch AS
(
SELECT
*,
ROW_NUMBER() OVER(ORDER BY EmployeeId) AS RowNumber
FROM
TemperoryTable
)
SELECT *
FROM SingleBatch
WHERE RowNumber BETWEEN 1 AND 200;
the result might be:
EmployeeID EffectiveDate
1 123 01/01/2016
2 541 01/01/2016
------------------------
------------------------
200 978 18/06/2015
for one batch.
This works fine and row numbers change with thread number.
Now suppose, second batch starts with EmployeeId 978. Then this employee will be in first batch as well as second batch. That is, same employee is being sent to multiple threads and may possibly cause conflict.
Although the scenario is very rare, I must avoid this.
What could be the possible solution here?
Sorry I don't get it before, you wish same empolyee can be gotten together? but the total return rows count possible is not fix number. May this is helpful for you.
;WITH t(RowNumber,EmployeeId,other)AS
(
SELECT 1,'a','1' UNION ALL
SELECT 2,'a','12' UNION ALL
SELECT 3,'a','13' UNION ALL
SELECT 4,'b','21' UNION ALL
SELECT 5,'d','41' UNION ALL
SELECT 6,'c','31' UNION ALL
SELECT 7,'c','32'
)
SELECT *,DENSE_RANK()OVER(ORDER BY EmployeeId) AS FilterID,RANK()OVER(ORDER BY EmployeeId) RowsCount FROM t
RowNumber EmployeeId other FilterID RowsCount
----------- ---------- ----- -------------------- --------------------
2 a 12 1 1
3 a 13 1 1
1 a 1 1 1
4 b 21 2 4
6 c 31 3 5
7 c 32 3 5
5 d 41 4 7
Same employeeid has same FilterID, and the RowsCount to control return rows count.
You should get data by RowsCount but rownumber.
For example:
Actual return 6 lines when the RowsCount between 1 and 5.
because the employeeID c have two lines.
Between mean RowNumber>=1 and RowNumber<=200
So next batch should be
RowNumber BETWEEN 201 AND 400
also you can change where clause to
RowNumber>=1 and RowNumber <200 (1-199)
RowNumber>=200 and RowNumber <400 (200-399)

Highest per each group

It's hard to show my actual table and data here so I'll describe my problem with a sample table and data:
create table foo(id int,x_part int,y_part int,out_id int,out_idx text);
insert into foo values (1,2,3,55,'BAK'),(2,3,4,77,'ZAK'),(3,4,8,55,'RGT'),(9,10,15,77,'UIT'),
(3,4,8,11,'UTL'),(3,4,8,65,'MAQ'),(3,4,8,77,'YTU');
Following is the table foo:
id x_part y_part out_id out_idx
-- ------ ------ ------ -------
3 4 8 11 UTL
3 4 8 55 RGT
1 2 3 55 BAK
3 4 8 65 MAQ
9 10 15 77 UIT
2 3 4 77 ZAK
3 4 8 77 YTU
I need to select all fields by sorting the highest id of each out_id.
Expected output:
id x_part y_part out_id out_idx
-- ------ ------ ------ -------
3 4 8 11 UTL
3 4 8 55 RGT
3 4 8 65 MAQ
9 10 15 77 UIT
Using PostgreSQL.
Postgres specific (and fastest) solution:
select distinct on (out_id) *
from foo
order by out_id, id desc;
Standard SQL solution using a window function (second fastest)
select id, x_part, y_part, out_id, out_idx
from (
select id, x_part, y_part, out_id, out_idx,
row_number() over (partition by out_id order by id desc) as rn
from foo
) t
where rn = 1
order by id;
Note that both solutions will only return each id once, even if there are multiple out_id values that are the same. If you want them all returned, use dense_rank() instead of row_number()
select *
from foo
where (id,out_id) in (
select max(id),out_id from foo group by out_id
) order by out_id
Finding max(val) := finding the record for which no larger val exists:
SELECT *
FROM foo f
WHERE NOT EXISTS (
SELECT 317
FROM foo nx
WHERE nx.out_id = f.out_id
AND nx.id > f.id
);

selecting a variable amount of records based on the sum of columns values

I have a table with a column that's a float.
I order the table by created_at:
Foo.order(:created_at)
I want to select from that ordered table a variable amount of records.
Its varied because I need to sum the values of the float column on the records to get a value greater than a target float.
So lets say I have ordered my table above and it has these 4 records, with their float values:
30
50
40
20
if my target float is 60, i want to return just the first 2 records
if my target float is 100, i want to return the 3 records
if my target float is 20, i want just the first record
Is there a way to do this in active record or straight sql?
Is it faster to do it this way, or should I query for all the records and use only the ones i need?
Edit:
Building on the problem, I want to select from tables that have been first ordered by one of their columns lets say fizz:
select foo1.id,
(select coalesce(sum(f2.amount),0)
from (select * from foo order by fizz) f2
where f2.id < f1.id) as amount_total
from (select * from foo order by fizz)
group by f1.id
having amount_total < target_total
This works, but isnt ideal, as you're ordering the table twice. Is there a way to order the table once, and then use it for both f1 and f2?
Assuming the table foo with id, bar and fizz columns:
id bar fizz
-- --- ----
1 30 3
2 50 2
3 40 1
4 20 4
You can do this in SQL but I don't think you could do it in ActiveRecord.
select f1.id, f1.bar, f1.fizz,
(select coalesce(sum(f2.bar),0)
from foo f2
where f2.fizz < f1.fizz) as bar_total
from foo f1
group by f1.id, f1.bar, f1.fizz
having bar_total < TEST_VALUE
order by f1.fizz;
Note: As pointed out in the comments below you may or may not need the group by clause. sqlite3 requires it but mysql does not.
For example, if TEST_VALUE = 60:
id bar fizz bar_total
-- --- ---- -------------
3 40 1 0
2 50 2 40
If TEST_VALUE = 100:
id bar fizz bar_total
-- --- ---- -------------
3 40 1 0
2 50 2 40
1 30 3 90
If TEST_VALUE = 20:
id bar fizz bar_total
-- --- ---- -------------
3 40 1 0
maybe do something like this:
def check_sum(target)
if target == 60.00
return Foo.order(:created_at).limit(2)
elsif target == 100.00
return Foo.order(:created_at).limit(3)
elsif target == 20.00
return Foo.order(:created_at).limit(1)
end
end
you can also sort it ASC/DESC example Foo.order("created_at DESC")

How to track how many times a column changed its value?

I have a table called crewWork as follows :
CREATE TABLE crewWork(
FloorNumber int, AptNumber int, WorkType int, simTime int )
After the table was populated, I need to know how many times a change in apt occurred and how many times a change in floor occurred. Usually I expect to find 10 rows on each apt and 40-50 on each floor.
I could just write a scalar function for that, but I was wondering if there's any way to do that in t-SQL without having to write scalar functions.
Thanks
The data will look like this:
FloorNumber AptNumber WorkType simTime
1 1 12 10
1 1 12 25
1 1 13 35
1 1 13 47
1 2 12 52
1 2 12 59
1 2 13 68
1 1 14 75
1 4 12 79
1 4 12 89
1 4 13 92
1 4 14 105
1 3 12 115
1 3 13 129
1 3 14 138
2 1 12 142
2 1 12 150
2 1 14 168
2 1 14 171
2 3 12 180
2 3 13 190
2 3 13 200
2 3 14 205
3 3 14 216
3 4 12 228
3 4 12 231
3 4 14 249
3 4 13 260
3 1 12 280
3 1 13 295
2 1 14 315
2 2 12 328
2 2 14 346
I need the information for a report, I don't need to store it anywhere.
If you use the accepted answer as written now (1/6/2023), you get correct results with the OP dataset, but I think you can get wrong results with other data.
CONFIRMED: ACCEPTED ANSWER HAS A MISTAKE (as of 1/6/2023)
I explain the potential for wrong results in my comments on the accepted answer.
In this db<>fiddle, I demonstrate the wrong results. I use a slightly modified form of accepted answer (my syntax works in SQL Server and PostgreSQL). I use a slightly modified form of the OP's data (I change two rows). I demonstrate how the accepted answer can be changed slightly, to produce correct results.
The accepted answer is clever but needs a small change to produce correct results (as demonstrated in the above db<>fiddle and described here:
Instead of doing this as seen in the accepted answer COUNT(DISTINCT AptGroup)...
You should do thisCOUNT(DISTINCT CONCAT(AptGroup, '_', AptNumber))...
DDL:
SELECT * INTO crewWork FROM (VALUES
-- data from question, with a couple changes to demonstrate problems with the accepted answer
-- https://stackoverflow.com/q/8666295/1175496
--FloorNumber AptNumber WorkType simTime
(1, 1, 12, 10 ),
-- (1, 1, 12, 25 ), -- original
(2, 1, 12, 25 ), -- new, changing FloorNumber 1->2->1
(1, 1, 13, 35 ),
(1, 1, 13, 47 ),
(1, 2, 12, 52 ),
(1, 2, 12, 59 ),
(1, 2, 13, 68 ),
(1, 1, 14, 75 ),
(1, 4, 12, 79 ),
-- (1, 4, 12, 89 ), -- original
(1, 1, 12, 89 ), -- new , changing AptNumber 4->1->4 ges)
(1, 4, 13, 92 ),
(1, 4, 14, 105 ),
(1, 3, 12, 115 ),
...
DML:
;
WITH groupedWithConcats as (SELECT
*,
CONCAT(AptGroup,'_', AptNumber) as AptCombo,
CONCAT(FloorGroup,'_',FloorNumber) as FloorCombo
-- SQL SERVER doesnt have TEMPORARY keyword; Postgres doesn't understand # for temp tables
-- INTO TEMPORARY groupedWithConcats
FROM
(
SELECT
-- the columns shown in Andriy's answer:
-- https://stackoverflow.com/a/8667477/1175496
ROW_NUMBER() OVER ( ORDER BY simTime) as RN,
-- AptNumber
AptNumber,
ROW_NUMBER() OVER (PARTITION BY AptNumber ORDER BY simTime) as RN_Apt,
ROW_NUMBER() OVER ( ORDER BY simTime)
- ROW_NUMBER() OVER (PARTITION BY AptNumber ORDER BY simTime) as AptGroup,
-- FloorNumber
FloorNumber,
ROW_NUMBER() OVER (PARTITION BY FloorNumber ORDER BY simTime) as RN_Floor,
ROW_NUMBER() OVER ( ORDER BY simTime)
- ROW_NUMBER() OVER (PARTITION BY FloorNumber ORDER BY simTime) as FloorGroup
FROM crewWork
) grouped
)
-- if you want to see how the groupings work:
-- SELECT * FROM groupedWithConcats
-- otherwise just run this query to see the counts of "changes":
SELECT
COUNT(DISTINCT AptCombo)-1 as CountAptChangesWithConcat_Correct,
COUNT(DISTINCT AptGroup)-1 as CountAptChangesWithoutConcat_Wrong,
COUNT(DISTINCT FloorCombo)-1 as CountFloorChangesWithConcat_Correct,
COUNT(DISTINCT FloorGroup)-1 as CountFloorChangesWithoutConcat_Wrong
FROM groupedWithConcats;
ALTERNATIVE ANSWER
The accepted-answer may eventually get updated to remove the mistake. If that happens I can remove my warning but I still want leave you with this alternative way to produce the answer.
My approach goes like this: "check the previous row, if the value is different in previous row vs current row, then there is a change". SQL doesn't have idea or row order functions per se (at least not like in Excel for example; )
Instead, SQL has window functions. With SQL's window functions, you can use the window function RANK plus a self-JOIN technique as seen here to combine current row values and previous row values so you can compare them. Here is a db<>fiddle showing my approach, which I pasted below.
The intermediate table, showing the columns which has a value 1 if there is a change, 0 otherwise (i.e. FloorChange, AptChange), is shown at the bottom of the post...
DDL:
...same as above...
DML:
;
WITH rowNumbered AS (
SELECT
*,
ROW_NUMBER() OVER ( ORDER BY simTime) as RN
FROM crewWork
)
,joinedOnItself AS (
SELECT
rowNumbered.*,
rowNumberedRowShift.FloorNumber as FloorShift,
rowNumberedRowShift.AptNumber as AptShift,
CASE WHEN rowNumbered.FloorNumber <> rowNumberedRowShift.FloorNumber THEN 1 ELSE 0 END as FloorChange,
CASE WHEN rowNumbered.AptNumber <> rowNumberedRowShift.AptNumber THEN 1 ELSE 0 END as AptChange
FROM rowNumbered
LEFT OUTER JOIN rowNumbered as rowNumberedRowShift
ON rowNumbered.RN = (rowNumberedRowShift.RN+1)
)
-- if you want to see:
-- SELECT * FROM joinedOnItself;
SELECT
SUM(FloorChange) as FloorChanges,
SUM(AptChange) as AptChanges
FROM joinedOnItself;
Below see the first few rows of the intermediate table (joinedOnItself). This shows how my approach works. Note the last two columns, which have a value of 1 when there is a change in FloorNumber compared to FloorShift (noted in FloorChange), or a change in AptNumber compared to AptShift (noted in AptChange).
floornumber
aptnumber
worktype
simtime
rn
floorshift
aptshift
floorchange
aptchange
1
1
12
10
1
0
0
2
1
12
25
2
1
1
1
0
1
1
13
35
3
2
1
1
0
1
1
13
47
4
1
1
0
0
1
2
12
52
5
1
1
0
1
1
2
12
59
6
1
2
0
0
1
2
13
68
7
1
2
0
0
Note instead of using the window function RANK and JOIN, you could use the window function LAG to compare values in the current row to the previous row directly (no need to JOIN). I don't have that solution here, but it is described in the Wikipedia article example:
Window functions allow access to data in the records right before and after the current record.
If I am not missing anything, you could use the following method to find the number of changes:
determine groups of sequential rows with identical values;
count those groups;
subtract 1.
Apply the method individually for AptNumber and for FloorNumber.
The groups could be determined like in this answer, only there's isn't a Seq column in your case. Instead, another ROW_NUMBER() expression could be used. Here's an approximate solution:
;
WITH marked AS (
SELECT
FloorGroup = ROW_NUMBER() OVER ( ORDER BY simTime)
- ROW_NUMBER() OVER (PARTITION BY FloorNumber ORDER BY simTime),
AptGroup = ROW_NUMBER() OVER ( ORDER BY simTime)
- ROW_NUMBER() OVER (PARTITION BY AptNumber ORDER BY simTime)
FROM crewWork
)
SELECT
FloorChanges = COUNT(DISTINCT FloorGroup) - 1,
AptChanges = COUNT(DISTINCT AptGroup) - 1
FROM marked
(I'm assuming here that the simTime column defines the timeline of changes.)
UPDATE
Below is a table that shows how the distinct groups are obtained for AptNumber.
AptNumber RN RN_Apt AptGroup (= RN - RN_Apt)
--------- -- ------ ---------
1 1 1 0
1 2 2 0
1 3 3 0
1 4 4 0
2 5 1 4
2 6 2 4
2 7 3 4
1 8 5 => 3
4 9 1 8
4 10 2 8
4 11 3 8
4 12 4 8
3 13 1 12
3 14 2 12
3 15 3 12
1 16 6 10
… … … …
Here RN is a pseudo-column that stands for ROW_NUMBER() OVER (ORDER BY simTime). You can see that this is just a sequence of rankings starting from 1.
Another pseudo-column, RN_Apt contains values produces by the other ROW_NUMBER, namely ROW_NUMBER() OVER (PARTITION BY AptNumber ORDER BY simTime). It contains rankings within individual groups of identical AptNumber values. You can see that, for a newly encountered value, the sequence starts over, and for a recurring one, it continues where it stopped last time.
You can also see from the table that if we subtract RN from RN_Apt (could be the other way round, doesn't matter in this situation), we get the value that uniquely identifies every distinct group of same AptNumber values. You might as well call that value a group ID.
So, now that we've got these IDs, it only remains for us to count them (count distinct values, of course). That will be the number of groups, and the number of changes is one less (assuming the first group is not counted as a change).
add an extra column changecount
CREATE TABLE crewWork(
FloorNumber int, AptNumber int, WorkType int, simTime int ,changecount int)
increment changecount value for each updation
if want to know count for each field then add columns corresponding to it for changecount
Assuming that each record represents a different change, you can find changes per floor by:
select FloorNumber, count(*)
from crewWork
group by FloorNumber
And changes per apartment (assuming AptNumber uniquely identifies apartment) by:
select AptNumber, count(*)
from crewWork
group by AptNumber
Or (assuming AptNumber and FloorNumber together uniquely identifies apartment) by:
select FloorNumber, AptNumber, count(*)
from crewWork
group by FloorNumber, AptNumber