SQL query for unique values.

SQL query for unique values.

am 15.02.2010 13:30:46 von ravishankar_bv

--_000_174230991E95D743B0C91DF471EF44E843D3EB8EC1MTW02MSG02m in_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi,

I'm very new to SQL and databases. I need a query for the following: (I'm =
sure google would have found the answer, but I could not really frame the s=
entence for the task I'm looking for. Also, please let me know how do I se=
arch in google for such tasks - so that I can try it myself in future).

Assume I have a table like:

Sl No

Fruit

Grown in states

1

Apple

KA

2

Orange

AP

3

Banana

TN

4

Jackfruit

MH

5

Mango

MP

6

Jackfruit

MP

7

Banana

AP

8

Mango

KA

9

Banana

TN

10

Apple

MH

11

Jackfruit

AP

12

Orange

MH

13

Mango

KA

14

Apple

TN

15

Banana

MP

16

Banana

MH

17

Mango

KA

18

Orange

MP

19

Jackfruit

AP

20

Apple

TN


From the above table, I want a SQL query which will list me the unique frui=
ts and the states in which they are grown, like:

Apple: KA, MH, TN
Banana: TN, AP, MP, MH
Jackfruit: MH,MP,AP
Mango: MP, KA
Orange: AP,MH,MP

Thanks in advance for the help,
Ravi.

________________________________
http://www.mindtree.com/email/disclaimer.html

--_000_174230991E95D743B0C91DF471EF44E843D3EB8EC1MTW02MSG02m in_--

Re: SQL query for unique values.

am 15.02.2010 14:08:04 von Manasi Save

Hi,

A simple group by function should work for this:

Select Fruit,GrownInStates From tbl1 Group By Fruit;

and if you want grownstates in comma separated format then you can use
Group_Concat function

Select Fruit, Group_Concat(GrownInStates, SEPARATOR ',') From tbl1
Group By Fruit;

Hope this will work fine.

--
Regards,
Manasi Save

Quoting "Ravishankar BV." :
> Hi,
>
> I'm very new to SQL and databases. I need a query for the following:
> (I'm sure
> google would have found the answer, but I could not really frame the sentence
> for the task I'm looking for. Also, please let me know how do I search in
> google for such tasks - so that I can try it myself in future).
>
> Assume I have a table like:
>
> Sl No
>
> Fruit
>
> Grown in states
>
> 1
>
> Apple
>
> KA
>
> 2
>
> Orange
>
> AP
>
> 3
>
> Banana
>
> TN
>
> 4
>
> Jackfruit
>
> MH
>
> 5
>
> Mango
>
> MP
>
> 6
>
> Jackfruit
>
> MP
>
> 7
>
> Banana
>
> AP
>
> 8
>
> Mango
>
> KA
>
> 9
>
> Banana
>
> TN
>
> 10
>
> Apple
>
> MH
>
> 11
>
> Jackfruit
>
> AP
>
> 12
>
> Orange
>
> MH
>
> 13
>
> Mango
>
> KA
>
> 14
>
> Apple
>
> TN
>
> 15
>
> Banana
>
> MP
>
> 16
>
> Banana
>
> MH
>
> 17
>
> Mango
>
> KA
>
> 18
>
> Orange
>
> MP
>
> 19
>
> Jackfruit
>
> AP
>
> 20
>
> Apple
>
> TN
>
>
> From the above table, I want a SQL query which will list me the unique fruits
> and the states in which they are grown, like:
>
> Apple: KA, MH, TN
> Banana: TN, AP, MP, MH
> Jackfruit: MH,MP,AP
> Mango: MP, KA
> Orange: AP,MH,MP
>
> Thanks in advance for the help,
> Ravi.
>
> ________________________________
> http://www.mindtree.com/email/disclaimer.html


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org