MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old April 29th, 2004, 07:58 AM
densma densma is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 37 densma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Where the experts are..urgent query help

--------------------------------------------------------------------------------

union or subquery? how can i put these two queries together to get one result below.
I'm trying to count total records and also total escalation from total record perday/per agent for previous day. first query gives me total escalation and 2nd one gives me total records. how can i put them together in one query?
thanks
here my 2 queries
.......
SELECT Count( DISTINCT Create_Date) 'Total Escalation',
Submitter 'Remedy Id',
(EMP_FRST+' '+EMP_LAST) AS Agent,
(SUP_FRST+' '+SUP_LAST) AS Supervisor,
(MGR_FRST+' '+MGR_LAST) AS Manager,
(getdate() - 1) as stampdate

FROM RemWebReports.dbo.raw_data
inner join WebForms.dbo.CNS on Submitter = remedy_ID
WHERE datediff(day, Create_Date, GetDate()) = 1 and IsDate(Create_Date) = 1 and Status <> 'closed'
GROUP BY (MGR_FRST+' '+MGR_LAST), (SUP_FRST+' '+SUP_LAST), (EMP_FRST+' '+EMP_LAST),Group_Assigned_To, Submitter
ORDER BY (MGR_FRST+' '+MGR_LAST), (SUP_FRST+' '+SUP_LAST), (EMP_FRST+' '+EMP_LAST),Group_Assigned_To,Submitter

query 2

SELECT Count( DISTINCT Create_Date) 'Total call vol',
Submitter 'Remedy Id',
(EMP_FRST+' '+EMP_LAST) AS Agent,
(SUP_FRST+' '+SUP_LAST) AS Supervisor,
(MGR_FRST+' '+MGR_LAST) AS Manager,
(getdate() - 1) as stampdate

FROM RemWebReports.dbo.raw_data
inner join WebForms.dbo.CNS on Submitter = remedy_ID
WHERE datediff(day, Create_Date, GetDate()) = 1 and IsDate(Create_Date) = 1
GROUP BY (MGR_FRST+' '+MGR_LAST), (SUP_FRST+' '+SUP_LAST), (EMP_FRST+' '+EMP_LAST), Submitter
ORDER BY (MGR_FRST+' '+MGR_LAST), (SUP_FRST+' '+SUP_LAST), (EMP_FRST+' '+EMP_LAST), Submitter ASC
.........
basically same querie with diff where clause
i want to get result together like
'Total Escalation' 'Total call vol' 'Remedy Id', Agent,Supervisor, Manager, stampdate

b./c im trying to calculate percentage b/w 'Total Escalation' and 'Total call vol'

Reply With Quote
  #2  
Old April 29th, 2004, 07:36 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Join those 2 queries from a seperate outer query, and just select the total escalation and total call volume based on the Agent, sup, mgr match. I had time to burn...
Code:
select	COUNT(distinct RD.CreateDate) 'Total Escalation',
	A.'Total Call Vol'
	Submitter 'Remedy Id',
	(CNS.EMP_FRST+' '+CNS.EMP_LAST) AS Agent,
        (CNS.SUP_FRST+' '+CNS.SUP_LAST) AS Supervisor,
        (CNS.MGR_FRST+' '+CNS.MGR_LAST) AS Manager,
        (getdate() - 1) as stampdate
FROM	RemWebReports.dbo.raw_data RD
inner	join WebForms.dbo.CNS CNS on Submitter = CNS.remedy_ID
inner	join
	(
	 SELECT	COUNT( DISTINCT Create_Date) 'Total call vol',
		Submitter 'Remedy Id',
		(EMP_FRST+' '+EMP_LAST) AS Agent,
		(SUP_FRST+' '+SUP_LAST) AS Supervisor,
		(MGR_FRST+' '+MGR_LAST) AS Manager,
		(getdate() - 1) as stampdate
	 FROM	RemWebReports.dbo.raw_data
	 inner	join WebForms.dbo.CNS on Submitter = remedy_ID
	 WHERE	datediff(day, Create_Date, GetDate()) = 1
 	 and	IsDate(Create_Date) = 1 
	 GROUP	BY (MGR_FRST+' '+MGR_LAST),
		   (SUP_FRST+' '+SUP_LAST),
	  	   (EMP_FRST+' '+EMP_LAST),
		    Submitter
	) as A
on	RD.submitter_id = A.'Redemy ID'
	and (CNS.EMP_FRST+' '+CNS.EMP_LAST) = A.Agent
	and (CNS.SUP_FRST+' '+CNS.SUP_LAST) = A.Supervisor,
	and (CNS.MGR_FRST+' '+CNS.MGR_LAST) = A.Manager,
WHERE	datediff(day, RD.Create_Date, GetDate()) = 1
and	IsDate(RD.Create_Date) = 1
and	Status <> 'closed'
GROUP	BY (CNS.MGR_FRST+' '+CNS.MGR_LAST),
	   (CNS.SUP_FRST+' '+CNS.SUP_LAST),
	   (CNS.EMP_FRST+' '+CNS.EMP_LAST),
	    Submitter,
            A.'Total Call Vol'
ORDER	BY (CNS.MGR_FRST+' '+CNS.MGR_LAST),
	   (CNS.SUP_FRST+' '+CNS.SUP_LAST),
	   (CNS.EMP_FRST+' '+CNS.EMP_LAST),
	    Group_Assigned_To,
	    Submitter

This may be loaded w/syntax errors, but the idea of the logic should get you what you want. Of course, there's always the concept of temp tables.

Last edited by Username=NULL : May 2nd, 2004 at 03:26 PM.

Reply With Quote
  #3  
Old April 30th, 2004, 09:22 AM
densma densma is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 37 densma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
thanks for your reply
but

EMP_FRST, EMP_LAST, SUP_FRST, SUP_LAST,
MGR_FRST, MGR_LAST, Remedy_ID is on diff database/table i.e WebForms.dbo.CNS
Remedy_iD is related to Submiter in RemWebReports.dbo.raw_data, i have to use join to get names/sup/manager b/c they are not available in RemWebReports.dbo.raw_data.
......................
let's take out join to make it easier. i will join my results and create view..
my new query shoud look like this..

query 1.

SELECT Count( DISTINCT Create_Date) 'Total Escalation',
Submitter 'Remedy Id',
Group_Assigned_To,
(getdate() - 1) as stampdate
FROM RemWebReports.dbo.raw_data
WHERE datediff(day, Create_Date, GetDate()) = 1 and IsDate(Create_Date) = 1 and Status <> 'closed'
group by Submitter, Group_Assigned_To
ORDER BY Submitter, Group_Assigned_To

query 2.

SELECT Count( DISTINCT Create_Date) 'Total Call vol',
Submitter 'Remedy Id',
(getdate() - 1) as stampdate
FROM RemWebReports.dbo.raw_data
WHERE datediff(day, Create_Date, GetDate()) = 1 and IsDate(Create_Date) = 1
group by Submitter, Group_Assigned_To
ORDER BY Submitter, Group_Assigned_To


wanna combine both to get
result like
'Total Escalation' 'Total call vol' 'Remedy Id', 'Group_Assigned_To' 'stampdate'

Reply With Quote
  #4  
Old April 30th, 2004, 12:43 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Ahhh, ok, my mistake then. So the fields I had in green here...
Code:
select  Distinct RD.EMP_FRST+' '+RD.EMP_LAST AS Agent,
        RD.SUP_FRST+' '+RD.SUP_LAST AS Supervisor,
        RD.MGR_FRST+' '+RD.MGR_LAST AS Manager,
        (getdate() - 1) as stampdate,
        A.[Total Escalation],
        B.[Total Call Vol]
from    RemWebReports.dbo.raw_data RD

...aren't actually in RemWebReports.dbo.raw_data @ all? I see earlier that the Submitter is what's joining to 2 DB's together...ok. I'm kinda tied up, but I'll give it another shot later on.

Last edited by Username=NULL : April 30th, 2004 at 12:52 PM.

Reply With Quote
  #5  
Old May 2nd, 2004, 02:10 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Ok, I made another attempt...I just edited my 1st query. Both of your queries looked exactly the same, except I finally saw the...

status <> 'Closed'

...criteria. Since that's the only diff between the 'Total Call Vol' and the 'Total Escalation' calc and all the other criteria is the same, I wrote one qry and joined to the other keeping the Submitter ID, Agent, Sup, and Mgr relations the same. I'm assuming your using SQL Server??...if so I have an idea using a CASE to extract both of the calc'd fields based off the status field values, not sure if it would be correct but if it is, it may save some room.

Hopefully the edit was on w/what you're looking for, the concept isn't that hard, but b/c there's redundancy in the subquery, I'm hesitant to say that there isn't a better solution...maybe someone else can chime in.

Last edited by Username=NULL : May 2nd, 2004 at 02:19 AM.

Reply With Quote
  #6  
Old May 2nd, 2004, 02:28 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
nevermind...thought I had a solution w/a CASE but no, edited.

Last edited by Username=NULL : May 2nd, 2004 at 03:38 AM.

Reply With Quote
  #7  
Old May 2nd, 2004, 06:17 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 26 m 34 sec
Reputation Power: 891
what is the deal with counting distinct valid dates?

wouldn't DATEDIFF barf if there actually was an invalid date?

what's the datatype of the Create_Date column?

and assuming that DATEDIFF actually does work as intended, only on valid dates, then there will only ever be 1 distinct date in the result, so why is there a DISTINCT in the COUNT() expression?
__________________
r937.com | rudy.ca

Reply With Quote
  #8  
Old May 2nd, 2004, 08:17 AM
densma densma is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 37 densma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Create_Date is varchar which is bad, im thnking about converting to datetime
what seem to work so far is...
i created package for each query to separate table.. then
i create a view to join them but i cant' seem to join 2 date columns unless i convert it to char.
im still verifiying data to make sure it accurate

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Where the experts are..urgent query help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway