[英]SQL database error for table join
I keep getting an error with a table I'm trying to create from my sql database. 我尝试从sql数据库创建的表不断出现错误。 I'm supposed to look at the inventory from my tables qsale and qdel, find any department(s) that sold the exact the number of some item that was delivered to that department.
我应该从我的表qsale和qdel中查看库存,找到所有出售与该部门相匹配的确切数量的部门。
This code I wrote keeps giving me this error: 我写的这段代码不断给我这个错误:
watson=> select itemname,deptname,saleqty from qsale WHERE NOT EXISTS (select su
m(delqty) as total_delqty from qdel where total_delqty > saleqty) group by itemn
ame,deptname,saleqty order by saleqty DESC;
ERROR: column "total_delqty" does not exist
LINE 1: ...elect sum(delqty) as total_delqty from qdel where total_delq...
I'm trying to join the tables and find where the delivery quantity equals the sale quantity for items from the qsale table. 我正在尝试加入表格,并从qsale表中查找交货数量等于项目销售数量的位置。 If the item is delivered to a different department then it is not combined with other items.
如果物料交付到其他部门,则不会与其他物料合并。 The result I should get is 3 rows with the following:
我应该得到的结果是3行,内容如下:
Geo positioning system | 地理定位系统| Books |
书籍| 1
1个
Sextant | 六分仪| Books |
书籍| 1
1个
Hat - polar explorer | 帽子-Polar Explorer | Clothes |
衣服 3
3
Basically I'm not sure what I'm doing wrong with my code to get these specific results. 基本上,我不确定自己的代码在做什么错,以获得这些特定的结果。 I don't know why SQL isn't recognizing the column 'total_delqty' I tried creating with my sum of delqty.
我不知道为什么SQL无法识别我尝试使用delqty的总和创建的列“ total_delqty”。
These are the tables for my SQL database (psql (9.4.4)): 这些是我的SQL数据库(psql(9.4.4))的表:
The sale table: 销售表:
watson=> select * from qsale;
saleno | saleqty | itemname | deptname
--------+---------+------------------------------+------------
1001 | 2 | Boots - snakeproof | Clothes
1002 | 1 | Pith helmet | Clothes
1003 | 1 | Sextant | Navigation
1004 | 3 | Hat - polar explorer | Clothes
1005 | 5 | Pith helmet | Equipment
1006 | 1 | Pocket knife - Nile | Clothes
1007 | 1 | Pocket knife - Nile | Recreation
1008 | 1 | Compass | Navigation
1009 | 1 | Geo positioning system | Navigation
1010 | 5 | Map measure | Navigation
1011 | 1 | Geo positioning system | Books
1012 | 1 | Sextant | Books
1013 | 3 | Pocket knife - Nile | Books
1014 | 1 | Pocket knife - Nile | Navigation
1015 | 1 | Pocket knife - Nile | Equipment
1016 | 1 | Sextant | Clothes
1017 | 1 | Sextant | Equipment
1018 | 1 | Sextant | Recreation
1019 | 1 | Sextant | Furniture
1020 | 1 | Pocket knife - Nile | Furniture
1021 | 1 | Exploring in 10 Easy Lessons | Books
1022 | 1 | How to Win Foreign Friends | Books
1023 | 1 | Compass | Books
1024 | 1 | Pith helmet | Books
1025 | 1 | Elephant polo stick | Recreation
1026 | 1 | Camel saddle | Recreation
(26 rows)
The delivery table (arranged to group items together and sum all the deliveries): 交货表(安排将项目分组在一起并汇总所有交货):
watson=> select itemname,deptname, sum(delqty) as total_delqty from qdel group
by itemname,deptname order by deptname DESC;
itemname | deptname | total_delqty
------------------------------+------------+--------------
Pocket knife - Avon | Recreation | 5
Pocket knife - Nile | Recreation | 10
Pith helmet | Recreation | 5
Sextant | Recreation | 5
How to Win Foreign Friends | Recreation | 2
Exploring in 10 Easy Lessons | Recreation | 2
Tent - 8 person | Recreation | 2
Tent - 2 person | Recreation | 5
How to Win Foreign Friends | Navigation | 5
Sextant | Navigation | 5
Geo positioning system | Navigation | 11
Exploring in 10 Easy Lessons | Navigation | 5
Compass | Navigation | 51
Pocket knife - Nile | Navigation | 60
Map case | Navigation | 30
Pith helmet | Navigation | 5
Map measure | Navigation | 35
Pith helmet | Furniture | 5
Pocket knife - Nile | Furniture | 10
Pith helmet | Equipment | 25
Compass | Equipment | 25
Pocket knife - Nile | Equipment | 30
Sextant | Equipment | 2
Boots - snakeproof | Equipment | 2
Pith helmet | Clothes | 19
Pocket knife - Nile | Clothes | 10
Hat - polar explorer | Clothes | 3
Stetson | Clothes | 3
Boots - snakeproof | Clothes | 5
Geo positioning system | Books | 1
How to Win Foreign Friends | Books | 10
Pith helmet | Books | 5
Pocket knife - Nile | Books | 10
Compass | Books | 5
Sextant | Books | 1
Exploring in 10 Easy Lessons | Books | 10
(36 rows)
In Postgres you cannot use column aliases in WHERE
clause. 在Postgres中,不能在
WHERE
子句中使用列别名。 Use HAVING
instead (because you gives an aggregate function in a condition): 请改用
HAVING
(因为您在条件中提供了聚合函数):
select itemname, deptname, saleqty
from qsale
WHERE NOT EXISTS (
select sum(delqty) as total_delqty
from qdel
having sum(delqty) > saleqty)
group by itemname, deptname, saleqty
order by saleqty DESC;
It looks like it doesn't recognize this 似乎无法识别
total_delqty
total_delqty
Because that's an alias for an aggregated function where you sum(delqty). 因为那是聚合函数的别名,在这里求和(delqty)。
So, if you created a view of the table on the bottom as it is, your query should work, otherwise you'll have to refer to it differently because you can't use an aggregate function in your where clause and you have to join on the aggregated value. 因此,如果按原样在底部创建了一个表视图,则查询应该可以工作,否则必须以不同的方式引用它,因为您不能在where子句中使用聚合函数,而必须加入汇总值。
You cannot use total_delqty
in your WHERE
clause predicate because it is an alias for the aggregate function. 您不能在
WHERE
子句谓词中使用total_delqty
,因为它是聚合函数的别名。 Normally, you can't use an aggregate function in a WHERE
Clause, but you can if it is already in your SELECT
list (your "projection" in database speak). 通常,您不能在
WHERE
子句中使用聚合函数,但是可以在SELECT
列表中使用它(您可以用数据库中的“投影”表示)。 Instead, try WHERE Sum(delqty) > saleqty
, thus: 而是尝试
WHERE Sum(delqty) > saleqty
,这样:
select itemname,deptname,saleqty
from qsale
WHERE NOT EXISTS
(select sum(delqty) as total_delqty
from qdel
where sum(delqty > saleqty))
group by itemname,deptname,saleqty
order by saleqty DESC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.