0

I have 2 tables in my database:

DetectorStatus

DetectorStatusID    DetectorID    DateTime    DetectorModeID    Status
      1                471          time            2            0.7

StationStatus

StationStatusID    DetectorID    DateTime    StationModeID    Status
      1                1541        time            2           0.74 

I want to create a view that looks like this:

StationStatusID    DetectorStatusID    DetectorID    DateTime    StationModeID    DetectorModeID    Status
      NULL                 1               471         time          NULL                2            0.7
      1                    NULL            1541        time          2                   NULL         0.74

Now, when i create the view like this:

CREATE view statusoverview AS
    SELECT * FROM [GMS_MAN].[dbo].[DetectorStatus]
    UNION ALL
    SELECT * FROM [GMS_MAN].[dbo].[StationStatus]

I get all the results inside 1 table. However StationModeID is inside DetectorModeID etc.

How do i create a view that looks like the given example?

2
  • 1
    Select null columns as well. Commented Mar 6, 2017 at 8:27
  • When i change the SELECT * FROM and add the columns + NULLS, It says: No column was specified for column 1 of viewname Commented Mar 6, 2017 at 8:30

3 Answers 3

1

You need to ensure that both result sets contain all of the columns you want:

CREATE view statusoverview AS
SELECT null as StationStatusID,
       DetectorStatusID,
       DetectorID,
       DateTime,
       null as StationModeID,
       DetectorModeID,
       Status
FROM [GMS_MAN].[dbo].[DetectorStatus]
UNION ALL
SELECT StationStatusID,
       null,
       DetectorID,
       DateTime,
       StationModeID,
       null,
       Status
FROM [GMS_MAN].[dbo].[StationStatus]

Note that the column names are taken from the first query, so it needs to name all of the columns. The second can omit naming the columns it's supplying nulls for.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It's giving me the exact result that i want. Also very clear and understandable written. Can accept answer in 5 minutes.
1
SELECT null as StationStatusID,DetectorStatusID,DetectorID,DateTime,null StationModeID,DetectorModeID,Status FROM [GMS_MAN].[dbo].[DetectorStatus]
    UNION ALL
    SELECT StationStatusID, null, DetectorID,DateTime,StationModeID, null ,Status FROM [GMS_MAN].[dbo].[StationStatus]

I might have do some column misspellings, but it iwll be something like that

Comments

0

Well, you won't use SELECT *, you will specify which columns you want to have in your table:

CREATE view statusoverview AS
SELECT detector.*, station.StationStatusID, station.StationMode,station.Status 
FROM DetectorStatus detector
LEFT JOIN StationStatus station ON station.DetectorID=detector.DetectorID.

Whatever you choose, I think you shouldn't "SELECT *, but specify what you need"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.