i have this query:
update B
set B.NBR_OF_BACKUP=B.NBR_OF_BACKUP - 1
FROM BACKUP_TABLE B
INNER JOIN tbl I ON B.ID_BACKUP=i.id_backup
in the table tbl i have:
ID_BACKUP ID_IMAGES
1 2
1 3
1 4
6 3
my query is updating only for distinct id_backup
but i need also to update the table BACKUP_TABLE as many times as the same id_backup is in the table tbl
I think what you want is this:
update B
set B.NBR_OF_BACKUP=I.NBR_OF_BACKUP - 1
FROM BACKUP_TABLE B
INNER JOIN (SELECT id_backup, count(*) as nbr_of_backup FROM tbl GROUP BY id_backup) I
ON B.ID_BACKUP=i.id_backup
Related
I have two tables
id
val
1
a
1
b
1
c
2
d
2
e
3
f
and
id
1
2
What I want is a count of the number of times an ID appears from the first table ONLY IF if it exists in the second table. How can I do this?
Example output:
id
count
1
3
2
2
3
0
Would you like to show ids that do not exist in the first table?
I made it show according to the ids that exist in the first table, if you want it to show up please comment below
select tb7.id, COUNT(tb6.id) as count
from Table_6 tb6 inner join Table_7 tb7 on tb6.id = tb7.id
group by tb7.id
You can use left outer join and count as follows:
Select t1.id, count(t2.id) as cnt
From table1 t1 left join
table2 t2
on t1.id = t2.id
Group by t1.id;
I have two tables
TableA
ID Qualification
1 A
2 A
3 B
TableB
ID Qualification
1 C
2 A
3 A
Unfortunately, the names of the columns in table A and B are the same - resulting in an error 8156 - The column 'Qualification' was specified multiple times.
My select looks like follows
SELECT *
FROM (
SELECT A.ID, A.Qualification, B.Qualification
FROM TableA A LEFT OUTER JOIN TableB B
ON A.ID = B.ID
)s
PIVOT
(SUM(ID)
FOR Qualification IN ([A],[B],[C])) pvt
TIA!
I have a table old_data and a table new_data. I want to write a select statement that gives me
Rows in old_data stay there
New rows in new_data get added to old_data
unique key is id so rows with id in new_data should update existing ones in old_data
I need to write a select statement that would give me old_data updated with new data and new data added to it.
Example:
Table a:
id count
1 2
2 19
3 4
Table b:
id count
2 22
5 7
I need a SELECT statement that gives me
id count
1 2
2 22
3 4
5 7
Based on your desired results:
SELECT
*
FROM
[TableB] AS B
UNION ALL
SELECT
*
FROM
[TableA] AS A
WHERE
A.id NOT IN (SELECT id FROM [TableB])
I think this would work pretty neatly with COALESCE:
SELECT a.id, COALESCE(b.count, a.count)
FROM a
FULL OUTER JOIN b
ON a.id = b.id
Note - if your RDBMS does not contain COALESCE, you can write out the function using CASE as follows:
SELECT a.id,
CASE WHEN b.count IS NULL THEN a.count
ELSE b.count END AS count
FROM ...
You can write a FULL OUTER JOIN as follows:
SELECT *
FROM a
LEFT JOIN b
ON a.id = b.id
UNION ALL
SELECT *
FROM b
LEFT a
ON b.id = a.id
You have to use UPSERT to update old data and add new data in Old_data table and select all rows from Old_data. Check following and let me know what you think about this query
UPDATE [old_data]
SET [count] = B.[count]
FROM [old_data] AS A
INNER JOIN [new_Data] AS B
ON A.[id] = B.[id]
INSERT INTO [old_data]
([id]
,[count])
SELECT A.[id]
,A.[count]
FROM [new_Data] AS A
LEFT JOIN [old_data] AS B
ON A.[id] = B.[id]
WHERE B.[id] IS NULL
SELECT *
FROM [old_data]
This one is hard to explain and I'm sure I will facepalm when I see the solution, but I just can't get it right...
I have three tables:
Table A contains new records that I want to do something with.
Table B contains all activities from Table C of a specific type (done beforehand).
Table C is sort of a "master" table that contains all activities as well as a customer id and a lot of other stuff.
I need to select all activities that is in Table A from Table B. So far so good.
The part I can't get together is that I also need all the activities from Table B that has the same customer id as an activity contained in Table A.
This is what I'm after:
activity
2
3
4
5
6
The trick here is to get activity 2, because activity 2 is also done by customer 2, even though it is not in Table A.
Here are the tables:
Table A (new records)
activity
3
4
5
6
Table B (all records of a specific type from Table C)
activity
1
2 <-- How do I get this record as well?
3
4
5
6
Table C (all records)
activity customer
1 1
2 2
3 2
4 3
5 3
6 4
7 5
I tried something like this...
SELECT *
FROM table_b b
INNER JOIN table_c c
ON c.activity = b.activity
INNER JOIN table_a a
ON a.activity = b.activity
... but of course it only yields:
activity
3
4
5
6
How can I get activity 2 as well here?
To do this returning one column I would recommend staging the customer_ids of activities in Table_b that are in Table_a into a CTE (common table expression MSDN CTE) then select activities in table_c and join to the CTE to get only activities with a valid customer_id.
example of CTE: (Note the semi-colon ; before the WITH keyword is workaround for an issue in SQL 2005 with multiple statements. It it not necessary if you are in a newer version, or not running batch statements.)
;WITH cte_1 AS (
SELECT distinct c.customer --(only need a distinct result of customer ids)
from table_b b
join table_a a on b.activity = a.activity --(gets only activities in b that are in a)
join table_c c on b.activity = c.activity --(gets customer id of activies in table b)
)
SELECT a.activity
FROM table_c a
JOIN cte_1 b ON a.customer = b.customer
Alternatively you could do this in three joins with a select distinct. However I find the CTE to be an easier way to develop and think about this problem regardless of the way you decide to implement your solution. Although the three join solution will most likely scale and perform better over time with a growing data-set.
Example:
SELECT distinct d.activity
from table_b b
join table_a a on b.activity = a.activity --(gets only activities in b that are in a)
join table_c c on b.activity = c.activity --(gets customer id of activies in table b)
join table_c d ON c.customer = d.customer
Both would output:
2
3
4
5
6
Here is one way to do it
SELECT *
FROM TableB b1
WHERE EXISTS (SELECT 1
FROM Tablec c1
WHERE EXISTS (SELECT 1
FROM TableA a
INNER JOIN Tablec c
ON a.activity = c.activity
WHERE c.customer = c1.customer)
AND c1.activity = b1.activity)
Can you try doing a left join?
SELECT *
FROM table_b b
INNER JOIN table_c c
ON c.activity = b.activity
LEFT JOIN table_a a
ON b.activity = a.activity
select * from tbl_a;
SRNO T_TEXT
1 a
1 b
1 c
2 a
2 b
3 a
3 b
4 a
----
select * from tbl_b;
SRNO T_TEXT
1
1
1
2
2
3
3
4
i want to update tbl_b.t_text with values from tbl_a.t_text.
i do that it gives returned too many rows. I can do this trough a for loop,
but want it through update statement only.
Here is the SQL I've tried#
update tbl_b b
set b.t_text = (select a.t_text from tbl_a a
where a.srno = b.srno and b.t_text is null)
where exists ( select 1 from tbl_a c
where c.srno = b.srno);
It throws error single row subquery return more than one row.
You can use this code to update tbl_b
begin
for i in (select a.* from tbl_a a) loop
update tbl_b b
set b.t_text=i.t_text
where b.srno=i.srno
and b.t_text is null
and rownum=1;
end loop;
end;
I wish it helps you.
you can use Below query
UPDATE tbl_a
SET tbl_a.T_TEXT=(SELECT tbl_b.T_TEXT
FROM tbl_b
WHERE tbl_b.SRNO=tbl_a.SRNO);