Using CASE WHEN To Update - sql

I have a few tables that look like
select * from stipend
order by subjectid, stipdate;
SUBJECTID STIPDATE AMOUNT
---------- ----------- ----------
10011 31-oct-2021 800
10111 31-jul-2019 2000
10111 31-jul-2021 1500
20022 31-jul-2020 1200
30033 29-feb-2020 1400
40044. 31-jul-2020 1200
40044 31-jul-2021 2000
50055 31-jul-2021 2000
50055 30-sep-2021 1000
select * from subject
order by subjectid;
SUBJECTID LNAME FNAME PROJID
---------- ------------ --------------------
10011 Indy Eva XYZ01
10111 Isner Monica XYZ04
11011 Dupont Marty XYZ05
20022 Jordan Sam XYZ01
30033 Jordan Mary XYZ01
40044 Belmont Renee XYZ02
50055 Pissaro Becky XYZ02
60066 Nadal Becky XYZ03
70077 Bardot Brigitte XYZ03
80088 null Eva XYZ03
90099 Garnet Larry XYZ04
And I want to update the stipends that are in Project XYZ01 and XYZ02 by 40% using the CASE WHEN construct. Obviously the two tables are connect by stipend.subjectid = subject.subjectid and the I would use the subject.projectid to determine this, but how would I do that with CASE WHEN?
This is how I did it before
update stipend
set amount = amount + (amount * .40)
where subjectid in
(select subjectid from subject
where subject.projid = 'XYZ01' or subject.projid = 'XYZ02');
and that worked, but now I need to do the same thing but with CASE WHEN.

Eddie, below is a solution that uses the CASE WHEN statement. But, as Mureinik mentioned on the comments, your solution is better than using a CASE WHEN statement in this case.
-- using CASE WHEN
update s
set s.amount = (case when x.projid in ('XYZ01','XYZ02')
then s.amount + (s.amount * .40)
else s.amount
end)
from stipend s
join subject x on x.subjectid = s.subjectid
Note: I did not add x.projid in ('XYZ01','XYZ02') to the where clause as that would defeat the purpose of using the CASE WHEN. So, this would run for all the rows.

Related

Using CASE WHEN across tables

I have a few tables that look like
SQL> select * from stipend
2 order by subjectid, stipdate;
SUBJECTID STIPDATE AMOUNT
---------- ----------- ----------
10011 31-oct-2021 800
10111 31-jul-2019 2000
10111 31-jul-2021 1500
20022 31-jul-2020 1200
30033 29-feb-2020 1400
40044. 31-jul-2020 1200
40044 31-jul-2021 2000
50055 31-jul-2021 2000
50055 30-sep-2021 1000
SQL> select * from subject
2 order by subjectid;
SUBJECTID LNAME FNAME PROJID
---------- ------------ --------------------
10011 Indy Eva XYZ01
10111 Isner Monica XYZ04
11011 Dupont Marty XYZ05
20022 Jordan Sam XYZ01
30033 Jordan Mary XYZ01
40044 Belmont Renee XYZ02
50055 Pissaro Becky XYZ02
60066 Nadal Becky XYZ03
70077 Bardot Brigitte XYZ03
80088 null Eva XYZ03
90099 Garnet Larry XYZ04
And I want to update the stipends that are in Project XYZ01 and XYZ02 by 40% using the CASE WHEN construct. Obviously the two tables are connect by stipend.subjectid = subject.subjectid and the I would use the subject.projectid to determine this, but how would I do that with CASE WHEN?
Have you tried this?
SELECT
SUBJECTID
,PROJID
,CASE WHEN ProjectID in ('XYZ01','XYZ02') THEN AMOUNT * 1.4 ELSE AMOUNT END as AMOUNT
FROM subject sb
LEFT JOIN stipend st ON st.SUBJECTID = sb.SUBJECTID
You may try this. Here I have used Stipend table as LEFT Table, you may alter this:
SELECT
st.SUBJECTID, sb.PROJID, Amount,
CASE WHEN ProjID in ('XYZ01','XYZ02') THEN AMOUNT * 1.4
ELSE AMOUNT
END as Updated_AMOUNT
FROM stipend st
LEFT JOIN subject sb
ON st.SUBJECTID = sb.SUBJECTID
Note: Using subject as left table may result in different level. Try them in Demo link below.
Demo
Pls check this. I have no data, so haven't tested it.
MySQL:
UPDATE
stipend st
join subject sub on st.SUBJECTID = sub.SUBJECTID
SET
AMOUNT = CASE WHEN
sub.ProjectID in ('XYZ01', 'XYZ02')
THEN AMOUNT * 1.4
ELSE AMOUNT
END
Oracle
I guess SQL Plus has similar syntax of Oracle DB, you can try this
Note: This syntax is converted online by https://www.sqlines.com/online . Pls check it
UPDATE
stipend st
join subject sub on st.SUBJECTID = sub.SUBJECTID
AMOUNT := CASE WHEN
sub.ProjectID in ('XYZ01', 'XYZ02')
THEN AMOUNT * 1.4
ELSE AMOUNT
END
Better to go for a regular join as left join would pull duplicate values in your result.
UPDATE STPIEND SET AMOUNT = CASE WHEN T1.AMOUNT IN ('XYZ01','XYZ02') THEN T1.AMOUNT * 1.4
ELSE T1.AMOUNT
END AS UPDATED_AMOUNT
FROM STIPEND T1 JOIN SUBJECT T2 ON T1.SUBJETID = T2.SUBJETID

Inputting data into a table from two different tables

I have these 3 tables with data
SQL> select * from subject;
SUBJECTID LNAME FNAME PROJID
---------- ------------ ---------- ----------
10011 Indy Eva XYZ01
20022 Jordan Sam XYZ01
30033 Jordan Mary XYZ01
40044 Belmont Renee XYZ02
50055 Pissaro Becky XYZ02
60066 Nadal Becky XYZ03
70077 Bardot Brigitte XYZ03
80088 null Eva XYZ03
90099 Garnet Larry XYZ04
10111 Isner Monica XYZ04
11011 Dupont Marty XYZ05
11 rows selected.
SQL> select * from project;
PROJID MEDICNAME PURPOSE START_DATE END_DATE PI_ID
---------- ---------- ------------ ----------- ----------- ----------
XYZ02 Medic1 diabetes 01-oct-2018 31-jul-2022 10001
XYZ01 Medic1 foot 01-sep-2019 31-jul-2021 10001
XYZ04 Medic3 spleen 10-jan-2019 31-jul-2021 10001
XYZ05 Medic5 spleen 10-jul-2020 1-jan-2021 10002
XYZ03 Medic3 lung 01-nov-2016 31-dec-2022 10002
SQL> select * from researcher;
PID LNAME FNAME
---------- ------------ ----------
10001 Elgar Dawn
10002 Jordan Daniel
10003 Jordan Athena
10004 Rivers Karen
10005 Gomez Tracy
10006 Gomez Jenny
10007 Perry Eva
10008 McHale Vicky
8 rows selected.
and then created a third table that looks like this
SQL> CREATE TABLE n_subject
2 (SubjID number(7),
3 Lastname varchar2(12),
4 Firstname varchar2(10));
I want to populate my new table with the Subjects who were involved in projects that were lead by Dawn Elgar (PID is 10001). Is there a way to do that across 3 tables? I am close with code that looks like this
SQL> insert into n_subject (subjid, lastname, firstname)
2 select subjectid, lname, fname
3 from subject where projid = 'XYZ01' or projid = 'XYZ02' or projid = 'XYZ04';
but am trying to get the data in there across all the three tables instead, using the ProjectId and the PID. Is this possible?
you can use select with inner join statement
INSERT INTO n_subject
(subjid, lastname, firstname)
SELECT subjectid,
lname,
fname
FROM [subject]
JOIN [project]
ON [project].projid = [subject].projid
WHERE [project].pi_id = '10001';

Display guest details who paid total charges RS.50000 and above. Write a query to fetch Guest id, Guest name and Sum of total charges

GUESTID NAME TOTALCHARGE
---------- ------------------------------ -----------
1234 RAJ GUPTA 30000
1235 JACK MARTIN 30000
1236 MARY GREY 32000
1234 RAJ GUPTA 30000
1235 JACK MARTIN 48000
This looks like an aggregate query with filtering:
select guestid, name, sum(totalcharge) sum_totalcharge
from mytable
group by guestid, name
having sum(totalcharge) >= 50000

Find duplicate batches based on multiple columns

I have a table that contains a series of related records (batches). Each batch has a unique id and can contain customer payments. I want to find if a batch is duplicate even if it is submitted on different days.
A batch can have 1 or more records. Here is sample data set:
BatchId InputAmount CustomerName BatchDate
------- ----------- ------------ ----------
182944 $475.00 Barry Smith 16-Mar-2019
182944 $260.00 John Smith 16-Mar-2019
182944 $265.00 Jane Smith 16-Mar-2019
182944 $400.00 Sara Smith 16-Mar-2019
182944 $175.00 Andy Smith 16-Mar-2019
182945 $475.00 Barry Smith 16-Mar-2019
182945 $260.00 John Smith 16-Mar-2019
182945 $265.00 Jane Smith 16-Mar-2019
182945 $400.00 Sara Smith 16-Mar-2019
182945 $175.00 Andy Smith 16-Mar-2019
183194 $100.00 Paul Green 21-Mar-2019
183195 $100.00 Nancy Green 21-Mar-2019
183197 $150.00 John Brown 20-Mar-2019
183197 $210.00 Sarah Brown 20-Mar-2019
183198 $150.00 John Brown 21-Mar-2019
183198 $210.00 Sarah Brown 21-Mar-2019
183200 $125.00 John Doe 20-Mar-2019
183200 $110.00 Sarah Doe 20-Mar-2019
183202 $125.00 John Doe 21-Mar-2019
183202 $110.00 Sarah Doe 21-Mar-2019
183202 $115.00 Paul Rudd 21-Mar-2019
Batches (182944, 182945) and (183197,183198) are duplicate while the other batches are not.
I thought maybe I could create a summary table with counts and sums and get close but I'm having trouble finding the true duplicates by including the names as well.
DECLARE #Summaries TABLE(
BatchId INT,
BatchDate DATETIME,
BatchCount INT,
BatchAmount MONEY)
-- Summarize the Data so we can look for duplicates
INSERT INTO #Summaries
SELECT a.BatchId, a.BatchDate, COUNT(*) AS RecordCount, SUM(a.InputAmount) AS BatchAmount
FROM Batches a
WHERE a.BatchDate BETWEEN '20190316' and '20190321'
GROUP BY a.BatchId, a.BatchDate
ORDER BY a.BatchId DESC
-- find the potential duplicate batches based on the Counts and Sums
SELECT A.* FROM #Summaries A
INNER JOIN (SELECT BatchCount, BatchAmount, BatchDate FROM #Summaries
GROUP BY BatchCount, BatchAmount, BatchDate
HAVING COUNT(*) > 1) B
ON A.BatchCount = B.BatchCount
AND A.BatchAmount = B.BatchAmount
WHERE DATEDIFF(DAY, a.BatchDate, b.BatchDate) BETWEEN -1 AND 1
Thank you for the help. I'm using a SQL Server 2012 database.
you can try like below
with cte as
(select BatchId from table_name
group by BatchId
having count(*)>1
) select * from table_name a where a.BatchId in (select BatchId from cte)

SQL - Return number times a value results from a query

I'm fairly new to SQL server (this is my second post on StackOverflow). I'm currently using SQL Server 2008 R2. I have a few tables that I've joined with the following query:
SELECT pt.first_name, pt.last_name, pt.provider_id,
pt.last_visit_date, pl.last_name
FROM dbo.pm_patient pt
INNER JOIN dbo.ProviderList pl
ON pt.provider_id = pl.provider_id
WHERE pt.last_visit_date >= '02/01/2012' AND pt.last_visit_date <= '02/01/2013'
ORDER BY provider_id
The result of the above query looks something like this:
first_name last_name provider_id last_visit_date last_name
Smith John 1 04/25/2012 Johnson
Doe Jane 1 02/25/2012 Johnson
Davies Ann 1 03/15/2012 Johnson
Dupree David 1 11/20/2012 Johnson
Jones Becky 1 04/21/2012 Smith
Diaz Mike 1 02/12/2012 Smith
Williams Allison 1 08/05/2012 Smith
Taylor Joe 1 10/01/2012 Smith
I would rather simply get the following result:
last_name NoOfPatients
Johnson 4
Smith 4
Could someone please help me?
Try :
SELECT pl.last_name, count(*) NoOfPatients
FROM dbo.pm_patient pt
INNER JOIN dbo.ProviderList pl
ON pt.provider_id = pl.provider_id
WHERE pt.last_visit_date >= '02/01/2012' AND pt.last_visit_date <= '02/01/2013'
GROUP BY pl.last_name
You need to read about how to use group in querys, basically what you need to do is count the number of patients and group by providers.
SELECT pt.last_name, COUNT(pt.*) NoOfPatients
FROM dbo.pm_patient pt
INNER JOIN dbo.ProviderList pl
ON pt.provider_id = pl.provider_id
WHERE pt.last_visit_date >= '02/01/2012' AND pt.last_visit_date <= '02/01/2013'
ORDER BY provider_id
GROUP BY pt.last_name