Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Multiple columns are specified in an aggregated ex

Author  Topic 

PSamsig
Constraint Violating Yak Guru

384 Posts

Posted - 2006-07-02 : 05:57:33
I have three tables: Invoices, InvoiceLines and Payments. I need a query to show payment status on all invoices. I store the appropriate VAT rate in the invoice and a 'flag' in the invoice line if the price needs adjusted with VAT (bad design?).

DECLARE @Invoices TABLE (InvoiceID int, CustId int, Vat float)
DECLARE @InvoiceLines TABLE (InvoiceID int, Price float, AddVat int)
DECLARE @Payments TABLE (InvoiceID int, Amount float)

INSERT @Invoices
SELECT 1, 1, 25.0 UNION ALL
SELECT 2, 2, 25.0
INSERT @InvoiceLines
SELECT 1, 100.0, 1 UNION ALL
SELECT 1, 100.0, 0 UNION ALL
SELECT 2, 100.0, 1 UNION ALL
SELECT 2, 100.0, 0
INSERT @Payments
SELECT 1, 50.0 UNION ALL
SELECT 2, 225.0


When I try doing this:
SELECT I.CustID
,I.InvoiceID
,(SELECT SUM(IL.Price * (100.0 + I.Vat * IL.AddVat) / 100.0)
FROM @InvoiceLines AS IL
WHERE (IL.InvoiceID = I.InvoiceID)
) AS Amount
,P.Payment
FROM @Invoices I
LEFT OUTER JOIN (
SELECT InvoiceID, SUM(Amount) AS Payment
FROM @Payments
GROUP BY InvoiceID) P
ON P.InvoiceID = I.InvoiceID
I get this error:
Msg 8124, Level 16, State 1, Line 1
Multiple columns are specified in an aggregated expression containing an outer reference. If an expression being aggregated contains an outer reference, then that outer reference must be the only column referenced in the expression.

so I rewrote it to:
SELECT I.CustID
,I.InvoiceID
,(SELECT SUM(IL.Price * (100.0 + iI.Vat * IL.AddVat) / 100.0)
FROM @Invoices iI
LEFT OUTER JOIN @InvoiceLines IL
ON IL.InvoiceID = iI.InvoiceID
WHERE iI.InvoiceID = I.InvoiceID
) AS Amount
,P.Payment
FROM @Invoices I
LEFT OUTER JOIN (
SELECT InvoiceID, SUM(Amount) AS Payment
FROM @Payments
GROUP BY InvoiceID) P
ON P.InvoiceID = I.InvoiceID
which seems a bit inefficient, any thoughts?


-- This one's tricky. You have to use calculus and imaginary numbers for this. You know, eleventeen, thirty-twelve and all those.

nr
SQLTeam MVY

12543 Posts

Posted - 2006-07-02 : 13:41:09
[code]
SELECT I.CustID
,I.InvoiceID
,I.Amount
,P.Payment
FROM ( select I.CustID, I.InvoiceID, amount = SUM(IL.Price * (100.0 + I.Vat * IL.AddVat) / 100.0)
from @Invoices I
join @InvoiceLines IL
on IL.InvoiceID = I.InvoiceID
group by I.CustID, I.InvoiceID
) I
LEFT OUTER JOIN
(
SELECT InvoiceID, SUM(Amount) AS Payment
FROM @Payments
GROUP BY InvoiceID
) P
ON P.InvoiceID = I.InvoiceID
[/code]

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

PSamsig
Constraint Violating Yak Guru

384 Posts

Posted - 2006-07-03 : 02:06:56
And now the question is why I didnt come up with that my self, guess I had stard my self blind on it (you have that expression in englih?).

Thank you Nigel, fresh eyes can do wonders.

-- This one's tricky. You have to use calculus and imaginary numbers for this. You know, eleventeen, thirty-twelve and all those.
Go to Top of Page

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2006-07-03 : 02:11:46
Paul,

A suggestion if I may...

Views, views and more views...

DavidM

Intelligent Design is NOT science.

A front-end is something that tries to violate a back-end.
Go to Top of Page

PSamsig
Constraint Violating Yak Guru

384 Posts

Posted - 2006-07-03 : 03:09:52
Feel free to comment all you like, I am here to learn.

This query IS in fact a combination of two views, but mostly because I need the the logic elsewhere. I havent really gotten into a habbit of making views though, apart from reusing buisness logic then what else does it help? As I have understood the query plan for the final query doesnt get any benefits from views, so speed wise there should be nothing to gain? Some may mention readbility (and thereby maintainability) but I rarely think that is a problem (as long as you use a desent formating).

-- This one's tricky. You have to use calculus and imaginary numbers for this. You know, eleventeen, thirty-twelve and all those.
Go to Top of Page

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2006-07-03 : 03:40:44
Data integrity and modularity are the main reasons I use views.
There is an invoicing component in my current project and most of the base tables are encapsulated by views. The views, in this case, provide more complex constraints to the business model.
As an example, I have a view that ensures that a Invoice cannot be overpaid. All payments must pass through this view... Nothing a trigger couldn't do of course, but I prefer views over triggers for data integrity if it can be done in a view definition..

The stored procedure layer (who ever writes it) only interacts with the views.. but that is my style...

I also have plenty of views for general statistical work.. Invoice_NotPaid, Invoice_FullyPaid, etc....

DavidM

Intelligent Design is NOT science.

A front-end is something that tries to violate a back-end.
Go to Top of Page

PSamsig
Constraint Violating Yak Guru

384 Posts

Posted - 2006-07-03 : 03:49:25
quote:
Originally posted by byrmol

As an example, I have a view that ensures that a Invoice cannot be overpaid. All payments must pass through this view... Nothing a trigger couldn't do of course, but I prefer views over triggers for data integrity if it can be done in a view definition..


Could you elaborate that a bit?

-- This one's tricky. You have to use calculus and imaginary numbers for this. You know, eleventeen, thirty-twelve and all those.
Go to Top of Page

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2006-07-03 : 05:47:38
>>Could you elaborate that a bit?

I'd love to be able to just script it out for you but legalese instructs me otherwise and I am too lazy otherwise... :-)

Essentially, the EXISTS operator is allowed in the definition of an updateable "checked" view (WITH CHECK OPTION). This allows you to form more intricate constraints that can span multiple tables, contain aggregations etc... The only "gotcha" (and this really annoys me) is that the base table can be referenced only once in the view definition.

I find them incredibly powerful tool that saves masses of code in both the database and middle tier.

This is how I played in SQL2K, have no idea what's changed in 2005. Anyone?

DavidM

Intelligent Design is NOT science.

A front-end is something that tries to violate a back-end.
Go to Top of Page
   

- Advertisement -