I would like to perform query in which as a result I have column with false if the value in former column is 0 and true if is greater then 0:
as example:
id count
1 1
2 3
3 0
4 5
5 2
result:
id count
1 true
2 true
3 false
4 true
5 true
select
id,
case
when count > 0 then 'true'
else 'false'
end as count
from myTable
select id
, case when count > 0 then cast(1 as bit) else cast(0 as bit) end as count
from myTable
Related
I have a table like this
I need to get the validity order by the number of true values in the last 4 columns.
For e.g. the output of the below query should be
1110 // 4 true values
1001 // 2 true values
1000 // 1 true value
You can use subquery;
SELECT subquery.VALIDITY,
concat(CAST ((subquery.TAXI + subquery.CAR + subquery.TRUCK + subquery.BIKE) AS TEXT) , ' True values') as COUNTTRUEVALUES
FROM (select VALIDITY,
CASE WHEN TAXI THEN 1 ELSE 0 END AS TAXI,
CASE WHEN CAR THEN 1 ELSE 0 END AS CAR,
CASE WHEN TRUCK THEN 1 ELSE 0 END AS TRUCK,
CASE WHEN BIKE THEN 1 ELSE 0 END AS BIKE
FROM YourTableName) as subquery
ORDER BY 2 DESC
output
VALIDITY COUNTTRUEVALUES
1110 4 True values
1001 2 True values
1000 1 True values
Convert the boolean values to integers and order descending by their total:
SELECT *
FROM tablename
ORDER BY taxi::int + car::int + truck::int + bike::int DESC;
See the demo.
What about something like this?
SELECT
id,
validity,
taxi1 + car1 + truck1 + bike1
FROM
(
SELECT
id,
validity
CASE WHEN taxi = 'TRUE' THEN 1 ELSE 0 END taxi1,
CASE WHEN car = 'TRUE' THEN 1 ELSE 0 END car1,
CASE WHEN truck = 'TRUE' THEN 1 ELSE 0 END truck1,
CASE WHEN bike = 'TRUE' THEN 1 ELSE 0 END bike1
FROM table
)
is that anyway to write similar query without using union?
select sum(decode(p.sumsend,0,1,0)) recvcnt,
sum(decode(p.sumsend,0,1,0)*p.sumserv) recvsum
from some_table p
where p.polefilter = 5
union
select sum(decode(p.sumsend,0,1,0)) recvcnt,
sum(decode(p.sumsend,0,1,0)*p.sumserv) recvsum
from some_table p
where p.polefilter != 5
If you are OK with having all 4 columns on one row, then one option is conditional aggregation:
select
sum(case when polefilter = 5 and sumsend = 0 then 1 else 0 end) recvcnt1,
sum(case when polefilter = 5 and sumsend = 0 then 1 else 0 end * sumserv) recvsum1,
sum(case when polefilter <> 5 and sumsend = 0 then 1 else 0 end) recvcnt2,
sum(case when polefilter <> 5 and sumsend = 0 then 1 else 0 end * sumserv) recvsum2
from some_table p
where polefilter is not null
On the other hand, if you want two rows in the resultset, then you can use aggregation and a case expression to define the groups:
select
case when polefilter = 5 then 1 else 0 end as polefilter_is_5
sum(case when sumsend = 0 then 1 else 0 end) recvcnt,
sum(case when sumsend = 0 then 1 else 0 end * sumserv) recvsum1
from some_table p
where p.polefilter is not null
group by case when polefilter = 5 then 1 else 0 end
Note that I changed the decode() functions to case expressions; both do the same thing, but the latest is standard SQL (and is somehow more flexible).
A query like the one below should work. Please provide sample data and expected output when asking a question next time.
SELECT SUM (CASE WHEN p.sumsend = 0 THEN 1 ELSE 0 END) recvcnt,
SUM (CASE WHEN p.sumsend = 0 THEN 1 ELSE 0 END * p.sumserv) recvsum
FROM some_table p
GROUP BY CASE p.polefilter WHEN 5 THEN 1 ELSE 0 END;
I have following table
ID UserID Email Status Count
1 16 kiran.shahi#example.com True 2
2 16 aibrahim#sbcglobal.net False 3
3 16 russotto#icloud.com False 3
4 16 seurat#optonline.net False 3
5 16 paley#aol.com False 3
6 16 tmaek#aol.com False 3
7 16 baveja#verizon.net False 3
8 16 wonderkid#mac.com False 3
9 16 mkearl#live.com False 3
10 16 solomon#att.net False 3
11 16 enintend#gmail.com False 3
12 16 rbarreira#outlook.com False 3
13 16 pavel#yahoo.ca False 3
NULL NULL NULL NULL NULL
And following stored procedure
#UserID INT
AS
BEGIN
SET NOCOUNT ON;
SELECT
#UserID AS UserID,
COUNT(T1.Status) AS Active,
COUNT(T2.Status) AS Inavtive
FROM UserStatus T1
JOIN UserStatus T2
ON T1.UserID = T2.UserID
WHERE T1.Status = 1 AND T2.Status = 0 AND T2.UserID = #UserID
END
Which returns
UserID Active Inavtive
1 16 12 12
How can I achieve result as
UserID Active Inavtive
1 16 1 12
Instead of COUNT try using SUM. Like this
SELECT
#UserID AS UserID,
SUM(CASE WHEN Status = 1 THEN 1 ELSE 0 END) AS Active,
SUM(CASE WHEN Status = 0 THEN 1 ELSE 0 END) AS Inavtive
FROM UserStatus
WHERE
UserID = #UserID
You can use SUM as below:
SELECT
SUM(CASE WHEN Status =1 THEN 1 ELSE 0 end) as Active,
SUM(CASE WHEN Status =1 THEN 0 ELSE 1 end) as InActive
FROM UserStatus
WHERE
UserID = #UserID
I have tried to implement case inside count() instead of sum() function because sum() function return null when there is no record where as count() returns which is helpful to do calculation in my code. Following code is working fine. In my case Status column must be bit so that my condition is satisfied and can get the count of true and false record.
#UserID INT
AS
BEGIN
SET NOCOUNT ON;
SELECT
#UserID AS UserID,
COUNT(CASE WHEN Status = 1 THEN ID END) AS Active,
COUNT(CASE WHEN Status = 0 THEN ID END) AS Inavtive
FROM UserStatus
WHERE
UserID = #UserID
END
I'm trying to figure out how to return a select query showing a count of all values in a column that are greater than 0. Then in the next column show a count of all values that = 0.
Example:
ID ColumnA
1 1
2 2
3 1
4 2
5 0
6 0
7 1
Would return a result for the select query of:
NumberOfGreaterThan0 NumberThatEqual0
5 2
You can use conditional aggregates for this via CASE expression:
SELECT COUNT(CASE WHEN ColumnA > 0 THEN 1 END) AS NumberOfGreaterThan0
,COUNT(CASE WHEN ColumnA = 0 THEN 1 END) AS NumberThatEqual0
FROM YourTable
This works because aggregate functions ignore NULL values.
You can use a couple of count functions over case expressions:
SELECT COUNT(CASE WHEN columa > 0 THEN 1 ELSE NULL END) AS NumberOfGreaterThan0,
COUNT(CASE columa WHEN 0 THEN 1 ELSE NULL END) AS NumberThatEqual0
FROM my_table
I am using database schema with the following column
TRAFFIC_DIRECTION
tinyint
The direction of traffic. Enum ( unknown = 0; inbound = 1; outbound = 2)
When I run query, TRAFFIC_DIRECTION displays as
TRAFFIC_DIRECTION
1
2
2
1
1
1
2
2
2
1
1
1
1
1
How do I make it such that, instead of outputting 1 it outputs "inbound", and instead of 2, it outputs "outbound"
Some RDBMSs support an ENUM type, but all of them support a CASE statement.
SELECT id,
CASE TRAFFIC_DIRECTION
WHEN 0 THEN 'Unknown'
WHEN 1 THEN 'Inbound'
WHEN 2 THEN 'Outbound'
ELSE 'ERROR'
END
FROM Table1
select case
when traffic_direction = 1 then 'inbound'
when traffic_direction = 2 then 'outbound'
else 'unknown'
end as direction
from the_table;
SELECT
CASE WHEN TRAFFIC_DIRECTION = 0 THEN "UNKNOWN"
WHEN TRAFFIC_DIRECTION = 1 THEN "inbound"
WHEN TRAFFIC_DIRECTION = 2 THEN "outbound"
END AS Traffic_Word
FROM sometable
WHERE some criteria