How to implement conditional COUNTIF in SQL? - sql

I am new to SQL and facing the problem for the while.
The question is how to create such Label column in SQL?
Goal: When group count is more than 1 it should be 'Y' otherwise 'N'
Here is what I do in Excel (please see screenshot).

I think you want case with a window function:
select (case when count(*) over (partition by item) > 1 then 'Y' else 'N' end) as label

Related

SQL Countifs function

Hello,
I reach a dead end. I have a table with piecemark_1, piecemark_2, and weld_type.
I wanted to have a final output as per image show.
In excel I would usually use the formula "COUNTIFS+COUNTIFS" but now my data is getting bigger.
I tried to UNION both piecemark together from column piecemark_1 and piecemark_2 to create another table but I do not know how to countifs from another table.
I was wondering how can I solve this issue.
You seems want :
select piecemark, SUM(CASE WHEN WELD_TYPE = 'SW' THEN 1 ELSE 0 END) AS SW,
SUM(CASE WHEN WELD_TYPE = 'FW' THEN 1 ELSE 0 END) AS FW, COUNT(*) AS Total
from t cross apply
( values (piecemark_1), (piecemark_2)
) tt(piecemark)
group by piecemark
having count(*) > 1;
For all piecemark you need to exclude having clause.

How to create an alias that counts Ws, Ls, and Ds to create a record

I have a table with sports results with a column labeled 'Result' where the values in that column are either W, L, or D. I would like to create an alias column that will quickly count the Ws, Ls, and Ds from the whole table in that columns and display it as 'Count W-Count L-Count D'.
I'm very new to SQL and I haven't figured this specific of a request out, nor can I find the correct search terms in Google to discover a video or forum result for the situation I am looking for.
If you want the values in separate columns, use conditional aggregation:
select sum(case when result = 'W' then 1 else 0 end) as w_cnt,
sum(case when result = 'L' then 1 else 0 end) as l_cnt,
sum(case when result = 'T' then 1 else 0 end) as t_cnt
from t;
Best option go for group by
Select result, count(*) from table
where column IN ('W' , 'L' , 'D' )
group by result

Creating flag column

I would like to ask you for your help. I want to create a flag column, that will mark specific contract numbers whith 1, where the "CLOSED" column is empty in some rows.
I tried
case when CLOSED is null then 1 else 0 end as flag
group by CONTRACT_NUMBER
but it's not working. Thank you for your responses
example picture
I think you need analytic max() here:
demo
select t.*,
max(case when closed is null
then 1
else 0
end) over (partition by contract_number) as flag
from t

Showing sum of conditions as one row

Hi I have a table like this
select id, value, condition from mytable
result
and I need a query to make it like this
is it possible?
Just use conditional aggregation:
select id,
sum(case when condition = 'true' then value else 0 end) as num_true,
sum(case when condition = 'false' then value else 0 end) as num_false
from t
group by id;
You have tagged the question with both Oracle and SQL Server. Neither supports a boolean type directly in SQL, so I am guessing that the condition is a string.
yes possible
select id "id", sum(decode(condition,'TRUE',value,0)) "sum_of_condition_true",
sum(decode(condition,'FALSE',value,0)) "sum_of_condition_false"
from mytable
group by id
order by id;

Multiple SUM values using WHERE clauses in Oracle

I am writing a SQL statement against an Oracle 10g database. I want to obtain the SUM of a field with three different conditions. Can I do this with one query?
This is pseudo-SQL for what I want:
SELECT SUM(CP) AS CPTotal,
(SUM(CP) FROM tasks WHERE Code='P') AS CPProd,
(SUM(CP) FROM tasks WHERE Code='S') AS CPSupp
FROM tasks;
A conditional SUM() can be had via CASE statements:
SELECT SUM(CP) AS CPTotal,
SUM(CASE WHEN Code = 'P' THEN CP ELSE 0 END) AS CPProd,
SUM(CASE WHEN Code = 'S' THEN CP ELSE 0 END) AS CPSupp
FROM tasks;
The ELSE portion is not needed as NULL results when a value does not match any criteria in a CASE statement, and NULL is ignored on aggregation, but some prefer to include it.
You can use CASE to conditional check for the value of code.
SELECT SUM(CP) AS CPTotal,
SUM(CASE WHEN Code = 'P' THEN CP END) AS CPProd,
SUM(CASE WHEN Code = 'S' THEN CP END) AS CPSupp
FROM tasks