how to find the highest number of items in a table - sql

Im using sql server 2012,I have entries created on different dates,I wish to find on which date i have the maximum number of entries..Does using max() will help me?

Use group by clause , group your results by date and then select max.

select top 1 entry_date from entries group by entry_date order by count(*) desc
(SQL Fiddle)

Related

how to get latest date column records when result should be filtered with unique column name in sql?

I have table as below:
I want write a sql query to get output as below:
the query should select all the records from the table but, when multiple records have same Id column value then it should take only one record having latest Date.
E.g., Here Rudolf id 1211 is present three times in input---in output only one Rudolf record having date 06-12-2010 is selected. same thing with James.
I tried to write a query but it was not succssful. So, please help me to form a query string in sql.
Thanks in advance
You can partition your data over Date Desc and get the first row of each partition
SELECT A.Id, A.Name, A.Place, A.Date FROM (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Date DESC) AS rn
FROM [Table]
) A WHERE A.rn = 1
you can use WITH TIES
select top 1 PERCENT WITH TIES * from t
order by (row_number() over(partition by id order by date desc))
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=280b7412b5c0c04c208f2914b44c7ce3
As i can see from your example, duplicate rows differ only in Date. If it's a case, then simple GROUP BY with MAX aggregate function will do the job for you.
SELECT Id, Name, Place, MAX(Date)
FROM [TABLE_NAME]
GROUP BY Id, Name, Place
Here is working example: http://sqlfiddle.com/#!18/7025e/2

how to sum result of count in sql query from one table and one column

I need to sum the result of count of a column in one query.
Is it possible to have like this query?
SELECT sum(count(pro_id)) from jalasat group by pro_id
You have not mentioned which SQL database you are using so you may modify this slightly to fit it to what you are using:
SELECT SUM(cnt) FROM (SELECT COUNT(pro_id) as cnt
FROM jalasat
GROUP BY continent) as t1

query for sql to retrive id deistic value count

I want to resolve below issue but not able to figure out how I can do that.
You can use the distinct keyword to get the number of different values in a count expression:
SELECT sensor_id, COUNT(DISTINCT event_type) types
FROM events
GROUP BY sensor_id
ORDER BY sensor_id ASC

Get latest updated records

I have an "Observation" table in SQL Server 2008. This table has a locationId column for a bunch of geographic locations, a few columns for observation details and a column for latest updated date.
Every week, a new observation record for each location is appended. So a location has many occurrences in the table.
What I want to achieve is to be able to get the most recent observation record for each location.
Can anyone help with any idea?
select * from observation where date=(select max(date) from observation)
or
select top 1 * from observation order by date desc
select a.* from observations a inner join
(select locationid ,max(updateddate) dates from observations
group by locationid) b
on a.locationid=b.locationid
and a.updateddate=b.dates
Run query
select * from Observation
group by location
order by viewdate desc
Please also give the full details about table and what you want to get.
EDIT : Backtick removed.
Add a column to your table with a datatype of [timestamp]
execute the following code:
select top(10) * from yourtablename order by columanname desc
Note:columanname should be the column you add with a timestamp type
Use the Getdate function as I used as below.
select * from TBL_MP_QC_CustomerWiseCOA_Master order by getdate() desc

SQL - How do I return the TOP 5 highest earning years?

I have a paystub table in which the payroll records are recorded.
The fields related to this query are the YearToDateGrossPay and the year.
I need to search through all the paystub records and select the top 5 highest paid years.
The last paystub of each year would tell me the final YTD amounts for that year and then I could select the top 5 of that subquery, but I'm not too sure how to do that.
Any help would be appreciated.
Thanks
If you already have the subquery which selects the final YTD amounts, you can finish it off by
SELECT * FROM final_ytd_amounts ORDER BY ytd_amount DESC LIMIT 5
The ORDER BY ytd_amount DESC sorts the table by ytd_amount in descending order
The LIMIT 5 selects the top 5 rows only and chops everything else off
If you are using SQL Server, the above won't work. In general, you should specify a particular database when asking a query. In SQL Server, you can do:
SELECT top 5 *
FROM final_ytd_amounts
ORDER BY ytd_amount DESC
For completeness, the following is how you do this in Oracle:
SELECT *
FROM final_ytd_amounts
where rownum <= 5
ORDER BY ytd_amount DESC
Standard SQL:
SELECT *
FROM final_ytd_amounts
WHERE ROWNUM <= 5
ORDER BY ytd_amount DESC