I've stumbled upon a table with three columns
id | isRed | isBlue | is Green
==============================
1 | 1 | 0 | 0
2 | 1 | 0 | 0
3 | 1 | 0 | 0
4 | 0 | 1 | 0
5 | 0 | 1 | 0
6 | 0 | 1 | 0
7 | 0 | 0 | 1
8 | 0 | 1 | 0
9 | 0 | 0 | 1
10 | 0 | 0 | 0
I want to create query as simple as possible to transform it into something like that:
id | Color
==============================
1 | red
2 | red
3 | red
4 | blue
5 | blue
6 | blue
7 | green
8 | blue
9 | green
10 | 0
The values can't be true in two different columns and I can't alter the database. I need it to append it to quite long query with as little complication as possible. Any ideas?
With a select and case:
select id,
(case when isRed = 1 then 'red'
when isBlue = 1 then 'blue'
when isGreen = 1 then 'green'
else '0'
end) as color
from t;
Related
This question already has an answer here:
SQL Set column with Key and max value from other colum
(1 answer)
Closed 1 year ago.
Taking the max value of column 'time', I need update the column 'register' group by Identification column, set register = 1 and the rest register = 0
Initial table:
Identification | time | register
1 | 0 | 0
1 | 7 | 0
1 | 3 | 0
2 | 10 | 0
2 | 5 | 0
2 | 0 | 0
3 | 6 | 0
3 | 5 | 0
3 | 0 | 0
Ending table:
Identification | time | register
1 | 0 | 0
1 | 7 | 1
1 | 3 | 0
2 | 10 | 1
2 | 5 | 0
2 | 0 | 0
3 | 6 | 1
3 | 5 | 0
3 | 0 | 0
In SQL 2017, the table contains thousand of registers
Seems like you want a CASE expression and a windowed MAX:
SELECT Identification,
[time],
CASE [time] WHEN MAX([time]) OVER (PARTITION BY Identification) THEN 1 ELSE 0 END AS register
FROM dbo.YourTable;
I have a table of Tasks. There may be many tasks with various statuses to one Activity. I need to add columns to the main Activity view that displays the count of how many open tasks, closed tasks and total tasks. I thought I would create a view with the totals grouped per Activity then join via the ActivityID. I am having trouble creating the view with the correct figures, please help.This doesn't return the correct numbers:
The status values that equate to an 'OpenTasks' is 0,1 & 3. 'ClosedTasks' is 3
Existing table - Tasks
ID | WorkID| Status
1 | 5 | 2
2 | 5 | 2
3 | 5 | 0
4 | 5 | 3
5 | 6 | 2
6 | 7 | 0
7 | 7 | 0
8 | 9 | 1
9 | 9 | 2
The resulting view I require is:
WorkID | OpenTasks | ClosedTasks | TotalTasks
5 | 2 | 2 | 4
6 | 0 | 1 | 1
7 | 2 | 0 | 2
9 | 1 | 1 | 2
SELECT top 10000 WorkActivityId,
COUNT(CASE WHEN status = 2 then 1 ELSE NULL END) as "ClosedTasks",
COUNT(CASE WHEN status <> 2 then 1 ELSE NULL END) as "OpenTasks",
COUNT(CASE WHEN status < 10 then 1 ELSE NULL END) as "TotalTasks"
FROM [SDMTWorkActivity].[WorkActivity].[Tasks]
GROUP BY WorkActivityId order by WorkActivityId
I am not great as SQL so appreciate any advice Thank you
Your query should be working but you can try the following using sum
SELECT
WorkId,
SUM(CASE WHEN status <> 2 then 1 ELSE 0 END) as "OpenTasks",
SUM(CASE WHEN status = 2 then 1 ELSE 0 END) as "ClosedTasks",
SUM(CASE WHEN status < 10 then 1 ELSE 0 END) as "TotalTasks"
FROM [SDMTWorkActivity].[WorkActivity].[Tasks]
GROUP BY WorkId
order by WorkId
output:
| WorkId | OpenTasks | ClosedTasks | TotalTasks |
| ------ | --------- | ----------- | ---------- |
| 5 | 2 | 2 | 4 |
| 6 | 0 | 1 | 1 |
| 7 | 2 | 0 | 2 |
| 9 | 1 | 1 | 2 |
Suppose I have a table like this:
id | part | value
----+-------+-------
1 | 0 | 8
2 | 0 | 3
3 | 0 | 4
4 | 1 | 6
5 | 0 | 13
6 | 0 | 4
7 | 1 | 2
8 | 0 | 11
9 | 0 | 15
10 | 0 | 3
11 | 0 | 2
I would like to enumerate groups that have part atribute 0.
Ultimately I want to get this:
id | part | value | number
----+-------+-----------------
1 | 0 | 8 | 1
2 | 0 | 3 | 2
3 | 0 | 4 | 3
4 | 1 | 6 | 0
5 | 0 | 13 | 1
6 | 0 | 4 | 2
7 | 1 | 2 | 0
8 | 0 | 11 | 1
9 | 0 | 15 | 2
10 | 0 | 3 | 3
11 | 0 | 2 | 4
Is it possible to solve this with Postgres window functions or is there another way?
Yes, that is simple:
SELECT id, part, value,
row_number() OVER (PARTITION BY grp ORDER BY id) - 1 AS number
FROM (SELECT id, part, value,
sum(part) OVER (ORDER BY id) AS grp
FROM mytable
) AS q;
id | part | value | number
----+------+-------+--------
1 | 0 | 8 | 0
2 | 0 | 3 | 1
3 | 0 | 4 | 2
4 | 1 | 6 | 0
5 | 0 | 13 | 1
6 | 0 | 4 | 2
7 | 1 | 2 | 0
8 | 0 | 11 | 1
9 | 0 | 15 | 2
10 | 0 | 3 | 3
11 | 0 | 2 | 4
(11 rows)
I have a table which has 7 different classes with an area value.
pid | class| area |
----+------+------+
2 | 1 | 10 |
2 | 2 | 10 |
2 | 6 | 20 |
4 | 1 | 30 |
4 | 2 | 40 |
4 | 3 | 50 |
4 | 4 | 60 |
4 | 5 | 70 |
9 | 6 | 80 |
11 | 1 | 90 |
11 | 4 | 10 |
11 | 7 | 20 |
However I want to present this data in a format that has each distinct pid as a column heading and then have each row correspond to a class area (i.e. first row is the area of class 1 for each pid).
2 | 4 | 9 | 11 |
---+-----+-----+----+
10 | 30 | 0 | 90 |
10 | 40 | 0 | 0 |
0 | 50 | 0 | 0 |
0 | 60 | 0 | 10 |
0 | 70 | 0 | 0 |
20 | 0 | 60 | 0 |
0 | 0 | 0 | 20 |
Is it possible to create an output like this in PostgreSQL?
Try this:
SELECT
SUM(CASE WHEN pid = 2 THEN area ELSE 0 END) As "2",
SUM(CASE WHEN pid = 4 THEN area ELSE 0 END) As "4",
SUM(CASE WHEN pid = 9 THEN area ELSE 0 END) As "9",
SUM(CASE WHEN pid = 11 THEN area ELSE 0 END) As "11"
FROM t
GROUP BY class
ORDER BY class
i am including column 13 as a dummy column here:
+----+---+---+---+----+---+---+---+---+---+----+----+----+----+
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
+----+---+---+---+----+---+---+---+---+---+----+----+----+----+
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 2 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 3 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 4 | 1 | 0 | 0 | 20 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 7 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 9 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
+----+---+---+---+----+---+---+---+---+---+----+----+----+----+
the reason i am including a dummy column is so that IF columns 1 through 12 are all zero, i would still like to include an entry for that row.
as you can see row 1 would not have been included.
this report is generated by SSRS.
i am wondering if there is a way to HIDE column 13?
is there some kind of conditional formatting i can do?
to clarify here's my query:
select tat.*, tat.tat as tat2 from tat
it is organized in the report this way:
this data set [TAT] contains dummy data specifically for column 13
Specific columns in a column group can be hidden based on values with the following steps.
Right-click header of the column group you want to hide, Column Group -> Group Properties
Click on the Visibility pane and select Show or hide based on an expression radio button. Use an expression to determine when column is hidden.
True hides the column, False displays it. You will need to update the field name in my example to match your month field name.
Don't include column 13 in your select? If you are doing a select *, change it to Select col1, col2, ...., col12