How to COUNT a value in a column? - sql

I have a column where I flag two values (0 and 1). How can I select all from the table but only counting the value 1 from the flag column?
SQL Server

You need a COUNT with CASE expression, like this:
COUNT(CASE WHEN flag = 1 then 1 END)

If you want to return all of your values and also have a total count (using sort of logic) you could use a window function aggregation using over like so:
select
t.*
, count(case when flag = 1 then 1 end) over(partition by 1) as flag_1_count
from t

Err, are you asking how to perform a simple select statement or am I missing something...
Like this?
SELECT *
FROM [YourTableName]
WHERE FlagColumnName = 1

This should do it
Select count(0)
from YourTableName
where ValueField = 1

I think, you mean something like this:
SELECT COUNT(*) FROM [YOUR_TABLE] WHERE [FLAGCOLUMN] = 1;

Not quite sure I understand the question, but wouldn't it be something like:
SELECT * FROM your_table WHERE flag = 1

Related

sql group by satisfying multiple conditions within the group

I have a table like below:
I want to select the group which has RELB_CD =9093 and INFO_SRC_CD with 7784. Both conditions should be present in the group. In the table below my output should be the group with id=139993690.
You can use aggregation with having:
select id
from t
group by id
having sum(case when relb_cd = 9093 then 1 else 0 end) > 0 and
sum(case when info_src_cde = 7784 then 1 else 0 end) > 0
hey use this code hope this will help you.
you have to ignore the date column because that one is not allowing to group
select id,fisc_ind, sum(sls_amt),relb_cd,info_scop,info_src_cd from yourtable group by id,fisc_ind,relb_cd,info_scop,info_src_cd
Another working answer. If your data are large, you could compare both GL's and this working answer and see which runs faster for you. I honestly don't know which is faster. This was slightly faster with a very short set of data.
select id
from table1
where relb_cd = 9093
intersect
select id
from table1
where info_src_cd = 7784

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;

SQL, return select results with different where clauses

I have table whose column is just the length of a session and I would like to return the number of session that have zero length and the number of sessions that have length greater than zero.
I can do that with two separate commands
select count(session_length) from my_table where session_length=0
select count(session_length) from my_table where session_length>0
But I would like to see the results combined in one table
You can do it with one query using conditional aggregation.
select
count(case when session_length = 0 then 1 end),
count(case when session_length > 0 then 1 end)
from my_table
select 1 as QryNo, count(session_length) as SessLen
from my_table
where session_length=0
union
select 2 as QryNo, count(session_length) as SessLen
from my_table
where session_length>0
or
select
case
when session_length = 0 then 1
else 2
end as QryNo,
count(session_length) as SessLen
from my_table
This may be too simple so apologies if I have misread your query but Can you use
select count(session_length) from my_table where session_length >= 0
Again, Apologies if this is not what you're looking for.

SQL : select a comparison ? eg, a boolean result?

I was wondering if something like this was possible in SQL :
select (
(select count(*) from T) = (select count(*) from T t where t.something = thing)
)
This is probably very far from the actual SQL if it is possible, I don't write database requests so often.
How could I get the result of my comparison with a single request ? Basically, if I had no time, I would just make two requests and compare the results in Java (boooooo !! I know).
Although your query should work, the following is probably faster because only a single query is needed
select total_count = thing_count
from (
select count(*) as total_count,
sum(case when something = 42 then 1 end) as thing_count
from t
) t
The above is ANSI SQL and should work in any DBMS supporting a real boolean type. In Oracle you would need to use an expression in the outer select:
select case when total_count = thing_count then 1 else 0 end
from (
select count(*) as total_count,
sum(case when something = 42 then 1 end) as thing_count
from t
) t
I would write your query like this:
SELECT (CASE WHEN (select count(*) from T) = (select count(*) from T t where t.something = thing) THEN 1 ELSE 0 END)
However, if the first T is the same as the second T then what you actually want to check is if there are any records where t.something <> thing .. right ?
In that case you could simply do :
SELECT (CASE WHEN EXISTS (select * from T t where t.something != thing) THEN 1 ELSE 0 END)

How to return multiple values using case statement in oracle

I want to return multiple values from a query in oracle. For ex:
select count(*)
from tablename a
where asofdate='10-nov-2009'
and a.FILENAME in (case
when 1 = 1 then (select distinct filename from tablename
where asofdate='10-nov-2009' and isin is null)
else null
end);
I am getting error: ora 01427 single row subquery returns more than one row
Please advice.
Thanks, Deepak
A CASE statement cannot return more than one value, it is a function working on one value.
It is not required for your statement, this statement should work:
select count(*)
from tablename a
where asofdate='10-nov-2009'
and a.FILENAME in (select distinct filename
from tablename
where asofdate='10-nov-2009'
and isin is null);
Maybe you have another usage scenario in mind? Something like this:
Select *
From aTable
Where in CASE
WHEN Then
WHEN Then
ELSE END
Then using CASE may not be the right scenario. Maybe this helps you in the right direction:
Select *
From aTable
Where <Case1> and column1 in <Subselect1>
Or <Case2> and column1 in <Subselect2>
OR Not (<Case1> Or <Case2>) and column1 in <Subselect3>
But this will probably be quite some work for the optimizer ...
The distinct in your Case statement is attempting to return multiple values when only one is allowed, and your SELECT statement will only return one value in one row currently. If you're trying to get the count of each filename, do
SELECT FileName, Count(*)
FROM tablename
WHERE asofdate='10-nov-2009' and isin is null
GROUP BY FileName
Run this query:
select distinct filename from tablename
where asofdate='10-nov-2009' and isin is null
You'll see that it returns more than a single row which causes the ORA-01427.
For all I can tell, you're looking for something like:
select a.filename, count(*)
from tablename a
where a.asofdate = '10-nov-2009'
and exists (
select *
from tablename b
where b.isin is null
and a.asofdate = '10-nov-2009'
and a.filename = b.filename
)
group by a.filename
This would find the count of filenames for a day, for which there exists at least one row where isin is null.
If you edit your question and add an explanation of what you're looking for, you might get better answers.