Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / December 2007

Tip: Looking for answers? Try searching our database.

SQL query question about combining two columns with the aggregate     function SUM():  UNION SELECT what?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
raylopez99 - 28 Dec 2007 21:47 GMT
Hi all,

Been a while since I've posted here but I'm back to doing some
database programming (using Access VB, which allows you to rapidly
code the front end GUI at the severe expense of some debugging tools).

Access apparently only allows your control (buttons, textboxes and the
like) to be bound to a single query or record source (dB), so here's
the problem:

I have a table, tblPQ, that has two columns, Product (P) and Quantity
(Q).  I want to take the product of P*Q for each row in the table,
then, take the sum of P*Q for the entire table.  Ideally I'd like to
do this with a SQL query; then I will bind this query to a button on
the front end form.  How to do this (write the SQL query that is)?

I looked in my textbook on SQL (I have several, including a
doorstopper by Itzik Ben-Gan "T-SQL Querying"), and though I got many
tantilizing hints, along the lines of SUM(x), "Self Join" and "UNION
SELECT", I couldn't quite get the right result.

Further, I'd like to add to the SQL query a equijoin (inner join),
because the table tblPQ shares a relationship with another table,
table ABC, sharing a column "id" (Primary Key/ Foreign Key), namely
"WHERE ABC.id = tblPQ.id"

Any ideas on what this SQL query would look like?

Thanks,

RL
Roy Harvey (SQL Server MVP) - 28 Dec 2007 22:08 GMT
This would be a lot easier if you proved DDL that defines the table,
INSERTs that create test data, and the desired output.  That might
resolve questions such as what exactly is the Product column that is
being multiplied by the Quantity column - if not for the
multiplication I would have assumed it was the key to Product.

At any rate, you might get away with something like...

SELECT 'I' as ItemOrTotal, P, Q, P * Q as Whatever
 FROM SomeTable
UNION ALL
SELECT 'T', 0, 0, SUM(P * Q)
 FROM SomeTable
ORDER BY ItemOrTotal

That will give you a row with the total.  The ORDER BY should place it
last.  If you can't handle the ItemOrTotal column, eliminate it and
live with the position of the total row being unpredictable.

Roy Harvey
Beacon Falls, CT

>Hi all,
>
[quoted text clipped - 27 lines]
>
>RL
raylopez99 - 29 Dec 2007 09:54 GMT
On Dec 28, 5:08 pm, "Roy Harvey (SQL Server MVP)"
<roy_har...@snet.net> wrote:

Roy Harvey I thank you for your time.

I will work on your example.

RL
raylopez99 - 31 Dec 2007 14:26 GMT
I never did try Roy Harvey's solution, which I'm sure would work,
since I found an easier solution here:

http://allenbrowne.com/TechniqueEnterCalcText.html

In particular, the SQL query and how to use "Sum()" to sum a column,
after getting rid of nulls etc using the Nz(x,y) function.

RL
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.