LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2016, 09:48 PM   #1
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Rep: Reputation: 108Reputation: 108
sqlite group by ... having ..... (then select all of them)


Hya,

I am using sqlite.

I would like to have
Code:
1   dog
1   cow
3   pig
3   horse
out of
Code:
1   dog
1   cow
1   apple
3   pig
3   horse
5   orange
8   peach
9   panda
The 1st column is ID, 2nd is whatever. I want to select all ID's associated to more than two animals and all of them.
I do not need red lines, (not animal). I do not need "9 panda", because it is associated to only one animal.

Following what I did.
Code:
select ID, whatever form table
where whatever in (dog, cow, pig, horse, panda)
group by ID
having count(*) >1
But what I get looks like
Code:
1   dog
3   pig
My textbook of general SQL says "it is not allowed to select "non-group by" column. (in this case "whatever") Also, this web page for mySQL reads something similar and history.

I can go back to the table and select with ID. but "1 apple" shows up, so additional step(s) is necessary. (error prone and waste of resources)

How does everybody survive this situation?

Thanks in advance.

cheers
 
Old 09-29-2016, 03:10 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
select t1.* from table t1 where
t1.ID in (select t2.ID from table t2 ... group by t2.ID having count(*) >1)
 
Old 09-29-2016, 09:53 PM   #3
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Original Poster
Rep: Reputation: 108Reputation: 108
Thanks for your comment.

Yes, it is one of solutions. A little bit tweak is necessary to avoid "1 apple".

Most probably, to make a new table with "whatever in (animal names....)" then go to your trick is less error prone.



Here is another web site.
 
Old 10-12-2016, 08:30 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
1.Have another table say ANIMALS which would have only the animal names you need to choose in a field called ANIMAL_NAME as records(dog, cow, pig, horse, panda)
2. Essentially enhancement of NEVEMTEME's solution in post #2.
Quote:
select t1.* from table t1 where
t1.ID in (select t2.ID from table t2, animals where t2.whatever=animal_name group by t2.ID having count(*) >1)
 
Old 10-17-2016, 06:49 PM   #5
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Original Poster
Rep: Reputation: 108Reputation: 108
Hya

Apology for delay.

Yes, your idea works.

cheers

(why is this NOT an easy task?)
 
Old 10-17-2016, 07:52 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by kaz2100 View Post
(why is this NOT an easy task?)
Because the data model (i.e. table structure) does not support the desired query.

For one thing, what is an "animal"? The model does not know so you must tell it - for two purposes - count and final select... messy.

Also, although you can name a column anything you like, ID is generally a unique identifier, but it is not so in your table. There is no unique identifier in fact.

The model (such as it exists) is just a table, not relational data - look into "normalization".

A properly modeled and normalized relational data structure (as opposed to just a table) would make such a query very efficient and easy for huge data sets. SQL is a relational query language, not a table query language. Think in sets (i.e., relationally) in your model, and in your queries, and they will take on their natural power with ease!

Last edited by astrogeek; 10-17-2016 at 08:02 PM. Reason: tpos, typs, typos
 
1 members found this post helpful.
Old 10-19-2016, 09:05 PM   #7
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Original Poster
Rep: Reputation: 108Reputation: 108
Hya,

Thanks for your comment.

Yes, the definition of animals is omitted for simplicity.

I did not think of modifying (normalize) the original table. Yes, it makes sense.

cheers

P.S.
I was trying to fiddle with group_concat() and additional tweaks.
 
  


Reply

Tags
select, sql, sqlite


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to select all the software in a group ztdep Debian 4 03-16-2015 07:01 PM
Sample SQLite read, write, select, compare statements for C++ miamagoo Programming 5 06-16-2010 09:05 AM
[SOLVED] MYSQL select ORDER BY, GROUP BY together sqn Programming 5 04-19-2010 06:07 AM
How can I select all files except a group? fpp666 Programming 3 07-16-2009 07:31 AM
SQL SELECT Group by 2 conditions MicahCarrick Programming 1 01-30-2007 10:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration