I would like to query with the result of
How can I achieve it?
This is how you can select the values without grouping:
select name, case when ket = 'dalam proses' then 1 else 0 end dalam_proses, 'selesai' then 1 else 0 end selesai
from yourtable;
Let's use it as a subquery:
select name, sum(dalam_proses) as dalam_proses, sum(selesai) as selesai
from (
select name, case when ket = 'dalam proses' then 1 else 0 end dalam_proses, 'selesai' then 1 else 0 end selesai
from yourtable
} t
group by name;
You can do it by using subquery, like this :
select
name,
distinct(name) ,
count(select ket from my_table where ket = 'dalam proses' ) as 'dalam proses',
count(select ket from my_table where ket = 'selesai' ) as 'selesai'
from my_table
group by name;
Use case expressions to do conditional aggregation:
select name,
sum(case when ket = 'dalam proses' then 1 else 0 end) as dalam_proses,
sum(case when ket = 'selesai' then 1 else 0 end) as selesai
from tablename
group by name
Related
Suppose I have a Yes/No data column. Then I can count the observations in each outcome category by
Select MyVar, count(MyVar) from MyTable
group by MyVaR;
The output is someting like:
MyVar count(MyVar)
Yes 10
No 5
How can I add another element to this outputtable which is the fraction (Yes/No) so my final table is something like:
MyVar count(MyVar)
Yes 10
No 5
Fraction 2
Is there a simple solution to this?
You can use group by grouping sets() for that:
Select
case
when grouping(MyVar)=0 then MyVar
else 'Fraction'
end grouping_value,
case
when grouping(MyVar)=0 then count(MyVar)
else count(decode(MyVar,'Yes',1)) / count(decode(MyVar,'No',1))
end c
from MyTable
group by grouping sets(MyVaR,())
Full test case with sample data:
with MyTable(MyVar) as (
select *
from table(sys.odcivarchar2list(
'Yes','Yes','Yes','Yes','Yes','Yes','Yes','Yes','Yes','Yes',
'No','No','No','No','No'
))
)
Select
case
when grouping(MyVar)=0 then MyVar
else 'Fraction'
end grouping_value,
case
when grouping(MyVar)=0 then count(MyVar)
else count(decode(MyVar,'Yes',1)) / count(decode(MyVar,'No',1))
end val
from MyTable
group by grouping sets(MyVaR,())
;
Results:
GROUPING_VALUE VAL
--------------- ----------
No 5
Yes 10
Fraction 2
This is simplest -- in this format -- using union all:
with t as (
Select MyVar, count(MyVar) as cnt
from MyTable
group by MyVaR
)
select MyVar
from t
union all
select 'Ratio', sum(case when MyVar = 'Yes' then cnt else 0 end) / sum(case when MyVar = 'No' then cnt else 0 end)
from t;
That said, I would just use conditional aggregation and put all values in a single row:
select sum(case when MyVar = 'Yes' then cnt else 0 end) as yes,
sum(case when MyVar = 'Yes' then cnt else 0 end) as no,
sum(case when MyVar = 'Yes' then cnt else 0 end) / sum(case when MyVar = 'No' then cnt else 0 end) as ratio
from t;
I have two queries and I want to get the maximum value of the two of them.
MAX((SELECT COUNT(p.[ItemID]) FROM [dbo].[Table] p WHERE HasHuman=0),
(SELECT COUNT(p.[ItemID]) FROM [dbo].[Table] p WHERE HasHuman=1))
You can calculate both result in a single query and then apply TOP:
select top 1
HasHuman,
COUNT(p.[ItemID]) as cnt
from [dbo].[Table]
group by HasHuman
order by cnt desc
You could even do this in a single query:
SELECT
CASE WHEN SUM(CASE WHEN HasHuman=0 THEN 1 ELSE 0 END) >
SUM(CASE WHEN HasHuman=1 THEN 1 ELSE 0 END)
THEN SUM(CASE WHEN HasHuman=0 THEN 1 ELSE 0 END)
ELSE SUM(CASE WHEN HasHuman=1 THEN 1 ELSE 0 END) END
FROM [dbo].[Table]
WHERE ItemID IS NOT NULL -- you were not counting NULLs
SELECT MAX(RC)
FROM (SELECT COUNT(p.ItemID) AS RC FROM dbo.[Table]
WHERE HasHuman=0
UNION
SELECT COUNT(p.ItemID) AS RC FROM dbo.[Table]
WHERE HasHuman=1
) A
I have problem with SQL query.
I have names in column Name in Table_Name, for example:
'Mila', 'Adrianna' 'Emma', 'Edward', 'Adam', 'Piter'
I would like to count how many names contain the letter 'A' and how many contain the letter 'E'.
The output should be:
letter_A ( 5 )| letter_E (3)
I tried to do this:
SELECT Name,
letter_A = CHARINDEX('A', Name),
letter_E = CHARINDEX('E', Name)
FROM Table_Name
GROUP BY Name
HAVING ( CHARINDEX('A', Nazwisko) != 0
OR ( CHARINDEX('E', Nazwisko) ) != 0 )
My query only shows if 'A' or 'E' is in Name :/
Can anyone help? :)
You can use conditional aggregation:
select sum(case when Nazwisko like '%A%' then 1 else 0 end) as A_cnt,
sum(case when Nazwisko like '%E%' then 1 else 0 end) as E_cnt
from table_name
where Nazwisko like '%A%' or Nazwisko like '%E%';
You just need to aggregate if you only need the counts.
select
sum(case when charindex('a',name) <> 0 then 1 else 0 end) as a_count
,sum(case when charindex('e',name) <> 0 then 1 else 0 end) as e_count
from table_name
;WITH CTE
AS (SELECT NAME
FROM (VALUES ('MILA'),
('ADRIANNA'),
('EMMA'),
('EDWARD'),
('ADAM'),
('PITER'))V(NAME)),
CTE_NAME
AS (SELECT COUNT(NAME_A) NAME_A,
COUNT(NAME_E) NAME_E
FROM (SELECT CASE
WHEN NAME LIKE '%A%' THEN NAME
END NAME_A,
CASE
WHEN NAME LIKE '%E%' THEN NAME
END NAME_E
FROM CTE
GROUP BY NAME)A)
SELECT *
FROM CTE_NAME
how i can result like this ?
in my mind i can query = >
select id, count(no1, no2, no3) where no1='B',no2='B',no3='B'
thank's very much.
Use Case Statement
select id,
case when no1='B' then 1 else 0 END +
case when no2='B' then 1 else 0 END +
case when no3='B' then 1 else 0 END As Count_All
From yourtable
Use Case When statements with Count aggregate. Lastly, Group them with id:
Select id,
count(case when no1='B' then 1 END) +
count(case when no2='B' then 1 END) +
count(case when no3='B' then 1 END) AS count_all
From yourtable
Group by id
I have returned rows which look like this:
2 - Eggs
3 - Bacon
4 - Bacon Smoked
I would like to group by '%Bacon%' so that my count is 2.
How can i do this is SQL?
I should see results like this:
Eggs - 1
Bacon - 2
How about the following (Demo):
SELECT 'Eggs' AS Category, COUNT(*) AS MyCount
FROM MyTable
WHERE MyField LIKE '%Eggs%'
UNION ALL
SELECT 'Bacon' AS Category, COUNT(*) AS MyCount
FROM MyTable
WHERE MyField LIKE '%Bacon%'
Not tested, but I think it should work
SELECT COUNT(*) as QTY, RS.FOOD_TYPE
FROM
(SELECT
Case patIndex ('%[ /-]%', LTrim (FOOD_TYPE))
When 0 Then LTrim (FOOD_TYPE)
Else substring (LTrim (FOOD_TYPE), 1, patIndex ('%[ /-]%', LTrim (FOOD_TYPE)) - 1)
End FOOD_TYPE
FROM YOUR_TABLE) RS
GROUP BY RS.FOOD_TYPE
Other solution:
SELECT
Eggs = SUM(CASE WHEN FoodColumn LIKE '%Eggs%' THEN 1 ELSE 0 END),
Bacon = SUM(CASE WHEN FoodColumn LIKE '%Bacon%' THEN 1 ELSE 0 END)
FROM Test
You can see demo here.
If you need to separate the result into two separate rows
SELECT *
FROM
(
SELECT
Eggs = SUM(CASE WHEN FoodColumn LIKE '%Eggs%' THEN 1 ELSE 0 END),
Bacon = SUM(CASE WHEN FoodColumn LIKE '%Bacon%' THEN 1 ELSE 0 END)
FROM Test
) AS Test
UNPIVOT
(
Quantity FOR Foods IN (Eggs, Bacon)
) AS Result
You can see demo here.
This is a very specific case.. can you provide more Data? Does this help in anyway?
with list (item) as (
select
item
from (
values
('Eggs'),
('Bacon'),
('Bacon Smoked')) list (item)
)
select
LEFT(item,
(case
when CHARINDEX(' ',item,1) = 0
then LEN(item)
else CHARINDEX(' ',item,1) end)
) filtered,
COUNT(*)
from list
group by
LEFT(item,
(case
when CHARINDEX(' ',item,1) = 0
then LEN(item)
else CHARINDEX(' ',item,1) end))
create table MyTable
(id int, FieldName varchar(50) )
insert into MyTable values (1, 'Eggs')
insert into MyTable values (2, 'Bacon')
insert into MyTable values (3, 'Bacon Smoked')
select count(FieldName), FieldName from (
select
case
when charindex('eggs', FieldName) > 0 then 'eggs'
when charindex('bacon', FieldName) > 0 then 'bacon'
end as FieldName
from MyTable) as myMyTablealias
group by FieldName
check it out