Solved

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

Posted on 2012-04-05
8
367 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 37

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 150 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 100 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

730 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