?
Solved

MS SQL Stored Procedure from different tables

Posted on 2007-10-08
6
Medium Priority
?
409 Views
Last Modified: 2012-08-14
HI,
  I am new in using Stored Procedure and would like to boost the speed of my code.
I got a table like
TABLE Product
1.  Q123
2.  Q234
3.  H456
4.  H653

and for each item it will refer to anohter table
Table A
1. Q123   4    5    6
2. Q234   5    1     2
3.  Q123  45   33   44
4.  Q123 190   33   21

Table B
1.  H456  5443
2.  H456  43
3.  H653  33

I would like to sum the value of TABLE PRODUCT so that the final table will store all the item with subtotal of each from Table A and Table B and the final reuslt should be ordered from large sum to lowesr (i.e. descending order)

Result
1. Q123   197130    ---------   (4x5x6+45x33x44+190x33x21)
2.  Q234  10    ----------(5x1x2)

etc..

is it possible to use stored procedure to do so ?  So that I can use this table often later
0
Comment
Question by:gorexy
  • 3
4 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 20035063
assuming "A" looks like (id, x, y, z)  and "B" looks like (id, x)  then


SELECT   ID, SUM(x * y * z)
    FROM a
GROUP BY ID
UNION
SELECT   ID, x
    FROM b
ORDER BY 2 DESC, 1
0
 

Author Comment

by:gorexy
ID: 20035167
hm..but how does this relate to the original table? i.e. TABLE PRODUCT
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 20035337
it doesn't, but it doesn't have to.

if you really want it to then. simply join it

SELECT   p.ID, x.RESULT
    FROM product p,
         (SELECT   ID, SUM(x * y * z) RESULT
              FROM a
          GROUP BY ID
          UNION
          SELECT ID, x RESULT
            FROM b) x
   WHERE p.ID = x.ID
ORDER BY RESULT DESC, ID
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 400 total points
ID: 20752288
I believe both of my answers are correct.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

840 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