Count with different group by used in a select statement - sql

I have a query as shown below
select sum(amount)/count(id) from tabel1 where name ='sam';
I have a table like this
name id transaction_type_id transaction_type
sam 1 23 direct
sam 1 56 direct
sam 1 21 indirect
sam 1 34 indirect
when I do count(id) am getting answer as '4' but i want it to be '2' because it have 2 transaction_type as 'direct and indirect'.please help me .
Thanks

Try this:
select sum(amount)/count(distinct transaction_type)
from tabel1
where name ='sam'
This will return a count of 2, since there are only 2 distinct transaction_type values.

Try to use:
select sum(amount)/count(distinct transaction_type) from tabel1 where name ='sam';

Try this:
SELECT
SUM(amount) / COUNT(id)
FROM tabel1
WHERE
name = 'sam'
GROUP BY
transaction_type;

Related

condense results into 1 result under a certain limit

So, I have a result set that has a percentage of the total in 1 column and a name in the other column.
So for example -
Name | Percentage
Jill | 12
Sam | 24
Steve| 2
Jeff | 3
Elvis | 59
I am trying to condense any result thats less than 15% into on row,
so my result would be
Name | Percentage
Everyone else| 17
Sam | 24
Elvis | 59
Thanks!
You can use APPLY :
SELECT tt.Name, SUM([Percentage]) AS [Percentage]
FROM table t CROSS APPLY
( VALUES (CASE WHEN [Percentage] < 15
THEN 'Everyone else'
ELSE Name
END)
) tt(Name)
GROUP BY tt.Name;
Use a case expression in a derived table (the sub-query) to put all less than 15% people together. GROUP BY its result:
select name, sum(Percentage)
from
(
select case when Percentage > 15 then name
else 'Everyone else'
end as name,
Percentage
from tablename
) dt
group by name
For such a simple condition, you can just use CASE expressions:
select (case when percentage < 15 then 'Everyone else'
else name
end) as name,
sum(percentage) as percentage
from t
group by (case when percentage < 15 then 'Everyone else'
else name
end)
order by sum(percentage) asc;

SQL How to count one column when another column is distinct

I have a table that looks something like this:
fin_aid_status | student_number
---------------|---------------
YES | 111222
YES | 111222
| 111333
YES | 111444
I want to count the number of fin_aid_status but not double count rows where student_number is duplicated. So the result I would like from this table would be 2. Not 3 because 111222 is in the table twice. There are many other columns in the table as well though so just looking for unique values in the table will not work.
EDIT: This is Oracle.
For example I am using code like this already:
select count(*), count(distinct student_number) from table
So for third column I would want to count the number on financial aid with unique student numbers.
So my expected output would be:
count(*) | count(distinct student_number) | count_fin_aid
4 | 3 | 2
Use a case statement to evaluate the student_number when the fin_aid_status is not null; then count the distinct values.
SELECT count(Distinct case when fin_aid_status is not null
then student_number end) as Distinct_Student
FROM tbl;
Result using sample data: 2
Given Oracle:
With cte (fin_aid_status, student_number) as (
SELECT 'YES' , 111222 from dual union all
SELECT 'YES' , 111222 from dual union all
SELECT '' , 111333 from dual union all
SELECT 'YES' , 111444 from dual )
SELECT count(Distinct case when fin_aid_status is not null
then student_number end) as DistinctStudentCnt
FROM cte;
If you are using MySQL you can write something as follows, if all you want is count
SELECT count(DISTINCT student_number) FROM your_table WHERE fin_aid_status = 'YES';
I'm assuming here, add some expected results but:
SELECT fin_aid_status,
COUNT(DISTINCT student_number)
FROM tablename
GROUP BY fin_aid_status;
Will give you count of distinct values in the student_number column for each value in the fin_aid_status column

Grouping of values in sql

My dataset looks like this:
Id MaxSpeed Distance
1 112 33
1 89 56
2 100 34
3 125 10
For each Id, I need to set up the count as 1.
Output has to be
Id count
1 1
2 1
3 1
I tried with group by on Id, it does not fetch me this result.
Any help would be really appreciated !!
Perhaps you are overthinking it.
Select Distinct ID,1 as Count From YourTable
Does the count always need to be 1? If so, could this query work?
SELECT DISTINCT ID, 1
FROM your_table;
Try following sql code:
Select distinct Id,1 as count From Table_Name;

How do I use group by on one column which is having many entries?

I want to use group by on one column which is having many entries
table_a
Name price
AAA 12
BBB 13
AAA 0
CCC 24
AAA 0
DDD 0
Now I want to find out Name which is having Price as 0
but as I'm having entries AAA 3 times I can't directly write simple sql with condition
NOT Equal to 0
Please help me I want to print result for above table_a should be
only D as it is having 0 as price.
I assumed price cannot be lower than zero:
SELECT Name
FROM TableName
GROUP BY Name
HAVING SUM(price) = 0
Or with additional condition of only one price, which is zero
SELECT Name
FROM TableName
GROUP BY Name
HAVING COUNT(*) = 1 AND SUM(price) = 0
You can achive this by using aggregate functions GROUP BY and HAVING clause as:
SQLFiddle Demo
SELECT name,SUM(price) as price
FROM table
GROUP BY name
HAVING SUM(price) = 0
select name,max(price) as price from table group by name having max(price) =0
select name,max(price)
group by name
having max(price)=0

SQL: Compare rows in a same table

I'm trying to compare rows in a single table
and figure out if "addr" and "zip" under the same id are same or different.
id | addr | zip
------+----------+----------
1 | 123 | 0000
1 | 123 | 0000
1 | 123 | 0001
2 | 222 | 1000
2 | 221 | 1000
So the result should say id 1 has valid addr and invalid zip
id 2 has invalid addr and valid zip.
Any hint will be appreciated! Thank you!!
The query...
SELECT id, COUNT(DISTINCT addr), COUNT(DISTINCT zip)
FROM YOUR_TABLE
GROUP BY id
...should give the following result on your example data...
1, 1, 2
2, 2, 1
The numbers in bold greater than 1 indicate "invalid" items.
If you want to actually filter on this, you can use HAVING clause, for example:
SELECT id, COUNT(DISTINCT addr) ADDR_COUNT, COUNT(DISTINCT zip) ZIP_COUNT
FROM YOUR_TABLE
GROUP BY id
HAVING ADDR_COUNT > 1 OR ZIP_COUNT > 1
May I suggest that if you don't actually want this kind of "mismatched" data in your database, redesign your data model so duplicates cannot happen in the first place. No duplicates, no mismatches!
Group by id. Select id, COUNT(DISTINCT addr) and COUNT(DISTINCT zip) columns.
Filter the rows where the number of distinct address or zips > 1.
This will give you the ids with inconsistent duplicate data.
Example:
SELECT id, COUNT(DISTINCT addr) nAddr, COUNT(DISTINCT zip) nZip
FROM [mytable]
GROUP BY id
HAVING nAddr > 1 OR nZip > 1
Cheers,
SELECT id
, CASE s.addrcount
WHEN 1 THEN 'valid'
ELSE 'invalid' END as addrok
, CASE s.zipcount
WHEN 1 THEN 'valid'
ELSE 'invalid' END as zipok
FROM
(
SELECT id
, count(distinct addr) as addrcount
, count(distinct zip) as zipcount
FROM table1
GROUP BY id
) as s