my table is
sh_name | sh_count | shoba
1 | 3 | 1
2 | 7 | 1
3 | 2 | 1
1 | 6 | 2
2 | 5 | 2
3 | 3 | 2
i want to update the max record in field sh_name from 3 to 11 in shoba 2
i use this code but not work
UPDATE sheet SET sh_count = 11 where sh_name = (select max(sh_name) from sheet where shoba = 2)
If you only want to update the one row you need to repeat the shoba = 2 condition like so:
UPDATE sheet SET sh_count = 11
WHERE sh_name = (SELECT MAX(sh_name) FROM sheet WHERE shoba = 2)
AND shoba = 2
Related
I have 2 tables A and B, as so:
A: B:
A_id | Val B_id | A_id
------------ ------------
1 | 1 1 | 1
2 | 1 2 | 1
3 | 2 3 | 2
4 | 1 4 | 2
5 | 3 5 | 5
6 | 1
I would like to select rows from A where Val = 1 OR those that are referenced in B.
So for this particular example, the select would retrieve:
A_id | Val
----------
1 | 1
2 | 1
4 | 1
5 | 3
Note that row 4 is not referenced in table B, but Val is equal to one, and Val from row 5 is != 1 but the row is referenced in table B.
I tried using the DISTINCT keyword but the problem is that it doesn't select row 4 because it is not referenced in table B.
Thanks for your help.
You can use EXISTS:
SELECT A.*
FROM A
WHERE A.Val = 1
OR EXISTS (SELECT 1 FROM B WHERE B.A_id = A.A_id);
How to calculate total length for all rows that have a certain value?
Let's say there's the following table:
id | unit_id | length | column to be filled with total length
1 | 1 | 10
2 | 1 | 4
3 | 1 | 5
4 | 2 | 3
5 | 3 | 3
6 | 3 | 6
In this case, how to update the table, making all the rows that have unit_id of 1 to have the sum of all the length of rows that have unit_id of 1 (10 + 4 + 5 = 19) then both rows that have a unit_id of 3 to have 9.
I've tried
update test.routes
set total_length = (select sum(length) from test.routes where unit_id = unit_id) where unit_id = unit_id
But what it does is that it just updates the entire table with the same value, how to update the correct sum for each unit_id?
try CTE:
t=# with a as (select *, sum(length) over (partition by unit_id) from routes)
t-# update routes u set total_length = a.sum
t-# from a
t-# where a.id = u.id;
UPDATE 6
Time: 0.520 ms
t=# select * from routes ;
id | unit_id | length | total_length
----+---------+--------+--------------
1 | 1 | 10 | 19
2 | 1 | 4 | 19
3 | 1 | 5 | 19
4 | 2 | 3 | 3
5 | 3 | 3 | 3
6 | 4 | 6 | 6
(6 rows)
You need to qualify the reference to attribute unit_id. Otherwise, a constraint like where unit_id = unit_id is (apart from null-values) always true and will therefore sum up everything:
update test.routes r1 set total_length = (select sum(length) from test.routes r2 where r2.unit_id = r1.unit_id)
This should do the work.
update
routes as s
inner join (
select unit_id, sum(length) as total_length from routes group by unit_id
) as g
set
s.total_length = g.total_length
where
s.unit_id = g.unit_id
Here we are creating a temporary table which has total length for the unit_id. By using the join between 2 tables we can do this bit efficiently then using a subquery
object_tbl:
objId(primary key) | name
1 | A
2 | B
3 | C
document_tbl:
documentId | sourceId
1 | 2
2 | 2
3 | 1
4 | 3
5 | 3
6 | 3
objToDoc_tbl:
id | objectId | documentId
1 | 1 | 2
2 | 2 | 4
3 | 2 | 6
4 | 1 | 5
5 | 3 | 1
6 | 1 | 2
Inner join of all table
A 2 2
B 4 3
B 6 3
A 5 3
C 1 2
A 2 2
A 2
B 1
C 1
so Answer is 1(As A is only 2)
Question: - How many object are related to documents from multiple sources.
How we can write sql query for this or it can only we solved by procedure
You can join all the tables and then use having to get objects with count > 1.
with t as (
select t.name, count(distinct d.sourceid) as count
from objtodoc_tbl o
join document_tbl d on o.documentid = d.documentid
join object_tbl t on o.objectid = t.name
group by t.name
having count(distinct d.sourceid) > 1
)
select count(t.name) from t;
I'm having trouble with SQLite query optimization, it runs fine, but for large tables it takes too much time and I need some help with optimizing it.
Source table:
-------+----------+----------------
IdMain | IdParent | ColumnToUpdate
-------+----------+----------------
1 | |
2 | 1 | 999 <-- IdParent = 1 \
3 | | \
4 | 5 | 123 > DISTINCT ITEMS COUNT = 1
5 | | / IdParent = 1
6 | 1 | 999 <-- IdParent = 1 / UPDATE Row with IdMain = IdParent
7 | 4 |
8 | 3 | 456
-------+----------+----------------
Query to optimize
UPDATE Table
SET ColumnToUpdate = (SELECT DISTINCT ColumnToUpdate
FROM Table T
WHERE T.ColumnToUpdate IS NOT NULL
AND T.IdParent = Table.IdMain)
WHERE Table.ColumnToUpdate IS NULL
AND (SELECT COUNT(*) FROM (SELECT DISTINCT ColumnToUpdate
FROM Table T2
WHERE T2.ColumnToUpdate IS NOT NULL
AND T2.IdParent = Table.IdMain)) = 1 ;
Expected table
-------+----------+----------------
IdMain | IdParent | ColumnToUpdate
-------+----------+----------------
1 | | 999 <-- UPDATE
2 | 1 | 999
3 | |
4 | 5 | 123
5 | |
6 | 1 | 999
7 | 4 |
8 | 3 | 456
-------+----------+----------------
Pseudo algorithm
FOR Row DO
BEGIN
IF ColumnToUpdate = NULL THEN
BEGIN
// count distinct values in ColumnToUpdate
X = COUNT(DISTINCT(ColumnToUpdate(WITH IdParent = IdMain))
// update row ONLY when number of distinct count equals = 1
IF X = 1 THEN
UPDATE(ColumnToUpdate)
END
END
I've tried to split it up within the source code (currently Delphi) but it works slow too. Is there any way to speed it up?
I wonder if this might speed things up:
UPDATE Table
SET ColumnToUpdate = coalesce((SELECT ColumnToUpdate
FROM Table T
WHERE T.ColumnToUpdate IS NOT NULL AND
T.IdParent = Table.IdMain
GROUP BY ColumnToUpdate
HAVING count(*) = 1),
Table.ColumnToUpdate
)
WHERE Table.ColumnToUpdate IS NULL;
This only executes the subquery once instead of twice.
Also, an index on Table(IdParent, ColumnToUpdate) might also improve performance.
My question is very similar to Multiple NOT distinct only it deals with multiple columns instead of one. I have a table like so:
A B C
1 1 0
1 2 1
2 1 2
2 1 3
2 2 4
2 3 5
2 3 6
3 1 7
3 3 8
3 1 9
And the result should be:
A B C
2 1 2
2 1 3
2 3 5
2 3 6
3 1 7
3 1 9
Essentially, like the above question, removing all unique entries only where uniqueness is determined by two columns instead of one. I already tried various tweaks to the above answer but couldn't get any of them to work.
You are using SQL Server, so this is easier than in Access:
select A, B, C
from (select t.*, count(*) over (partition by A, B) as cnt
from t
) t
where cnt > 1;
This use of count(*) is as a window function. It is counting the number of rows with the same value of A and B. The final where just selects the rows that have more than one entry.
Another possible solution with EXISTS
SELECT a, b, c
FROM Table1 t
WHERE EXISTS
(
SELECT 1
FROM Table1
WHERE a = t.a
AND b = t.b
AND c <> t.c
)
It should be fast enough.
Output:
| A | B | C |
-------------
| 2 | 1 | 2 |
| 2 | 1 | 3 |
| 2 | 3 | 5 |
| 2 | 3 | 6 |
| 3 | 1 | 7 |
| 3 | 1 | 9 |
Here is SQLFiddle demo