Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
2revup
Posting Yak Master
112 Posts |
Posted - 2013-11-14 : 21:00:42
|
So I have the strangest issue I need someone to help me with cause I am going crazy lookin at it. I have a query that looks like this:Select site.*,Network.Network_countfrom (Select Year(comm_date_utc) as Year, Month(comm_date_utc) as Month,COUNT(comm_id)as Correspondences_count, location from Correspondencesleft join Agents a on owner_agent_login = agent_login_idwhere location in ('AUS', 'USA', 'SA', 'IRE')and comm_date_utc between '2012/11/01 12:47:00' and '2013/10/31 23:59:00'and start_date < DATEADD(day,DATEDIFF(day,0,'2013/10/31 23:59:00')-90,0)group by Year(comm_date_utc), Month(comm_date_utc),location) SiteLeft outer join(Select Year(comm_date_utc) as Year, Month(comm_date_utc) as Month,COUNT(comm_id)as Network_count from Correspondencesleft join Agents a on owner_agent_login = agent_login_idwhere comm_date_utc between '2012/11/01 12:47:00' and '2013/10/31 23:59:00'and start_date < DATEADD(day,DATEDIFF(day,0,'2013/10/31 23:59:00')-90,0)group by Year(comm_date_utc), Month(comm_date_utc)) Networkon site.Year = network.Yearand site.month = network.Monthgroup bysite.Year,site.Month, Site.Correspondences_count, site.location, network.Network_countorder by year asc, Month ascrather than giving you all values let me share the part that is important (Cant share the data public forum and all):So for the the NETWORK_COUNT for the the 8th, 9th and 10th month I am getting as follows:8th = 197159th = 1810610th = 18542But If I go and run the same query BUT change the dates as follows:Select site.*,Network.Network_countfrom (Select Year(comm_date_utc) as Year, Month(comm_date_utc) as Month,COUNT(comm_id)as Correspondences_count, location from Correspondencesleft join Agents a on owner_agent_login = agent_login_idwhere location in ('AUS', 'USA', 'SA', 'IRE')and comm_date_utc between '2012/11/01 12:47:00' and '2013/11/30 23:59:00'and start_date < DATEADD(day,DATEDIFF(day,0,'2013/11/30 23:59:00')-90,0)group by Year(comm_date_utc), Month(comm_date_utc),location) SiteLeft outer join(Select Year(comm_date_utc) as Year, Month(comm_date_utc) as Month,COUNT(comm_id)as Network_count from Correspondencesleft join Agents a on owner_agent_login = agent_login_idwhere comm_date_utc between '2012/11/01 12:47:00' and '2013/11/3023:59:00'and start_date < DATEADD(day,DATEDIFF(day,0,'2013/11/30 23:59:00')-90,0)group by Year(comm_date_utc), Month(comm_date_utc)) Networkon site.Year = network.Yearand site.month = network.Monthgroup bysite.Year,site.Month, Site.Correspondences_count, site.location, network.Network_countorder by year asc, Month ascthen I am getting these results!8th = 197719th = 1868610th = 20261You can see the number is accumulating, and it should not be at all. Why on earth would this happen? |
|
2revup
Posting Yak Master
112 Posts |
Posted - 2013-11-16 : 16:54:59
|
Anyone? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
2revup
Posting Yak Master
112 Posts |
Posted - 2013-11-17 : 21:20:33
|
I have worked the issue out and I am not sure how to correct it, giving you sample data would take quite a few hours let me try and explain this a little. I will break the query down by a smaller one. So IF I run this:Select Year(comm_date_utc) as Year, Month(comm_date_utc) as Month, owner_agent_login, COUNT(comm_id)as Correspondences_count, location from Correspondencesleft join Agents a on owner_agent_login = agent_login_idwhere location in ('AUS', 'USA', 'SA', 'IRE')and comm_date_utc between '2012/11/01 12:47:00' and '2013/9/30 23:59:00'and start_date < DATEADD(day,DATEDIFF(day,0,'2013/9/30 23:59:00')-90,0)group by Year(comm_date_utc), Month(comm_date_utc),location, owner_agent_loginIts giving me results showing me each agent and how many items they have done broken down into months. IF I change the end date which is these 2 items:and '2013/9/30 23:59:00'and start_date < DATEADD(day,DATEDIFF(day,0,'2013/9/30 23:59:00')-90,0)What is happening is that because the end date is NOT showing any results for an employee that has started within the last 90 days, which works well. BUT when someone has been on for more than 90 days it then show all of the employees results, what I need this to actully do is say on 90 days show ONLY the last month, not the last 3 months. So to best articulate this from data here is what it might look like:If I ran the query and the user login3 start date has been longer than 90 days it will show ALL of his results like so.Year Month owner_agent_login Correspondences_count location2013 1 Login1 180 IRE2013 2 Login1 209 IRE2013 3 Login1 208 IRE2013 4 Login1 189 IRE2013 5 Login1 191 IRE2013 1 Login2 140 USA2013 2 Login2 103 USA2013 3 Login2 137 USA2013 4 Login2 156 USA2013 5 Login2 183 USA2013 3 Login3 30 USA2013 4 Login3 76 AUS2013 5 Login3 98 AUSWhat I need it to do is show only the results after the 90 days, so the other results would be NOT counted. it would look like this:Year Month owner_agent_login Correspondences_count location2013 1 Login1 180 IRE2013 2 Login1 209 IRE2013 3 Login1 208 IRE2013 4 Login1 189 IRE2013 5 Login1 191 IRE2013 1 Login2 140 USA2013 2 Login2 103 USA2013 3 Login2 137 USA2013 4 Login2 156 USA2013 5 Login2 183 USA2013 5 Login3 12 AUSDoes this make more sense? |
|
|
2revup
Posting Yak Master
112 Posts |
Posted - 2013-11-18 : 02:12:53
|
Turns out this was an easy correction, I just needed stop over complicating it. just change this:AND start_date < DATEADD(day, DATEDIFF(day, 0, '2013/9/30 23:59:00') - 90, 0)to: and comm_date_utc between DATEADD(MONTH,3,start_date) and '2013/10/31 23:59:00' |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-18 : 05:55:33
|
quote: Originally posted by 2revup Turns out this was an easy correction, I just needed stop over complicating it. just change this:AND start_date < DATEADD(day, DATEDIFF(day, 0, '2013/9/30 23:59:00') - 90, 0)to: and comm_date_utc between DATEADD(MONTH,3,start_date) and '2013/10/31 23:59:00'
dont hardcode time like this. This has a chance of losing out some records if created between 23:59 and 00:00why not use >= and < insteadseehttp://visakhm.blogspot.in/2012/12/different-ways-to-implement-date-range.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|