Solved

SQL Script to remove a list of names from query

Posted on 2014-11-14
7
95 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
  • 4
  • 3
7 Comments
 
LVL 45

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 45

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

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 45

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

757 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now