|
Sprawl posted:This is MSSQL server 2005/8 right? 2008. I figured it out though, had to tie the providerID in table_two.
|
# ? Apr 14, 2011 03:10 |
|
|
# ? Jun 11, 2024 03:26 |
|
Thel posted:(MS SQL Server 2005, will be going to 2008 later this year and can wait until then if needed.) Right, I've solved this for Oracle, and hopefully you can take it the rest of the way and adapt it for SQL Server. I've made two assumptions: That there is a unique id field that identifies every row in your table, and that in the case of a staff member seeing a client from 11-12 and then another from 12-1 that these times should not be rolled up into 11-1. So first, my test data: code:
1) Select an inline view of the table with each row containing a foreign key to another row that it should be merged with, or null if such a row does not exist. 2) Select from that view every root row in the hierarchy (ie rows with null as their fk), and every row, including the root row itself, that "reports to" that root either directly or indirectly. That's the tricky part. 3) Now that we have a list of all the times that belong in a merged time block we can group by the root row for each block, and select the min and max times from the set of rows that "report to" that root. Here's the Oracle query that I used to do this. I've tried to make it as database agnostic as possible, but there's no getting around the CONNECT BY for the hierarchy query (The ORDER BY is only there to make the results a bit more readable): code:
code:
I'm sure I could have made that way more clear, so let me know if some part of it doesn't make sense or is flat out wrong. And thanks for posting the problem, it was quite a fun one to try and solve. edit: cleaned up the query a bit Goat Bastard fucked around with this message at 04:15 on Apr 20, 2011 |
# ? Apr 18, 2011 02:26 |
|
I've been teaching myself some SQL on the side and I can't figure out what the proper syntax for getting a count value from a sub-query. I'm using microsoft access for what it's worth. So let's say I'm getting a username, and a count for the number of sales user has in another table in a certain category, lets say widgets. code:
|
# ? Apr 20, 2011 23:19 |
|
I'm coming at this from an MSSQL standpoint so it'll be a bit different, but couldn't you just use a group by? e.g.code:
|
# ? Apr 20, 2011 23:33 |
|
Yeah I tried that but access is still bitching at me. It's really hating that count function in the first line. For what it's worth the error is: "You tried to execute a query that does not include the specified expression 'username' as part of an aggregate function." edit: well drat I took out the count and I'm getting the same error. welp.
|
# ? Apr 20, 2011 23:42 |
|
Try this abortioncode:
|
# ? Apr 20, 2011 23:47 |
|
I guess I forgot to mention I'm using an intermediary (I have no idea what all the proper lingo is) table between the sales guy and the actual sales. So I have my salesGuys table, a table relating their ID to their multiple sales IDs, then another table that actually contains all of the sales data. I'm looking more to understand this than just get some code, so bear with me. So we first start at the salesGuy table, and look at his ID number. We take that ID number, and doing an inner join we expand our table to (for now) all of his sales. We then do ANOTHER inner join to get to the actual table with data on it, then constrain it based on the widget thing. Right? So far we have our SELECT FROM INNER JOIN INNER JOIN WHERE GROUP BY **RAN A TEST HALFWAY THROUGH TYPING THIS** I just took the liberty of printing out more data to see what my query was actually doing, and it looks like I had the right idea but convinced myself it was wrong. Turns out the problem is in my comparator in the WHERE clause, which is a bit more complicated than just a string comparison. Ok! So new question: Let's say I have two fields, a and b, where a is usually a subset of b but not the other way around. Right now I'm using a WHERE a NOT IN(b) and that's not working at all, pretty obvious why now that I think about it. So what should I be using? I guess an example would be a = "FOO" and b = "FOO 505050" how do I go about comparing these two values? I would think a LIKE would work but then I don't know how to assign wildcards onto fields.
|
# ? Apr 21, 2011 00:10 |
|
The big problem is I have no idea what to google for this one. I can't search 'sql like access' because the only examples I get are the beginner 'compare a field to a string with wildcards' tutorials and not how to compare two fields together or how to concatenate a wildcard onto a field for comparing. edit: Turns out I again had the right idea (fieldName+'*') but I was comparing the two fields backwards Its working like I wanted it to. Sorry about all that. Necc0 fucked around with this message at 00:54 on Apr 21, 2011 |
# ? Apr 21, 2011 00:32 |
|
A friend of mine's backend developer bailed on him, and so he has me looking at his web server, and I'm not too well versed in this. He wants me to take a database and export some of the data based on some criteria. I've got WAMP but I don't have much of the site's files running locally yet because everything is spread out. Now, if this database is being updated pretty much daily, then the file details for it should show it as having been changed or modified very recently? In var/lib/mysql I've found custom directories containing .frm .myd .myi files but I'm not sure if they're active or the "live" ones I want. The server has directories for several sites, both beta and active, but I'm not even sure about the latter because he has a few servers with a couple different hosting companies. Obviously I've bitten off more than I can chew, but can anyone tell me what I should be looking for, in terms of the active databases?
|
# ? Apr 22, 2011 21:01 |
|
Can you log into the database? Whatever user/pass you need should be stored in a config file for the web app. Then you could use mysqldump (or use MySQL workbench) to make a backup of the databases.
|
# ? Apr 22, 2011 22:25 |
|
When I ssh to the webserver I can immediately bring up a mysql prompt, but I'm unsure what to load or where from. I have root access; I haven't seen any config files that detail a user/pass. Also, the previous developer had mentioned that the database was encrypted, and that a decrypt function was available in a php file, but I wasn't able to find the specific filename, if such a procedure is common/possible.
|
# ? Apr 22, 2011 23:01 |
|
Necc0 posted:I've been teaching myself some SQL on the side and I can't figure out what the proper syntax for getting a count value from a sub-query. I'm using microsoft access for what it's worth. What about this? Just replace "salesID" with your salesTable primary key... This should work... code:
|
# ? Apr 23, 2011 17:59 |
|
Project: I've got totals for each month of a year I'm pulling back in a query. Currently I have implemented all my calculations in Excel, but my boss wants me to have a completely query based approach to what I'm doing, ideally in SQL Server Reporting Services. So, I want to pull in totals for each month of a 12 month period, i.e. Jan - Dec / Apr - Mar, whatever. I then want to build a moving average of the totals for the proceeding 13 months prior to each month, then build a confidence interval/standard deviation for acceptable variations in the data. Right now in Excel, I just run an average of the period, then a standard deviation, then a confidence interval. For each month, for each separate "entity" in the data. Usually between 30-40 entities, so it is a lot of data and calculations. Honestly my excel based approach takes me 30 seconds to build a spreadsheet report returning the data from a web service, so I don't see the necessity - but it is what it is, and he wants me to make it completely separate from excel. So... any thoughts?
|
# ? Apr 25, 2011 16:18 |
|
I have a large XLSX file with multiple sheets and I was wondering how I can write a SQL statement against it. I have seen it before where someone would write SQL against an excel spreadsheet, treating the "sheets" as tables and the top rows as "column" names and they were able to make it work by unioning all the sheets. I searched Google and all over but can't find it. Anyone know how to get this done?
|
# ? Apr 26, 2011 22:47 |
|
cannibustacap posted:I have a large XLSX file with multiple sheets and I was wondering how I can write a SQL statement against it. Never tried it but I've always been curious about this: http://www.sqldrill.com/
|
# ? Apr 26, 2011 22:55 |
|
cannibustacap posted:I have a large XLSX file with multiple sheets and I was wondering how I can write a SQL statement against it. What rdbms are you using? If it's Oracle just use SQL*Loader to actually get the data in the db, I've quickly imported MASSIVE amounts of data into my database from xls/csv/txt files with it. TOAD even has a neat SQL*Loader wizard that lets you use it while staying away from scary command line interfaces. If you're using something other than Oracle I'm sure there's an equivalent for your db flavor. EDIT: I'm assuming you do have a db of some flavor, which is silly but hey this is the SQL questions thread. If you don't you could always install postgres or MySQL Vanadium Dame fucked around with this message at 04:35 on Apr 27, 2011 |
# ? Apr 27, 2011 04:32 |
|
Markoff Chaney posted:What rdbms are you using? If it's Oracle just use SQL*Loader to actually get the data in the db, I've quickly imported MASSIVE amounts of data into my database from xls/csv/txt files with it. TOAD even has a neat SQL*Loader wizard that lets you use it while staying away from scary command line interfaces. If you're using something other than Oracle I'm sure there's an equivalent for your db flavor. Well, I was kind of looking for a way to treat my excel as the "db engine" and just run queries off of it...
|
# ? Apr 27, 2011 06:29 |
|
cannibustacap posted:Well, I was kind of looking for a way to treat my excel as the "db engine" and just run queries off of it... Do you have SQL Server available? If so, you can connect to your Excel workbook as a 'Linked Server' and query it that way: http://support.microsoft.com/kb/306397
|
# ? Apr 27, 2011 13:54 |
|
Edit: You can do what ^^ he said with MS Access as well, but it might be a bear if there are a ton of sheets, as I recall that you'll have to link them one by one.cannibustacap posted:Well, I was kind of looking for a way to treat my excel as the "db engine" and just run queries off of it... You can do this with VBA. You'll want to loop over the sheets and dump their contents into an ADO recordset and query against that. But there's almost certainly a less cumbersome and more direct way to accomplish whatever it is that you're doing.
|
# ? Apr 27, 2011 13:55 |
|
I had some help in the php thread, and now I'm very much stuck on this bit of mysql. A band can make a profile on the site, if they do their band.name is added to the master list . At the moment I LEFT JOIN and if the band name in the event listings matches the band in the master list, I use the unique id on the master list to make an html link to the band profile. . If band_id === null it just prints out the band name without a link on the line. This works for one band at one venue. But what if multiple bands (`mainlist`.`band2` `band3` etc), play the same show and more than 2 have profiles uploaded, how do I get their unique band_id as their link. I hope that makes sense. Here is the code for the mysql so far quote:
tiki-guru fucked around with this message at 14:33 on Apr 27, 2011 |
# ? Apr 27, 2011 14:30 |
|
tiki-guru posted:I had some help in the php thread, and now I'm very much stuck on this bit of mysql. code:
|
# ? Apr 27, 2011 15:12 |
|
Jethro posted:Thank you.
|
# ? Apr 27, 2011 17:59 |
|
Quick question: I have a table with startDates and endDates, and Recommendations. Recommendations last from the startDate to the endDate (generally this is a 1 week period). I want to take the average recommendation for a month. I figure I can take the datediff(EndDate,Startdate), multiply that by the recommendation, and divide that value by the number of days in the month for middle weeks. However, beginning and end weeks pose more of a challenge. If a week has a startDate of 01/26 and an endDate of 02/01, I should weight it accordingly (that is, only (1*recommendation)/28). How can I get a count of how many days in a given time period are in a certain month?
|
# ? Apr 27, 2011 18:09 |
|
Just like to say I nominate Jethro for SQL Thread MVP.
|
# ? Apr 27, 2011 20:35 |
|
Scaramouche posted:Just like to say I nominate Jethro for SQL Thread MVP. He's added three pages to this thread with his responses. Jeeez... Thanks Jethro! I have a request where, depending on one of the three values of AccountType, I need to place the AccountNumber into one of three columns in the query output. In addition, if a userID is linked to multiple types of account, the account number information should be all in a single row of the output. code:
code:
Agrikk fucked around with this message at 05:18 on Apr 28, 2011 |
# ? Apr 28, 2011 05:14 |
|
Agrikk posted:
code:
edit: added distinct to select into baquerd fucked around with this message at 05:41 on Apr 28, 2011 |
# ? Apr 28, 2011 05:37 |
|
Agrikk posted:He's added three pages to this thread with his responses. Jeeez... Thanks Jethro! What you're looking for is a PIVOT or crosstab query. How to do this depends on what RDBMS you are using. It's better to do this in a single query instead of using a temporary table.
|
# ? Apr 28, 2011 07:57 |
|
KiloVictorDongs posted:Quick question: I have a table with startDates and endDates, and Recommendations. Recommendations last from the startDate to the endDate (generally this is a 1 week period). I want to take the average recommendation for a month. I figure I can take the datediff(EndDate,Startdate), multiply that by the recommendation, and divide that value by the number of days in the month for middle weeks. However, beginning and end weeks pose more of a challenge. If a week has a startDate of 01/26 and an endDate of 02/01, I should weight it accordingly (that is, only (1*recommendation)/28). How can I get a count of how many days in a given time period are in a certain month? Use a calendar table, which is a lookup table you can join to. Here's an example article: http://searchoracle.techtarget.com/answer/Populate-a-calendar-table; google for more info. Basically you join the calendar table on the calendar date BETWEEN startDate and endDate and the calendar month = MONTH(endDate) and count the rows in the filtered calendar table. The calendar table has one row per day.
|
# ? Apr 28, 2011 08:02 |
|
Agrikk posted:... Here's one that should be database agnostic as long as there's support for the max() function: code:
Goat Bastard fucked around with this message at 13:37 on Apr 28, 2011 |
# ? Apr 28, 2011 08:46 |
|
Agrikk posted:He's added three pages to this thread with his responses. Jeeez... Thanks Jethro! Its pretty easy if you think about it in simple terms. code:
A Pivot table or Crosstab would be best but if your using mysql you dont have those.
|
# ? Apr 28, 2011 21:42 |
|
camels posted:What you're looking for is a PIVOT or crosstab query. How to do this depends on what RDBMS you are using. It's better to do this in a single query instead of using a temporary table. Sprawl posted:A Pivot table or Crosstab would be best but if your using mysql you dont have those. I'm using SQL 2008 Enterprise. Can you explain further or point me to a good reference? I've never used PIVOT before...
|
# ? Apr 29, 2011 05:20 |
|
Agrikk posted:I'm using SQL 2008 Enterprise. Can you explain further or point me to a good reference? I've never used PIVOT before... Search for PIVOT in Books Online, or "SQL Server" PIVOT in msdn.microsoft.com and you'll find this: http://msdn.microsoft.com/en-us/library/ms177410.aspx
|
# ? Apr 29, 2011 19:41 |
|
Moving Average Using SQL Server 2008 sample data: code:
What I've tried is the following: code:
Any thoughts? The code doesn't work as-is, I was using a tutorial here: http://oreilly.com/catalog/transqlcook/chapter/ch08.html
|
# ? Apr 29, 2011 19:44 |
|
Agrikk posted:I'm using SQL 2008 Enterprise. Can you explain further or point me to a good reference? I've never used PIVOT before... If you find anything good that aren't the same dry MSDN examples post it here. I can do PIVOTs but I can't visualize one from scratch like I can with all kinds of other statements; it always ends up being a messy couple of minutes of add a statement, remove one, tinker here, tinker there, until it eventually works.
|
# ? Apr 29, 2011 20:35 |
|
Agrikk posted:I'm using SQL 2008 Enterprise. Can you explain further or point me to a good reference? I've never used PIVOT before... code:
code:
Sprawl fucked around with this message at 18:08 on May 2, 2011 |
# ? May 2, 2011 17:38 |
|
This should be fairly simple. I have 0 DB experience but need to have this query working by Monday.code:
Edit - There will be a lot of cases where Client_refer will have no data. These still need to be returned. Swink fucked around with this message at 04:20 on May 7, 2011 |
# ? May 7, 2011 03:30 |
|
Swink posted:This should be fairly simple. I have 0 DB experience but need to have this query working by Monday. Which particular SQL flavor are you using? EDIT: In any case, you'll be looking at a left outer (also self) join. Something along the lines of: SELECT Client.ClientID, Client.ClientName, Referral.ClientName FROM ClientTable AS Client LEFT OUTER JOIN ClientTable AS Referral ON Client.Client_Refer = Referral.ClientID That is, if your referrals are from people also in the same table, if I gather right. This'll also show NULLs in the referral name if there's no matching person. Aionic Duck fucked around with this message at 05:28 on May 7, 2011 |
# ? May 7, 2011 05:05 |
|
Aionic Duck posted:if your referrals are from people also in the same table, Yes they are. So that solution should work fine. Thanks! Edit - You've got a "ClientTable" in there that bugs out my query. I'm on SQL2008. Sorted it out. Working thing looks like this: code:
Swink fucked around with this message at 08:33 on May 7, 2011 |
# ? May 7, 2011 05:46 |
|
Another SQL Server 2008 question: I need to count the number of specific events that occur on a user account during a specific date range based on its creation date. I'm confused on how to write a query that retreives UserID and ActiveDate, then counts occurences of EventX between ActiveDate and Activedate +42 days. I keep bumping up against a "A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations." error and I don't understand how to dump a column record into a variable and then use that variable in a subquery of the original statement. update: I tried a different tack and tried to precalculate the range for each userid, returning a temp table #Dates that has: UserID, ActiveDate, SixWeeksLater Then running this query: code:
Agrikk fucked around with this message at 06:00 on May 9, 2011 |
# ? May 7, 2011 23:36 |
|
|
# ? Jun 11, 2024 03:26 |
|
Agrikk posted:Another SQL Server 2008 question: Select userid, activedate, count(*) from sometable Where activedate between somedate and dateadd(dd,42,somedate) And eventx = 'whatever' Group by userid, activedate At least I think that's what you're asking. If your dates include time then that's not going to be very fun. You migh want to do something like convert(varchar,activedate,101) This sucks typing on iPad. --- looking at your example in your edit.... It doesn't match up with what you've said. You don't need that first item in your where clause since it's handled by an ON statement and you don't have to use BETWEEN like I have but you might want to do something like <= >= benisntfunny fucked around with this message at 06:09 on May 9, 2011 |
# ? May 9, 2011 06:02 |