Jump to content

Bizar result using SUM


cpd

Recommended Posts

I've got a list of sundries in a relational database and there are currently 2 for this particular booking. When using the SUM function it only returns a single sundry where as without the SUM it returns both rows.

 

SELECT s.sundryID, s.name, s.cost, bs.quantity, (bs.quantity * s.cost) AS sundry_total, r.roomNo, SUM(bs.quantity * s.cost) AS total FROM bookings_sundries AS bs
LEFT JOIN sundries AS s ON s.sundryID = bs.sundryID
LEFT JOIN bookings_details AS bd ON bd.id = bs.bookingDetailsID
LEFT JOIN rooms AS r ON r.roomID = bd.roomID
WHERE b.bookingID = 1

 

I'm baffled as to why. The total column appears no problem but like I said it only shows a single row as opposed to both the rows. I would have expected both rows to be shown with the `total` appearing at the end of each row.

 

Bare in mind this is all happening in phpMyAdmin.

Link to comment
Share on other sites

Yeah I was having a bad day. When I reviewed this the next day I realised what I'd been doing and I didn't want a sub query as I don't believe it would have been particularly efficient.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.