pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Can anybody help me with SQL?
3+ messages / 3 participants
[nested] [flat]

* Can anybody help me with SQL?
@ 2001-12-10 08:42  Richard Lockwood <rickardo2010@hotmail.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Richard Lockwood @ 2001-12-10 08:42 UTC (permalink / raw)
  To: pgsql-novice@postgreSQL.org; pgsql-sql

Hi

I was wondering if anyone could help me with the following SQL questions:


1.Which car was rented for the longest single period? (regno)

SELECT regno, MAX(datein-dateout) "Longest Single Period" FROM rental group 
by regno;

Although this works - it shows ALL the regno's longest periods - how would i 
just display the regno?

2. Which car was rented for the longest total period? (regno)
3. Which customers (if any) were born on Monday? (surname)

For this question i thought that this would work:

SELECT * FROM cust WHERE TO_CHAR(cd_o_b,'DAY') = 'MONDAY'

But it never works - it says 'no rows selected'
although there are 2 cusotmers born on Monday
However if you put wednesday instead of monday or any other day it seems to 
work correctly
Anyone know why this is?

4. Which customer averaged the most miles/day? (surname)(single rental)
5. Which customer averaged the least miles/day? (surname)(single rental)
6. Which customer had to wait for the previous renter to return the car? 
(surname)
6. What % of rentals are for one day only?
7. Can cars be rented on a Sunday? (i.e. are there any?) (regno)
8. List the total amount of money paid to each garage during this period.
9. What is the average miles driven per day?

The rest of the questions just baffle me as i cannot work them out at all.

If you can give me any help at all it would be very much appreciated!

The data/tables are defined below:

SERVICE
regno
gid
sdate
smileage
scost
servrep

CAR
regno
model_id
curr_milge
next_serv
cyear
colour

MODEL
model_id
make
unit_price
mileage_da

REPLACE
regno
gid
pno

PART
partno
pdesc

RENTAL
hireno
regno
rentalno
dateout
datein
milesout
milesin
cost

CUST
custno
cname
cname_1
cadd_1
ccode
cd_o_b

GARAGE
gid
gname
gadd

Any help would be very much appreciated!

Thanks

Rickardo


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Can anybody help me with SQL?
@ 2001-12-10 18:10  Andrew Bell <acbell@iastate.edu>
  parent: Richard Lockwood <rickardo2010@hotmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Andrew Bell @ 2001-12-10 18:10 UTC (permalink / raw)
  To: pgsql-novice@postgresql.org; pgsql-sql

At 08:42 AM 12/10/2001 +0000, Richard Lockwood wrote:
>4. Which customer averaged the most miles/day? (surname)(single rental)
>5. Which customer averaged the least miles/day? (surname)(single rental)
>6. Which customer had to wait for the previous renter to return the car? 
>(surname)
>6. What % of rentals are for one day only?
>7. Can cars be rented on a Sunday? (i.e. are there any?) (regno)
>8. List the total amount of money paid to each garage during this period.
>9. What is the average miles driven per day?

This sounds like a homework problem to me :)


-- Andrew Bell
acbell@iastate.edu
        




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: [SQL] Can anybody help me with SQL?
@ 2001-12-10 21:33  Stephan Szabo <sszabo@megazone23.bigpanda.com>
  parent: Richard Lockwood <rickardo2010@hotmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Stephan Szabo @ 2001-12-10 21:33 UTC (permalink / raw)
  To: Richard Lockwood <rickardo2010@hotmail.com>; +Cc: pgsql-novice@postgresql.org; pgsql-sql



> 1.Which car was rented for the longest single period? (regno)
>
> SELECT regno, MAX(datein-dateout) "Longest Single Period" FROM rental group
> by regno;
>
> Although this works - it shows ALL the regno's longest periods - how would i
> just display the regno?

There's probably a better way, but if I'm getting the question you want
answered right I think these will do it.

select regno, m from rental, (select max(datein-dateout) as m from rental)
foo where m=datein-dateout;

or

select regno, datein-dateout from rental where datein-dateout=(select
max(datein-dateout) from rental);

> 2. Which car was rented for the longest total period? (regno)
Maybe something like this?  Not entirely sure

select regno, t from (select regno, sum(datein-dateout) as t from rental
group by regno) r where t=(select max(total) from (select
sum(datein-dateout) as total from rental group by regno) foo);

> 3. Which customers (if any) were born on Monday? (surname)
>
> For this question i thought that this would work:
>
> SELECT * FROM cust WHERE TO_CHAR(cd_o_b,'DAY') = 'MONDAY'
>
> But it never works - it says 'no rows selected'
> although there are 2 cusotmers born on Monday
> However if you put wednesday instead of monday or any other day it seems to
> work correctly
> Anyone know why this is?

To_char fills out to a given length with spaces, I'd guess that the value
is actually something like 'MONDAY   '
I think you probably want the format FMDAY which should not have the
spaces.






^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2001-12-10 21:33 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-12-10 08:42 Can anybody help me with SQL? Richard Lockwood <rickardo2010@hotmail.com>
2001-12-10 18:10 ` Andrew Bell <acbell@iastate.edu>
2001-12-10 21:33 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox