sql group by satisfying multiple conditions within the group - sql

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

Related

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;

How to COUNT a value in a column?

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

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.

Subquery returned more than 1 value - error

I'm trying to make this query work but I can't understand what I am doing wrong. I saw other question with same problem but I did not understand anyway.
SELECT DoctorsVisits.VisitDate,
(SELECT COUNT(1) AS Total
FROM DoctorsVisits
WHERE (DoctorsVisits.PeriodOfVisitId=2)
GROUP BY DoctorsVisits.VisitDate) AS Manhas
FROM DoctorsVisits;
Are you sure you don't simply want a plain group by without sub-query?
SELECT DoctorsVisits.VisitDate, count(*) as Manhas
FROM DoctorsVisits
group by DoctorsVisits.VisitDate
Or perhaps:
SELECT DoctorsVisits.VisitDate,
sum(case when DoctorsVisits.PeriodOfVisitId=2 then 1 else 0 end) as Manhas
FROM DoctorsVisits
group by DoctorsVisits.VisitDate
You can just do the following. You're using an unnecessary sub-query for your COUNT.
SELECT DoctorsVisits.VisitDate, COUNT(*) As Total
FROM DoctorsVisits
WHERE DoctorsVisits.PeriodOfVisitId=2
GROUP BY DoctorsVisits.VisitDate

I want a case statetement that count more than 1 as 1

Please help solve the below query.
The column in question has Y and N and I want the N to show zero and the Y to show 1.
I want it to aggregate the no of times visited for each machine and if >= 1 to show 1. Client requirement is whether machine has been visited regardless of the number of times.
Select MachineNo,
[Date_of_Visit],
Month([Date_of_Visit])[Month],
Year([Date_of_Visit])[Year],
sum(case when [Visited] = 'Y' then 1 else 0 end)[No of Visits]
FROM [MachineVisit]
Group by [Date_of_Visit],
[MachineNo]
Use Max instead of Sum
Max(case when [Visited] = 'Y' then 1 else 0 end)[Visits]
This is a little confusing, the way it is written, but I am assuming you want something like:
select sum(case when [visited] >1 then 1 else null end) visists
from visit_table
where visit_date = '2015-01-30'
This is assuming that you have a table that counts the visits for a single day for 1 IP address as single entry.
If you have a table that has an entry for every single page visit, then you would probably need to do:
select count(distinct ip_address)
from (
select ip_address
from visit_table
where visit_date = '2015-01-30'
group by ip_address
having count(1) >1
) x
EDIT:
Well then the simplist way should be:
select count(1)
from visit_table
where [visited]='Y'
and visit_date = '2015-01-30'
;
That should work...
Although I don't have the table in front of me - so if this doesn't work, please post the fully query.