5 ms·
I've always used this. Any rows returned means there are differences select * from ( ( select * from table1 minus select *
by wfriesen 3y ago
I've always used this. Any rows returned means there are differences
select * from (
(
select *
from table1
minus
select *
from table2
)
union all
(
select *
from table2
minus
select *
from table1
)
)
- globular-toast 3y agoThis is symmetric difference, but still has the problem that it's a set operation whereas in general a table is a bag (multiset).
- DougBTX 3y agoIn theory, yes, however the vast majority of tables will have some form of unique ID in each record... so in practice, there’s usually no difference. But if it must work for all tables...
- EddieJLSH 3y agoRealistically which production DB tables don't have a unique id? Genuine question, never used one in my life.
- wodenokoto 3y agoDon’t things like BigQuery always allow duplicates?
- EddieJLSH 3y agoGood point, have not used it before but looks like you have to add a unique ID if you want one
- valenterry 3y agoFor example tables that store huge amount of logs or sensor data where IDs are not very useful and just increase space usage and decrease performance.
- erinnh 3y agoIt happens. I’m currently working on a project where the CRM tool I need to access for data, actually does not have a unique id in its db. I have no idea if I will be able to successfully complete the project yet.
- justinclift 3y agoIs there any chance that the rows actually do have a unique id, but it's not being displayed without some magic incantation? Asking because I've seen that before in some software, where it tries to "keep things simple" by default. But that behaviour can be toggled off so it shows the full schema (and data) for those with the need. :)
- erinnh 3y agoSadly, no. The manufacturer is just really incompetent. I was told their reason when asked was „it was easier (for us)“.
- justinclift 3y ago> it was easier (for us) That's not all that unusual when something gets implemented, as people tend to take the easy approach for things that meet the desired goal. It just sounds like the spec they were writing to wasn't very clear or it was just a checkbox list of features provided to them by marketing. So "lets get this list done then ship it". ;)
- Dylan16807 3y agoThe question is whether it was actually easier. Even a couple minutes of extra debugging takes longer than learning how to add a synthetic primary.
- dspillett 3y agoLog analytics or warehouse tables often have no simple useful key for this sort of comparison. Also in a more general case you might be comparing tables that may contain the same data but have been constructed from different sources. Or perhaps a distributed dataset became disconnected and may have seen updates in both partitions, and you have brought them together to compare to try decide which to keep or if it is worth trying to merge. In those and other circumstances there may be a key but if it is a surrogate key it will be meaningless for comparing data from two sets of updates, so you would have to disregard it and compare on the other data (which might not include useful candidate keys).
- theodpHN 3y agoAlso, database tables where unique key constraints aren't enforced. Programming and operational mistakes happen. :-) https://stackoverflow.com/questions/62735776/what-is-the-point-of-snowflakes-unique-constraint https://stackoverflow.com/questions/62735776/what-is-the-poi...
- hobs 3y agoPostTags in the published Stack Overflow schema - https://data.stackexchange.com/stackoverflow/query/edit/1772609 https://data.stackexchange.com/stackoverflow/query/edit/1772... It happens a lot when people are implementing something quick and often happens in linking tables.
- OskarS 3y agoDoes this work with the bag/multiset distinction that the author uses? Like, if table1 has two copies of some row and table2 has a single copy of that row, wont this query return that they're the same? But they're not: table1 has two copies of the same row, whereas table2 just has one?
- LeonB 3y agoI found that a weird edge case for the original author to fixate on. In mathematics or academia sure, but in “real” sql tables, that serve any kind of purpose, duplicate rows are not something you need to support, let alone go to twice the engineering effort to support. Duplicates are more likely to be something you deliberately eradicate (before the comparison) than preserve and respect.
- deely3 3y ago> duplicate rows are not something you need to support I can imagine that you want to have duplicates rows in a logging. If some events happens twice - you definitely want to log it twice.
- SoftTalker 3y agoLogs usually have a timestamp that would differentiate the two events.
- vikingerik 3y agoNot necessarily - the clock source for logging is often at millisecond resolution, but at the speed of modern systems you could pile up quite a few log entries in a millisecond. I handle this by having a guid field for a primary key on such tables where there isn't a naturally unique index in the shape of the data. So something is unique, and you can delete or ignore other rows relative to that. (Just don't make your guid PK clustered; I use create-date or log-date for that.)
- LeonB 3y ago
- deleted 3y ago[deleted]
- ryzvonusef 3y agoI don't think most SQL flavours support MINUS function, imho. Bing Chat says: > The MINUS operator is not supported in all SQL databases. It can be used in databases like MySQL and Oracle. For databases like SQL Server, PostgreSQL, and SQLite, use the EXCEPT operator to perform this type of query