Solved

Query Help

Posted on 2014-01-13
5
207 Views
Last Modified: 2014-01-13
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
Comment
Question by:ccleebelt
5 Comments
 
LVL 11

Accepted Solution

by:
Simone B earned 500 total points
Comment Utility
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)

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
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), 
	-- 2 is bad
	(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

Open in new window

0
 

Author Closing Comment

by:ccleebelt
Comment Utility
Ah.  I was so close.  Thanks!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
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

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now