?
Solved

need help with query

Posted on 2013-05-17
12
Medium Priority
?
238 Views
Last Modified: 2013-05-22
Hi experts,
I have a situation where I should find customers who have a deleted flag 'D' and blank ' ' for the same email address. Please find below example:

customer_no  | emailAddress  | Deleted
 1111                ap@gmail.com       D
 2221                bp@gmail.com       D
 1111                ap@gmail.com      
 3333                cp@gmail.com
0
Comment
Question by:sqlcurious
[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
  • 2
  • +3
12 Comments
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 664 total points
ID: 39176261
Give this a whirl...

SELECT DISTINCT yt1.customer_no, yt1.emailAddress
FROM YourTable yt1
WHERE Deleted = 'D'
JOIN (
   SELECT DISTINCT customer_no, emailAddress
   FROM YourTable
   WHERE Deleted = '') yt2 ON yt1.customer_no = yt2.customer_no AND yt1.email_address = yt2.email_address
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39176268
check this out

selecet * from YourTable
where Delted = 'D'
AND emailAddress  = ''

Open in new window

0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 668 total points
ID: 39176401
Although you asked for the reverse of what I'm suggesting here, I assume you are trying to find the records without the 'D' so they are either removed or to have the Deleted field set to 'D'. If this is the case then this is similar to Jim's suggestion but with a couple of tweaks. The inner subquery finds those records with Deleted = 'D' so that the final result is of those records of Deleted = ''. And; the where clause is moved under the from clause.
SELECT
      c1.customer_no
    , c1.emailAddress
FROM Customers c1
INNER JOIN (
            SELECT DISTINCT
                  customer_no
                , emailAddress
            FROM Customers
            WHERE Deleted = 'D'
            ) c2 ON c1.customer_no = c2.customer_no
                    AND c1.email_address = c2.email_address
WHERE c1.Deleted = ''
    -- or c1.Deleted is null

Open in new window

although you only asked for '' in the filtering I'd suggest you also look for nulls in the Deleted field.
0
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
LVL 41

Expert Comment

by:Sharath
ID: 39178673
try this.
SELECT customer_no, 
       emailAddress 
  FROM test 
 WHERE Deleted IN ( '', 'D' ) 
 GROUP BY customer_no, 
          emailAddress 
HAVING COUNT(*) = 2 
       AND MIN(Deleted) = '' 
       AND MAX(Deleted) = 'D' 

Open in new window


see the example here: http://sqlfiddle.com/#!3/e1153/2
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 668 total points
ID: 39181904
SELECT
    emailAddress,
    MIN(customer_no) AS minCustomer,
    MAX(customer_no) AS maxCustomer
FROM
    dbo.tablename
WHERE
    Deleted IN ( '', 'D' )
GROUP BY
    emailAddress
HAVING
    MIN(Deleted) = '' AND
    MAX(Deleted) = 'D'
ORDER BY
    emailAddress
0
 

Author Comment

by:sqlcurious
ID: 39185861
For some reason none of the queries above are working :( any idea as to what's going on?
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39185875
Define 'not working'.  Is there an error message?  No return records?  
Does the 'deleted flag' and email address values have any trailing spaces?
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39186062
"not working" does not give us much to work with and it would be good to know what the symptoms really are. Perhaps try the following and provide the results?
SELECT 'c1.Deleted ""' AS filterfor
	, count(*) AS counted
FROM Customers c1
WHERE c1.Deleted = ''

UNION ALL

SELECT 'c1.Deleted "" or null' AS filterfor
	, count(*) AS counted
FROM Customers c1
WHERE c1.Deleted = ''
	OR c1.Deleted IS NULL

UNION ALL

SELECT 'c1.Deleted not "D" or null' AS filterfor
	, count(*) AS counted
FROM Customers c1
WHERE c1.Deleted NOT = 'D'
	OR c1.Deleted IS NULL

UNION ALL

SELECT 'Deleted = "D"' AS filterfor
	, count(*) AS counted
FROM Customers
WHERE Deleted = 'D'

UNION ALL

SELECT DISTINCT 'Deleted like "D%"' AS filterfor
	, count(*) AS counted
FROM Customers
WHERE Deleted LIKE 'D%';

Open in new window

0
 

Author Comment

by:sqlcurious
ID: 39187286
Hi all, I am sorry about not being specific, @Jimhorn I wasnt getting any results.
@PortletPaul please find the results below for the query you suggested:

filterfor                       Counted
c1.Deleted ""      1752036
c1.Deleted "" or null      1752036
c1.Deleted not "D" or null      1752037
Deleted = "D"      1734839
Deleted like "D%"      1734839

Pls suggest, thanks
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39187574
sqlcurious, thanks very much for the results. they show that the data is very much as you explain it - so I'm really not sure why you are not get results from any of the queries.

did you try the one from Scott? (ID: 39181904) using an aggregation may be just the ticket.
0
 

Author Comment

by:sqlcurious
ID: 39187852
yes Scott's worked, guess I was doing something wrong with the group by earlier, thanks !
0
 

Author Closing Comment

by:sqlcurious
ID: 39187858
Thanks!
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

800 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