I have a table which looks like this :
------------------------
|id | status |value|
------------------------
|1 | Y |10 |
|2 | N |10 |
|3 | Y |10 |
|4 | N |10 |
|5 | N |10 |
------------------------
For every status = 'N', I'd like to add 3 to its value and set its status to 'Y'. So, the outcome table should be:
------------------------
|id | status |value|
------------------------
|1 | Y |10 |
|2 | Y |13 |
|3 | Y |10 |
|4 | Y |13 |
|5 | Y |13 |
------------------------
How can I do this in the best way in SQL Server?
update your_table
set status = 'Y',
value = value + 3
where status = 'N'
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 months ago.
Improve this question
This is only sample case, my original table is more complex.
Table A
| SchoolId| ClubId | ChildID|TeacherId|AttendanceDate|IsPresent|
|:------- |:------:| :-----:|:-------:|:------------:|:-------:|
| A | 1 | 1 |1 |22-MAY-2022 |1 |
| A | 1 | 2 |1 |22-MAY-2022 |0 |
| A | 1 | 3 |1 |22-MAY-2022 |1 |
| B | 2 | 11 |2 |22-MAY-2022 |1 |
| B | 2 | 22 |2 |22-MAY-2022 |0 |
| B | 2 | 33 |2 |22-MAY-2022 |0 |
Table B
|ChildID|TeacherId |CreateOn |IsPresent|ReasonId |
|:-----:|:-------: |:--------------:|:-------:|:-------:|
|2 |1 |22-MAY-2022 |0 |1 |
|2 |1 |23-MAY-2022 |0 |2 |
|22 |2 |22-MAY-2022 |0 |2 |
|33 |2 |22-MAY-2022 |0 |3 |
Table C
| ReasonId | ReasonMaster |
| -------- | -------------- |
| 1 | Health |
| 2 | Social |
| 3 | Unknown |
I want the left join result like this :
| SchoolId| ClubId | ChildID|TeacherId|AttendanceDate|IsPresent|ReasonId |ReasonMaster |
|:------- |:------:| :-----:|:-------:|:------------:|:-------:|:-------:|:------------:
| A | 1 | 1 |1 |22-MAY-2022 |1 | | |
| A | 1 | 2 |1 |22-MAY-2022 |0 |2 |Social |
| A | 1 | 3 |1 |22-MAY-2022 |1 | | |
| B | 2 | 11 |2 |22-MAY-2022 |1 | | |
| B | 2 | 22 |2 |22-MAY-2022 |0 |2 |Social |
| B | 2 | 33 |2 |22-MAY-2022 |0 |3 |Unknown |
Here are my cases:
I only want to retrieve the latest data from table B based on create on column on the table B to the table A. Because there is a duplicate input by users. for instance in the table B child Id= 2 & Teacher Id=1.
I only need to retrieve the the data if the status in the table A, column IsPresent=0
There is an additional data from table C which reason master.
Try this
Assuming MS SQL Server
--step 1 - Get the latest rec from table B
;with MaxCreate as ( Select ChildID,TeacherID,Max(CreateOn) as MaxCreateOn
from TableB
group by ChildID,TeacherID)
,LatestCreate as (select TableB.ChildID, TableB.TeacherId, TableB.CreateOn, TableB.IsPresent, TableB.ReasonId
from TableB
inner join MaxCreate
on TableB.ChildID = MaxCreate.ChildID
and TableB.TeacherId = MaxCreate.TeacherId
and TableB.CreateOn = MaxCreate.MaxCreateOn)
-- Now Join the latest to the other tables
Select
TableA.SchoolId
,TableA.ClubId
,TableA.ChildID
,TableA.TeacherId
,TableA.AttendanceDate
,TableA.IsPresent
,LatestCreate.ReasonId
,TableC.ReasonMaster
From
TableA Left join LatestCreate
on TableA.ChildID = LatestCreate.ChildID
and TableA.TeacherID = LatestCreate.TeacherID
left join TableC
on LatestCreate.ReasonId = TableC.ReasonId
Non CTE method
Select
TableA.SchoolId
,TableA.ClubId
,TableA.ChildID
,TableA.TeacherId
,TableA.AttendanceDate
,TableA.IsPresent
,LatestCreate.ReasonId
,TableC.ReasonMaster
From
TableB
inner join
( Select ChildID,TeacherID,Max(CreateOn) as MaxCreateOn
from TableB
group by ChildID,TeacherID) as MaxCreate
on TableB.ChildID = MaxCreate.ChildID
and TableB.TeacherId = MaxCreate.TeacherId
and TableB.CreateOn = MaxCreate.MaxCreateOn
Left join TableA
on TableA.ChildID = TableB.ChildID
and TableA.TeacherID = TableB.TeacherID
left join TableC
on TableB.ReasonId = TableC.ReasonId
i have a table named transaction.
acc_type are the types of transactions that exist.
income account = 101,102,103,104,105,106
outcome account = 111,112,113,114,115,116
In the example table below 101 is income and 111 is the Outcome.
___________________________________________
|id|mem_id|name|date |acc_type|amount |
-------------------------------------------|
| 1|m01 |A |1/6/2020|101 |150 |
| 2|m01 |A |2/6/2020|101 |200 |
| 3|m01 |A |3/6/2020|111 |100 |
| 4|m02 |B |3/6/2020|101 |150 |
| 5|m02 |B |3/6/2020|111 | 50 |
| 6|m02 |B |4/6/2020|101 |100 |
| 7|m03 |C |4/6/2020|102 |500 |
| 8|m03 |C |5/6/2020|112 | 50 |
| 9|m03 |C |5/6/2020|112 |100 |
--------------------------------------------
So, if the data has the same mem_id and acc_type = 101 will be aggregated. But if acc_type 111 will be substracted.
it is possible to create an up to date balance table like this :
_______________________________________________________
|id|mem_id|name|date |acc_type|amount |balance |
-------------------------------------------|------------
| 1|m01 |A |1/6/2020|101 |150 |150 |
| 2|m01 |A |2/6/2020|101 |200 |350 |
| 3|m01 |A |3/6/2020|111 |100 |250 |
| 4|m02 |B |3/6/2020|101 |150 |150 |
| 5|m02 |B |3/6/2020|111 | 50 |100 |
| 6|m02 |B |4/6/2020|101 |100 |200 |
| 7|m03 |C |4/6/2020|102 |500 |500 |
| 8|m03 |C |5/6/2020|112 | 50 |450 |
| 9|m03 |C |5/6/2020|112 |100 |350 |
--------------------------------------------------------
if yes,what is the best method to applied(is it view, trigger, procedure or transact?). I need this balance table especially the balance value of every account type for each member.
i'm using 10.4.6-MariaDB and vb.net for the interface
thanks.
i'm using postgre sql and i want to select data from database like this
------------------------------------
total1 | total2 | total3 | province|
------------------------------------
1 |1 |2 |Maluku |
2 |3 |4 |Aceh |
4 |7 |2 |Riau |
------------------------------------
but my result from query like this
------------------------------------
total1 | total2 | total3 | province|
------------------------------------
1 |1 |2 |Maluku |
1 |1 |2 |Aceh |
1 |1 |2 |Riau |
------------------------------------
my query
SELECT (SELECT COALESCE(COUNT(id),0) FROM vent) as total1,
(SELECT COALESCE(COUNT(id),0) FROM ventWHERE jenis='Vent-I') as total2,
(SELECT COALESCE(COUNT(id),0) FROM ventWHERE status='Terpakai') as total3,
b.provinsi as province
FROM public."admin_provinsi" as b LEFT JOIN
public."rs" as a
on a.provno = b.idprov
GROUP BY b.provinsi, b.idprov
ORDER BY total1 DESC
how to make result of the query like that?
my scheme database
table vent
|id|jenis |id_rs|status |
--------------------------
|1 |vent-i|1 |Terpakai|
|2 |vent-i|2 |Tidak |
table rs
|gid|name |provno|
-----------------
|1 |rs depok | 1 |
|1 |rs depok2| 1 |
table admin_provinsi
|idprov|nama |
---------------
|1 |Maluku|
|2 |Aceh |
|3 |Riau |
I have a data set with 4 columns and want to count the number of times that the value in column 2 was equal to the value in one of the rows in column 0 and also the number of times that the value in column 3 was equal to the value in one of the rows in column 0. Also I want to filter the data based on the value in column 1.
Here is an example:
|0 |1 |2 |3 |
-----------------------------------------
|a |post |b |c |
|x |share |a |d |
|b |post |a |l |
|d |post |N/A |a |
-----------------------------------------
the result should look like this:
|0 |1 |2 |3 |4 |5 |
-------------------------------------------------------------
|a |post |b |c |2 |1 |
|b |post |a |l |1 |0 |
|d |post |N/A |a |0 |1 |
-------------------------------------------------------------
Therefore I need to add two columns to my data set. My initial thought is that I can use nested query. Here's my code:
SELECT *
FROM
(
select t.*,
(select count(*) from
(
select t.*,
(select count(*) from tab where [2] = t.[0]) [4]
from tab t
)
where [3] = t.[0]) [5]
from tab t
)
WHERE [1] = 'post'
but the the result of my query does not return column 4. Can you help me figure out the problem with my code?
Your query contains almost the correct code:
select
t.*,
(select count(*) from tab where [2] = t.[0]) [4],
(select count(*) from tab where [3] = t.[0]) [5]
from tab t
where t.[1] = 'post'
See the demo.
Results:
| 0 | 1 | 2 | 3 | 4 | 5 |
| --- | ---- | --- | --- | --- | --- |
| a | post | b | c | 2 | 1 |
| b | post | a | l | 1 | 0 |
| d | post | N/A | a | 0 | 1 |
My I/P table is T1:
+----+------+----+------+
| Id | v1 |v2 |v3 |
+----+------+----+------+
| 1 | a |b |c |
| 2 | null |b |null |
| 3 | d |null|null |
| 4 | null |e |null |
| 5 | e |f |null |
+----+------+----+------+
My Requirement : I have to compare one row with the others on the basis of id's.If they have all the values same or null/empty then I have to club the values of id separated by commas.
Required output:
+----+---------------------+
| Id |v1 |v2 |v3 |
+----+---------------------+
| 1,2| a |b |c |
| 3,4| d |e |null |
| 5 | e |f |null |
+----+---------------------+
Please assist.I am trying to use while loop but it is taking me very long.
I want optimize solution as I have run the statement on large record set.