-2

I want to get numbers of rows in a table according to certain criteria.

Please see the below table:-

SQL Table Image

Herein I want to get numbers of rows according to Column StationTo. I want to get numbers of rows of each StationTo entries.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    Hint : `select StationTo, count(*) ... group by StationTo` – Barbaros Özhan Nov 20 '18 at 19:13
  • 1
    Possible duplicate of [SQL: How to get the count of each distinct value in a column?](https://stackoverflow.com/questions/7053902/sql-how-to-get-the-count-of-each-distinct-value-in-a-column) – JNevill Nov 20 '18 at 19:14
  • @BarbarosÖzhan Inserting StationTo data should give me number of rows only. – Mohit Kumar Nov 20 '18 at 19:22
  • @JNevill Same as above – Mohit Kumar Nov 20 '18 at 19:22
  • I'm not sure what you are saying here "Inserting StationTo Data..." You already have data in this table and you want a count of rows for each distinct StationTo so `SELECT stationto, count(*) FROM yourtable GROUP BY stationto;` is the query you want to run. If that doesn't give the results you want, then you'll need to explain your question better. Please edit your question with desired results from the sample data you shared and perhaps we can figure it out from there. – JNevill Nov 20 '18 at 19:28
  • 1
    `select count(*) from tablename where StationTo = 'P11004400000'` – jarlh Nov 20 '18 at 19:29
  • @JNevill I meant i want to find out number of rows by apply where condition like StationTo= 'datavalueofthecolum' Just like jarlh did. But jarlh what if i want to get count of entries of a particular date. Present condition should also stay. – Mohit Kumar Nov 20 '18 at 20:38
  • Then add that as a condition `where StationTo = 'P1100440000' AND TDate = 'somedateyouwant'`. This is all very very very basic SQL. I don't think a stackoverflow question is the right place for this as it's covered in neaarly every tutorial and has probably been answered in some form on this site hundreds of times. – JNevill Nov 20 '18 at 20:41
  • The TDate Column has date as well as time. What if i want count of 20/11/17, what to do about 02:24 time entry ? – Mohit Kumar Nov 20 '18 at 20:44

3 Answers3

2

You could group by the StationTo and use the aggregate count(*) function:

SELECT   StationTo, COUNT(*)
FROM     mytable
GROUP BY StationTo

EDIT:

If you just want the number of rows for a single StationTo, you could use a where clause:

SELECT COUNT(*)
FROM   mytable
WHERE  StationTo = 'P11004400000'
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I only want to get number of rows for a particular entry of StationTo (Column). I would be searching by StationTo code number e.g. by P11004400000. Inserting P11004400000 should give me number of rows only. – Mohit Kumar Nov 20 '18 at 19:15
  • @MohitKumar You could use a simple `where` clause - see my edited answer – Mureinik Nov 20 '18 at 19:57
  • Ya nearly there. I want to apply date condition also. How can we do so ? – Mohit Kumar Nov 20 '18 at 20:41
0

Select StationTo,Date, count(*) from table group by StationTo, Datemeaning all the stationTo having rows display their count. or select count(distinct StationTo) from table or Select count(*) from table where stationTo='yourvalue'

Himanshu
  • 3,830
  • 2
  • 10
  • 29
0

Hi have you master table for stationTo records?

select s.stationto, count(data.*) from stationtomaster
left join data on data.stationto=stationtomaster.stationto
group by s.stationto
László Tóth
  • 483
  • 5
  • 15