Solved

DB Select - Logic Meltdown!

Posted on 2011-09-17
5
420 Views
Last Modified: 2012-05-12
Hello Experts - I've been struggling with this one for a while.
[sample table]
----------------------
ITEM         GROUP
----------------------
4            A
7            A
3            A
8            A
7            B
6            B
9            B
0            C
4            C
2            C
5            C
4            C
7            C
5            D
9            D
2            E
7            E
1            E
4            E
7            F
3            F
9            F
6            F
8            G
4            H
5            H
3            H
9            H
0            H
8            H



I need an sql query that will tell me how many times each "ITEM", on a range of SELECT DISTINCT ITEM appeared with one another in a particular group.  

IE:
Items 4 and 8 appeared 2 times (groups A and H).
Items 0 and 4 appeared 2 times (groups C and H).
Items 7, 6 and 9 appeared appeared 2 times (groups B and F).

Can it be done?  Thanks
0
Comment
Question by:rita_corp
  • 2
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36554697
so, you want to find out the "pairs" of values that appear in the same group(s)?
0
 

Author Comment

by:rita_corp
ID: 36554711
yes (pardon) pairs of items (two items or greater) in the same group(s)
0
 
LVL 4

Accepted Solution

by:
Fugas earned 500 total points
ID: 36555707
Hi, I don't know what you really need, but this could help you
select item, group_concat(DISTINCT `group` order by `group` ASC) from items group by item;
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36556742
let's try:
select i.item item_1, i2.item_2, i.group group_1, i2.group group_2
from yourtable i
join yourtable oi
  on o.group = i.group
 and o.item <> i.item
join yourtable og
  on og.item = i.item
  and og.group <> i.group
join yourtable i2
  on i2.item = oi.item
 and i2.group = og.group 

Open in new window


0
 

Author Closing Comment

by:rita_corp
ID: 36558187
Wow - what a gem group_concat was.  Definitely have application for this in the future!  I'm trying to see if I can get this functionality over oracle, but this was a fantastic start!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query group by data in SQL Server - cursor? 3 47
What's the best way to programatically have a MySQL DB update another MySQL DB 4 67
Currency in SQL? 2 30
SQL Syntax 14 34
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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