473,425 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

Trouble using JOINS, GROUP BY

I have 2 tables:
Users --> email, UID (pk)
Orders --> Ordernumber, UID (pk)

I am trying to clean up the database. Some users have created multiple accounts in the database. Each account they create will generate a new UID but will have their same email address. I need to find which email addresses have multiple UIDs and which of those UIDs have an Ordernumber associated with it (for example, an account could have been created but there may not be an Ordernumber associated with that account). So in the end I hope to see an email address with all of its UIDs and do those UIDs have an Ordernumber associated with it…yes or no or some other designation. Does this seem doable, reasonable? I have very limited experience. I have been working with GROUP BY, JOINS, VIEWS, etc., and have been getting nowhere. Any help will be appreciated.

The following code shows me which emails have multiple accounts but I still need to display the UIDs for each account and whether or not there is an ordernumber associated with it...

Expand|Select|Wrap|Line Numbers
  1. Select EMAIL, COUNT(EMAIL) AS eCNT
  2.  FROM USERS
  3.  GROUP BY EMAIL
  4.  HAVING (COUNT(EMAIL) > 1)
  5.  
Jul 18 '10 #1

✓ answered by Delerna

Expand|Select|Wrap|Line Numbers
  1. SELECT b.UID,b.Email,c.OrderNumber
  2. FROM
  3. (   Select EMAIL 
  4.     FROM USERS GROUP BY EMAIL 
  5.     HAVING COUNT(EMAIL) > 1
  6. )a
  7. JOIN USERS b on a.Email=b.Email
  8. LEFT JOIN Orders c on b.UID=c.UID
  9.  
For all duplicated emails, order number will be null for UID's that don't have any orders on them


or


Expand|Select|Wrap|Line Numbers
  1. SELECT b.UID,b.Email,count(c.OrderNumber) as NumOrds
  2. FROM
  3. (   Select EMAIL 
  4.     FROM USERS GROUP BY EMAIL 
  5.     HAVING COUNT(EMAIL) > 1
  6. )a
  7. JOIN USERS b on a.Email=b.Email
  8. LEFT JOIN Orders c on b.UID=c.UID
  9. GROUP BY b.UID,b.Email
  10.  
will tell you how many orders each UID has


Hope I didn't make any typo's there :)


The query inside the brackets is called a sub query or derived table

2 1804
Delerna
1,134 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. SELECT b.UID,b.Email,c.OrderNumber
  2. FROM
  3. (   Select EMAIL 
  4.     FROM USERS GROUP BY EMAIL 
  5.     HAVING COUNT(EMAIL) > 1
  6. )a
  7. JOIN USERS b on a.Email=b.Email
  8. LEFT JOIN Orders c on b.UID=c.UID
  9.  
For all duplicated emails, order number will be null for UID's that don't have any orders on them


or


Expand|Select|Wrap|Line Numbers
  1. SELECT b.UID,b.Email,count(c.OrderNumber) as NumOrds
  2. FROM
  3. (   Select EMAIL 
  4.     FROM USERS GROUP BY EMAIL 
  5.     HAVING COUNT(EMAIL) > 1
  6. )a
  7. JOIN USERS b on a.Email=b.Email
  8. LEFT JOIN Orders c on b.UID=c.UID
  9. GROUP BY b.UID,b.Email
  10.  
will tell you how many orders each UID has


Hope I didn't make any typo's there :)


The query inside the brackets is called a sub query or derived table
Jul 19 '10 #2
Thank you so much. Both of your solutions worked perfectly! I appreciate your help on this. I can see I need to spend a lot more time on derived Tables and Joins...
Jul 19 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Morten Gulbrandsen | last post by:
Hello, starting from some software database spesification, defined in some Enhanced entity relationship diagram, resulting in all kinds of relationships, 1:1 1:Many Many:1
3
by: Jason | last post by:
I am having trouble using the CONTAINS function in sql server(enterprise manager). I am typing the following: Select * FROM mytable WHERE CONTAINS(myfield,'mystring') This returns the...
1
by: mr_burns | last post by:
hi there, Ive been having a few problems recently with this group. Im not sure if any of the other groups have been giving me trouble but recently this one has a couple of times, when attempting...
0
by: lkrubner | last post by:
The idea I'm trying to get at is that I want the tag info for the tag "photography", and I want the date, and I want a count of any comments a tag may have. This following query gets back all the ...
2
by: shengmin.ruan | last post by:
when i use delegate like this: ----------------- protected override void WndProc(ref Message m) { delegate_ReplyFromDataProcess = new...
5
by: tkondal | last post by:
Hi all. I just started looking at Python's ctypes lib and I am having trouble using it for a function. For starters, here's my Python code: from ctypes import*; myStringDLL=...
2
by: holdench | last post by:
I've got two important tables. One has a list of questions within categories... One has a rating for questions the user chose to give points to... Columns in table1 (questions) are: id...
1
by: yasinirshad | last post by:
Hi.. how to use joins for 4 tables.. in my query i have CALLS,STATUS,USERS,MESSAGES (4 tables). Query: "SELECT CALLS.CALL_ID, REQUESTOR, USERS.USER_NAME REQUESTOR_NAME, DESCRIPTION, TYPE, SCOPE,...
5
AdusumalliGopikumar
by: AdusumalliGopikumar | last post by:
can anybody write a query using where,group by ,and having and explain me
3
nathj
by: nathj | last post by:
Hi everyone, I am currently working on a project where data is being passed from the cilent side to the server side quite a lot. I thought that AJAX and JSON would be perfect for this. I have...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.