Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
370 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

609 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