Solved

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

Posted on 2012-04-05
8
364 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql server concatenate fields 10 35
sql server insert 12 30
Need some help to cast ntext to nvarchar SQL 2000 7 33
Sql Server group by 10 27
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

773 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