Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.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
为什么postgresql查询中的和值错误?_Sql_Postgresql - Fatal编程技术网

为什么postgresql查询中的和值错误?

为什么postgresql查询中的和值错误?,sql,postgresql,Sql,Postgresql,我使用postgresql,在形成正确的查询以获得所需结果方面遇到了困难 我有一个问题 select distinct tt.ticket,tt.submitter,tt.seconds_worked as hours from ticket_time tt join ticket_custom tc on tt.ticket = tc.ticket where tc.ticket in (select ticket from ticket_custom where name='oppo

我使用postgresql,在形成正确的查询以获得所需结果方面遇到了困难

我有一个问题

    select distinct tt.ticket,tt.submitter,tt.seconds_worked as hours from ticket_time tt join ticket_custom tc on tt.ticket = tc.ticket where tc.ticket in (select ticket from ticket_custom where name='opportunity_id' and value in ('ABCD020003')) group by tt.ticket,tt.submitter,tt.seconds_worked;
它给我的结果如下

ticket |      submitter      | hours 
--------+---------------------+-------
    360 | elango.kumar        | 10800
    360 | elango.kumar        | 18000
    360 | elango.kumar        | 21600
    360 | elango.kumar        | 32400
    360 | gopenath.palanisamy |  7200
    360 | gopenath.palanisamy | 10800
    360 | gopenath.palanisamy | 14400
    360 | lea.nair            |  3600
    360 | lea.nair            | 10800
    360 | lea.nair            | 14400
    360 | lea.nair            | 18000
    360 | lea.nair            | 21600
    360 | lea.nair            | 25200
    360 | lea.nair            | 32400
    360 | muthupandi.selvaraj |  7200
    360 | muthupandi.selvaraj | 10800
    360 | muthupandi.selvaraj | 19800
    360 | prabodh.panda       | 10800
    360 | prabodh.panda       | 14400
    360 | prabodh.panda       | 16200
    361 | bhashyam.narasimhan |  5400
    361 | bhashyam.narasimhan |  7200
    361 | bhashyam.narasimhan | 19800
    361 | bhashyam.narasimhan | 30600
    361 | bhashyam.narasimhan | 32400
    361 | lea.nair            | 28800
    366 | sangeetha.sankar    |  7200
    366 | sangeetha.sankar    | 32400
    398 | pradeep.kirthivasan | 14400
    398 | pradeep.kirthivasan | 25200
(30 rows)
我希望得到这样的结果

360 | elango.kumar        | 82800
360 | gopenath.palanisamy | 32400
所以我尝试对第三列求和,所以对查询做了如下更改

select distinct tt.ticket,tt.submitter,SUM(tt.seconds_worked) as hours from ticket_time tt join ticket_custom tc on tt.ticket = tc.ticket where tc.ticket in (select ticket from ticket_custom where name='opportunity_id' and value in ('ABCD020003')) group by tt.ticket,tt.submitter,tt.seconds_worked;
但结果如下所示

 ticket |      submitter      |   hours    
--------+---------------------+------------
    360 | gopenath.palanisamy |     158400
    361 | bhashyam.narasimhan |     217800
    361 | bhashyam.narasimhan |     712800
    361 | bhashyam.narasimhan |     336600
    360 | prabodh.panda       |     178200
    360 | lea.nair            |     158400
    360 | elango.kumar        | 2.1384e+06
    360 | prabodh.panda       |     118800
    366 | sangeetha.sankar    |     158400
    361 | lea.nair            |     316800
    360 | gopenath.palanisamy |     633600
    360 | lea.nair            |     475200
    360 | prabodh.panda       |     316800
    360 | lea.nair            |     198000
    360 | elango.kumar        |     198000
    360 | elango.kumar        |     118800
    398 | pradeep.kirthivasan |     277200
    360 | muthupandi.selvaraj |     217800
    398 | pradeep.kirthivasan |     158400
    366 | sangeetha.sankar    | 1.4256e+06
    360 | muthupandi.selvaraj |      79200
    361 | bhashyam.narasimhan |      59400
    360 | elango.kumar        |     237600
    360 | lea.nair            |     237600
    360 | lea.nair            |      79200
    360 | muthupandi.selvaraj |     118800
    360 | lea.nair            |     712800
    360 | lea.nair            |     831600
    361 | bhashyam.narasimhan |     158400
    360 | gopenath.palanisamy |     237600
(30 rows)

首先,这不是我所期望的,上面结果的第三列有一些带有
+
符号的值,还有一个浮点值

我真的不明白这里出了什么问题。请帮助我理解这一点,并告诉我如何才能在我凝视上面时获得所需的结果


提前谢谢

由于您希望获得基于票证和提交者的信息,因此您只需要按这些列进行分组。大概是

select tt.ticket,tt.submitter,sum(tt.seconds_worked) as hours
    from ticket_time tt
    join ticket_custom tc on tt.ticket = tc.ticket
    where tc.ticket in
        (select ticket from ticket_custom where name='opportunity_id' and value in ('ABCD020003'))
    group by tt.ticket,tt.submitter;
此外,我没有看到在结果中的任何地方使用的
ticket\u custom
,因此可能也可以完全删除它

select tt.ticket,tt.submitter,sum(tt.seconds_worked) as hours
    from ticket_time tt
    where tt.ticket in
        (select ticket from ticket_custom where name='opportunity_id' and value in ('ABCD020003'))
    group by tt.ticket,tt.submitter;
或者在没有子选择的情况下加入他们

select tt.ticket,tt.submitter,sum(tt.seconds_worked) as hours
    from ticket_time tt
    join ticket_custom tc on tt.ticket = tc.ticket and tc.name='opportunity_id' and tc.value in ('ABCD020003')
    group by tt.ticket,tt.submitter;

成功了!谢谢!我同意你的最后一个答案——不加选择。