Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Query Help

Posted on 2014-01-13
Medium Priority
214 Views
Hi All - need help writing a query for MSSQL - cannot seem to get it right

I have a table of orders o

order_id int pk
sub_total  money

I have another table of lineitems l

lineitem_id int pk
order_id fk to o.orders_id
linetotal money

An there may be many lineitems in an order

I am trying to write a query that finds any order that has a sub total that is <> to the sum of its lineitems (linetotals).
0
Question by:ccleebelt
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 11

Accepted Solution

Simone B earned 2000 total points
ID: 39777076
Try this:

``````SELECT o.order_id, o.sub_total, SUM(l.linetotal) AS linetotal
FROM orders o
INNER JOIN lineitems l ON o.order_id = l.order_id
GROUP BY o.order_id, o.sub_total
HAVING o.sub_total <> SUM(l.linetotal)
``````
0

LVL 66

Expert Comment

ID: 39777087
You can use a <> in a JOIN clause to find non-matches.

Copy-paste the below T-SQL into your SSMS, execute to verify it works for your question, then modify to meet your needs.
``````CREATE TABLE orders (order_id int, sub_total money)
CREATE TABLE lineitems (lineitemid int, order_id int, sub_total money)
GO

INSERT INTO orders (order_id, sub_total)
VALUES (1, 100), (2, 200), (3, 300)

INSERT INTO lineitems (lineitemid, order_id, sub_total)
VALUES
-- 1 is good
(1, 1, 30),
(2, 1, 70),
(3, 2, 42),
(4, 2, 99),
(5, 2, 86),
-- 3 is good
(6, 3, 100),
(7, 3, 75),
(8, 3, 125)

SELECT o.order_id, o.sub_total, l.sub_total
FROM orders o
JOIN (SELECT order_id, SUM(sub_total) as sub_total FROM lineitems GROUP BY order_id) l ON o.order_id = l.order_id AND o.sub_total <> l.sub_total
``````
0

Author Closing Comment

ID: 39777495
Ah.  I was so close.  Thanks!
0

LVL 75

Expert Comment

ID: 39777554
I would do it this way:
``````SELECT  o.order_id,
o.sub_total
FROM    orders o
INNER JOIN (
SELECT  order_id,
SUM(linetotal) Sumlinetotal
FROM    lineitems
GROUP BY order_id
) l ON o.order_id = l.order_id
WHERE   o.sub_total <> l.Sumlinetotal
``````
0

LVL 75

Expert Comment

ID: 39777560
The reason I think my solution is better is because I see no reason to do a GROUP BY on the orders table.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
###### Suggested Courses
Course of the Month7 days, 1 hour left to enroll