In MS Access:
I am trying to compare two tables with:
- TABLE1.docnumb1 = TABLE2.docnumb2
- looking for: TABLE1.sum <> TABLE2.sum2
But query retrieves an error: syntax error in from clause (or when creating left join I get an error that JOIN isn't supported):
SELECT docnumb1, sum
FROM Table1
JOIN Table2 ON docnumb1 = docnumb2;
How do I query the rows with different values?
looking to your sample (image)
you could compare the subquery for sum
select t1.rownumb, t1.sum1 -t2.sum2
from (
SELECT rownumb, sum(value) sum1
FROM Table1
group by rownumb
) t1
INNER JOIN
(
SELECT rownumb, sum(value) sum2
FROM Table2
group by rownumb
) t2 ON t1.rownumb = t2.rownumb and (t1.sum1 -t2.sum2 ) <> 0
use left join
SELECT docnumb1, sum
FROM Table1 a
left JOIN Table2 b ON a.docnumb1 = b.docnumb2 and a.value=b.value
where b.docnumb2 is null
Related
I am trying to merge the rows based on condition in mysql.
I have table as shown below :
Looking merge the row 1 into row 2 (where the attendance count is larger)
and need to shown the result as :
I was trying to divide the dataset into 2 parts using the below query
select
a.student_id,a.school_id,a.name,a.grant,a.classification,a.original_classification,,a.consent_type
from (
select * from school_temp where original_classification='all' and availability='implicit')a
join(select * from school_temp where original_classification!='all' and availability!='implicit')b
on a.student_id = b.student_id and a.school_id=b.school_id and a.name=b.name
But unable to merge the rows and get total attendance count .
Please help me ,i am badly stuck in this
Split this into two queries that you combine with UNION.
The first joins the implicit row with the row with the highest attendance among the explicit rows for each student. See Retrieving the last record in each group - MySQL for how that works. Use SUM(attendance_count) to combine the attendances.
The second query in the UNION gets all the rows that don't have the highest attendance.
WITH explicit as (
SELECT *
FROM school_temp
WHERE original_classification!='all' and availability!='implicit'
)
SELECT a.student_id, a.school_id, a.name, SUM(attendance_count) AS attendance_count,
b.grant, b.classification, b.original_classification, b.consent_type
FROM school_temp AS a
JOIN (
SELECT t1.*
FROM explicit AS t1
JOIN (
SELECT student_id, school_id, name, MAX(attendance_count) AS max_attendance
FROM explicit AS t2
GROUP BY student_id, school_id, name
) AS t2 ON t1.student_id = t2.student_id AND t1.school_id = t2.school_id AND t1.name = t2.name AND t1.attendance_count = t2.max_attendance
) AS b ON a.student_id = b.student_id and a.school_id=b.school_id and a.name=b.name
WHERE a.original_classication = 'all' AND a.availability = 'implicit'
UNION ALL
SELECT t1.*
FROM explicit AS t1
JOIN (
SELECT student_id, school_id, name, MAX(attendance_count) AS max_attendance
FROM explicit AS t2
GROUP BY student_id, school_id, name
) AS t2 ON t1.student_id = t2.student_id AND t1.school_id = t2.school_id AND t1.name = t2.name AND t1.attendance_count < t2.max_attendance
I've used a CTE to give a name to the subquery that gets all the explicit rows. If you're using MySQL 5.x, you'll need to replace explicit with that subquery throughout the query. Or you could define it as a view.
I have a query that produces 3 columns (ApplicationID, FamilySize, GrantID). In the initial query FamilySize is a count. The query works, takes about 5 minutes to produce results, and produces expected results. The query is as such:
SELECT t1.ApplicationID, COUNT(*) FamilySize, t1.GrantID
FROM
(
SELECT g.ApplicationID, ChildApplicationID `RefID`, g.GrantID
FROM CONTINUITYCHILD_P `child`
JOIN uspto.GRANT as g ON child.ApplicationID = g.ApplicationID
UNION
SELECT g2.ApplicationID, ParentApplicationID `RefID`, g2.GrantID
FROM CONTINUITYPARENT_P `par`
JOIN uspto.GRANT as g2 ON par.ApplicationID = g2.ApplicationID
) t1
GROUP BY ApplicationID
That initial query works fine. However, I want to use it to update a field in another table Where the GrantID match. I'm using MariaDB but looked at the MySQL 8 documentation since MariaDB and MySQL are supposed to have compatible syntax: https://dev.mysql.com/doc/refman/8.0/en/update.html
I am trying to use the syntax similar to this:
UPDATE t1, t2
SET t1.column = t2.column
WHERE t1.column2 = t2.column2
So that would convert my entire initial query into a table (t2) and the table being updated (t1) would be able to match on a column between t1 and t2.
This is the resulting query:
UPDATE METRICS_G m,
(
SELECT ApplicationID, COUNT(*) FamilySize, GrantID
FROM
(
SELECT g.ApplicationID, ChildApplicationID `RefID`, g.GrantID
FROM CONTINUITYCHILD_P `child`
JOIN uspto.GRANT as g ON child.ApplicationID = g.ApplicationID
UNION
SELECT g2.ApplicationID, ParentApplicationID `RefID`, g2.GrantID
FROM CONTINUITYPARENT_P `par`
JOIN uspto.GRANT as g2 ON par.ApplicationID = g2.ApplicationID
) t1
GROUP BY ApplicationID
) t2
SET m.FamilySize = t2.FamilySize
WHERE m.GrantID = t2.GrantID;
The query gives no errors, and returns in 3ms with 0 rows affected.
This must mean that the sub-query is not being executed since it will normally take a few minutes for that query to return.
What have I done wrong to use the output of a query as a table to match in an UPDATE statement?
You must join the query to the table METRICS_G in the UPDATE statement:
UPDATE METRICS_G m
INNER JOIN (
SELECT ApplicationID, COUNT(*) FamilySize, GrantID
FROM (
SELECT g.ApplicationID, ChildApplicationID `RefID`, g.GrantID
FROM CONTINUITYCHILD_P `child`
JOIN uspto.GRANT as g ON child.ApplicationID = g.ApplicationID
UNION
SELECT g2.ApplicationID, ParentApplicationID `RefID`, g2.GrantID
FROM CONTINUITYPARENT_P `par`
JOIN uspto.GRANT as g2 ON par.ApplicationID = g2.ApplicationID
) t1
GROUP BY ApplicationID
) t2 ON m.GrantID = t2.GrantID
SET m.FamilySize = t2.FamilySize
select a, count (b)
from table1 where b in ( select distict b from table2)
and table1.dated>=DATE('yy/mm/dd')
group by a;
In the above SQL, when I have count(b)>0 then it returns columns but when count=0 then no rows were returned
I did try UNION, NULLIF() and SELECT(SELECT()) as something but nothing worked.
I was expecting to get 0 returned if the count is equal to 0.
https://www.db-fiddle.com/#&togetherjs=2AkxeMUrPF
You could use:
select table1.a, count(DISTINCT table2.b)
from table1
LEFT JOIN table2
ON table1.b = table2.b
AND table1.dated>=DATE('yy/mm/dd') -- this comparision is simply incorrect
group by table1.a
We can ensure that a query returns a row by having the query guarantee it.
Here's an example that retrieves exactly one row from an inline view i.
Then an outer join to another inline view s that gets a distinct list of values.
And then and then another outer join to table1.
SELECT t.a
, COUNT(t.b) AS cnt
FROM ( SELECT 1 AS n ) i
LEFT
JOIN ( SELECT DISTINCT r.b
FROM table2 r
) s
LEFT
JOIN table1 t
ON t.b = s.b
AND t.dated >= ...
GROUP
BY i.n
, t.a
If inline view s returns no rows, the query should return
a cnt
---- ---
NULL 0
I am trying to select the COUNT with three tables with one single query (with WHERE conditions).
Here is my code which doesn't work correctly.
SELECT t1.count(id) AS car_model_count,t2.count(id) AS list_item_count,t3.count(id)
FROM `car_model` AS t1
INNER JOIN `list_item` AS t2
INNER JOIN `part_item` AS t3
WHERE t1.user_id=3;
Possible by using Sub-Query OR UNION is possible to get the COUNT from multiple table.
Try this query :
SELECT
(SELECT count(*) FROM `car_model` WHERE user_id=3 ) AS car_model_count,
(SELECT COUNT(*) FROM `list_item` WHERE user_id=3) AS list_item_count,
(SELECT count(*) FROM `part_item` WHERE user_id=3) AS part_item_count;
I have two tables as follows :-
Table 1 Table 2
date value date value
1120101 v11 1120102 v21
1120202 v12 1120303 v22
1120203 v13 1120104 v23
what is the sql query to get following output
date value
1120101 (v11)
1120102 (v12+v21)
1120103 (v13+v22)
1120104 ( v23)
I tired following query but failed to get desired output
select table1.date,
table2.date,
table1.delta+table2.delta as delta
from table1,
table2
where table1.date=table2.date;
thanks in advance .
SELECT date, SUM(value)
FROM
(
SELECT date, value FROM table1
UNION ALL
SELECT date, value FROM table2
) a
GROUP BY date
SQLFiddle Demo (data is different but idea is the same)
play with subqueries and union:
SELECT
c.date,
COALESCE(a.value, 0) + COALESCE(z.value,0)
FROM (
SELECT
date
FROM table1
UNION
SELECT date
FROM table2
) AS c
LEFT OUTER JOIN table1 a
ON a.date = c.date
LEFT OUTER JOIN table2 z
ON z.date = c.date