Solved

SQL Script to remove a list of names from query

Posted on 2014-11-14
7
107 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 49

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 49

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

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 49

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to handle time out inside the stored procedure 10 27
SCOM to SQL port 14xx failed? 1 32
SQL Select Query help 1 38
Delete old Sharepoint backups 2 24
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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

730 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