|
|
| Author |
Message |
| t121anf |
This post is not being displayed .
|
 t121anf World Chat Champion

Joined: 23 Feb 2007 Karma :     
|
 Posted: 17:13 - 16 Mar 2011 Post subject: SQL Help |
 |
|
i need a little SQL help as I'm stumped and i've stumped one of my colleagues whose a little geeky at this kind of thing.
another colleague has a DB for a laptop booking system which when returned late produces fines, problem is no one can pay them online so I need to get them into my DB.
The laptop DB has numerous tables but then ones I am interested in are PurseAudit and Users.
Users holds a Username field which I need to link to my DB.
PurseAudit contains a running balance field, UserID Key (linking it to Users) and CreatedDate which might be handy.
So far I have the following.
| Code: | SELECT Users.Username, PurseAudit.Balance, PurseAudit.CreatedDate FROM Users, PurseAudit
WHERE PurseAudit.UserID=Users.ID
ORDER BY Users.Username ASC, PurseAudit.CreatedDate DESC |
This returns me
Username / Balance / CreatedDate
user1 / -45 / 16/03/2011
user1 / -35 / 15/03/2011
user1 / -25 / 14/03/2011
user2 / -35 / 16/03/2011
user2 / -25/ 15/03/2011
Any ideas on how to get just 1 row for every user who has a balance less than 0
i.e.
Username / Balance / CreatedDate
user1 / -45 / 16/03/2011
user2 / -35 / 16/03/2011
The Laptop DB is held on a MS Sql 2005 server of which I have no experience, if this were my server's DB i'd just run a loop or something in perl, but limited as to what I can do here.
ideas? |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| mjn51 |
This post is not being displayed .
|
 mjn51 Traffic Copper
Joined: 04 Feb 2011 Karma :  
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| t121anf |
This post is not being displayed .
|
 t121anf World Chat Champion

Joined: 23 Feb 2007 Karma :     
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| panrider_uk |
This post is not being displayed .
|
 panrider_uk World Chat Champion

Joined: 23 Sep 2007 Karma :  
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| t121anf |
This post is not being displayed .
|
 t121anf World Chat Champion

Joined: 23 Feb 2007 Karma :     
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
 Posted: 17:38 - 16 Mar 2011 Post subject: |
 |
|
Hi
Should be pretty easy, but the problem is that you are now returning details from the non unique rows (ie, creation date, which one would you like back?).
| Code: | SELECT Users.Username, SUM(PurseAudit.Balance)
FROM Users
INNER JOIN PurseAudit
ON PurseAudit.UserID=Users.ID
GROUP BY Users.Username |
This isn't ideal. For a start Username is quite likely non unique so you would be best to use the id field. Or you can group by the Id and the Username and bring both back.
All the best
Keith ____________________ Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| t121anf |
This post is not being displayed .
|
 t121anf World Chat Champion

Joined: 23 Feb 2007 Karma :     
|
 Posted: 17:49 - 16 Mar 2011 Post subject: |
 |
|
Hi Keith,
I think you are on the right lines, however the SUM rightly is adding each row of Balance together giving an incorrect figure.
This is probably my fault as I didn't really explain the issue fully.
User 1, borrows laptop, returns late, fine added (Balance in PurseAudit), this could be paid off and user 1 repeats being naughty adding a second load of rows into Balance. i.e. fine 1, £25, fine 2 £25 table would look like this
£25
0
£25
the SUM returns £50, rather than £25.
I don't need CreatedDate so if it helps this can be dropped, all I really need to the current balance and username. the ID field is of no use to me as it is unique to this application, username applies to everything a user does. |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
 Posted: 18:20 - 16 Mar 2011 Post subject: |
 |
|
Hi
I used sum thinking there was a current record for each "debt". From your reply seems it is only the latest debt you care about.
| Code: | SELECT a.ID, a.Username, b.LatestCreatedDate, c.Balance
FROM Users a
INNER JOIN (SELECT UserId, MAX(CreatedDate) AS LatestCreatedDate
FROM PurseAudit
GROUP BY UserId) b
ON a.ID = b.UserId
INNER JOIN PurseAudit c
ON b.UserId = c.UserId AND b.LatestCreatedDate = c.CreatedDate
WHERE c.Balance < 0 |
All the best
Keith ____________________ Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| t121anf |
This post is not being displayed .
|
 t121anf World Chat Champion

Joined: 23 Feb 2007 Karma :     
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Wabby |
This post is not being displayed because it has a low rating (Flame). Unhide this post / all posts.
|
 Wabby Trackday Trickster
Joined: 25 Sep 2007 Karma :    
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| gazzeh |
This post is not being displayed .
|
 gazzeh Nitrous Nuisance

Joined: 01 Oct 2008 Karma :    
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| t121anf |
This post is not being displayed .
|
 t121anf World Chat Champion

Joined: 23 Feb 2007 Karma :     
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| t121anf |
This post is not being displayed .
|
 t121anf World Chat Champion

Joined: 23 Feb 2007 Karma :     
|
 Posted: 12:50 - 17 Mar 2011 Post subject: |
 |
|
Keith, as promised you have Karma not that you need it lol.
I tested the output earlier, thankfully not too many results so I check them all and they are correct. I made a few simple changes to improve the output but on the whole I have retained your work.
Also worked out how to run this outside of the MS SQL client, much easier than expected. |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
Old Thread Alert!
The last post was made 15 years, 185 days ago. Instead of replying here, would creating a new thread be more useful? |
 |
|
|