Shoot, why don't I just make each record a self-contained IN/OUT? That is, instead of a timestamp and a status, drop the status and replace it with a second timestamp. When they check in, set that timestamp as now + 12 hours, and when they check out, update it. If they don't check out, done. Then I have my start/stop time and the rest is easy as pie. I'll have to go back and refigure all the data, but it shouldn't be that hard, and this'll make working with the data a hell of a lot easier in pretty much every regard.
|
|
# ? Dec 3, 2012 04:23 |
|
|
# ? May 30, 2024 19:07 |
|
If you're in the design phase definitely consider a redesign. I assumed it was in production already and you were trying to report off of a dirty log. You may also want to update the previous OUT stamp when a user clocks IN for certain cases. My reasoning for this is that in the following scenario I think it would be more accurate: 8:00 AM - IN 1:00 PM - IN 5:00 PM - OUT We would not want to assume that the user was in the building until 8PM when they clearly left at 5PM. What would you want the system to do in this case: 8:00 AM - IN 9:00 PM - OUT Out of curiosity, what is the business sensitivity to accuracy in this data? Is it worth making 2 IN scans in a row invalid and forcing a supervisor to fix their time at the time? I don't have time to put it together right now, but the reporting piece is pretty easy. I would generate a dummy table of dates and hours that we want to output and left join to this new, and easier to use table. I'll post more later on how to do that.
|
# ? Dec 3, 2012 04:47 |
Haha, it's nothing even close to that serious. It's for a new makerspace that is trying to get organized. Someone hacked together a quick check-in system, I'm giving it a massive overhaul to be more useful/robust. The old one had a list of codes in a text file and wasn't even properly compiled (they were literally running it from within eclipse every time) and would post to twitter which then got forwarded/spammed to facebook. This new version posts to a DB-driven web service which keeps the website updated with who is around and lets users manage their check-in code through their forum/wiki profile, which will also allow for alternate check-in mechanisms (qr code, nfc, keypad, etc.) Right now, I'm trying to get some useful/interesting data out of the log in order to make some pretty plots. I shouldn't even need to use an intermediate table, the left outer join I posted above using the generated series and coalesce should suffice with a very little bit of modification. e: Yeah, this is a lot better. Now getting currently-checked-in members is as easy as looking for anyone with an out time in the future. That is, it went from this: code:
code:
code:
Bad Munki fucked around with this message at 05:48 on Dec 3, 2012 |
|
# ? Dec 3, 2012 04:56 |
|
Here is my attempt that doesn't account for several of the corner cases discussed, but is pretty straightforward: https://dataclips.heroku.com/qnldojpgggerodgmezjckiecidvp The result of just the with clause is in version 1 Even with the omissions of some corner cases, this might be close enough.
|
# ? Dec 3, 2012 06:42 |
Yeah, if I ignore ensuring continuous data, getting a head-count log is pretty easy now with the new table design:code:
However, for this, I need the data to be continuous, i.e. covering every period in the report, even if nobody was there and thus no log entry exists in that time. So I came up with this: code:
That sql isn't quite right, but I'm pretty sure it's very close. That is, I think the data is technically accurate, Bad Munki fucked around with this message at 07:08 on Dec 3, 2012 |
|
# ? Dec 3, 2012 07:03 |
|
It's been a while since I've used PostgreSQL, but I think this works using the generate_series magic that I don't get to play with in MSSQL 2008 . If you start with the series as your base table you souldn't need to do anything special to generate the 0 rows for hours with no users. I based the conditions off of your cases at the top of the screen and as best I can tell it counts only the 4 cases we would count as valid for that hour. code:
Aredna fucked around with this message at 08:11 on Dec 3, 2012 |
# ? Dec 3, 2012 07:57 |
|
Edit: Nevermind...
technic fucked around with this message at 16:04 on Dec 3, 2012 |
# ? Dec 3, 2012 15:51 |
Aredna posted:It's been a while since I've used PostgreSQL, but I think this works using the generate_series magic that I don't get to play with in MSSQL 2008 . Yeah, that's basically what I had last time, except I had some late-night-induced frenzy going on with an extra level of nesting and an unnecessary generate_series call that was hosing it all up. I used a left outer join instead of a left join, I honestly don't really know the difference (sql is not my strong point)...both options work, although of course the simpler version works better. Made a couple small adjustments, but mostly cosmetic code:
e: Oh, and you took a different approach to the interval intersection than I did. In the end, I reduced it a little further to 4 cases in 2 isomorphisms, but I'm not sure if there are any real performance differences there. Bad Munki fucked around with this message at 17:33 on Dec 3, 2012 |
|
# ? Dec 3, 2012 17:00 |
|
How many records do you have in the scan table? If it's still half a second it's going to scale pretty poorly. Obviously the first thing would to be to look at some indexes, maybe try one on (time_in, time_out, userid) to see if that helps? LEFT JOIN is just shorthand for LEFT OUTER JOIN - no difference.
|
# ? Dec 3, 2012 17:07 |
Right now, not many (maybe a hundred?), but I'm just playing around at the moment. And yeah, I'm pretty sure the big problem right now is a lack of useful indexes for this operation. I'm not sure how to get an execute plan like I can with oracle, but I'm sure it includes a full table scan somewhere in the tree. e: Ah ha! code:
Difference: using betweens and the four-way OR statement, and changing how the series is generated slightly in that it generates both start and end times. Bad Munki fucked around with this message at 17:14 on Dec 3, 2012 |
|
# ? Dec 3, 2012 17:08 |
|
I actually have a question about PL/SQL in Oracle and I assume this is the correct thread to ask. Anyway, I am trying to print off the names of people who participated in a certain event based on the meet_id that is passed in. I am attempting to do this with a stored procedure and the SQL seems to work fine, but when I put it into the procedure it just lists everbody no matter what meet_id I provide. Even if I ask for meet 5 (which doesn't exist) it still lists everyone's name which should be impossible. Anyway my procedure is:code:
|
# ? Dec 4, 2012 00:38 |
|
Suran37 posted:I actually have a question about PL/SQL in Oracle and I assume this is the correct thread to ask. Anyway, I am trying to print off the names of people who participated in a certain event based on the meet_id that is passed in. I am attempting to do this with a stored procedure and the SQL seems to work fine, but when I put it into the procedure it just lists everbody no matter what meet_id I provide. Even if I ask for meet 5 (which doesn't exist) it still lists everyone's name which should be impossible. Anyway my procedure is: Choose a different parameter name (p_meet_id); naming it the same as a column introduces ambiguity, and is probably adding an "always true" to your statement.
|
# ? Dec 4, 2012 01:28 |
|
var1ety posted:Choose a different parameter name (p_meet_id); naming it the same as a column introduces ambiguity, and is probably adding an "always true" to your statement. Once again I feel like a jackass, thanks!
|
# ? Dec 4, 2012 01:32 |
|
Suran37 posted:I actually have a question about PL/SQL in Oracle and I assume this is the correct thread to ask. Anyway, I am trying to print off the names of people who participated in a certain event based on the meet_id that is passed in. I am attempting to do this with a stored procedure and the SQL seems to work fine, but when I put it into the procedure it just lists everbody no matter what meet_id I provide. Even if I ask for meet 5 (which doesn't exist) it still lists everyone's name which should be impossible. Anyway my procedure is: Why are you using a cursor? It will perform like rear end. I would use a straight SELECT statement and not call a procedure to output each line, which mixes presentation layer stuff with the database stuff.
|
# ? Dec 4, 2012 04:02 |
Okay, I got another one. In my statement, I'm selecting the following:code:
That is, the following data currently gives me a count of 3 (67, 54, 122) 122 122 54 54 54 67 But I want it to give me a count of 4 (67, 54, 122, 122). Is there a straightforward way to do this? Here's the full statement in question: code:
|
|
# ? Dec 4, 2012 17:29 |
|
Bad Munki posted:Okay, I got another one. In my statement, I'm selecting the following: I would just aggregate the results, and then sum those using your rules code:
The first method (count/sum) would be more straightforward and self-documenting.
|
# ? Dec 4, 2012 17:41 |
|
Bad Munki posted:Okay, I got another one. In my statement, I'm selecting the following: I normally would do that by changing 122 to NULL in the distinct count and then counting 122 separately. You've also got several options as var1ety as shown as well. This is a case where I would add a comment to explain why I'm doing this for anyone else that has to read it since in a year someone may wonder what exactly is special about 122. code:
|
# ? Dec 5, 2012 01:36 |
Oh, actually, I like that option more. I ended up changing it just a little further to give me separate columns for non-122 and 122 (members vs. guests) because it makes for a more "interesting" report. Thanks, all, you've all been super helpful.
|
|
# ? Dec 5, 2012 01:45 |
|
So we completely redesigned an old system, and everything is fine there, but they want the old data available in the new one. Copying the data is easy enough (and I can reformat the outdated parts), but the problem arises when trying to preserve the bridges between different tables. With the PK's obviously changing, I'm not sure how I can go about recreating the Many-to-Many relationships. Is there anything simple I'm not aware of to do this?
|
# ? Dec 6, 2012 17:23 |
|
subx posted:So we completely redesigned an old system, and everything is fine there, but they want the old data available in the new one. Copying the data is easy enough (and I can reformat the outdated parts), but the problem arises when trying to preserve the bridges between different tables. With the PK's obviously changing, I'm not sure how I can go about recreating the Many-to-Many relationships. Can you just insert the old data with the same old PKs? Were they incrementing IDs? If so you could just start your new tables somewhere higher than the old records. For instance if your old tables max ID was 850232 you could just make all your new tables ID's start at 1000000 and then set IDENTITY_INSERT on and populate the tables with the old data preserving their original ID's. That would also have the added bonus of making it easy to identify old data versus new data. EDIT: Assuming this is in MSSQL. If it's some other DBMS I'm sure there's a similar setting.
|
# ? Dec 6, 2012 21:02 |
|
Ok I'm getting really tired of PL/SQL. Anyway, I have to make a package and after googling for well over an hour I came up with: http://pastebin.com/rsmxABKq However, I just get an error on compile. PLS-00103: Encountered the symbol / Pointing to line 73.
|
# ? Dec 7, 2012 04:20 |
How are you trying to execute it? I've had problems with some tools, like DBVisualizer. It works for 90% of the stuff I need to do, but it chokes on some things, specifically those slashes in packages or scripts. Sometimes executing the entire buffer as a single command helps, depending on the sql in question.
|
|
# ? Dec 7, 2012 04:34 |
|
JeansW posted:Can you just insert the old data with the same old PKs? Were they incrementing IDs? If so you could just start your new tables somewhere higher than the old records. For instance if your old tables max ID was 850232 you could just make all your new tables ID's start at 1000000 and then set IDENTITY_INSERT on and populate the tables with the old data preserving their original ID's. Well we already have some entries in the new format, but I might take your idea and modify it a bit. Easier to renumber all the tables/bridges with +100000 and then insert the old ones with their old PK's than it is to try renumbering the new inserts. Thanks for the idea!
|
# ? Dec 7, 2012 04:54 |
|
Bad Munki posted:How are you trying to execute it? I've had problems with some tools, like DBVisualizer. It works for 90% of the stuff I need to do, but it chokes on some things, specifically those slashes in packages or scripts. Sometimes executing the entire buffer as a single command helps, depending on the sql in question. Are you talking to me? I'm using oracle. I clicked on procedures > new, which generated some of the code, I filled in the rest, and when I press the compile button I get the Error. If I get rid of the / I get the same error, but CREATE instead of /.
|
# ? Dec 7, 2012 05:31 |
Suran37 posted:Are you talking to me? I'm using oracle. I clicked on procedures > new, which generated some of the code, I filled in the rest, and when I press the compile button I get the Error. If I get rid of the / I get the same error, but CREATE instead of /. Yeah, I was wondering what client you're using to actually apply this to the DB. You're using their little web interfacey thing, I guess? As opposed to, say, sqlplus or what have you. There are lots of options, but if you're doing it through their own provided mechanism, that's probably not the issue, I guess. vv
|
|
# ? Dec 7, 2012 05:42 |
|
Suran37 posted:Ok I'm getting really tired of PL/SQL. Anyway, I have to make a package and after googling for well over an hour I came up with: http://pastebin.com/rsmxABKq That particular error usually means that there was a syntax error earlier, but for whatever reason it could keep interpreting stuff until you try and close it all off. I ran the package create part in SQL*Plus (on 10g r2 XE because that's what I have on this machine) and got code:
Edit: actually that error says nothing about a package body. This link suggests you need to define a return type for your cursors. I'd normally just use an implicit cursor myself, or maybe a view if I need to reuse the query like you seem to, although I don't have any performance stats or snappy words about best practices to back that up. Goat Bastard fucked around with this message at 07:49 on Dec 7, 2012 |
# ? Dec 7, 2012 07:38 |
|
Finally figured it out. Had the body in with the spec, when I needed to hit a button and paste the body into a different window.
Suran37 fucked around with this message at 18:25 on Dec 7, 2012 |
# ? Dec 7, 2012 18:20 |
|
Alright so... writing some dumb loving query and I'm not sure how to proceed from where I'm at. (Oracle) Building this stupid facebook knock-off as our final project for a databases course, Almost done, I think this is the last actual SQL thing I need to knock out. Relevant to this are two tables, FRIENDS (userid1, userid2, jdate, message) MESSAGES (msgid, fromid, message, touserid, msgdate) I'll let my professor's assignment speak for what I'm doing here: quote:My Statistics Right now I've got all of that except I'm querying all users instead of just friends. So, my current query: SQL code:
gently caress this is getting complicated to type out I need to join that query up there with something along the lines of this query here: SQL code:
e: I might have figured this out, having typed it all out. Rubber duck debugging. e2: yep, looks like I got it. Now I wish the professor had given us enough loving sample data to actually test our code. e3: No, I'm stupid and didn't do anything and I still need help here. Sockser fucked around with this message at 02:18 on Dec 9, 2012 |
# ? Dec 9, 2012 01:09 |
|
How many records does your innermost derived table contain for each message? How many records should your innermost derived table contain for each message? e: This is one of the dangers of reasoning about a problem entirely in natural languages. English is not designed to be a database query language, so "find the user who has sent or received the most messages" sounds incredibly similar to the very different problem "find the friend (of this user) who has sent or received the most messages (to this user)". Amarkov fucked around with this message at 05:14 on Dec 9, 2012 |
# ? Dec 9, 2012 05:04 |
|
yeah the latter problem there is really easy to work out and when I thought I had a solution it was actually just that. The former is uhhh yeah I'm not sure how to do, and I'm not sure I entirely understand your questions.
|
# ? Dec 9, 2012 05:33 |
|
The way I'm interpreting your professor's assignment is this: Given a user, find the k friends of that user who have communicated with that user the most times within the past n months. If that's not right, please translate for me or something, because the description is confusing. e: And the former is the problem that your unmodified query solves, so I'm not quite sure what you mean by not knowing how to do it...
|
# ? Dec 9, 2012 05:57 |
|
Given a user, find the k friends who have communicated with anyone the most within the past n months. And yeah, basically the entire course has been that confusing and muddled and awful so I'm stressing a bit. e: and the problem is that I have the most communicating users but I need the most communicating friends of the current user and augh I'm having trouble even explaining what I'm doing. e: it solves for all users, not one particular user. Sockser fucked around with this message at 06:06 on Dec 9, 2012 |
# ? Dec 9, 2012 06:02 |
|
What why in the world would such a thing ever be useful Anyway, your basic query structure will work for this. What you're currently doing is selecting all fromids where the touserid is anything, and selecting all touserids where the fromid is anything. What you want to do is select all fromids where the touserid is the given user and the users are friends, and select all touserids where the fromid is the given user and the users are friends. All you need to do is replace the table "messages" with a derived table. The derived table will be just as long and ugly as you're thinking; as far as I can tell, there's no way to avoid this with the database structure you have. e: No, wait, that's for the way I want to interpret it. If it's supposed to be all communications, the only condition you want is that the users are friends. Amarkov fucked around with this message at 06:30 on Dec 9, 2012 |
# ? Dec 9, 2012 06:17 |
Are you sure that's what the question is after, for all users? As opposed to "find the top k friends who have communicated most with the user who's asking, for the past x months." That is, "Who is talking to me the most?"
|
|
# ? Dec 9, 2012 06:20 |
|
Holy poo poo guys SQL Server 2012 AlwaysOn is awesome. I just installed an availability group (kinda like a clustered mirror with readable secondary) in production this weekend and it went so smooth. Didn't even have to take the database down to do it. (our old singular production database had been built as a node in the cluster to prepare)
|
# ? Dec 9, 2012 17:06 |
|
code:
Issue: The issue with the above is that there are two tables (inventory_trans and part_location) both with the field name QTY however they are unique fields with unique data they just share the same field name. If I sum inventory_trans.qty and group by part_location.qty I don't have a problem, but if I try and sum inventory_trans.qty AND part_location.qty the part_location.qty blows up and gives me huge incorrect numbers. Any suggestions?
|
# ? Dec 12, 2012 22:18 |
|
Bugamol posted:
I think the best solution for you would be something like: code:
|
# ? Dec 12, 2012 22:47 |
|
Jethro posted:The problem has nothing to do with duplicate field names. The problem is that each unique part_location record is getting repeated for each matching inventory_trans record. You'll need to rewrite the query to aggregate before you join. This looks good. I'm getting a syntax error "Incorrect syntax near the keyword 'WHERE'.", line 16, which I'm trying to troubleshoot currently, but I think understand what I need to do now. I appreciate the help and explanation of what was going on.
|
# ? Dec 12, 2012 23:29 |
|
The syntax error is that it has INNER JOIN inventory_trans but never says which column to join on.
|
# ? Dec 12, 2012 23:41 |
|
|
# ? May 30, 2024 19:07 |
|
Golbez posted:The syntax error is that it has INNER JOIN inventory_trans but never says which column to join on. Just got this figured out. Thanks for pointing it out though. Thanks both for your help looks like everything is working great now.
|
# ? Dec 12, 2012 23:43 |