Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help comparing Sum to Total amt


Help comparing Sum to Total amt

Author
Message
CheckPoint
CheckPoint
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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!
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4275 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
CheckPoint
CheckPoint
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 42
That took care of the issue. Thought I had already tried that. Thanks for your help!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search