PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old March 30th, 2008, 02:44 PM
sanv sanv is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 2 sanv User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 21 sec
Reputation Power: 0
Problem with query - sum()

Hey,

I have a problem with a query. I would like to sum the columns grouped by the passportID. the sum is retrieved by a calculation:

However, it does not work properly.
Any suggestions?

SELECT c.passportid,(extract(epoch FROM (b.departuredate-b.arrivaldate))::int / (60*60*24)) as "duration", sum(extract(epoch FROM(b.departuredate-b.arrivaldate))::int / (60*60*24)) FROM customer c, booking b, customer_jct_booking cjb WHERE c.passportid = cjb.customers_passportid AND cjb.bookings_bookingid = b.bookingid group by (c.passportid, b.arrivaldate, b.departuredate) ORDER BY (b.departuredate-b.arrivaldate) DESC;

Reply With Quote
  #2  
Old March 31st, 2008, 01:13 AM
rski rski is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 97 rski User rank is Sergeant (500 - 2000 Reputation Level)rski User rank is Sergeant (500 - 2000 Reputation Level)rski User rank is Sergeant (500 - 2000 Reputation Level)rski User rank is Sergeant (500 - 2000 Reputation Level)rski User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 4 h 32 m 57 sec
Reputation Power: 7
Quote:
Originally Posted by sanv
Hey,

I have a problem with a query. I would like to sum the columns grouped by the passportID. the sum is retrieved by a calculation:

However, it does not work properly.
Any suggestions?

SELECT c.passportid,(extract(epoch FROM (b.departuredate-b.arrivaldate))::int / (60*60*24)) as "duration", sum(extract(epoch FROM(b.departuredate-b.arrivaldate))::int / (60*60*24)) FROM customer c, booking b, customer_jct_booking cjb WHERE c.passportid = cjb.customers_passportid AND cjb.bookings_bookingid = b.bookingid group by (c.passportid, b.arrivaldate, b.departuredate) ORDER BY (b.departuredate-b.arrivaldate) DESC;

Is this query run?, i think there should be group by clause

Reply With Quote
  #3  
Old March 31st, 2008, 03:56 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,711 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 6 Days 5 h 6 m 18 sec
Reputation Power: 259
It does not work properly means nothing, describe the exact problem or error and show your case with a meaningful example.
Also formatting your queries makes them more readable ...
sql Code:
Original - sql Code
  1. SELECT
  2.  c.passportid,
  3.  (extract(epoch FROM (b.departuredate-b.arrivaldate))::int / (60*60*24)) AS "duration",
  4.  SUM(extract(epoch FROM(b.departuredate-b.arrivaldate))::int / (60*60*24))
  5. FROM
  6.  customer c,
  7.  booking b,
  8.  customer_jct_booking cjb
  9. WHERE
  10.   c.passportid = cjb.customers_passportid
  11.  AND
  12.   cjb.bookings_bookingid = b.bookingid
  13. GROUP BY
  14. -- (c.passportid, b.arrivaldate, b.departuredate)
  15.  c.passportid,
  16.  (extract(epoch FROM (b.departuredate-b.arrivaldate))::int / (60*60*24)) AS "duration"
  17. ORDER BY
  18.  (b.departuredate-b.arrivaldate) DESC;
You should be grouping by passportid and duration

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Problem with query - sum()


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway