Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Help comparing Sum to Total amt Expand / Collapse
Author
Message
Posted Sunday, November 3, 2013 8:07 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 6:09 AM
Points: 11, Visits: 42
I need a little help. I have an invoice table and and invoice detail table. I pulled the data I care about from the invoice details table to a temp table (for other calculations). I need to find the invoices where the sum of the detail lines do not equal the total cost in the invoice table. Here is my code:
Create table #inv_det (
invco varchar(6),
invno int,
invtot money,
invpd int);

Insert into #inv_det (invco,invno,invtot,invpd)
Select invd_inv_co, invd_inv_no, invd_total_cost, invd_glpd_no from invdet inner join invoice on
invd_inv_co=inv_co and invd_inv_no=inv_no where
inv_dt>=cast('10-01-13 00:00:00' as datetime) and inv_dt<=cast('10-31-13 11:59:59' as datetime);

Here is my query:


Select invco,invno from #inv_det inner join invoice on
invco=inv_co and invno=inv_no where inv_dt>=cast('10-01-13 00:00:00' as datetime)
and inv_dt<=cast('10-31-13 11:59:59' as datetime)
having sum(invtot)<>inv_total_amt
group by invco, invno;

When I run my query I get "Server: Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'group'."

I hope I explained my goal well enough. Hopefully someone can point out what I'm missing. I've tried many variations.

Thanks!
Post #1510891
Posted Sunday, November 3, 2013 7:03 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, November 24, 2014 4:53 AM
Points: 3,422, Visits: 5,368
Try moving HAVING to after GROUP BY.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1510958
Posted Monday, November 4, 2013 9:18 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 6:09 AM
Points: 11, Visits: 42
That took care of the issue. Thought I had already tried that. Thanks for your help!

Post #1511161
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse