Getting Number of records in oracle - sql

Am trying to fetch the number of records in the table using Count(*) along with my query condition
Sample Table is
Table: STUD_NAME
Id Name
1 Steven
2 smith
2 Ben
1 Willy
My query is
select std.name
from STUD_Name where id='2'
for this it will display the output as "Smith" and "Ben", along with i need the total number of records in the STUD_NAME table.
By right it should display the total records as "4", please help me out to solve this issue and how to form the query in this case

SELECT name,
cnt as total_count
FROM (
SELECT id
name,
count(*) over () as cnt
FROM stud_name
) t
WHERE id = 2
Assuming that id is a numeric column the single quotes around the value 2 are not needed (and are actually harmful due to the implicit data type conversion that happens in the background)

What about:
select
std.name
,(select count(1) from STUD_Name) nrofstds
from STUD_Name std where std.id='2'

select STUD_NAME.name, CNT.count
from STUD_NAME
, (select count(*) COUNT from STUD_NAME) CNT
where id='2'

Related

How to query how many different IDs use the same column value?

I have this homework assignment where I'm attempting to query a table to find the id numbers that are all using the same column value, let's say last name in this case. I'd like to find the ids that use the same last name more than once, and have a column that tells me the total number of unique IDs that used that same last name.
SELECT id, COUNT(*) as ID_count
FROM [table]
WHERE l_name IN
(
SELECT l_name
FROM [table]
GROUP BY l_name HAVING COUNT(*)>1
)
GROUP BY id;
This is what I have so far. It grants me the ID number, but the count(*) is not what I'm going for. What I'm instead trying to get is how many unique IDs have "Smith" as their last name, instead of all the occurrences of one specific ID that has used "Smith".
I've tried different things but I feel like I'm at a roadblock. Any hints or tips are nice; I don't need this problem solved 100%, but I feel as if I can't past the idea of using count(*).
Thanks all.
It sounds like you were already there WITHIN the inner query. Just add the count to it for the output.
SELECT
t1.id,
t1.l_name,
max( PQ.UniqCount ) UniqCount,
COUNT(*) as countForThisSingleID
FROM
[table] t1
JOIN
( SELECT
t.l_name,
COUNT( DISTINCT t.ID ) as UniqCount
FROM
[table] t
GROUP BY
t.l_name
HAVING
COUNT( DISTINCT t.ID ) > 1 ) PQ
on t1.l_name = PQ.l_name
group by
t1.id,
t1.l_name
order by
t1.l_name,
t1.id
So by doing a COUNT( DISTINCT ) on the inner pre-query (alias PQ), for each L_Name, you are getting a count of distinct IDs. I dont know if your [table] has multiple entries for the same ID in it or not, so applying the DISTINCT. Same for the HAVING clause. But at least now the inner pre-query gets the overall distinct counts for a given L_Name value.
Now, doing a JOIN to the outer table on that L_Name will get the corresponding count in the result query, along with showing the l_name that it qualified against. So if you have a table with 18 DISTINCT ID instances of John, 37 of Karen, 11 of Mike, your inner query will get those. Now joined to the outer, you will get the output of EACH instance of John and their corresponding IDs, then all Karen instance and Mike instances.
The count for the outer query is getting the count of the one ID (and name) times that it appears in the table. So if the table had ID = 5, L_Name = John and ID 5 appeared 3 times in the table, the output of his record might look like
ID L_Name countForThisSingleID UniqCount
5 John 3 18
72 John 8 18
127 John 2 18
etc...
Similarly the output would include all Karen's and Mike's within the table (and any others that qualify).
Again, without knowing if your [table] is a unique instance per ID such as a master customer lookup table where it would only appear once vs an order table where the ID may appear more than once for a single person's ID, not positive what your final answer is looking for.
But I think I have given you a bunch to chew on and run with.

How can i do it in single SQL?

I have this Table like this
ID | NAME | AGE | ADDRESS | SALARY
and an ID (2)
AGE of ID 2 is 25,
Now i need to count total number of record with AGE 25. How can i do it in single SQL Query? is there any why?
i currently doing it in 2 query. in one i return AGE with ID.
select AGE from table_name where ID = 2
it return AGE 25
and 2nd query i count number of record with this AGE.
select COUNT(*) from table_name where age =25
i want to do it in one SQL Query. is there any way ?
use SQL COUNT function
select COUNT(*) from mytable where age =25
You want to count the number of rows with the same age as the specified ID?
You can use a windowed count
select Agecount
from (
select *, Count(*) over (partition by Age) Agecount
from YourTable
)t
where id=?
Use Group by with subquery
SELECT COUNT(*) AS ageCount
FROM table_name
WHERE age in (SELECT age FROM table_name WHERE ID = 2)
GROUP BY age
For Questions like this the best way is use multiple conditions using Where keyword with And-Or Operators.
This query should work for your requirement without complicating anything.
select count(*) from table_name where ID=2 and age=25;

What exactly does SELECT DISTINCT(COUNT(*)) do?

I used the following query and it returned what I wanted it to return, but I'm having a tough time wrapping my head around what the query is doing.
Query is nothing fancier than what's in the title: select distinct(count(*)) from table1
Distinct is not required in your SQL ,as you are going to get only result, count(*) without group by clause returns, count of all rows within that table.
Hence try this :
select count(*) from table1
Distinct is used for finding distinct values from a group of values:
say you have table1 , with column1 as :
Column1
----------
a
a
b
b
a
c
following sqls are run you will get output as :
1) select count(*) from table1
output :6
2) select distinct(count(*)) from table1
output :6
3) select count( distinct column1) from table1
output :3
Usually distinct is used inside count preferably with a particular column .
select count( distinct column_name_n ) from table1
The distinct is redundant... Select Count(*) with only one table can only generate one value, so distinct (which would eliminate duplicates) is irelelvant.
If you had multiple outputs, (if for example you were grouping on something) then it would cause the query to only display one output row for every distinct value of count(*) that would other wise be generated...
if, for example, you had
name
Bob
Bob
Bob
Bob
Mary
Mary
Mary
Mary
Dave
Dave
Al
George
then
select count(*)
From table
group By name
would result in
4
4
2
1
1
but
select distinct count(*)
From table
group By name
would result in
4
2
1

SQL Separating Distinct Values using single column

Does anyone happen to know a way of basically taking the 'Distinct' command but only using it on a single column. For lack of example, something similar to this:
Select (Distinct ID), Name, Term from Table
So it would get rid of row with duplicate ID's but still use the other column information. I would use distinct on the full query but the rows are all different due to certain columns data set. And I would need to output only the top most term between the two duplicates:
ID Name Term
1 Suzy A
1 Suzy B
2 John A
2 John B
3 Pete A
4 Carl A
5 Sally B
Any suggestions would be helpful.
select t.Id, t.Name, t.Term
from (select distinct ID from Table order by id, term) t
You can use row number for this
Select ID, Name, Term from(
Select ID, Name, Term, ROW_NUMBER ( )
OVER ( PARTITION BY ID order by Name) as rn from Table
Where rn = 1)
as tbl
Order by determines the order from which the first row will be picked.

Distinct SQL Query

I have a SQL Server 2008 database with the following information in a table:
ID Name
-- ----
1 John
2 Jill
3 John
4 Phil
5 Matt
6 Jill
I want to display the unique names in a drop down list. Because of this, I need just one of the IDs associated with the unique name. I know it's dirty. I didn't create this mess. I just need the unique names with one of the ids. How do I write a query that will do that? I know that the following won't work because of the ID field.
SELECT DISTINCT
[ID], [Name]
FROM
MyTable
SELECT MIN(ID) AS ID, [Name]
FROM MyTable
GROUP BY [Name]
This will return the first (i.e. MINimum) ID for each distinct Name
You could also do it with rank over function
SELECT
Id,
Name
FROM
(
SELECT
Id,
[Name],
RANK() OVER (PARTITION BY [Name] Order By Id) As Idx
FROM Test
) A
WHERE Idx = 1
To get understanding about rank over function read this:
http://msdn.microsoft.com/en-us/library/ms176102.aspx