Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

Sql Query

Tapas Jha
Valued Contributor

Sql Query

Hi,

I have one small query. My query is like below:
"Display all columns from table1 for the uniq(column1) which has more than one column6"

That means there are some records in the table where for the same column query there should be one record but wrongly there is not. For that i want to extract those records which has more than one column6 for the same column1.

Rgds
Tapas
Tapas Jha
13 REPLIES
Brian Crabtree
Honored Contributor

Re: Sql Query

Uhmm. Can you explain a little bit more, or post an example of what you mean?

Brian
Indira Aramandla
Honored Contributor

Re: Sql Query

Hi Tapas,

From you query I understand that you have someting like this.

For example a table has two columns, column1 and column6. For same value of column1 you have multiple entries of column6.

The following sql will list the value of column1 and the number of column6 values associated with the same value of column1.

Here are the values in emp table
column1 column6
---------- ----------
101 AAAAAAA
101 SSSSSSS
101 BBBBBBB
102 xxxxxxxx
103 mmmmm

select count(1), column1
from emp
having count(1) > 1
group by column1;

COUNT(1) ENO
---------- ----------
3 101
2 102

I hope this helps.

Indira A.
Never give up, Keep Trying
Yogeeraj_1
Honored Contributor

Re: Sql Query

hi tapas,

i tried the following simple example:
============================================
yd@MYDB.MU> select * from tab1;

C
_
b
c
a

Elapsed: 00:00:00.00
yd@MYDB.MU> select * from tab2;

C
_
a
a
a
a
a
b
b
c

8 rows selected.

Elapsed: 00:00:00.01
yd@MYDB.MU> select *
from tab1
where column1 in (select column6 from tab2 where column6=column1 having count(*) > 1
group by column6);

C
_
b
a

Elapsed: 00:00:00.01
yd@MYDB.MU>
============================================

let us know it this helps!
regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Tapas Jha
Valued Contributor

Re: Sql Query

Hi All,

I am sorry that your sql didn't work. May be i have clearly stated you.

Below is the sample output. I hope it will clear your doubt.
If you see you will find that for tstAB=4839530267 there are two records. I want those records seperately. How can i get? Waiting for your feedback.

Rgds
Tapas

tstkey 44964913
tstAB 5637791967
tstacct 159392515
tstsndr
tstshpr ABC
tstscity
tstsstt
tstscnty
tstszip 3240505
tststel 022-22303949
tstattn
tstcnsg bcd
tstcadr1 Addr1
tstcadr2 addr2
tstccity
tstcstt
tstccnty
tstczip 900001
tstctel 011912222303949
tstorig X
tstdest Y
tstori_sub
tstloc_prod
tstgbl_prod A
tstpcs 1
tstwght 1.4
tstwgtu K
tstdwght 0.0
tstdtawb
tstdtpu 05/01/2004
tsttmpu 19:25
tstisogmtpu 800
tstdtky
tsttmky 19:25
tstisogmtky 800
tstrte
tstbill
tstfrom F@XAL
tstvsn 21
tstssndx
tstcsndx
tstdtmadd 1083466422
tstdtmfl 1083464753


tstkey 45482954
tstAB 4839530267
tstacct 121235539
tstsndr MR. X
tstshpr A.P LIBRARY
tstscity
tstsstt
tstscnty
tstszip 9700002
tststel 12122699
tstattn NA
tstcnsg XYZ
tstcadr1 AB Park
tstcadr2 AREA1
tstccity READING
tstcstt
tstccnty
tstczip UY154 4ZZ
tstctel 1235866600
tstorig CHK
tstdest RED
tstori_sub MAL
tstloc_prod X
tstgbl_prod O
tstpcs 1
tstwght 0.5
tstwgtu K
tstdwght 0.0
tstdtawb
tstdtpu 06/03/2004
tsttmpu 19:05
tstisogmtpu -530
tstdtky 06/03/2004
tsttmky 19:08
tstisogmtky -530
tstrte PL18C
tstbill 000000000
tstfrom LSM@CHK
tstvsn 15021
tstssndx
tstcsndx
tstdtmadd 1086278916
tstdtmfl 1086278554


tstkey 45488682
tstAB 4839530267
tstacct 121235539
tstsndr MR. X
tstshpr A.P LIBRARY
tstscity
tstsstt
tstscnty
tstszip 9700002
tststel 12122699
tstattn NA
tstcnsg XYZ
tstcadr1 AB Park
tstcadr2 AREA1
tstccity READING
tstcstt
tstccnty
tstczip UY154 4ZZ
tstctel 1235866600
tstorig CHK
tstdest RED
tstori_sub MAL
tstloc_prod X
tstgbl_prod O
tstpcs 1
tstwght 1.0
tstwgtu K
tstdwght 0.0
tstdtawb
tstdtpu 06/03/2004
tsttmpu 19:05
tstisogmtpu -530
tstdtky 06/03/2004
tsttmky 19:08
tstisogmtky -530
tstrte PL18C
tstbill 000000000
tstfrom LSM@CHK
tstvsn 15021
tstssndx
tstcsndx
tstdtmadd 1086288799
tstdtmfl 1086288406
Tapas Jha
Sanjay Kumar Suri
Honored Contributor

Re: Sql Query

Just check if something like below meets your need (it is not tested).

select * from table1
where column1 exists
(select count(*)
from table1 group by column6
having count(*) > 1);

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Tapas Jha
Valued Contributor

Re: Sql Query

Hi Sanjay,

No, it is not working. It is giving syntax error on exists.

Rgds
Tapas
Tapas Jha
Sanjay Kumar Suri
Honored Contributor

Re: Sql Query

Check the following (it is tested but on different table. Make suitable changes and use:

select * from employees
where department_id in
(select department_id
from employees group by department_id
having count(*) > 1);

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Tapas Jha
Valued Contributor

Re: Sql Query

Sanjay,
Thanx a lot.
Your sql is closing to solution. But i think i need to clarify exactly what i need( i think i have not given clear picture). If you see my example you will find that for the same tstAB(4839530267) there are two tstwght fields. I want those records which have diffrent tstwght fields.
(That means my question is like: Display those records which has different tstwght for the same tstAB fileds)
Is it clear to you or you need more..?

Rgds
Tapas
Tapas Jha
Sanjay Kumar Suri
Honored Contributor

Re: Sql Query

Include the AND clause

select * from employees
where department_id in
(select department_id
from employees group by department_id
having count(*) > 1)
and column2 <> column3;


Just check and try it.

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Tapas Jha
Valued Contributor

Re: Sql Query

Sanjay,

Sorry But I didn't get you. Can you explain by column1 and column2. Here in my case what will be column1 and column2. Since tstwght is the filed which will be different for the same tstAB.
Below is the output from where you can say.( for the same tstAB there are diff tstwght. We need those)
tstkey 45482954
tstAB 4839530267
tstacct 121235539
tstwght 0.5

tstkey 45488682
tstAB 4839530267
tstacct 121235539
tstwght 1.0

Rgds
Tapas
Tapas Jha
Brian Crabtree
Honored Contributor

Re: Sql Query

select tstab, tstwght from tableA where tstab in (select tstab from tableA group by tstab having count(*) > 1);

That should work. Let me know.

Thanks,

Brian
Tapas Jha
Valued Contributor

Re: Sql Query

Hi Brian,

Thanx for your reply. Your query results like below:

tstab tstwght
15581882 2.0
15581882 2.0
206367781 0.5
206367781 0.5
308102874 0.5
308102874 10.5
336157080 0.5
336157080 0.5
339287550 0.5
339287550 1.0
339582261 4.5
339582261 0.5
But i want those which have different tstwght. My expected output will be like:
tstab tstwght
308102874 0.5
308102874 10.5
339287550 0.5
339287550 1.0
339582261 4.5
339582261 0.5

Hope it is clear to you.
Rgds
Tapa
Tapas Jha
Fred Ruffet
Honored Contributor

Re: Sql Query

Maybe you should add tstwght to the group by clause.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)