Getting Count on 2 columns on SQL - sql

I am trying to get a count on approved orders in a separate column.
Initial Table looks like this,
User name
Status
Count
User 1
Approved
1
Rejected
2
User 2
Approved
5
User 3
Approved
1
User 4
Approved
2
Rejected
5
But I want to get another column as a Approved Count,
User name
Status
Count
Approved Count
User 1
Approved
1
1
Rejected
2
User 2
Approved
5
5
User 3
Approved
1
1
User 4
Approved
2
2
Rejected
5
What is the best way of doing this?

You probably want to do something like this:
Select Count(User_Id) FROM table WHERE Status = 'Approved'
You could also try adding GROUP BY User_Id at the end

Try this one. I used same column names as you mentioned, but not recommended
Your impotent part is this one:
case when Status = 'Approved' then count else 0 end
Example
select *, case when Status = 'Approved' then count else 0 end Approved_count from table_X
where row_num = 1;
Full sample code
select *, case when Status = 'Approved' then count else 0 end Approved_count from (
select ROW_NUMBER() OVER (partition by user_name, Status ORDER BY user_name) row_num,
user_name, Status, count(Status) over (partition by user_name, Status) count
from [Count_test] )
A where row_num = 1;

You could try something using a case statement.
SELECT
Username,
Status,
Count,
CASE
WHEN Status = 'Approved'
THEN Count
ELSE ''
END AS approved_count
FROM
table1
db fiddle

Related

Grouping by id and looking at another column in a particular order to see if the id group satisfies a particular condition

customer_id
transaction success
1
Failed
2
Complete
1
Failed
1
Complete
3
Failed
2
Failed
3
Complete
3
Failed
3
Failed
3
Complete
Essentially I want to write a statement to identify if the customer has had a completed transaction after having had a failed transaction sometime before. So in this example, customer 1 and customer 2 would be satisfy this. Assume that there is an added timestamp column next to transaction success.
The resulting table should look like this:
customer_id
returning_success
1
True
2
False
3
True
Assuming that is not important if the Complete was after or prior to the Cancellation, you can LEFT JOIN the table with a subquery that only takes the completes. If the result is NULL, then is not have a complete state. Otherwise is true.
As you don't provide your DBMS (Please read: Why should I "tag my RDBMS"?) we take in consideration IFNULL but this can change in other DBMS: https://www.w3schools.com/sql/sql_isnull.asp
SELECT
yt.customer_id,
IFNULL(completes.customer_id,'false','true') as returning_success
FROM
yourtable yt
LEFT JOIN
(
SELECT
customer_id
FROM
yourTable
WHERE transaction_success = 'Complete') completes
ON completes.customer_id = yt.customer_id
 If you just need customers that had had both succesfull and faild transactions, you should implement this:
select customer_id, case when sum(case
when transaction='Faild'
then 1
else 0 end)>0
and
sum(case
when transaction='Complete'
then 1
else 0 end)>0
then 'True'
else 'False' end
returning_success
from table_
group by customer_id
 If you actually do have some timestamp column:
select nvl(c.customer_id, f.customer_id) customer_id
, case when last_complete_time is null
or first_fail_time is null
or first_fail_time>last_complete_time
then 'False'
else 'True' end
returning_success
from (
select customer_id, max(time_) last_complete_time
from table_
group by customer_id
where transaction='Complete'
) c
full join (
select customer_id, min(time_) first_fail_time
from table_
group by customer_id
where transaction='Fail'
) f on c.customer_id=f.customer_id
 You also can use this query to filter all True cases and then just union or join the rest:
select f.customer_id, 'True'
from (
select customer_id, max(time_) last_complete_time
from table_
group by customer_id
where transaction='Complete'
) c
join (
select customer_id, min(time_) first_fail_time
from table_
group by customer_id
where transaction='Fail'
) f on c.customer_id=f.customer_id
where first_fail_time<last_complete_time

SQLite - Count and group duplicates

I have a problem figuring out how to count duplicates in a table like below:
Campaign
Approved
Disqualified
campaign-1
1
null
campaign-1
null
2
campaign-2
5
null
campaign-2
null
3
My query:
select
"Campaign"
,case when "Status" = 'Approved' then count("Id") end as "Approved"
,case when "Status" = 'Disqualified' then count("Id") end as "Disqualified"
from "table"
group by "Campaign","Status"
having count(*) > 1
order by "Campaign"
I would like to have a table as result below?
result:
Campaign
Approved
Disqualified
campaign-1
1
2
campaign-2
5
3
...
You must group by Campaign only.
Also, it is easier to use SUM() instead of COUNT(), because SQLite evaluates boolean expressions like Status = 'Approved' as 1 or 0:
SELECT Campaign,
SUM(Status = 'Approved') AS Approved,
SUM(Status = 'Disqualified') AS Disqualified
FROM "table"
GROUP BY Campaign
HAVING COUNT(*) > 1
get_info = """
WITH query1 AS
(
SELECT Campaign,
SUM(Approved) AS sum_a,
SUM(Disqualified) AS sum_d
FROM Campaign
GROUP BY Campaign
)
SELECT Campaign, sum_a, sum_d FROM query1
GROUP BY Campaign
"""

convert column to row in postgresql database

My current query is:
select rulename,status, count(*)
from triggered_rule_info
group by status, rulename
And the result is:
rulename status count
eorule1 ack 1
eorule1 open 1
eorule1 close 7
eorule2 open 1
eorule2 ack 1
But I want the result to be:
rulename ack open close
eorule1 1 1 7
eorule2 1 1
How can I achieve this? My postgresql version is 9.4.
For that you can use the filter clause:
select rulename
count(*) filter (where status = 'ack') as ack,
count(*) filter (where status = 'open') as open,
count(*) filter (where status = 'close') as closed
from triggered_rule_info
group by rulename
order by rulename;
You can make use of CASE - WHEN and GROUP BY clauses to get the desired result. You may require to add more CASE statements if status has more other values.
Example:
SELECT rulename
, SUM( CASE status WHEN 'ack' THEN 1 ELSE 0 END ) AS "ack"
, SUM( CASE status WHEN 'open' THEN 1 ELSE 0 END ) AS "open"
, SUM( CASE status WHEN 'close' THEN 1 ELSE 0 END ) AS "close"
FROM triggered_rule_info
GROUP BY rulename
ORDER BY rulename

SQL select from the column and show specific output based on values in the column

I have a scenario where i need to show specific value in new column if I find any specific value( i.e Open, close,WIP then open) and if status =closed then show closed
Data :
ID SUBID Status
1 5 new
1 6 close
1 7 wip
2 22 closed
output i want to get
ID status
1 open
2 closed
You could think about this backwards, and use the else to catch all 'non-closed' rows.
select distinct
id,
case when status = 'closed' then 'closed' else 'open' end
from
yourTable
Scsimon's answer is 100% the best way to handle this based on your sample data.
Just want to add that if an id can have records with 'closed' and other records too (open, new, wip, etc.), use CASE WHEN EXISTS:
SELECT DISTINCT
ID,
CASE WHEN EXISTS (SELECT 1
FROM YourTable T2
WHERE T.ID = T2.ID
AND Status = 'closed')
THEN 'closed'
ELSE 'open'
END as Status
FROM YourTable T

How to group and get count of rows based on value of one column in sql server

I have a table of requests with columns RequestType,status .Status column values can be in-progress,complated etc.
I would like to get the list like
RequestType In-Progress Completed Total
Type1 10 5 15
Type2 10 10 20
I tried with group by using the 2 columns( RequestType,status) ,but it does not give me the exact result.
Please help me with the sql query.
Thanks in advance
Subin
One way to do it is using conditional aggrigation:
SELECT RequestType,
SUM(CASE WHEN Status = 'In-Progress' THEN 1 ELSE 0 END) As 'In-Progress',
SUM(CASE WHEN Status = 'Completed' THEN 1 ELSE 0 END) As 'Completed',
COUNT(Status) As 'Total'
FROM TableName
WHERE Status IN('In-Progress', 'Completed')
GROUP BY RequestType
Use PIVOT
select *, [In-Progress]+[Completed] total
from TableName
pivot ( count(status) for status in ([In-Progress], [Completed])) as p