473,386 Members | 1,674 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,386 software developers and data experts.

select distinct with conditions attached

170 100+
I'll try to explain the problem, but it may not make sense.

I have a mysql table with columns like id,title,speaker,day,month,year, topic etc.

At present, my syntax is
Expand|Select|Wrap|Line Numbers
  1. select * from files order by year, month, day
What this does is return all the entries as requested. However, initially, when an entry is made, it can have up to 3 distinct topics associated with it. So in the table, id1, id2 and id3 will have exactly the same info in every column except for the topic column.

Obviously, when I run the above select statement it prints out the details for each entry, id1, id2 and id3 as separate entries (as it's supposed to).

What I want is for mysql to look for any entries that are identical except for topic. And then instead of listing all 3 entries, list only 1 entry. When I print to the screen the information on topic, it would say "health, wealth, happiness".

At the moment there are 3 distinct entries. The first will say topic: "health". The second "wealth" etc etc.

I can't just use a select distinct title statement, as its possible that there may be two entries with exactly the same title, but different day, month and year.

Below I've put what it currently prints and what I need it to print. Hopefully someone(s) can interpret my request and point me in the right direction.

Thanks.

Currently:

Title: Making Money
Date: 07/09/28
Topic: money

~~~~~~~~~~~~~~~

Title: Making Money
Date: 07/09/28
Topic: happiness


What I need:

Title: Making Money
Date:07/09/28
Topic: money, happiness
Nov 19 '07 #1
7 2141
pradeep kaltari
102 Expert 100+
Currently:

Title: Making Money
Date: 07/09/28
Topic: money

~~~~~~~~~~~~~~~

Title: Making Money
Date: 07/09/28
Topic: happiness


What I need:

Title: Making Money
Date:07/09/28
Topic: money, happiness
Hi,
As per my understanding about your problem, this is what you are looking for:

Expand|Select|Wrap|Line Numbers
  1. Select title, date, GROUP_CONCAT(topic)
  2. From <your_table>
  3. Group By title, date
  4. Order By title, date
  5.  
Let me know if you were looking for something else.

-Pradeep
Nov 20 '07 #2
beary
170 100+
Hi,
As per my understanding about your problem, this is what you are looking for:

Expand|Select|Wrap|Line Numbers
  1. Select title, date, GROUP_CONCAT(topic)
  2. From <your_table>
  3. Group By title, date
  4. Order By title, date
  5.  
Let me know if you were looking for something else.

-Pradeep

Just about there...

My statement is now "select *, GROUP_CONCAT(topic) from files group by title order by year, month, day"

It has successfully eliminated the repeated entries. However, next to Topic: it only shows the first topic, not the others.

So now it looks like...

Title: Making Money
Date:07/09/28
Topic: money

instead of what I need it to look like:

Title: Making Money
Date:07/09/28
Topic: money, happiness

I figure it's something minor to get that working.

Thanks so far for your help and I'm looking forward to the solution.
Nov 20 '07 #3
pradeep kaltari
102 Expert 100+
Just about there...

My statement is now "select *, GROUP_CONCAT(topic) from files group by title order by year, month, day"

It has successfully eliminated the repeated entries. However, next to Topic: it only shows the first topic, not the others.

So now it looks like...

Title: Making Money
Date:07/09/28
Topic: money

instead of what I need it to look like:

Title: Making Money
Date:07/09/28
Topic: money, happiness

I figure it's something minor to get that working.

Thanks so far for your help and I'm looking forward to the solution.
Hi beary,
Please include "date" column in Group By clause and also make sure you have GROUP_CONCAT() function in the Select clause.

For more details on GROUP_CONCAT() please refer:
GROUP_CONCAT

In case of further doubts plz get back.

Thanks,
pradeep
Nov 20 '07 #4
beary
170 100+
Hi beary,
Please include "date" column in Group By clause and also make sure you have GROUP_CONCAT() function in the Select clause.
It now reads "select *, GROUP_CONCAT(topic) from files group by year, month, day, title order by year, month, day"

Still no change...

I am clearly doing something wrong, so I thought I'd post the sql for creating the table, and inserting the records followed by my required output. Perhaps you can create the statement which will work after testing it. (If you would be so good!)

CREATE TABLE `files` (
`id` tinyint(6) NOT NULL auto_increment,
`year` text NOT NULL,
`month` text NOT NULL,
`day` text NOT NULL,
`series` text NOT NULL,
`title` text NOT NULL,
`speaker` text NOT NULL,
`topic` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;


INSERT INTO `files` (`id`, `year`, `month`, `day`, `series`, `title`, `speaker`, `topic`) VALUES
(12, '07', '01', '01', 'hope', 'money and happiness', 'J Avery', 'money'),
(13, '07', '01', '01', 'hope', 'money and happiness', 'J Avery', 'wealth')

What I need displayed is:

Title: Money and happiness
Date: 07/01/01
Speaker: J Avery
Topic(s): money, wealth

Any hope?
Nov 20 '07 #5
pradeep kaltari
102 Expert 100+
It now reads "select *, GROUP_CONCAT(topic) from files group by year, month, day, title order by year, month, day"

Still no change...

I am clearly doing something wrong, so I thought I'd post the sql for creating the table, and inserting the records followed by my required output. Perhaps you can create the statement which will work after testing it. (If you would be so good!)

CREATE TABLE `files` (
`id` tinyint(6) NOT NULL auto_increment,
`year` text NOT NULL,
`month` text NOT NULL,
`day` text NOT NULL,
`series` text NOT NULL,
`title` text NOT NULL,
`speaker` text NOT NULL,
`topic` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;


INSERT INTO `files` (`id`, `year`, `month`, `day`, `series`, `title`, `speaker`, `topic`) VALUES
(12, '07', '01', '01', 'hope', 'money and happiness', 'J Avery', 'money'),
(13, '07', '01', '01', 'hope', 'money and happiness', 'J Avery', 'wealth')

What I need displayed is:

Title: Money and happiness
Date: 07/01/01
Speaker: J Avery
Topic(s): money, wealth

Any hope?
Hi beary,
I checked it with your table structure - the query works absolutely FINE.

Please try the following:
Expand|Select|Wrap|Line Numbers
  1. select year, month, day, title, speaker, GROUP_CONCAT(topic) as TOPICS 
  2. from files group by year, month, day, title 
  3. order by year, month, day
  4.  
Your topics "money,wealth" will be under TOPICS column.

- Pradeep
Nov 21 '07 #6
beary
170 100+
Hi beary,
I checked it with your table structure - the query works absolutely FINE.

Please try the following:
Expand|Select|Wrap|Line Numbers
  1. select year, month, day, title, speaker, GROUP_CONCAT(topic) as TOPICS 
  2. from files group by year, month, day, title 
  3. order by year, month, day
  4.  
Your topics "money,wealth" will be under TOPICS column.

- Pradeep
Pradeep,

Thankyou so much for sticking with me on this one! It now works perfectly, as you said. It's nice to learn a completely new (to me) and very cool mysql thing.

I was wondering why it wouldn't work for me. The only essential difference between your first post in response and the last one is that in the last one you had group_concat(topic) as topics, whereas in the 1st the phrase "as topics" was not there. Perhaps that has been the issue.

Regardless, I'm grateful for your help!
Nov 21 '07 #7
pradeep kaltari
102 Expert 100+
Pradeep,

Thankyou so much for sticking with me on this one! It now works perfectly, as you said. It's nice to learn a completely new (to me) and very cool mysql thing.

I was wondering why it wouldn't work for me. The only essential difference between your first post in response and the last one is that in the last one you had group_concat(topic) as topics, whereas in the 1st the phrase "as topics" was not there. Perhaps that has been the issue.

Regardless, I'm grateful for your help!
Your Welcome!

Glad to have helped you.

Rgds,
Pradeep
Nov 21 '07 #8

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

Similar topics

2
by: Bill Sneddon | last post by:
I am using the for-each below to build a table where the header is a unique nonblank EQUIP_TYPE. This works well. What I would like to do is not execute the for loop on on EQUIP_TYPE unless one...
1
by: anmar | last post by:
I'm trying to figure out how to select all the records in one table which have multiple specified records in a second table. Here's a simplified version of my problem. I have two tables,...
4
by: stoppal | last post by:
I need some help. I am trying to write a query which does the following SELECT * from table1 where field1=(SELECT distinct field1 FROM table1 WHERE field2='2005' or field2='2010')
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
18
by: mathilda | last post by:
My boss has been adamant that SELECT DISTINCT is a faster query than SELECT all other factors being equal. I disagree. We are linking an Access front end to a SQL Server back end and normally are...
2
by: b. hotting | last post by:
Hi, Which msAccess SQL guru can help my out here? i would like a list of sales(count) per day per customer end result, something like: distinct(date), customer, cnt(ItemsSold) 20/5/2005, c1,...
6
by: Bob Stearns | last post by:
I am getting duplicate rows back from a select distinct statement of the form: SELECT DISTINCT 'jhough', '000111', t0.bhid FROM (SELECT lots of good stuff) t0 LEFT OUTER JOIN another_table ...
5
by: rAinDeEr | last post by:
Hi, I have a web application with a table to store terms and conditions of a Company. This may some times run into many pages and some times it may be just a few sentences. It is a character...
1
by: fong.yang | last post by:
I have a tbl with a dealernumber field and zipcode field. Each dealer number can have multiple zipcodes attached to it. I do I select just one instance of the dealer number and all of the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.