Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQl union query

Posted on 2007-03-29
2
241 Views
Last Modified: 2010-08-05
If I run the individual queries from the following union, i get the expected result.  However, if I run the union as is, I get an incorrect result.  Can anyone tell me why?  Also, the second query in the union takes 30 seconds to run - why's that?

query:

SELECT
Count(Calls) as calls, sum(booking) as booking, count(calls)/sum(Booking)+ count(calls) as hitrate
FROM

(SELECT
Count(CallLogger.CallLoggerId) AS Calls, 0.0 as booking
FROM
RIO.CallLogger.dbo.CallLogger [CallLogger]
INNER JOIN RIO.CallLogger.dbo.Extensions [Extensions] ON [CallLogger].Extension1 = [Extensions].Extension
WHERE
DateDiff(y, CallLogger.[Date], getdate()) = 0
AND [CallLogger].CallType=1
AND [Extensions].[Group]='Telesales'

UNION

SELECT
0.0 as calls,
SUM(Calls.Booking) AS Booking
FROM
RFVSQL.RFPUB.dbo.Calls [Calls] INNER JOIN RFVSQL.RFPUB.dbo.Users [Users] ON [Calls].UserId = [Users].UserId
WHERE
DateDiff(y, [Calls].DateTimeCreated, getdate()) = 0
AND [Users].DepartmentId=11) X

Thanks
Lapchien
0
Comment
Question by:Lapchien
2 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 18814638
Your entire statement returns 2 for Count(Calls), right?  Is that your problem?

The top-level SELECT should say Sum(Calls).  You counted the calls in the first query & you knew the second section was returning 0 calls.  You want to add the 0 for the 2nd block to the number in the 1st block.

Not sure why the 2nd block takes as long as it does.  How many rows are in [calls]?  For this query, you should have a couple indices:

Create Index IX_CALLS_User_DT on [Calls] (UserID, DateTimeCreated)
go
Create Index IX_Users_User_Dept on [Users] (UserID, DepartmentID)
go

If you already have indices like that, and the tables are not huge it should not take so long.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Addition to SQL for dynamic fields 6 47
sql help 2 46
SQL Server - Set Field Values ito Zero Based on Related Table 4 40
Query for timesheet application 3 18
In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question