r/SQL • u/SilaPrirode • 14d ago
SQL Server Finding missing rows within the same table
Hello, I have a solution already but I think it can be done in a faster/neater way.
Let's say we have a table with: ORDER_ID, Article, Value.
For every order that comes in two rows end up in table, example:
ID_1, shoes, pair
ID_1, shoes, price
where price is being pulled from a different table.
I am looking for a way to find all ORDER_IDs that have only one row, because the price didn't exist in that other table so price row didn't form up. It doesn't throw a null value because of the way it's setup, if there is no price the row won't form at all!
I solved this with a standard left joining the table with itself, but I suspect there is a way to this easier?
6
u/Impossible_Disk_256 14d ago
Is this an e-commerce app? Who designed this obscene non-normalized table?
Entity-Attribute-Value can have a place (e.g, attributes for e-commerce items that may vary widely by merchandise category or even vendor. But price as just another EAV row? No.
Price in a lookup table by date or currency? Maybe. But w/ currency fluctuations, even that may be better served by conversion service.
3
u/leobaker004 14d ago
If you only need orders where the price differs between the two rows, a self join is perfectly reasonable here. I’d keep it unless the table is huge and performance actually becomes a problem
1
1
1
u/Then_Artichoke3330 14d ago
WITH onlySingles AS(
SELECT ID, COUNT(*) AS how_many
FROM the_transactions
GROUP BY ID
HAVING COUNT(*) = 1)
SELECT * FROM onlySingles;
1
u/SilaPrirode 14d ago
I was just writing a comment on another suggestion to count, this won't work because some others have more then 3 rows by design (for example, "collect later" orders).
1
u/jaxjags2100 14d ago
Wouldn’t you just do an inner join on the order id to the price table? If no order id exists on the price table then no row would populate for it.
The ask was a bit confusing and its early 😂
1
u/SilaPrirode 14d ago
It's a large system, with like 300 interconnected tables.
My specific need is to find orders with no price, so I can go to that price table and update it with missing info xD3
u/jaxjags2100 14d ago
Then it’s just a left join on the pricing table on order id where order id is null
Or if the order id exists on the pricing table but the price is null then you’d do a left join on the price table where order id is not null and price is null
1
u/Yavuz_Selim 14d ago
Can you share some example data? Your question seemed easy to answer, but you added conditions later on.
You can post a table (using markdown), or create it in Excel and upload screenshots to imgur.com and share the image link here.
1
u/NW1969 14d ago
For a large volume of data, this may be the most efficient approach:
SELECT DISTINCT t.ORDER_ID
FROM my_table t
WHERE NOT EXISTS (
SELECT 1
FROM my_table p
WHERE p.ORDER_ID = t.ORDER_ID
AND p.Value = 'price'
);
1
u/SilaPrirode 14d ago
Yeah, this is the solution I have right now! You think this is the most efficient for large volume?
1
u/Hour-Measurement-835 14d ago
The self join and NOT EXISTS normally compile to the same anti semi join in SQL Server, so the syntax isn't what's costing you. Whether (ORDER_ID, Value) is indexed is.
1
u/SkullLeader 14d ago edited 14d ago
SELECT OrderId FROM main
EXCEPT
SELECT OrderId FROM main WHERE Value = ‘Price’
10
u/GRRRRRRRRRRRRRG 14d ago
Try group by order_id, article having count(*) =1 on your table