Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 selecting rows with specific IDs

Author  Topic 

lasqlhelp
Starting Member

14 Posts

Posted - 2008-10-01 : 00:47:51
Hi,

I'm working hard on SQL and I came across a hurdle I'm hoping you can help me out with.

I have two tables

table1
headers: chrom start end name score strand
11   9720685 9720721 U0      0       +
21 9721043 9721079 U0 0 -
1 9721093 9721129 U0 0 +
20 9721485 9721521 U0 0 +
22 9721863 9721899 U0 0 -
21 9721863 9721899 U0 0 -
21 9721871 9721907 U0 0 -
21 9721872 9721908 U0 0 +
5 9722423 9722459 U0 0 +
4 9722434 9722470 U0 0 +


and table 2
headers: chrom start end name score strand
21   9719765 9749365 DOM1158 61.7441 +
21 10056465 10211865 DOM1159 321.867 +
1 31678598 31859898 DOM1160 73.8664 +
21 32133698 32300198 DOM1161 152.658 +
3 32599898 32910698 DOM1162 435.853 +
21 33177098 33447198 DOM1163 326.637 +
2 33669198 33728398 DOM1164 46.8648 +
21 33824598 34013298 DOM1165 116.821 +
20 34120598 34246398 DOM1166 197.878 +
20 34412898 34505398 DOM1167 98.2537 +


both tables contain 100+ entries and column one has numbers from 1-22.

my objective is to try to run this command

>select count(*) from table1, table2 where table1.start >= table2.start and table1.end <= table2.end;

but I want to get the information for rows matching chrom=21 only.

I've tried group by and then having, but it doesn't seem to work.


Can someone help me out? Thanks.

aoriju
Posting Yak Master

156 Posts

Posted - 2008-10-01 : 00:58:32
Hello,

You Can Use Exists or In Operator for getting the row matching chrom =21 only
Go to Top of Page

lasqlhelp
Starting Member

14 Posts

Posted - 2008-10-01 : 01:03:12
I see so I would simply type:

SELECT count(*) FROM table1, table2 WHERE table1.chrom IN 21 AND table1.start >= table2.start AND table1.end <= table2.end;
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-01 : 01:10:10
quote:
Originally posted by lasqlhelp

I see so I would simply type:

SELECT count(*) FROM table1, table2 WHERE table1.chrom IN =21 AND table1.start >= table2.start AND table1.end <= table2.end;


for single value comparisons use = instead of IN
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-01 : 01:12:07
quote:
Originally posted by lasqlhelp

I see so I would simply type:

SELECT count(*) FROM table1, table2 WHERE table1.chrom IN 21 AND table1.start >= table2.start AND table1.end <= table2.end;


also do you just want to look for start and end values alone? i think you need to match on chrom values as well.
Go to Top of Page

lasqlhelp
Starting Member

14 Posts

Posted - 2008-10-01 : 01:17:52
well, is there a way to get the count value for each chrom?

output
1: COUNT VALUE
2: COUNT VALUE
3: COUNT VALUE
4: COUNT VALUE
.
.
.
22: COUNT VALUE

So I have table1.start >= table2.start AND table1.end <= table2.end for each matching chrom?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-01 : 01:23:47
[code]SELECT t1.chrom,COUNT(*)
from table1 t1
INNER JOIN table2 t2
ON t2.chrom=t2.chrom
WHERE t1.chrom=21
and t1.start >= t2.start
and t1.end <= t2.end
GROUP BY t1.chrom[/code]
Go to Top of Page

lasqlhelp
Starting Member

14 Posts

Posted - 2008-10-01 : 01:33:41
thanks for the help. it gave me an unexpected result in my two table i have
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-01 : 01:34:25
quote:
Originally posted by lasqlhelp

thanks for the help. it gave me an unexpected result in my two table i have


then post some sample data and give your expected result out of them.
Go to Top of Page

lasqlhelp
Starting Member

14 Posts

Posted - 2008-10-01 : 01:53:05
well for the example above,

I would assume the result should be

21 4

where 21 is the common id and 4 is the count result.
Go to Top of Page

lasqlhelp
Starting Member

14 Posts

Posted - 2008-10-01 : 01:58:38
never mind, i'm getting the result
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-01 : 02:00:21
not sure what you want

declare @test1 table
(
chrom int,
start int,
[end] int,
name varchar(10),
score decimal(10,4),
strand char(1)
)
declare @test2 table
(
chrom int,
start int,
[end] int,
name varchar(10),
score decimal(10,4),
strand char(1)
)

insert into @test1

select 11, 9720685, 9720721, 'U0', 0, '+' union all
select 21 , 9721043, 9721079 ,'U0' ,0 ,'-' union all
select 1 ,9721093, 9721129 ,'U0' ,0 ,'+' union all
select 20 , 9721485, 9721521, 'U0' , 0 , '+' union all
select 22 , 9721863, 9721899 ,'U0' , 0 , '-' union all
select 21 , 9721863, 9721899 ,'U0' , 0 , '-' union all
select 21 , 9721871, 9721907 ,'U0' , 0 , '-' union all
select 21 , 9721872, 9721908, 'U0' , 0 , '+' union all
select 5 ,9722423, 9722459 ,'U0' ,0 ,'+' union all
select 4 ,9722434, 9722470 ,'U0' ,0 ,'+'



insert into @test2
select 21, 9719765, 9749365, 'DOM1158', 61.7441 ,'+' union all
select 21, 10056465, 10211865 , 'DOM1159', 321.867, '+' union all
select 1, 31678598, 31859898, 'DOM1160', 73.8664, '+' union all
select 21, 32133698, 32300198 , 'DOM1161', 152.658, '+' union all
select 3, 32599898, 32910698, 'DOM1162', 435.853, '+' union all
select 21, 33177098, 33447198, 'DOM1163', 326.637, '+' union all
select 2, 33669198, 33728398, 'DOM1164', 46.8648, '+' union all
select 21, 33824598, 34013298, 'DOM1165', 116.821 ,'+' union all
select 20, 34120598, 34246398, 'DOM1166', 197.878, '+' union all
select 20, 34412898, 34505398, 'DOM1167' ,98.2537, '+'

--soln 1
select t1.chrom,count(*)
from @test1 t1
join @test2 t2
on t1.chrom=t2.chrom
where t1.start>=t2.start
and t1.[end] <= t2.[end]
group by t1.chrom


--soln2
select t1.chrom,count(*)
from @test1 t1
join @test2 t2
on t1.start>=t2.start
and t1.[end] <= t2.[end]
where t2.chrom=21
group by t1.chrom

output
------------------
soln1
-----------------
chrom count
21 4

soln2
--------------------
chrom count
1 1
4 1
5 1
11 1
20 1
21 4
22 1
Go to Top of Page

lasqlhelp
Starting Member

14 Posts

Posted - 2008-10-01 : 02:15:39
wow, looks great. I tried coping your first statement with the declare into my sql (I'm using sqlite) but it says sql error: near "declare": syntax error

any idea?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-01 : 02:18:20
quote:
Originally posted by lasqlhelp

wow, looks great. I tried coping your first statement with the declare into my sql (I'm using sqlite) but it says sql error: near "declare": syntax error

any idea?


not sure abt sqllite.i've not used it.
Go to Top of Page

lasqlhelp
Starting Member

14 Posts

Posted - 2008-10-01 : 02:18:54
what do you use? that's free and compatible with my mac system?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-01 : 02:26:24
quote:
Originally posted by lasqlhelp

what do you use? that's free and compatible with my mac system?


i use sql server managment studio
Go to Top of Page

lasqlhelp
Starting Member

14 Posts

Posted - 2008-10-01 : 13:21:37
Hi,

Is there one that I can use on a Mac that would help me with the code you posted so I can test it?
Go to Top of Page

lasqlhelp
Starting Member

14 Posts

Posted - 2008-10-01 : 14:11:33
hi, installed mysql on my mac.

but when i copied the first 'declare statement' i get this error

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'declare @test1 table


any ideas?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-01 : 14:15:53
quote:
Originally posted by lasqlhelp

hi, installed mysql on my mac.

but when i copied the first 'declare statement' i get this error

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'declare @test1 table


any ideas?


what i gave is MS SQL Server syntax which may not be same in others dbs also. If you're using MySQL post your question in some MySQL forums as then you will get solutions specific to MySQL. This is MS SQL Server forum and all solutions provided here will be MS SQL Server specific.
Go to Top of Page

lasqlhelp
Starting Member

14 Posts

Posted - 2008-10-01 : 14:19:39
okay, that makes more sense. i'll try to use my PC.
Go to Top of Page
   

- Advertisement -