Creating an SQL view using UNION and a second SELECT

Bryan

I've got a table with records that have some properties. One of the properties is their state.

I also have a view that counts and groups the states. It looks like this:

State        Total

Canceled     9
Failed       3
Pending      10
Succeeded    7

Since there is no record with the 'Processing' state in my reference table, the view doesn't show it in the view. However, I want each of the five possible states in my view, even if it doesn't exist in the table.

I made the following query and am close to getting it to work (testing it with one of the available states):

SELECT State, COUNT(State) AS Total
FROM dbo.Job
GROUP BY State
UNION
SELECT 'Processing' AS State, 0 AS Total

The problem is my '0 AS Total'. The 0 should be dynamic. Is there a way I can replace it?

Eric Brandt

In order to get a comprehensive list of State, all of the values have to come from somewhere other than your starting data set (assuming that not all values are represented, as is the current case with 'Processing'). That can be a table, which is your best option if you have the privileges to create such a table, or, if need be, from a list, which you'll then have to maintain if new State values are added.

In the second case, a table value constructor will do the job for you. Start with that constructor, then LEFT JOIN to your data source. An INNER JOIN will, again, filter off values missing in your underlying data.

SELECT
  s.[State]
 ,COUNT(j.[State]) AS Total
FROM
    (
      VALUES
        ('Canceled')
       ,('Failed')
       ,('Pending')
       ,('Succeeded')
       ,('Processing')
    ) AS s ([State])
  LEFT JOIN 
    dbo.Job AS j
      ON
      s.[State] = j.[State]
GROUP BY
  s.[State];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

create view with union all using select not working

MS Access SQL: Using SELECT INTO with a UNION ALL query

SQL UNION - Is it possible to use a result from first SELECT in the second SELECT

Add a row at the end of SQL Select using UNION

Creating a view in Sql with connected tables

SQL - Select rows from same table twice without using UNION

#SQL UNION two WITH SELECT

How to select records from table using union such that result of first query concatenates each result with corresponding result of second query?

SQL Union Select for alternate data

SQL : Creating View using MAX() + SUM() with 3 related tables

Creating a SQL view with transformation and join (??)

Creating a temporary view in Spark SQL

Backbone error when creating a second (collection) view

Oracle SQL - Creating A Temporary Table Then Union To That Table

Use value from first select for second select in union?

Creating a view in SQL Server

Creating a view in SQL Server from select statement

SELECT USING UNION QUERY

SQL - 2 SELECT statements with UNION and WITH

Creating sql view where the select is dependent on the value of two columns

Creating A View Code in SQL

Oracle SQL UNION with SELECT AS subqueries

SQL - Priority using UNION

Join SQL select statements using Union

GRANT SELECT ON view TO user using script on SQL developer, than SELECT * FROM that view in SQL Plus

SQL - Sub select, Union and using functions

How to remove duplicates from second select using union sql

Multiple Views combined into 1 Table/View using UNION (SQL)

creating a sql view