|
I'm attempting to teach myself SQL using various books and internet tools to try and work my way through the ranks at the company I work for, and I've been using w3schools and sqlzoo to try and hone my knowledge. I have a question about question # 5 on this page, the database structure that they're using is here. Question #5 states: Find the number of left racing socks ('Racing Socks, L') ordered by CompanyName 'Riding Cycles'. The correct answer is: 3. I've found the answer using the following: code:
|
# ¿ Dec 10, 2013 19:11 |
|
|
# ¿ May 5, 2024 21:49 |
|
NULL isn't a number, it's the inherent lack of knowledge of a value. Therefore, the expression 'NULL =' anything really doesn't make sense, as you don't know if it's 0, a billion, -3.14, undefined, etc.
|
# ¿ Mar 10, 2014 22:50 |
|
the posted:How do I format something like this: I'm an SQL novice but this should work: Select Name FROM List WHERE Gender = 'Man' AND Color IN ('Blue', 'Red', 'Yellow')
|
# ¿ Jul 1, 2014 20:59 |
|
I'm struggling to compare some rate records in our database and need a nudge in the right direction. I think it can be solved joining the table with itself but as I am a total novice (I've written a few nested selects and that's about the extent of my experience, so for this question Type is most certainly equal to Stupid) I'm at a loss how to proceed! Essentially I'm trying to pull a list of accounts where our customer has lapsed their contracted rate for some amount of time, so instead of receiving a contracted rate they instead receive a default. Rate table structure is (roughly) below:code:
Since 12345's newest fixed rate begins a full month after the previous expired, I'd like it to be pulled, and since 67890's started immediately after the previous one, it would need to be excluded, regardless of the fact that 67890 still has that default in there. My problem is I'm not sure how to set it up to compare the dates. code:
|
# ¿ Oct 13, 2014 21:03 |
|
Anaxandrides posted:Try this: This was the nudge I needed in the right direction, thank you! What I ultimately ended up going with was (account# is ieinum and is actually in a different table hence the extra join): code:
|
# ¿ Oct 14, 2014 21:30 |
|
What's the best way to extract a date out of the middle of a text string in MSSQL? Ex: String = 'Text1 1/1/14 Text2' Text1 is always the same length, but Text2 can be of varying length. I want to be able to order my query by the date pulled out of this string if at all possible, so I assume I need to somehow get rid of Text1 & Text2 while at the same time casting the date as datetime but I am unsure how to attack it. I tried using the TRIM function from this SQL reference book I have like so: SELECT TRIM(LEADING 'Text1' FROM TRIM(TRAILING 'Text2' FROM String)) but MSSQL doesn't seem to have this function and I'm not entirely sure that function is even supposed to do what I'm trying to do with it. LTRIM and RTRIM only trim blanks so that's useless to me. What do I do here? edit: Figured it out myself! code:
kumba fucked around with this message at 20:53 on Nov 6, 2014 |
# ¿ Nov 6, 2014 20:23 |
|
Anaxandrides posted:Do Text1 and Text2 ever have spaces? If not, XML PATH is almost always faster than substringing. Both Text1 and Text2 always have spaces, so it sounds like that wouldn't work
|
# ¿ Nov 6, 2014 22:59 |
|
COOL CORN posted:I'm curious if there's an easier way to do what I'm doing here. I'd use a left join instead of a subselect. This: code:
|
# ¿ Apr 16, 2015 19:41 |
|
Roundboy posted:While I can't offer you too many specifics without knowing your db setup, the answer is almost all universally powershell. Someone on my team just wrote something like this in Powershell for our company last week, so I can confirm this is a good approach.
|
# ¿ Apr 17, 2015 13:57 |
|
I'm looking to calculate some averages over various different date ranges all at once and trying to figure out the best approach. I have a way that works but it is ugly and involves multiple temp tables that I'm trying to clean up to make things more readable. At the core, I am attempting to calculate Daily, Monthly, and Yearly averages for various payment methods that our customers use. I'm starting off with ACH & Credit/Debit Card Payments and will expand it to other methods once I figure out a better way to do this if there is one. We have a table PaymentQueue (PK is ID) that houses all ACH & Card payments made, and a separate table ScheduledPayment that references PaymentQueue that specifies payments that were either one-time payments scheduled ahead of time or are recurring every month. I want to break out the averages by One-Time payments (so anything that is in PaymentQueue but not in ScheduledPayment), Recurring Payments, and Scheduled One-Time Payments. Here's what I have so far: code:
|
# ¿ May 8, 2015 21:56 |
|
Those were just example dates thrown in there to see if the calculations work, which they do - I have no intention of leaving it like that. Eventually it will likely be parameterized so a user can input a range of years to get the averages over. Also this will never be run on anything other than SQL Server.
|
# ¿ May 11, 2015 17:55 |
|
Gatac posted:Okay, this feels like it should be easy, but I just can't figure out a smart way to do it. Oracle 11g, names changed for clarity, and I can't make any changes to the schema or the data, I'm just trying to query it the best way I can. My SQL knowledge is limited to what I've picked up over the last year, so if I'm being an idiot, feel free to call me out on it. Though I've never used Oracle, a cursory google search seems to indicate Oracle can use CTEs, so I would go that route: code:
|
# ¿ May 22, 2015 18:30 |
|
Fruit Smoothies posted:I have part of a table that looks like this If I'm understanding right you could also do this with a case statement code:
|
# ¿ Jul 5, 2015 22:25 |
|
ljw1004 posted:SELECT * FROM Posts WHERE Title LIKE '%resources%' If you're learning SQL Server, the 70-461 is, afaik, the industry standard for starting out: http://www.amazon.com/Training-70-461-Querying-Microsoft-Server/dp/0735666059. At least it is around these parts. There's also the MTA which is way more dumbed down and somehow more expensive than the 70-461: http://www.amazon.com/Exam-98-364-Database-Administration-Fundamentals/dp/0470889160
|
# ¿ Jul 21, 2015 20:09 |
|
I'm trying to write a sproc that takes a tablename as a parameter as well as a recordid, which will use the tablename in an update statement to update that record. I am having no luck. Here's what I have:code:
Msg 245, Level 16, State 1, Procedure MarkRecordAsCompleted, Line 9 Conversion failed when converting the varchar value 'update NestRatesWithoutTPSCRecordsResults SET isShown = 0, MarkedCompletedTime = getdate(), WHERE [ID]=' to data type int. I'm not sure how to stop the sproc from attempting to return an int like this. I thought maybe it was my roundabout way of grabbing the username, but I deleted that and tried without it and no dice. I tried just adding a line at the end to RETURN 'Success!' instead of the integer value but that doesn't seem to work either. I tried adding an output variable to the proc and that was no good. Not sure where to go from here!
|
# ¿ Jul 21, 2015 20:20 |
|
Nth Doctor posted:Do a cast of @RecordID: You're a gentleman and a scholar. Thank you!
|
# ¿ Jul 21, 2015 20:58 |
|
Whenever you connect to the server, your connection defaults to the "master" database and you have to either include a use statement to let your query know which database you want to talk to, or you need to just change it in the menu at the top: To avoid doing either of these steps, you should get used to using 3 (or even 4 if you deal with multiple servers) part naming in your queries instead. So, if you have a server named Server1, and a database within that called DBMaster, and a table called Customers in the dbo schema within that database, after connecting to Server1 you could write: code:
code:
code:
|
# ¿ Jul 29, 2015 15:57 |
|
No Safe Word posted:Just be aware that like 99% of the time this is the wrong way to do such a thing. Other than occasional one-off things. This is the first I've heard this - how else can you accomplish the same thing? We have several reporting servers that do various things and we have nightly processes where batches of monitoring queries are run and we've never run into a problem with the servers talking to each other. e: This post may sound snarky but it's not meant to, if we could do this in a better way I'm totally open to it! kumba fucked around with this message at 17:20 on Jul 29, 2015 |
# ¿ Jul 29, 2015 17:16 |
|
You have to separate them, yeah. There's no way to combine LIKE and IN (without including every possibility something could be LIKE inside the IN).
|
# ¿ Aug 25, 2015 21:54 |
|
That is correct. The order they're evaluated in is: FROM WHERE GROUP BY HAVING SELECT ORDER BY
|
# ¿ Sep 1, 2015 21:39 |
|
Knifegrab posted:Awesome thanks, this helps a ton! I will keep all of this in mind when building my application. If I'm understanding your question right you can use the output clause, here's an example: code:
|
# ¿ Oct 19, 2015 14:48 |
|
My first thought was to put NULL where it's undefined and use coalesce from order of most specific to least specific in order to get the best hit, so: select coalesce(City.speedLimit, State.speedLimit, Federal.speedLimit); Should always give you the right answer
|
# ¿ Dec 8, 2015 15:13 |
|
Are the fields before and after it consistently the same? My first thought is to use the various substring/charindex/patindex built-in functions to find the position of the "LastName" & "ProxyOnly" fields, find the MRN in between it, and use commas as delimiters If they're not, you're going to probably have to make an ugly custom function to count occurrences of MRN, then find the starting position of the final occurrence and work from there Something like: code:
|
# ¿ Dec 15, 2015 20:20 |
|
As long as you know that there is only one occurrence of MRN in between LastName and ProxyOnly (seems like it?), then it doesn't actually matter how long anything is cause you can figure it all outcode:
|
# ¿ Dec 15, 2015 22:49 |
|
This would be significantly easier if you A.) didn't have to use SQL or B.) could convert the data to XML easily somehow I don't know much about non-SQL stuff since I'm a data monkey and not a dev, but if you can convert whatever data this is to XML then you can use the built-in XML stuff to yank out the value you need code:
|
# ¿ Dec 15, 2015 23:08 |
|
Gatac posted:Note that this will only give you a product and the highest revision, not the price of that revision. You will need a ranking function for that. Which is also relatively easy, just adds a few lines: code:
|
# ¿ Feb 15, 2016 22:47 |
|
Huh. Never thought to use an inline-view like that instead of ranking. Interesting approach. I am a sucker for CTEs so that's where my mind always goes first
|
# ¿ Feb 16, 2016 17:12 |
|
From personal experience option b has always worked out easiest for me, but I'm pretty sure it's just going to boil down to checking your execution plan and seeing what works best with the tables you're working with.
|
# ¿ Feb 19, 2016 19:03 |
|
Do we have an SSRS thread anywhere or would this be a decent place to ask a question? I'm trying to figure out if there is anyway in SSRS to load up the front page of a report that has parameters without the parameters being supplied yet. Essentially I'm trying to populate an instructions page of sorts before the user actually inputs the information and hits the 'View Report' button, but for the life of me I cannot figure out how to do it or if it's even possible. I would think something like this would be very common and was surprised to find how obtuse this software can be.
|
# ¿ Feb 23, 2016 19:56 |
|
I left out a very important detail that will likely impact the solution: I'm using SSRS for something that is definitely not what it was designed for. There are reasons I'm doing this (mostly because development time is not really available due to other things taking far higher priority than this), but suffice to say that I'm not using this report to actually report on anything, but rather as a way for a user to be able to add information to a database without an actual UI. Yes, that means I'm using Insert statements in my SSRS Dataset. It's ugly and stupid but really I don't have any other way to do what I need to do. The problem is that if I just populate defaults, it's going to insert that default information into the database whenever someone opens it. I would love if I could just have the database table have non-nullable fields and default all the SSRS parameters to NULL and fire away, but it throws an error in that case instead of doing what I want it to. This is going to be a two part solution, as the "Report" I'm building is just inserting into a dummy sandbox table on a non-production environment, and a daily job will run to crawl through what's inserted into my sandbox and put what is necessary into production. What I'm thinking is to have the defaults set to something clearly erroneous, and then part of the daily job would be to remove anything it recognizes as a default and process the rest. This sounds dangerous but I'm not sure I have any other options.
|
# ¿ Feb 23, 2016 21:16 |
|
The sproc method you suggested set off a lightbulb in my head that I don't know why I didn't think of earlier: I can just filter out the dummy placeholder data in the insert itself. The whole purpose of this tool was to allow a select few members of our organization to manually put in orders for a specific product. There's a quantity variable dictating how many units they're trying to order, so I can just set the placeholder data to have a quantity of 0 and do a simple INSERT INTO MyTable (Quantity) SELECT @Quantity WHERE @Quantity > 0 (since I care not about orders with 0 or less quantity) Obviously there's more columns than that but the logic works out the same. Can't believe I never thought to do that. Thanks for the nudge!
|
# ¿ Feb 24, 2016 15:30 |
|
Indeed. Necessary evils and all that.
|
# ¿ Feb 24, 2016 16:53 |
|
Can you select top in oracle? If so this should workcode:
|
# ¿ Mar 1, 2016 18:36 |
|
A sproc could be using the built-in SYSTEM_USER function to generate the username in which case whoever fires it off will be listed as the user
|
# ¿ Apr 20, 2016 20:11 |
|
HoboMan posted:Ok so this is breaking my brain: I got a table that has a FK to itself (PK is ID, FK is LinkID). I need a query that gives me everything where an item is not linked to another item. In other words I need all rows where its ID does not appear in another row's LinkID column. If I understand your problem correctly: code:
|
# ¿ May 12, 2016 18:41 |
|
Subqueries used properly are not gross, but that left join method will work just fine yeah. Explanation without venn diagram: left joins keep all data from the table on the left side of the join regardless of whether the key being evaluated has a match in the table on the right. If you remove the where clause, this will return every ID in t1 (regardless of if there is a corresponding LinkID in t2), as well as the match in t2 if it exists. It will return NULL if it does not find a match, instead of a normal join which will filter out the row entirely. Thus, when you add the where clause back in it is returning all IDs in t1 that do not have a corresponding match in t2, which is exactly the condition you're looking for.
|
# ¿ May 12, 2016 19:36 |
|
I would do something like this instead:code:
|
# ¿ May 12, 2016 20:37 |
|
Moreau posted:So, I have two databases sitting on an MSSQL server. Database A has a table, Table A, that a user updates arbitrarily, 4 or 5 times a year. Database B has a view of Table A, View A. How quickly do you need to respond to changes in Table A? Is some delay acceptable or does it need to be immediate? If you're okay with some delay, you could setup a simple (hourly/daily/whatever increment is appropriate) job that would crawl through table A looking for changes. I'm assuming table A has some sort of LastUpdatedOn column or something of that nature, so you could store the current LastUpdatedOn in some table in Database B and do something like code:
|
# ¿ May 27, 2016 14:15 |
|
I'm sure there's a better way to do this but this is the first thing I thought of (temp table is just for demonstration, obviously you don't need that part in your case): code:
|
# ¿ Aug 12, 2016 18:47 |
|
|
# ¿ May 5, 2024 21:49 |
|
COOL CORN posted:stuff You should be able just use that query as an inline view and group by it: code:
|
# ¿ Sep 12, 2016 17:16 |