?
Solved

Is it possible with SQL to combine 2 Select statements results together in one temp table.

Posted on 2012-04-05
8
Medium Priority
?
369 Views
Last Modified: 2012-04-05
I have 2 Select statements on a View (vReportData). I've done them separately as I thought it would be too complex to do them together.  However, I need the results of both SELECT statements to be combined for my report.  Is there a way to execute both these select statements but displaying the results in one temporary table so I can use the temp table for my report (in ASP.Net).  Both my SELECT statements are as follows:

SELECT
    Port, Boat,
    SUM(CASE WHEN Product_Name = 'Lobsters' THEN ProductKiloQty ELSE 0 END) AS [LobstersKG],
    SUM(CASE WHEN Product_Name LIKE '%Crabs' THEN ProductKiloQty ELSE 0 END) AS [CrabsKG],
    SUM(CASE WHEN Product_Name LIKE 'Velvet Crabs%' THEN ProductKiloQty ELSE 0 END) AS [VelvetsKG],
    SUM(CASE WHEN Product_Name LIKE 'Langoustine%' THEN ProductKiloQty ELSE 0 END) AS [LangKG],
    SUM(CASE WHEN Product_Name = 'Crabs (Cut)' THEN ProductKiloQty ELSE 0 END) AS [CutCrKG]
FROM vReportData
WHERE Landing_Day = 5
AND DateOfCatch BETWEEN DATEADD(day, - 8, @EndDate) AND DATEADD(day, - 1, @EndDate)
GROUP BY
    Port, Boat
ORDER BY --optional, of course
    Port, Boat
      RETURN

Then the same except Landing_Day = 6:

      SELECT
    Port, Boat,
    SUM(CASE WHEN Product_Name = 'Lobsters' THEN ProductKiloQty ELSE 0 END) AS [LobstersKG],
    SUM(CASE WHEN Product_Name LIKE '%Crabs' THEN ProductKiloQty ELSE 0 END) AS [CrabsKG],
    SUM(CASE WHEN Product_Name LIKE 'Velvet Crabs%' THEN ProductKiloQty ELSE 0 END) AS [VelvetsKG],
    SUM(CASE WHEN Product_Name LIKE 'Langoustine%' THEN ProductKiloQty ELSE 0 END) AS [LangKG],
    SUM(CASE WHEN Product_Name = 'Crabs (Cut)' THEN ProductKiloQty ELSE 0 END) AS [CutCrKG]
FROM vReportData
WHERE Landing_Day = 6
AND DateOfCatch BETWEEN DATEADD(day, - 8, @EndDate) AND DATEADD(day, - 1, @EndDate)
GROUP BY
    Port, Boat
ORDER BY --optional, of course
    Port, Boat
      RETURN
0
Comment
Question by:Lorna70
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 37810561
why not just add the Landing_Day criteria to the group by ?

   SELECT
    Port, Boat, Landing_Day,
    SUM(CASE WHEN Product_Name = 'Lobsters' THEN ProductKiloQty ELSE 0 END) AS [LobstersKG],
    SUM(CASE WHEN Product_Name LIKE '%Crabs' THEN ProductKiloQty ELSE 0 END) AS [CrabsKG],
    SUM(CASE WHEN Product_Name LIKE 'Velvet Crabs%' THEN ProductKiloQty ELSE 0 END) AS [VelvetsKG],
    SUM(CASE WHEN Product_Name LIKE 'Langoustine%' THEN ProductKiloQty ELSE 0 END) AS [LangKG],
    SUM(CASE WHEN Product_Name = 'Crabs (Cut)' THEN ProductKiloQty ELSE 0 END) AS [CutCrKG]
FROM vReportData
WHERE Landing_Day in (5, 6)
AND DateOfCatch BETWEEN DATEADD(day, - 8, @EndDate) AND DATEADD(day, - 1, @EndDate)
GROUP BY
    Port, Boat, Landing_Day
ORDER BY --optional, of course
    Port, Boat, Landing_Day

Open in new window

0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 600 total points
ID: 37810566
Use a union.

SELECT
    Port, Boat,
    SUM(CASE WHEN Product_Name = 'Lobsters' THEN ProductKiloQty ELSE 0 END) AS [LobstersKG],
    SUM(CASE WHEN Product_Name LIKE '%Crabs' THEN ProductKiloQty ELSE 0 END) AS [CrabsKG],
    SUM(CASE WHEN Product_Name LIKE 'Velvet Crabs%' THEN ProductKiloQty ELSE 0 END) AS [VelvetsKG],
    SUM(CASE WHEN Product_Name LIKE 'Langoustine%' THEN ProductKiloQty ELSE 0 END) AS [LangKG],
    SUM(CASE WHEN Product_Name = 'Crabs (Cut)' THEN ProductKiloQty ELSE 0 END) AS [CutCrKG]
FROM vReportData
WHERE Landing_Day = 5
AND DateOfCatch BETWEEN DATEADD(day, - 8, @EndDate) AND DATEADD(day, - 1, @EndDate)
GROUP BY
    Port, Boat

UNION ALL

      SELECT
    Port, Boat,
    SUM(CASE WHEN Product_Name = 'Lobsters' THEN ProductKiloQty ELSE 0 END) AS [LobstersKG],
    SUM(CASE WHEN Product_Name LIKE '%Crabs' THEN ProductKiloQty ELSE 0 END) AS [CrabsKG],
    SUM(CASE WHEN Product_Name LIKE 'Velvet Crabs%' THEN ProductKiloQty ELSE 0 END) AS [VelvetsKG],
    SUM(CASE WHEN Product_Name LIKE 'Langoustine%' THEN ProductKiloQty ELSE 0 END) AS [LangKG],
    SUM(CASE WHEN Product_Name = 'Crabs (Cut)' THEN ProductKiloQty ELSE 0 END) AS [CutCrKG]
FROM vReportData
WHERE Landing_Day = 6
AND DateOfCatch BETWEEN DATEADD(day, - 8, @EndDate) AND DATEADD(day, - 1, @EndDate)
GROUP BY
    Port, Boat
ORDER BY --optional, of course
    Landing_Day, Port, Boat
0
 

Author Comment

by:Lorna70
ID: 37810664
Oops sorry Geert_Gruwez my code for Landing_Day = 6 should have read:

AND DateOfCatch BETWEEN DATEADD(day, - 8, @EndDate) AND @EndDate) so they need to display different date ranges.  I think Union should work so thanks lsavidge - will try that now.
0
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 

Author Comment

by:Lorna70
ID: 37810686
dbo.TESTReport
      (
      @StartDate date,
      @EndDate date      )
AS
SELECT
    Port, Boat,
    SUM(CASE WHEN Product_Name = 'Lobsters' THEN ProductKiloQty ELSE 0 END) AS [LobstersKG],
    SUM(CASE WHEN Product_Name LIKE '%Crabs' THEN ProductKiloQty ELSE 0 END) AS [CrabsKG],
    SUM(CASE WHEN Product_Name LIKE 'Velvet Crabs%' THEN ProductKiloQty ELSE 0 END) AS [VelvetsKG],
    SUM(CASE WHEN Product_Name LIKE 'Langoustine%' THEN ProductKiloQty ELSE 0 END) AS [LangKG],
    SUM(CASE WHEN Product_Name = 'Crabs (Cut)' THEN ProductKiloQty ELSE 0 END) AS [CutCrKG]
FROM vReportData
WHERE Landing_Day = 5
AND DateOfCatch BETWEEN DATEADD(day, - 7, @EndDate) AND DATEADD(day, - 1, @EndDate)
GROUP BY
    Port, Boat

UNION ALL

      SELECT
    Port, Boat,
    SUM(CASE WHEN Product_Name = 'Lobsters' THEN ProductKiloQty ELSE 0 END) AS [LobstersKG],
    SUM(CASE WHEN Product_Name LIKE '%Crabs' THEN ProductKiloQty ELSE 0 END) AS [CrabsKG],
    SUM(CASE WHEN Product_Name LIKE 'Velvet Crabs%' THEN ProductKiloQty ELSE 0 END) AS [VelvetsKG],
    SUM(CASE WHEN Product_Name LIKE 'Langoustine%' THEN ProductKiloQty ELSE 0 END) AS [LangKG],
    SUM(CASE WHEN Product_Name = 'Crabs (Cut)' THEN ProductKiloQty ELSE 0 END) AS [CutCrKG]
FROM vReportData
WHERE Landing_Day = 6
AND DateOfCatch BETWEEN DATEADD(day, - 8, @EndDate) AND @EndDate
GROUP BY
    Port, Boat
ORDER BY --optional, of course
    Landing_Day, Port, Boat

RETURN

Can someone please tell me what's wrong with this syntax??
0
 

Author Comment

by:Lorna70
ID: 37810697
Meant to say, I'm using SQL Server
0
 
LVL 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 400 total points
ID: 37812024
What does this give ?

SELECT Landing_Day, Port, Boat, 
    SUM(CASE WHEN Product_Name = 'Lobsters' THEN ProductKiloQty ELSE 0 END) AS [LobstersKG],
    SUM(CASE WHEN Product_Name LIKE '%Crabs' THEN ProductKiloQty ELSE 0 END) AS [CrabsKG],
    SUM(CASE WHEN Product_Name LIKE 'Velvet Crabs%' THEN ProductKiloQty ELSE 0 END) AS [VelvetsKG],
    SUM(CASE WHEN Product_Name LIKE 'Langoustine%' THEN ProductKiloQty ELSE 0 END) AS [LangKG],
    SUM(CASE WHEN Product_Name = 'Crabs (Cut)' THEN ProductKiloQty ELSE 0 END) AS [CutCrKG]
FROM 
  (
  SELECT
      Landing_Day, Port, Boat, ProductKiloQty, Product_Name
  FROM vReportData
  WHERE (Landing_Day = 5 AND DateOfCatch BETWEEN DATEADD(day, - 8, @EndDate) AND DATEADD(day, - 1, @EndDate)
    OR (Landing_Day = 6 AND DateOfCatch BETWEEN DATEADD(day, - 8, @EndDate) AND @EndDate)
  ) As X
GROUP BY
    Landing_Day, Port, Boat
ORDER BY 
  Landing_Day, Port, Boat

Open in new window

0
 

Author Comment

by:Lorna70
ID: 37812345
Thanks - that looks like it would work too but I'm getting an error near 'AS x'.

The 'UNION ALL' method works if I take out the first 'ORDER BY' statement :-)
0
 
LVL 3

Expert Comment

by:Chris__W
ID: 37812560
Hello Lorna70,

From what I've always seen in TSQL, your 2 select statements are basically tables in the FROM clause.

Put a Select statement above:

SELECT field1, field2, field3 FROM
(
{Your first select statement}
UNION ALL
{Your second select statement}
)
GROUP BY
ORDER BY


Also, remove all GROUP BY and ORDER BY clauses that are within the UNION section and put them at the end as shown above.

Thanks,
Chris
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

766 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