Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL-不存在的列_Sql_Postgresql - Fatal编程技术网

SQL-不存在的列

SQL-不存在的列,sql,postgresql,Sql,Postgresql,我正在使用POSTGRESQL数据库: 我需要返回客户的名字、姓氏以及他们将一部电影保存在某个限制之上的总小时数,比如说10小时。我写了这样的疑问: SELECT customer.first_name AS first_name, customer.last_name AS last_name, DATE_PART('day', rental.return_date - rental.rental_date)*24 + DATE_PART('hour', return_date

我正在使用POSTGRESQL数据库:

我需要返回客户的名字、姓氏以及他们将一部电影保存在某个限制之上的总小时数,比如说10小时。我写了这样的疑问:

SELECT customer.first_name AS first_name, customer.last_name AS last_name,
       DATE_PART('day', rental.return_date - rental.rental_date)*24 + DATE_PART('hour', return_date - rental_date) AS sum
FROM rental JOIN
     customer
     ON rental.customer_id = customer.customer_id
GROUP BY sum, first_name, last_name
HAVING MIN(sum) > 10;

但它说没有sum这样的列。如何克服此问题?

您希望通过未聚合的列进行聚合:

假设customer_id在customers表中是唯一的,您也可以使用它。您可能希望该总和是某种类型的总和,因为这也是查询的问题。也许你想要:

SELECT c.first_name AS first_name, c.last_name AS last_name,
       SUM(DATE_PART('day', r.return_date - r.rental_date)*24 + DATE_PART('hour', r.return_date - r.rental_date)
          ) AS total_period
FROM customer c JOIN
     rental r
     ON r.customer_id = c.customer_id
GROUP BY c.customer_id
HAVING SUM(DATE_PART('day', r.return_date - r.rental_date)*24 + DATE_PART('hour', r.return_date - r.rental_date)
          ) > 10;

选择c.first\u name,c.last\u name, 日期(部分日期),归还日期-租赁日期(总天数) 来自abc c


在您的情况下,请尝试将整个部分放在括号中,然后“作为天数的总和”

大多数数据库不允许在同一级别上使用Select In Group By中的别名。您需要将整个聚合语句复制到GROUPBY中,或者将聚合移动到另一个级别

你想做什么还不完全清楚。你说他们拍了一部电影的总时间,但你没有按电影分组

请不要使用关键字作为列名

我的假设可能是正确的,但不确定需求:

select
   a.first_name
,  a.last_name
,  min(a.time_val) as min_time
from ( 
   SELECT customer.first_name AS first_name, customer.last_name AS last_name,
       DATE_PART('day', rental.return_date - rental.rental_date)*24 
       + DATE_PART('hour', return_date - rental_date) AS time_val
   FROM rental 
   JOIN customer
     ON rental.customer_id = customer.customer_id
) as a
where min(a.time_val) > 10

我尝试了您的示例,但收到错误消息:列total_period不存在exist@Ginny . . . 正当在Postgres中,你需要重复这个表达式。你只是缺少了一个括号!
select
   a.first_name
,  a.last_name
,  min(a.time_val) as min_time
from ( 
   SELECT customer.first_name AS first_name, customer.last_name AS last_name,
       DATE_PART('day', rental.return_date - rental.rental_date)*24 
       + DATE_PART('hour', return_date - rental_date) AS time_val
   FROM rental 
   JOIN customer
     ON rental.customer_id = customer.customer_id
) as a
where min(a.time_val) > 10