Resend my activation email : Register : Log in 
BCF: Bike Chat Forums


SQL Help

Reply to topic
Bike Chat Forums Index -> The Geek Zone
View previous topic : View next topic  
Author Message

t121anf
World Chat Champion



Joined: 23 Feb 2007
Karma :

PostPosted: 17:13 - 16 Mar 2011    Post subject: SQL Help Reply with quote

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
View user's profile Send private message You must be logged in to rate posts

mjn51
Traffic Copper



Joined: 04 Feb 2011
Karma :

PostPosted: 17:18 - 16 Mar 2011    Post subject: Reply with quote

Have you tried select DISTINCT users

?
 Back to top
View user's profile Send private message You must be logged in to rate posts

t121anf
World Chat Champion



Joined: 23 Feb 2007
Karma :

PostPosted: 17:21 - 16 Mar 2011    Post subject: Reply with quote

produces the same 252 rows :-S
 Back to top
View user's profile Send private message You must be logged in to rate posts

panrider_uk
World Chat Champion



Joined: 23 Sep 2007
Karma :

PostPosted: 17:28 - 16 Mar 2011    Post subject: Reply with quote

Use Group By?

Mark
____________________
Current bikes: Honda ST1100 Pan European. Moto Guzzi V85 TT Travel
 Back to top
View user's profile Send private message You must be logged in to rate posts

t121anf
World Chat Champion



Joined: 23 Feb 2007
Karma :

PostPosted: 17:37 - 16 Mar 2011    Post subject: Reply with quote

geeky colleague had tried that and it returned an error message,

i think he tried group by username, but it complained about not having everything that was in the select in the group by. (seems my sql allows this , ms doesn't)
 Back to top
View user's profile Send private message You must be logged in to rate posts

Kickstart
The Oracle



Joined: 04 Feb 2002
Karma :

PostPosted: 17:38 - 16 Mar 2011    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail You must be logged in to rate posts

t121anf
World Chat Champion



Joined: 23 Feb 2007
Karma :

PostPosted: 17:49 - 16 Mar 2011    Post subject: Reply with quote

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
View user's profile Send private message You must be logged in to rate posts

Kickstart
The Oracle



Joined: 04 Feb 2002
Karma :

PostPosted: 18:20 - 16 Mar 2011    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail You must be logged in to rate posts

t121anf
World Chat Champion



Joined: 23 Feb 2007
Karma :

PostPosted: 20:42 - 16 Mar 2011    Post subject: Reply with quote

Thanks Keith,

I ran this through as a quick test prior to going home and it seems to work.

i'll check the results tomorrow all bieng well i'll more onto how to make this useful as i have no idea how to run this automatically and produce a sensible output (i.e. text file)

(Karma cookies to follow tomorrow if it works btw)
 Back to top
View user's profile Send private message 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.

gazzeh
Nitrous Nuisance



Joined: 01 Oct 2008
Karma :

PostPosted: 12:27 - 17 Mar 2011    Post subject: Reply with quote

Wabby wrote:
t121anf wrote:
Thanks Keith,

I ran this through as a quick test prior to going home and it seems to work.

i'll check the results tomorrow all bieng well i'll more onto how to make this useful as i have no idea how to run this automatically and produce a sensible output (i.e. text file)

(Karma cookies to follow tomorrow if it works btw)


If you have no idea on how to do this, how are you employed on a project where you clearly lack basic skillsets relevant to the task?


That comment is counter productive to the thread. What was the point in even asking him? The guy wanted help.. in the geek section, he got it.
 Back to top
View user's profile Send private message You must be logged in to rate posts

t121anf
World Chat Champion



Joined: 23 Feb 2007
Karma :

PostPosted: 12:48 - 17 Mar 2011    Post subject: Reply with quote

Wabby wrote:
t121anf wrote:
Thanks Keith,

I ran this through as a quick test prior to going home and it seems to work.

i'll check the results tomorrow all bieng well i'll more onto how to make this useful as i have no idea how to run this automatically and produce a sensible output (i.e. text file)

(Karma cookies to follow tomorrow if it works btw)


If you have no idea on how to do this, how are you employed on a project where you clearly lack basic skillsets relevant to the task?


because if i sat back and did nothing the this place would never move forward.

besides people aren't born with these skills they develop them, i had a go and got stuck so I looked for help, no different to what anyone else would do.

the choice of help is down to the individual.


what is important to me is getting the money this place is owed, not the process of extracting the data.
thanks to Keith I have the relevant data which means I can and have put a proposal to the management team.

it might help to know that the colleague whose DB this is has the attitude of "i couldn't care less if the charges are ever paid" wonder if he will say that when he is made redundant?
 Back to top
View user's profile Send private message You must be logged in to rate posts

t121anf
World Chat Champion



Joined: 23 Feb 2007
Karma :

PostPosted: 12:50 - 17 Mar 2011    Post subject: Reply with quote

Keith, as promised you have Karma Smile 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
View user's profile Send private message You must be logged in to rate posts

Kickstart
The Oracle



Joined: 04 Feb 2002
Karma :

PostPosted: 13:02 - 17 Mar 2011    Post subject: Reply with quote

Hi

Glad it worked. 90% of the SQL I do is either using MySQL (quite nice) or Access ( Sick ), but the basics of most flavours of SQL are the same.

All the best

Keith
____________________
Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing
 Back to top
View user's profile Send private message Send e-mail 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?
  Display posts from previous:   
This page may contain affiliate links, which means we may earn a small commission if a visitor clicks through and makes a purchase. By clicking on an affiliate link, you accept that third-party cookies will be set.

Post new topic   Reply to topic    Bike Chat Forums Index -> The Geek Zone All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Read the Terms of Use! - Powered by phpBB © phpBB Group
 

Debug Mode: ON - Server: birks (www) - Page Generation Time: 0.08 Sec - Server Load: 0.91 - MySQL Queries: 13 - Page Size: 86.42 Kb