|
Let's say I have the following tables: Control, with BatchID and Source columns Translation, with Source and Translate columns Raw, with BatchID and Data columns I want the following logic, but in T-SQL: code:
Thermopyle posted:What's a good book or other resource on practical SQL? Preferably something that doesn't spend eons of time on how to do basic poo poo that every programmer who has to touch any SQL already knows how to do...
|
# ? Jun 20, 2018 18:54 |
|
|
# ? Jun 3, 2024 12:38 |
|
I really enjoyed reading SQL Antipatterns, it's very focused on how to not gently caress UP your database design. But it's not going to teach you advanced SQL mojo if that's what you're looking for.
|
# ? Jun 20, 2018 18:58 |
|
Janitor Prime posted:I really enjoyed reading SQL Antipatterns, it's very focused on how to not gently caress UP your database design. But it's not going to teach you advanced SQL mojo if that's what you're looking for.
|
# ? Jun 20, 2018 19:00 |
|
anthonypants posted:Let's say I have the following tables: You don't need T-SQL: code:
|
# ? Jun 20, 2018 19:13 |
|
Thermopyle posted:What's a good book or other resource on practical SQL? Preferably something that doesn't spend eons of time on how to do basic poo poo that every programmer who has to touch any SQL already knows how to do... The Art Of SQL is a superb book on querying efficiently.
|
# ? Jun 20, 2018 19:19 |
|
Moonwolf posted:You don't need T-SQL: This also illustrates the difference between RBAR and set-based operations. Generally avoid things like loops and cursors when possible; the set-based approach almost always exists and is almost always the best solution.
|
# ? Jun 20, 2018 19:45 |
|
Moonwolf posted:The reason your other way doesn't work is, having done it on the actually super useful Oracle livesql (https://livesql.oracle.com) is hard to show because the data explodes out, but with 5 players, who each kill each other once, you end up with 150 rows with joining for kills and deaths at the same time. You get for each kill row, every possible death row, because they're independent rows joined in. This is pretty slick, but MySQL doesn't have FULL OUTER JOINs. The recommended MySQL workaround for a lack of FULL OUTER is doing a UNION of a LEFT JOIN and a RIGHT JOIN, so we are back to three joins.
|
# ? Jun 20, 2018 21:37 |
|
Yeah, if you don't have it then just left join the subqueries onto the core table. On the cursors thing, I'd say only use them if you're going to do more than 1 discrete thing per output row. Anything where you're only generating one set of output can be done in a single statement and doesn't need them.
|
# ? Jun 20, 2018 23:30 |
|
I've got a table that tracks start/end dates, and I'd like to get a "usage-days" sum on a monthly basis. I have it working for a date range, but I'm stuck figuring out how to get it by month. It's not a simple as a group by since each month's end date needs to be accounted for. Here's the range version (for 2017-04-28 to 2018-01-01). The LEAST clause covers the cases where the real_end_date is after the query range, otherwise I wouldn't need the CTE. code:
code:
code:
|
# ? Jul 2, 2018 05:46 |
|
Take a look at the EXTRACT function.
|
# ? Jul 2, 2018 07:02 |
|
Minus Pants posted:I've got a table that tracks start/end dates, and I'd like to get a "usage-days" sum on a monthly basis. I have it working for a date range, but I'm stuck figuring out how to get it by month. It's not a simple as a group by since each month's end date needs to be accounted for. Yeah - calendar table is the way to go. Calendar tables generally have a row per day and can have tons of columns that represent properties of that date. Examples of possible columns are DayOfWeek, MonthNumber, FirstDayOfMonth, IsHoliday, etc. Some of those columns can be trivial to calculate manually and are just nice to have (MonthNumber), others not so much (IsHoliday). Anyway, you can probably get away with a two column table: CalendarDt which represents the row’s date and MonthDt which represents the first day of the month for that month. To solve your problem you join your usage table to the calendar table using the between operator, then group on MonthDt. Pseudo (phone) code: SELECT MonthDt, COUNT(*) Days FROM usage u INNER JOIN calendar c ON c.CalendarDt BETWEEN u.start_date and u.end_date GROUP BY MonthDt
|
# ? Jul 2, 2018 16:33 |
|
Ruggan posted:SELECT MonthDt, COUNT(*) Days FROM usage u INNER JOIN calendar c ON c.CalendarDt BETWEEN u.start_date and u.end_date GROUP BY MonthDt Ahhh that makes sense. Thanks!
|
# ? Jul 2, 2018 16:43 |
|
So I used T-SQL's LAG() function to get the previous day's entry for some reporting and it's doing something... real weird? The code is like this: code:
Was lag just extremely the wrong function to use or what
|
# ? Jul 3, 2018 11:19 |
|
Shugojin posted:So I used T-SQL's LAG() function to get the previous day's entry for some reporting and it's doing something... real weird? Why are you supplying the 2nd and 3rd parameters of lag? Those are optional and in 99% of use cases you don’t need them. The 2nd parameter is offset, which defaults to 1. That’s how many rows to look back. You’re passing the default. The 3rd parameter is default, and is what is causing your odd behavior. This parameter specifies what should be returned when your lag function returns null. 0 converted to a date is 1/1/1900, which is why your query randomly returns that value. What I can’t tell you without more info on your dataset is why your lag function is returning NULL. But what that does mean is there is no prior value based on your partition/order OR that prior value is NULL.
|
# ? Jul 3, 2018 14:08 |
|
I was supplying the parameters because the documentation had them supplied It also had the DAY(date) in an example similar to what I was doing that I had forgotten when posting that from home and was causing sql to re-sort everything weirdly, if I ditch the DAY it sorts everything properly
|
# ? Jul 3, 2018 14:38 |
|
I'm trying to use week numbers to filter for the last few weeks (WHERE clause), since the date_column in the database is of YYYYWW (ISO week) type and it of course has a problem with periods in the beginning of the year. Using Redshift (PostgreSQL 8.3 compatible)code:
This works well since we are in the middle of the year, but this: code:
Any ideas? Maybe the best thing is to somehow do a date_trunc('week', date_column) on the date_column.
|
# ? Jul 3, 2018 14:46 |
|
Shugojin posted:I was supplying the parameters because the documentation had them supplied Yeah DAY(Date) would probably give you really weird results if your data spans more than a month. Here's an experiment I did for you: code:
pre:Date_Col Number Day_Value Original_Lag_Record Lag_Record Corrected_Lag_Record ---------- ----------- ----------- ------------------- ----------- -------------------- 01/02/2018 1 2 32 32 NULL 01/03/2018 2 3 1 1 1 01/04/2018 3 4 34 34 2 01/05/2018 4 5 3 3 3 01/06/2018 5 6 35 35 4 01/07/2018 6 7 5 5 5 01/08/2018 7 8 6 6 6 01/09/2018 8 9 7 7 7 01/10/2018 9 10 8 8 8 01/11/2018 10 11 9 9 9 01/12/2018 11 12 10 10 10 01/13/2018 12 13 11 11 11 01/14/2018 13 14 12 12 12 01/15/2018 14 15 13 13 13 01/16/2018 15 16 14 14 14 01/17/2018 16 17 15 15 15 01/18/2018 17 18 16 16 16 01/19/2018 18 19 17 17 17 01/20/2018 19 20 18 18 18 01/21/2018 20 21 19 19 19 01/22/2018 21 22 20 20 20 01/23/2018 22 23 21 21 21 01/24/2018 23 24 22 22 22 01/25/2018 24 25 23 23 23 01/26/2018 25 26 24 24 24 01/27/2018 26 27 25 25 25 01/28/2018 27 28 26 26 26 01/29/2018 28 29 27 27 27 01/30/2018 29 30 28 28 28 01/31/2018 30 31 29 29 29 02/01/2018 31 1 0 NULL 30 02/02/2018 32 2 31 31 31 02/03/2018 33 3 2 2 32 02/04/2018 34 4 33 33 33 02/05/2018 35 5 4 4 34 (35 row(s) affected) An excess of NULLs in your lag column could mean, as Ruggan stated, either NULLs in your data, or a lot of partitions in your data, since the first record in your partition can't have a LAG from the previous row.
|
# ? Jul 3, 2018 14:58 |
|
Yeah, currently the only nulls I am getting are just for the first record in each partition and that's not total garbage. I will probably muck about with it more today.
|
# ? Jul 3, 2018 16:08 |
|
Why would a SELECT ... statement inside of a cursor generate results I want, but a SELECT @var = ... doesn't, and includes results not generated by the first statement?
|
# ? Jul 5, 2018 20:39 |
|
anthonypants posted:Why would a SELECT ... statement inside of a cursor generate results I want, but a SELECT @var = ... doesn't, and includes results not generated by the first statement? The second case sounds like you're getting PHANTOM READS, and I can only assume that a higher transaction isolation level is being used in the first case. Can you share more information about the context in which each of them is being called?
|
# ? Jul 6, 2018 00:08 |
|
Janitor Prime posted:The second case sounds like you're getting PHANTOM READS, and I can only assume that a higher transaction isolation level is being used in the first case. Can you share more information about the context in which each of them is being called? This cursor goes through a table and generates an UPDATE statement for each row. This UPDATE statement performs an XQuery modify(), and since modify() only accepts string literals, I need to generate a unique UPDATE statement for each row in this table. It looks like this: SQL code:
SQL code:
If I change it to the following, then I get one "table", where the UPDATE command is run for all three combinations of "b1.b2.b3", regardless of what's in the XML tag, and for only one xID: SQL code:
Additionally, I have no idea how to execute these commands. I can't replace the SELECT with EXEC or EXEC sp_executesql because of the CAST()s, and even if I try to do SELECT @var = ...EXEC @var it throws an error because it truncates @var after 129 characters for some reason. PRINT @var or SELECT @var don't show it as being truncated.
|
# ? Jul 6, 2018 01:02 |
|
So wait, is there anything stopping you from doing something like this?code:
A spot for further cleanup is that subquery selecting r.xID. Is there any way we can relate that directly to dbo.t or dbo.dl? Also is the r.xID query ALWAYS expected to return a single value?
|
# ? Jul 6, 2018 05:22 |
|
Is there a sql white space/tab standard that doesn’t cause suicidal ideation?
|
# ? Jul 6, 2018 14:47 |
|
Nth Doctor posted:So wait, is there anything stopping you from doing something like this? The xID is a row-unique ID, so, yes? e: Here's a version that works: SQL code:
anthonypants fucked around with this message at 23:38 on Jul 6, 2018 |
# ? Jul 6, 2018 16:08 |
|
Are there any small programs that will let me generate a visualization of the relationships in a Postgres database from the command line in Linux? Something that doesn't require me to install java, a JDBC driver, or a bunch of random PHP modules. Everything I've run into can't hit one of those three requirements and those are the kind of things that are verboten on that side of the network. Bonus points if the program itself doesn't require X windows and just dumps the visualization to some sort of file that I can sendmail to the project manager. Sheep fucked around with this message at 17:55 on Jul 6, 2018 |
# ? Jul 6, 2018 17:46 |
|
if those are really your requirements then why don't you hack it yourself? pg_dump the table and constraint definitions, do some regex to get them in graphviz dot form, then you can generate a pdf/png from the command line
|
# ? Jul 6, 2018 21:44 |
|
Bobcats posted:Is there a sql white space/tab standard that doesn’t cause suicidal ideation? 2 space tabs. Any serious reporting query will otherwise end up so indented it linewraps.
|
# ? Jul 7, 2018 01:06 |
|
Moonwolf posted:2 space tabs. Any serious reporting query will otherwise end up so indented it linewraps.
|
# ? Jul 7, 2018 02:33 |
|
Bobcats posted:Is there a sql white space/tab standard that doesn’t cause suicidal ideation? Perhaps instead you meant: code:
|
# ? Jul 7, 2018 20:17 |
|
I have a beginner T-SQL question that is tripping me up: I have a table: code:
So my end result is ideally something like this: code:
|
# ? Jul 15, 2018 01:40 |
|
Hughmoris posted:I have a beginner T-SQL question that is tripping me up: There are multiple ways of achieving this sort of result IIRC. One way to do it is to construct a subquery that gets the minimum and maximum for each order, then join it against two copies of the original table so as to get the PERSON data from that table. code:
|
# ? Jul 15, 2018 12:57 |
|
Hammerite posted:There are multiple ways of achieving this sort of result IIRC. One way to do it is to construct a subquery that gets the minimum and maximum for each order, then join it against two copies of the original table so as to get the PERSON data from that table. I was able to apply this concept to my problem and it worked like a champ. Thank you!
|
# ? Jul 15, 2018 20:21 |
|
I keep being told I should stop using MySQL and use a "real" database. What do people mean by this? I always ask for more clarification and then conversation stops.
|
# ? Jul 17, 2018 18:15 |
|
They're probably either telling you to use PostgreSQL or a noSQL and you will know for sure which if you ask them which they use.
|
# ? Jul 17, 2018 18:31 |
|
kiwid posted:I keep being told I should stop using MySQL and use a "real" database. Like PHP, MySQL was a lot shittier in previous versions and that reputation lives on. AFAIK the largest problems these days are actually bad defaults, that it's possible to configure it to be decent enough but the default settings do a lot of insane things like silently dropping or modifying data.
|
# ? Jul 17, 2018 18:33 |
|
pangstrom posted:They're probably either telling you to use PostgreSQL or a noSQL and you will know for sure which if you ask them which they use. I believe they're referring to MSSQL, Oracle, and Postgres. MSSQL and Oracle are two I'll likely never be interested in because I work primarily in PHP and Python. Should I however invest time into learning Postgres over MySQL? kiwid fucked around with this message at 18:54 on Jul 17, 2018 |
# ? Jul 17, 2018 18:42 |
|
kiwid posted:I believe they're referring to MSSQL, Oracle, and Postgres. Doesn’t MSSQL integrate just fine with PHP and Python? I think there are drivers for both.
|
# ? Jul 17, 2018 19:42 |
|
Ruggan posted:Doesn’t MSSQL integrate just fine with PHP and Python? I think there are drivers for both. https://docs.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server https://docs.microsoft.com/en-us/sql/connect/python/python-driver-for-sql-server
|
# ? Jul 17, 2018 19:56 |
|
kiwid posted:I believe they're referring to MSSQL, Oracle, and Postgres. Be solid on the relational part of a database, then look how other db's differ. It's better to have a good base before switching since that is the most important bit. Mysql is good enough for this, as is postgres, mssql, oracle, etc etc etc
|
# ? Jul 17, 2018 20:45 |
|
|
# ? Jun 3, 2024 12:38 |
|
Maybe it's just the nerd in me being overly strict, but I have zero tolerance for MySQL's default behavior. I don't know if this has changed, but the last time I used it with a complicated GROUP BY query that included some columns that weren't part of the GROUP BY clause it just happily returned some random data from the matching rows instead of throwing an error like Postgres and Oracle do. This lead to a subtle bug in production that wasn't caught during testing because our test data always happened to return the same rows and nothing seemed amiss. Another stupid behavior I ran into was where date columns that contained NULL values would return 0000-00-00 as the value instead of NULL and breaking our ORM mapping. You can fix it by setting a property on the connection string or turning on Strict mode in the server config, but this is something that's so rear end backwards it boggles my mind. Then there's all the historically broken Unicode support that it shipped with. The final nail in the coffin was all the licensing hassle that Oracle tried to impose on us. Oracle licenses the JDBC drivers under the GPL and when we tried to purchase an enterprise license to ship the driver with our product their legal team went crazy claiming that the way we used the product would require us to be a 3rd party reseller and wanted us to have minimum commitments and other crap like that.
|
# ? Jul 17, 2018 22:00 |