Hi Guys,
I need urgent help. I am working on a SQL query. Below is the sample data.
id B_id Station Client B_Date B_time B_rate
1 2030823 BRAVO CASL 2019-05-27 1900-01-01 08:14:00.000 1150.00
2 2030823 BRAVO CASL 2019-05-27 1900-01-01 08:41:00.000 1150.00
3 2030823 BRAVO CASL 2019-05-27 1900-01-01 08:55:30.000 20.00
4 2030823 BRAVO CASL 2019-05-28 1900-01-01 08:28:06.000 1150.00
5 2030823 BRAVO CASL 2019-05-28 1900-01-01 08:38:06.000 20.00
6 2030823 BRAVO CASL 2019-05-28 1900-01-01 08:48:06.000 20.00
7 2030823 BRAVO CASL 2019-05-28 1900-01-01 08:58:06.000 60.00
8 2030823 BRAVO CASL 2019-05-28 1900-01-01 09:15:30.000 1150.00
9 2030823 BRAVO CASL 2019-06-02 1900-01-01 08:14:00.000 2600.00
10 2030823 BRAVO CASL 2019-06-02 1900-01-01 08:44:00.000 20.00
First step:- I want to group by B_id/Station/Client and B_date
Second Step:- Then I want to compare each B_time if It is 30m different then Sum(b_rate) and total count.
Here is the first example
1 2030823 BRAVO CASL 2019-05-27 1900-01-01 08:14:00.000 1150.00
2 2030823 BRAVO CASL 2019-05-27 1900-01-01 08:41:00.000 1150.00
3 2030823 BRAVO CASL 2019-05-27 1900-01-01 08:55:30.000 20.00
Compare Time:- 08:14 == 08:41 = Yes
08:14 == 08:55 = No
08:41 == 08:14 = Yes
08:41 == 08:55 = Yes
08:55 == 08:14 = No
08:55 == 08:41 = NO (BECAUSE WE already compare this one so it should be NO)
Please help me out to how I accomplished this one.
Blockquote
Create table #tbl_Main
(
id int identity(1,1)
,B_id int
,Station varchar(20)
,Client varchar(20)
,B_Date Date
,B_time Datetime
,B_rate Decimal(10,2)
)
Insert into #tbl_Main (B_id,Station,Client,B_Date,B_time,B_rate)
Select '2030823','BRAVO','CASL','2019-05-27 00:00:00.000','1900-01-01 08:55:30.000','20.00'
Union
Select '2030823','BRAVO','CASL','2019-05-27 00:00:00.000','1900-01-01 08:41:00.000','1150.00'
Union
Select '2030823','BRAVO','CASL','2019-05-27 00:00:00.000','1900-01-01 08:14:00.000','1150.00'
Union
Select '2030823','BRAVO','CASL','2019-05-28 00:00:00.000','1900-01-01 08:58:06.000','60.00'
Union
Select '2030823','BRAVO','CASL','2019-05-28 00:00:00.000','1900-01-01 08:48:06.000','20.00'
Union
Select '2030823','BRAVO','CASL','2019-05-28 00:00:00.000','1900-01-01 08:38:06.000','20.00'
Union
Select '2030823','BRAVO','CASL','2019-05-28 00:00:00.000','1900-01-01 08:28:06.000','1150.00'
Union
Select '2030823','BRAVO','CASL','2019-05-28 00:00:00.000','1900-01-01 09:15:30.000','1150.00'
Union
Select '2030823','BRAVO','CASL','2019-06-02 00:00:00.000','1900-01-01 08:44:00.000','20.00'
Union
Select '2030823','BRAVO','CASL','2019-06-02 00:00:00.000','1900-01-01 08:14:00.000','2600.00'
Select * from #tbl_Main
Blockquote