0

I have a data set in which I need each issue to have 3 rows. 1 for each status that is possible. Below is an example of what I currently have.

Issue  Status  Time 
-------------------
1      SLM      30
1      SNB      43
1      EOB      22
2      SLM      12
2      EOB      87

I need something like this, where is an issue doesn't have a status then a row is added and 0 is set for the time.

 Issue   Status    Time 
 ----------------------
    1      SLM      30
    1      SNB      43
    1      EOB      22
    2      SLM      12
    2      EOB      87
    2      SNB      0

How can I do this?

1
  • Do you have a table that contains the three types of Status values? Commented Jun 6, 2017 at 16:25

2 Answers 2

8

Cross join id's with statuses and left join the table on to that.

select i.issue,s.status,coalesce(t.time,0) as time
from (select distinct status from tbl) s --replace this with status table if you have one
cross join (select distinct issue from tbl) i
left join tbl t on t.issue=i.issue and t.status=s.status
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic answer. I was able to take this and easily expand it to my needs, simply by adding more rows of cross join for additional unique columns I have and then more t.col=i.col for the final join. So clearly written and concise, bravo!
-1

You can easily add another row by using this sql command.

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

for instance if your tables name is

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.