Solved

I need help combining two queries in SQL Server 2008

Posted on 2014-09-16
3
184 Views
Last Modified: 2014-09-18
Hi experts,
I need your assistance combining the two queries below.
I need to take the sum of query #1 and subtract the sum of query #2 from it.
How can I do this?  I have listed both my queries below.

NOTE: The PONUMBER field in query 1 matches the PONUMBER field in query #2.


--Query 1
SELECT 
  SUM(I.[QTYORDER])
  FROM POPHEADER as I
  inner join POPDETAIL as P
  on I.PONUMBER = P.PONUMBER
  where I.[POLNESTA] not in (4,5,6) and P.[DOCDATE] >= '1/1/2014' and I.[ITEMNMBR] = '209822011'
  and I.POTYPE =1
  
--Query 2
SELECT SUM(QTYRECVD) AS QTY
     FROM INVOICE_TABLE
where [DATERECD] >= '1/1/2014' and RCPTSOLD = 0
and  [PONUMBER] = '07277' and [ITEMNMBR] =  '209822011'

Open in new window



Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
  • 2
3 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40326841
Well, your second query only contains PONUMBER = '07277', so you just could do this:
SELECT  SUM(I.[QTYORDER])
FROM    POPHEADER AS I
        INNER JOIN POPDETAIL AS P ON I.PONUMBER = P.PONUMBER
WHERE   I.[POLNESTA] NOT IN (4, 5, 6)
        AND P.[DOCDATE] >= '1/1/2014'
        AND I.[ITEMNMBR] = '209822011'
        AND I.POTYPE = 1
	AND i.PONUMBER <> '07277'

Open in new window

But I get the feeling you are looking for something more than that, so perhaps this:
--Query 1
SELECT  SUM(I.[QTYORDER])
FROM    POPHEADER AS I
        INNER JOIN POPDETAIL AS P ON I.PONUMBER = P.PONUMBER
        LEFT JOIN (SELECT   PONUMBER
                   FROM     INVOICE_TABLE
                   WHERE    [DATERECD] >= '1/1/2014'
                            AND RCPTSOLD = 0
                            --AND [PONUMBER] = '07277'
                            AND [ITEMNMBR] = '209822011'
                   GROUP BY PONUMBER
                  ) d ON i.PONUMBER = d.PONUMBER
WHERE   I.[POLNESTA] NOT IN (4, 5, 6)
        AND P.[DOCDATE] >= '1/1/2014'
        AND I.[ITEMNMBR] = '209822011'
        AND I.POTYPE = 1
        AND d.PONUMBER IS NULL

Open in new window

0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40326846
SELECT
      SUM(QTY) AS QTY
FROM (

            --Query 1
            SELECT
                  SUM(I.[QTYORDER]) AS QTY
            FROM POPHEADER AS I
                  INNER JOIN POPDETAIL AS P
                              ON I.PONUMBER = P.PONUMBER
            WHERE I.[POLNESTA] NOT IN (4, 5, 6)
                  AND P.[DOCDATE] >= '1/1/2014'
                  AND I.[ITEMNMBR] = '209822011'
                  AND I.POTYPE = 1
            UNION ALL

                  --Query 2
                  SELECT
                        -SUM(QTYRECVD) AS QTY -- Note the minus
                  FROM INVOICE_TABLE
                  WHERE [DATERECD] >= '1/1/2014'
                        AND RCPTSOLD = 0
                        AND [PONUMBER] = '07277'
                        AND [ITEMNMBR] = '209822011'
      ) AS u

Open in new window

OR: With PONUMBER in the output
SELECT
      PONUMBER
    , SUM(QTY) AS QTY
FROM (

            --Query 1
            SELECT
                  PONUMBER
                , SUM(I.[QTYORDER]) AS QTY
            FROM POPHEADER AS I
                  INNER JOIN POPDETAIL AS P
                              ON I.PONUMBER = P.PONUMBER
            WHERE I.[POLNESTA] NOT IN (4, 5, 6)
                  AND P.[DOCDATE] >= '1/1/2014'
                  AND I.[ITEMNMBR] = '209822011'
                  AND I.POTYPE = 1
            UNION ALL

                  --Query 2
                  SELECT
                        PONUMBER
                      , -SUM(QTYRECVD) AS QTY -- Note the minus
                  FROM INVOICE_TABLE
                  WHERE [DATERECD] >= '1/1/2014'
                        AND RCPTSOLD = 0
                        AND [PONUMBER] = '07277'
                        AND [ITEMNMBR] = '209822011'
      ) AS u
GROUP BY
      PONUMBER
;

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40326849
Ah, never mind, it looks like I misread the question.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now