Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 3 tables like below :

orders : order_id,order_date
drivers : driver_id, driver_name
notifications : driver_id , order_id,order_delivery

i want record count from table notifications
where order_date >= startDate AND order_date <= endDate


Example 1
startDate=2014/01/01
endDate= 2014/01/31

Result------------------
driverName1 123
dirverName2 427
driverName3 234


Example 1
startDate=2014/01/01
endDate= 2014/01/20

Result------------------
driverName1 38
dirverName2 147
driverName3 94


Thanks a lot
Posted

Here is your result hope this will help you.

SQL
-- Create Table
CREATE TABLE [dbo].[orders](
[order_id] [int] NOT NULL,
[order_date] DateTime NULL
)
 
CREATE TABLE [dbo].[drivers](
[driver_id] [int] NOT NULL,
[driver_name] varchar(50) NULL
)
CREATE TABLE [dbo].[notifications](
[driver_id] [int] NOT NULL,
[order_id] [int] NOT NULL,
[order_delivery] varchar(50) NULL
)
GO
-- Sample Insert
INSERT [dbo].[orders]  VALUES (1, getdate())
INSERT [dbo].[orders]  VALUES (2, DATEADD(day,30,getdate()) )
INSERT [dbo].[orders]  VALUES (3, DATEADD(day,15,getdate()) )
INSERT [dbo].[drivers] VALUES (1, 'Driver1')
INSERT [dbo].[drivers] VALUES (2, 'Driver2')
INSERT [dbo].[drivers] VALUES (3, 'Driver3')
INSERT [dbo].[drivers] VALUES (4, 'Driver4')
INSERT [dbo].[notifications] VALUES (1,1, 'YES')
INSERT [dbo].[notifications] VALUES (1,1, 'YES')
INSERT [dbo].[notifications] VALUES (1,2, 'YES')
INSERT [dbo].[notifications] VALUES (1,3, 'YES')
INSERT [dbo].[notifications] VALUES (1,4, 'YES')
INSERT [dbo].[notifications] VALUES (2,1, 'YES')
INSERT [dbo].[notifications] VALUES (2,2, 'YES')
INSERT [dbo].[notifications] VALUES (2,2, 'YES')
INSERT [dbo].[notifications] VALUES (2,2, 'YES')
INSERT [dbo].[notifications] VALUES (2,3, 'YES')
INSERT [dbo].[notifications] VALUES (3,2, 'YES')
INSERT [dbo].[notifications] VALUES (3,2, 'YES')
INSERT [dbo].[notifications] VALUES (3,2, 'YES')
INSERT [dbo].[notifications] VALUES (3,2, 'YES')
INSERT [dbo].[notifications] VALUES (3,3, 'YES')
---------------

-- Your select Query 
Declare @StartDate datetime= getdate(),
@endate datetime=DATEADD(day,30,getdate())
Select D.Driver_Name ,
		Count(N.order_delivery) TotalCounts
	 FROM
		notifications N 
		Left Outer JOIN orders O
		ON N.order_id=O.Order_id
		Left Outer JOIN drivers D
		ON N.driver_id=D.driver_id
		Where
		O.order_date between @StartDate and @endate
		Group BY
		D.Driver_Name
 
Share this answer
 
I guess you want something like below

SQL
select 
      driverName,count(Order_id)
from orders O 
Join notifications N On O.Order_ID=N.Order_Id
Join drivers D On  N.Driver_id=D.Driver_ID 
where order_date >= startDate 
      AND order_date <= endDate
Group by driverName


Also refer for Joins

SQL Joins - W3Schools[^]
 
Share this answer
 
v3
Comments
[no name] 1-Dec-14 4:49am    
your code not working
i want count from table notifications , i think you count from orders !
Shweta N Mishra 1-Dec-14 5:03am    
put your sample table data above your expected result to better understand your issue.

that was a sample code , you could modify it as per your requirement.
[no name] 1-Dec-14 6:10am    
thanks Shweta N Mishra
my problem was solved
Shweta N Mishra 1-Dec-14 6:12am    
np;)
select count(N.order_id) from orders O left join notifications N on O.order_id=N.order_id where O.order_date >=startDate AND order_date <=EndDate
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900