Solved

SQL Script to remove a list of names from query

Posted on 2014-11-14
7
110 Views
Last Modified: 2014-11-14
Hello Experts Exchange
I have a SQL query that I want to remove a list of names using a sub-query, but when I run my query using the sub-query I have not records return, and there should be records that return.

Here is my query;

Select a.[First Name],a.[Surname],
		a.[Clock Number],a.[Cost Centre], a.[shift],
		Convert(varchar,a.[Date],103) as [Date],
		a.[TimeCal] as [Losses Attendance Hours],b.[Hours] as [PrimetimeHours], 
		a.[TimeCal] - b.[Hours] as [Difference]
from [dbo].[T&A_Temp] a
inner join [dbo].[T&A_Temp_Primetime] b
on a. [First Name]= b.[First Name]
and a.surname = b.surname
Where a.[TimeCal] <> b.[Hours]
and NOT Exists (select [first name],Surname
				  from [T&A_Temp]
				  group by [first name],Surname
				  having count(*) > 30)
order by a.[Cost Centre]

Open in new window


Can anyone help me with my query please?

Regards

SQLSearcher
0
Comment
Question by:SQLSearcher
[X]
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
  • 4
  • 3
7 Comments
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 40442453
What for you need the having count(*) > 30 clause in the subquery?
Did you try to run the query without that clause?
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 40442455
And I can't also see a relationship between the subquery and the main query.
0
 

Author Comment

by:SQLSearcher
ID: 40442460
Hello Vitor
I run it without the having count(*) > 30 clause but same result no records.  I need the clause so it selects the right names.

Regards

SQLSearcher
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 40442472
Can you explain better what you are trying to achieve?
That query doesn't looks me that will work, that's why.
0
 

Author Comment

by:SQLSearcher
ID: 40442510
Hi Vitor
So my main query joins table A and B using First Name, Surname and Date.  This gives me a report of where my losses list does not match my primetime list.  This query works fine when the sub-query is not in the script.

Now I have a second query a list of users that appear on the list more than 30 times, this means they are assigned to more than one cost centre.  This is my sub-query, it works fine on its own it returns 10 first name and surnames.

My query is wrong when put together I am looking for the right syntax.

I want the 10 names returned in my sub-query to be removed from my main list.

Regards

SQLSearcher
0
 
LVL 50

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40442660
Then you just missed the WHERE clause to join the subquery with the main query. Try this one:
Select a.[First Name],a.[Surname],
		a.[Clock Number],a.[Cost Centre], a.[shift],
		Convert(varchar,a.[Date],103) as [Date],
		a.[TimeCal] as [Losses Attendance Hours],b.[Hours] as [PrimetimeHours], 
		a.[TimeCal] - b.[Hours] as [Difference]
from [dbo].[T&A_Temp] a
	inner join [dbo].[T&A_Temp_Primetime] b 
		on a.[First Name]= b.[First Name] and a.surname = b.surname
Where a.[TimeCal] <> b.[Hours]
	and NOT Exists (select c.[first name],c.Surname
				  from [T&A_Temp] c
				  where a.[First Name]= c.[First Name] and a.surname = c.surname
				  group by c.[first name],c.Surname
				  having count(1) > 30)
order by a.[Cost Centre]

Open in new window

0
 

Author Closing Comment

by:SQLSearcher
ID: 40442749
Thank you very much for your help.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

691 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