Solved

SQL Script to remove a list of names from query

Posted on 2014-11-14
7
104 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 47

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 47

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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 47

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 47

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best counters for cpu high usage 3 33
sql 2014,  lock limit 5 32
TSQL - How to declare table name 26 31
SQL Server Connection String through a VPN 8 29
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

831 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