|
noonches posted:I have a table of survey submission answers set up like this,
|
# ¿ Jan 8, 2008 21:27 |
|
|
# ¿ May 4, 2024 05:32 |
|
noonches posted:Unfortunately, I did not set up the table or the framework I'm using for this. So I can't change the way the table is set up, and I can't use a PHP script to tear it apart and get the count because I need to feed a single query into a class that spits out a styled html table for the website. Is this as hard as it seems to me, or am I just having an off day?
|
# ¿ Jan 8, 2008 22:02 |
|
You shouldn't have to join to your questions table, that's probably why you're having to do the group by.
|
# ¿ Jan 8, 2008 23:48 |
|
So, I have a site where users can make predictions on events. Users can be grouped into leagues. When I calculate the score for a league, I only want to take the 15 top-scoring users from the league and total only their scores to store as the league's score. (to prevent leagues with <20 users having zero chance to rank against a league with 50+ users) code:
I got around it by just getting a list of league IDs, and looping over that and only summing up the results a league at a time, but I'd prefer to have it all in one query. Anyone have any ideas?
|
# ¿ Jan 9, 2008 17:40 |
|
imBen posted:By the way, I don't think your inner join works at all. Your inner join selects 15 points... unless I'm reading something wrong. I probably am. Maybe if I put the tempTable in the FROM and then join that to the league table, I could get it to work. Since I can just loop over the items, it's not a huge issue - and these are cached totals so it's not like I'm too concerned about efficiency - I'd just like to learn the way to do that. I haven't done too much SQL like this before, so I have a hard time visualizing what I need to do. edit - here's the SQL we're talking about code:
cletus42o fucked around with this message at 02:46 on Jan 10, 2008 |
# ¿ Jan 10, 2008 02:43 |
|
Victor posted:If you give me sample data (more rows than would be displayed in your desired output) and the desired output, I'll give you a SQL statement that works. Or I'll be really amusing in my attempt. code:
|
# ¿ Jan 10, 2008 16:26 |
|
Nice, thanks. I will see if I can get that to work. Took me a second to realize what was going on until I saw the "start reading here." edit - Actually, I'm still not really sure how to use this. Could you show me what I should only need to stick into QA to get the proper results, if you don't mind? SQL 2000, by the way. I also don't actually need a string showing "x+x+x+x+x", I was only doing that to show you what numbers were being summed up. Okay, I think I have it all properly entered.. but it's getting caught on row_number() - what function is this? Is there a 2000 equivalent? cletus42o fucked around with this message at 17:33 on Jan 10, 2008 |
# ¿ Jan 10, 2008 17:21 |
|
So I was able to write this based on some advice I got elsewhere.. (couldn't get your SQL to work, Victor, since row_number() doesn't seem to have any equivalent in 2000 - thanks though!)code:
|
# ¿ Jan 10, 2008 21:11 |
|
Victor posted:The above can be simplified -- I'm too lazy and tired.
|
# ¿ Jan 10, 2008 23:02 |
|
minato posted:In the past I've implemented Option 1, like a good little database normalizer. However the clunkiness of doing so has led me to want to implement Option 2. The rest of my team (not very DB-design-savvy) is advocating Option 3, which to me is right out because it would take god himself to pry FK constraints from my dead hands. But I don't know, I've never had problems with it. The most common way I've used option 3 is in a "files" table, where a file can be attached to any other item in the system. I have the file table itself, which only stores info pertaining to the file, then a table I usually call file_item_jn - the join table has the file ID and two fields called foreign_table and foreign_key. So I haven't regretted it in the places I've used it. But then I did develop another system where I have separate join tables for each "type" - and then I have a view that unions them all and creates the type field for me (so a "virtual option 3"), since there are places where I have to provide a snapshot of all the data at once.
|
# ¿ Jan 16, 2008 14:35 |
|
SQL Server 2000 I want to run a regular expression on a column when comparing it to text entered through a search form. WHERE REREPLACE(fieldName, '<[^>]*>', '') LIKE '%#searchKeyword#%' Basically to do a comparison on a field that may have HTML in it. I want to strip the HTML out as I do the comparison.
|
# ¿ Oct 2, 2009 17:04 |
|
I'm attempting to migrate a database from SQL Server 2000 to 2008. I restored the database on 2008 with no problem. Suddenly, a lot of my queries are returning results in a totally different order than in 2000. Here is an example of a query exhibiting this problem: code:
I don't have a problem rewriting this stuff, I'm just trying to figure out what exactly changed in SQL's behavior. Anyone have any ideas? It's absolutely possible this happened between 2000 and 2005, I haven't tried restoring it to a 2005 DB to check yet. edit - Looks like it may have to do with the TOP 100 PERCENT. Don't ask me why that's being used. cletus42o fucked around with this message at 23:15 on May 27, 2010 |
# ¿ May 27, 2010 23:13 |
|
|
# ¿ May 4, 2024 05:32 |
|
Sprawl posted:Why would you even attempt do that kind of a join that way? All it looks like its doing is sniping off the top 50 mails it really doesn't need to be that hard. But I just found that apparently this was a bug that the ORDER BY isn't seen when using TOP 100 PERCENT, and there is a hotfix available. Not sure why our automatic updating didn't pick up this update, but we've got a request in to have the hotfix applied. I'll try it again once it gets patched up, and hopefully that'll have taken care of it. Wasn't really sure how to Google it at first, until I noticed that changing it to TOP 1000 from TOP 100 PERCENT seemed to do the trick - so then I Googled "select top 100 percent SQL 2008" and the issue came up on the first page of results. edit - This was the first result from Google, if anyone is interested. http://blog.sqlauthority.com/2009/11/24/sql-server-interesting-observation-top-100-percent-and-order-by/ It sounds like using the TOP 100 PERCENT with an ORDER BY isn't exactly a best practice, but again, I'd rather get it working first, rewrite later. cletus42o fucked around with this message at 23:34 on May 27, 2010 |
# ¿ May 27, 2010 23:31 |