简体   繁体   中英

sql query to fetch datewise count from table

Table name = Master

I need an sql query which fetches a datewise result from table.

Data is present in my database as displayed below.

 date                   copiesbooked             surveycount
13-03-2015                  700                      raj
13-03-2015                  700                      raj
13-03-2015                  700                      raj
13-03-2015                  700                      raj

13-03-2015                  701                      raj
13-03-2015                  701                      raj
13-03-2015                  701                      raj
13-03-2015                  701                      raj

13-03-2015                  702                      akash
13-03-2015                  702                      akash
13-03-2015                  702                      akash
13-03-2015                  702                      akash

14-03-2015                  703                      sunil
14-03-2015                  703                     sunil
14-03-2015                  703                      sunil
14-03-2015                  703                      sunil

15-03-2015                  704                      raj
15-03-2015                  704                      raj
15-03-2015                  704                      raj
15-03-2015                  704                      raj

In the above table, there are 3 copies booked on 13-03-2015 named 700,701,702 and only 2 surveyor worked raj and akash....hence surveycount is 2.

Again, there are 1 copiesbooked on 14-03-2015 named 703 and only 1 surveyor worked sunil ...hence surveycount is 1...like wise...

I need output like below

 date            copiesbooked             surveycount
13-03-2015            3                       2
14-03-2015            1                       1
15-03-2015            1                       1

Could you please help to make the sql query for getting such output...

below is my query which i tried...but this does not get distinct count of copiesbooked and surveycount column..my query gets all rows group by date..

select date,
count(copiesbooked) as copiesbooked,
count(surveycount) as  surveycount from master 
group by date

group by the date column is what you want. use Distinct to show unique count of entries

select date,
count(distinct copiesbooked) as copiesbooked,
count(distinct surveycount) as  surveycount from master 
group by date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM