473,473 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL a range in the all dataset

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Greetings:

I have a dataset of two columns:
price amount
99.5 10000
99.7 8000
100 3000
100.1 1000
100.5 500
100.8 1500
105 2000
200 100
etc
I have to write a SQL query on how many price tags are within [price+-1]
such as 98.5 to 100.5, 100+-1,etc for each records.
Here I know the price tags counts are 5 for 99.5-100.5, 5 for
99.7-100.7, 6 for 99-101, etc
How should I do for all of the records?

Thanks in advance.

Wei
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFwozpllCA8yArcwwRAlzhAJ0dXvJeN8r5tCMwbikokr I9qXok0ACfcWA9
4WJ90KIbVaXu6aznolw8CDE=
=K+6d
-----END PGP SIGNATURE-----
Feb 2 '07 #1
4 1687
On Feb 1, 4:59 pm, Wei ZOU <w...@ucdavis.eduwrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Greetings:

I have a dataset of two columns:
price amount
99.5 10000
99.7 8000
100 3000
100.1 1000
100.5 500
100.8 1500
105 2000
200 100
etc
I have to write a SQL query on how many price tags are within [price+-1]
such as 98.5 to 100.5, 100+-1,etc for each records.
Here I know the price tags counts are 5 for 99.5-100.5, 5 for
99.7-100.7, 6 for 99-101, etc
How should I do for all of the records?

Thanks in advance.

Wei
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org

iD8DBQFFwozpllCA8yArcwwRAlzhAJ0dXvJeN8r5tCMwbikokr I9qXok0ACfcWA9
4WJ90KIbVaXu6aznolw8CDE=
=K+6d
-----END PGP SIGNATURE-----
SELECT COUNT(*)
FROM MyTable
WHERE price BETWEEN startPrice AND endPrice;

Feb 2 '07 #2
Wei,

This should work for you:

create table #MyTable (Price decimal(9,2), Amount int)

insert #MyTable select 99.5, 10000
insert #MyTable select 99.7, 8000
insert #MyTable select 100, 3000
insert #MyTable select 100.1, 1000
insert #MyTable select 100.5, 500
insert #MyTable select 100.8, 1500
insert #MyTable select 105, 2000
insert #MyTable select 200, 100

select
cast(Price + .5 as int) Price
, sum(Amount) SumOfAmount
from #MyTable
group by
cast(Price + .5 as int)
order by
cast(Price + .5 as int)

-- Bill

"Wei ZOU" <wz**@ucdavis.eduwrote in message
news:ep**********@skeeter.ucdavis.edu...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Greetings:

I have a dataset of two columns:
price amount
99.5 10000
99.7 8000
100 3000
100.1 1000
100.5 500
100.8 1500
105 2000
200 100
etc
I have to write a SQL query on how many price tags are within [price+-1]
such as 98.5 to 100.5, 100+-1,etc for each records.
Here I know the price tags counts are 5 for 99.5-100.5, 5 for
99.7-100.7, 6 for 99-101, etc
How should I do for all of the records?

Thanks in advance.

Wei
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFwozpllCA8yArcwwRAlzhAJ0dXvJeN8r5tCMwbikokr I9qXok0ACfcWA9
4WJ90KIbVaXu6aznolw8CDE=
=K+6d
-----END PGP SIGNATURE-----

Feb 2 '07 #3
AlterEgo wrote:
"Wei ZOU" <wz**@ucdavis.eduwrote in message
news:ep**********@skeeter.ucdavis.edu...
>I have a dataset of two columns:
price amount
99.5 10000
99.7 8000
100 3000
100.1 1000
100.5 500
100.8 1500
105 2000
200 100
etc
I have to write a SQL query on how many price tags are within [price+-1]
such as 98.5 to 100.5, 100+-1,etc for each records.
Here I know the price tags counts are 5 for 99.5-100.5, 5 for
99.7-100.7, 6 for 99-101, etc
How should I do for all of the records?
create table #MyTable (Price decimal(9,2), Amount int)

insert #MyTable select 99.5, 10000
insert #MyTable select 99.7, 8000
insert #MyTable select 100, 3000
insert #MyTable select 100.1, 1000
insert #MyTable select 100.5, 500
insert #MyTable select 100.8, 1500
insert #MyTable select 105, 2000
insert #MyTable select 200, 100

select
cast(Price + .5 as int) Price
, sum(Amount) SumOfAmount
from #MyTable
group by
cast(Price + .5 as int)
order by
cast(Price + .5 as int)
(Please don't top-post. Fixed.)

This is wrong in a couple ways. Steve's attempt is wrong in a couple
other ways. I believe this matches the original specs:

select a.Price, count(b.Amount) TagCount
from #MyTable a
join #MyTable b on abs(a.Price - b.Price) <= 1.0
group by a.Price
order by a.Price
Feb 2 '07 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thank you guys all for the help. Ed's code works. I thought of using a
VB procedure to do this. But now......Very cool!
select a.Price, count(b.Amount) TagCount
from #MyTable a
join #MyTable b on abs(a.Price - b.Price) <= 1.0
group by a.Price
order by a.Price
Ed Murphy wrote:
AlterEgo wrote:
>"Wei ZOU" <wz**@ucdavis.eduwrote in message
news:ep**********@skeeter.ucdavis.edu...
>>I have a dataset of two columns:
price amount
99.5 10000
99.7 8000
100 3000
100.1 1000
100.5 500
100.8 1500
105 2000
200 100
etc
I have to write a SQL query on how many price tags are within [price+-1]
such as 98.5 to 100.5, 100+-1,etc for each records.
Here I know the price tags counts are 5 for 99.5-100.5, 5 for
99.7-100.7, 6 for 99-101, etc
How should I do for all of the records?
>create table #MyTable (Price decimal(9,2), Amount int)

insert #MyTable select 99.5, 10000
insert #MyTable select 99.7, 8000
insert #MyTable select 100, 3000
insert #MyTable select 100.1, 1000
insert #MyTable select 100.5, 500
insert #MyTable select 100.8, 1500
insert #MyTable select 105, 2000
insert #MyTable select 200, 100

select
cast(Price + .5 as int) Price
, sum(Amount) SumOfAmount
from #MyTable
group by
cast(Price + .5 as int)
order by
cast(Price + .5 as int)

(Please don't top-post. Fixed.)

This is wrong in a couple ways. Steve's attempt is wrong in a couple
other ways. I believe this matches the original specs:

select a.Price, count(b.Amount) TagCount
from #MyTable a
join #MyTable b on abs(a.Price - b.Price) <= 1.0
group by a.Price
order by a.Price
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFw9ZcllCA8yArcwwRAmrEAJ9uz9pLcuqPXyjjT+5Pcs NliPd6yQCfQfr3
ttkljmItZFg4q4qSGRB1G6o=
=qTpu
-----END PGP SIGNATURE-----
Feb 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: bardo | last post by:
Hi all, I have a Access *.MDB database. With multiple tables. One table (call it Table 1) is linked to all the others. (column is name indexID). If I load all the tables in a DataSet...
1
by: dedipya | last post by:
Environment W2003K with Oracle 10 client connected to oracle using oracle odp provider.orcle is on solaries machine. while filling the dataset .net is throughing the error "argument was out of the...
0
by: dalaeth | last post by:
I have searched Google high and low and haven't found anything that works. Here's my problem, hopefully someone will be able to help! I'm using 1.1 Framework, and ODP.NET 9.5.0.7 on a Windows...
4
by: TCook | last post by:
Hey All, I've seen lots of stuff regarding pumping data into Excel from .Net but haven't found anything for the reverse. Does anyone have a snippet, article ID, etc. demonstrating how to take...
2
by: Chapi | last post by:
hi, i'm having problems with a datagrid paging. it's populates fine, but when i click the paging button appears this error:"Specified argument was out of the range of valid values.Parameter name:...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.