MySQL subquery as alias - unknown column error - mysql

Column not found: 1054 Unknown column 'expensetot' in 'field list'
Based on the query below, what is the best way to produce grandtotal without an error?
SELECT
table1.cost,
(SELECT SUM(expense) FROM table2 WHERE table2.key=table1.id) as expensetot
(table1.cost+expensetot) as grandtotal,
table3.label
FROM
table1
LEFT JOIN table3 ON table3.key=table1.id
WHERE
table1.saledate>SUBDATE(CURDATE(), INTERVAL 1 YEAR)
ORDER BY grandtotal

You can't use alias in select clause you must repeat the code
and you missed a comma before table1.cost + ...
and in subselect the outer table are not in scope so you should use a proper join with subquery for sum
SELECT
table1.cost,
t.expensetot,
table1.cost + t.expensetot as grandtotal,
table3.label
FROM table1
INNER JOIN (
select table2.key, sum(table2.expense) expensetot
from table2
group by table2.key
) t on t..key=table1.id
LEFT JOIN table3 ON table3.key=table1.id
WHERE
table1.saledate>SUBDATE(CURDATE(), INTERVAL 1 YEAR)
ORDER BY grandtotal

Maybe that would help
SELECT
table1.cost,
(SELECT SUM(expense) FROM table2 WHERE table2.key=table1.id) as expensetot,
(table1.cost+(SELECT SUM(expense) FROM table2 WHERE table2.key=table1.id)) as grandtotal,
table3.label
FROM
table1
LEFT JOIN table3 ON table3.key=table1.id
WHERE
table1.saledate>SUBDATE(CURDATE(), INTERVAL 1 YEAR)
ORDER BY grandtotal
or you can read about user variables in queries:
https://dev.mysql.com/doc/refman/8.0/en/user-variables.html

Related

MS Access: find rows with different fields in two tables

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

MySQL Select Statement Create Column Then Compare

Very New to SQL. Trying to get the results to show only when TotalSales is Less Than the goal.
SELECT SUM(Table1.Column1) AS TotalSales
FROM Table 1
WHERE Table1.Goal > TotalSales
GROUP BY EmployeeID;
It is giving me error 1054: Unknown column.
Again, very basic but I'm stuck. Thanks again for the help.
You need subquery with JOIN :
SELECT t1.*, t2.TotalSales
FROM TABLE1 t1 INNER JOIN (
SELECT EmployeeID, SUM(Table1.Column1) AS TotalSales
FROM Table1
GROUP BY EmployeeID ) t2
ON t1.EmployeeID = t2.EmployeeID
WHERE t1.Goal > t2.TotalSales;

subquery that uses a value from the outer query in the where caluse

I wanna run a subquery that uses the value of the outer query in its where clause. Here's and example of what I wanna do:
SELECT * FROM `tbl1`
WHERE `tbl1`.`max_count` < (
SELECT COUNT(*) rc FROM `tbl2`
WHERE `tbl2`.`id` = `tbl1`.`id
)
There is tbl1 with a column named max_count, and there is tbl2 with rows referring to a row in tbl1(many-to-one relationship). What I wanna do is select rows in tbl1 where the number of rows in tbl2 referencing it is less than the max_count value of that row. But I'm pretty sure that what I wrote here, ain't gonna cut it. Any ideas?
Thanks a lot
try this -
SELECT * FROM `tbl1` t1
WHERE t1.`max_count` < (
SELECT COUNT(*) FROM `tbl2` t2
WHERE t2.`id` = t1.`id`
)
try using JOIN.
SELECT DISTINCT a.*
FROM tb1 a
INNER JOIN
(
SELECT id, COUNT(*) totalCount
FROM tbl2
GROUP BY id
) b ON a.ID = b.ID
WHERE a.max_count < b.totalCount
As an alternate solution, it's probably easier to just use a LEFT JOIN with HAVING than a subquery;
SELECT tbl1.*, COUNT(tbl2.id) current_count
FROM tbl1
LEFT JOIN tbl2
ON tbl1.id=tbl2.id
GROUP BY tbl1.id
HAVING COUNT(tbl2.id) < max_count
An SQLfiddle to test with.
Note that the GROUP BY in this case is a MySQL only thing, normally you'd need to GROUP BY every selected field in tbl1 even if tbl1.id is known to be unique per row.

Adding two columns from tables having same structure

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

mysql subquery field application Range

Here is my code:
SELECT field1, field2,
(SELECT * FROM table1 WHERE a = field2),
(SELECT COUNT(*)
FROM (SELECT *
FROM table2
WHERE c = field2) as t1) as count
FROM table3;
...and here is error message:
/* SQL Error (1054): Unknown column
'field2' in 'where clause' */
I want to execute one query, to get table2's total counts.
The problem is that you're trying to use a variable declared in the outer query in the inner query. Variables are scoped the opposite way in SQL, you only have access to inner queries. What you want to do (I believe) is look for the number of table2 that have a c that matches the a from table1. This should give you that answer.
SELECT
table1.a,
table2.c,
count(*)
FROM
table1
JOIN
table2 ON table2.c = table1.a
GROUP BY
table1.a
I re-wrote your query as:
SELECT t3.field1,
t3.field2,
t1.*,
t2.cnt
FROM TABLE3 t3
LEFT JOIN TABLE1 t1 ON t1.a = t3.field2
LEFT JOIN (SELECT t.c,
COUNT(*) AS cnt
FROM TABLE2 t
GROUP BY t.c) t2 ON t2.c = t3.field2
The 1054 error is due to referencing field2 two subqueries deep - most only support one level deep.
UPDATE:
I want to edit the answer, since in MySQL 8.0 your query is already correct, derived tables can access external references no matter the level.
SELECT field1, field2,
(SELECT 'hola' FROM table1 WHERE a = field2),
(SELECT COUNT(*)
FROM (SELECT *
FROM table2
WHERE c = field2) as t1) as count
FROM table3;
"Prior to MySQL 8.0.14, a derived table cannot contain outer references. This is a MySQL restriction that is lifted in MySQL 8.0.14, not a restriction of the SQL standard. For example, the derived table dt in the following query contains a reference t1.b to the table t1 in the outer query:" https://dev.mysql.com/doc/refman/8.0/en/derived-tables.html