Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

SQL 2000 Query question

 
   Database Help (Home) -> Programming RSS
Next:  Programming Resources, Articles and News.  
Author Message
TimXox

External


Since: Jun 12, 2008
Posts: 7



(Msg. 1) Posted: Fri Feb 13, 2009 1:46 pm
Post subject: SQL 2000 Query question
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi,

CREATE TABLE Baskets (BasketID INT, ItemID INT, AsOf DATETIME , ItemValue
FLOAT, X FLOAT)

I need to create groups ( AsOf, BasketID ) and order items (ItemID ) in each
group by ItemValue.
And calculate X like this:
x= 1 if the Item located in first half particular group (COUNT(*) /2)
ESLE X= 0

THANK YOU VERY MUCH.
Tim.

Data samples:

BasketID ItemID AsOf ItemValue X
1 14266 2008-08-29 00:00:00.000 -0.0671412258877745 NULL
1 14267 2008-08-29 00:00:00.000 -0.00226765556928243 NULL
1 14269 2008-08-29 00:00:00.000 -0.023545706371 NULL
1 14270 2008-08-29 00:00:00.000 -0.01329305136 NULL
1 14271 2008-08-29 00:00:00.000 0.0249221183799999 NULL
1 14272 2008-08-29 00:00:00.000 0.0152941176470001 NULL
1 14273 2008-08-29 00:00:00.000 -0.00392985053499995 NULL
1 14125 2008-08-29 00:00:00.000 NULL NULL
1 9190 2008-08-29 00:00:00.000 0.0069431258399999 NULL
1 14268 2008-08-29 00:00:00.000 NULL NULL
2 14266 2008-08-29 00:00:00.000 -0.0671412258877745 NULL
2 14267 2008-08-29 00:00:00.000 -0.00226765556928243 NULL
2 14269 2008-08-29 00:00:00.000 -0.023545706371 NULL
2 14270 2008-08-29 00:00:00.000 -0.01329305136 NULL
2 14271 2008-08-29 00:00:00.000 0.0249221183799999 NULL
2 14272 2008-08-29 00:00:00.000 0.0152941176470001 NULL
2 14273 2008-08-29 00:00:00.000 -0.00392985053499995 NULL
2 14125 2008-08-29 00:00:00.000 NULL NULL
2 9190 2008-08-29 00:00:00.000 0.0069431258399999 NULL
2 14268 2008-08-29 00:00:00.000 NULL NULL

 >> Stay informed about: SQL 2000 Query question 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2060



(Msg. 2) Posted: Fri Feb 13, 2009 2:42 pm
Post subject: Re: SQL 2000 Query question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

TimXox ( ) writes:
> CREATE TABLE Baskets (BasketID INT, ItemID INT, AsOf DATETIME , ItemValue
> FLOAT, X FLOAT)
>
> I need to create groups ( AsOf, BasketID ) and order items (ItemID ) in
> each group by ItemValue. And calculate X like this:
> x= 1 if the Item located in first half particular group (COUNT(*) /2)
> ESLE X= 0

In SQL 2005 this should be doable in one query with help of the
row_number and ntile functions.

Now we are on SQL 2000, and while it maybe could be one in one query,
it would not be very efficient.

So:

CREATE TABLE #numbered (ident int IDENTITY,
no_in_group int NULL,
<columns from Baskets follow here>)

INSERT #numbered(<columns from Baskets>)
SELECT BasketID, etc
FROM Baskets
ORDER BY AsOf, BasketID, ItemValue


UPDATE #numbered
SET no_in_group = n.ident - m.mindent + 1,
X = CASE WHEN n.ident - m.mindent + 1 >
(m.maxident - m.minident + 1 / 2)
THEN 1
ELSE 0
FROM #numbered n
JOIN (SELECT AsOf, BasketID, minident = MIN(ident), maxident = MAX(ident)
FROM #numbered
GROUP BY AsOf, BasketID) AS m ON n.AsOf = m.AsOf
AND n.BasketID = m.BasketID

UPDATE Baskets
SET X = n.x
FROM Baskets B
JOIN #numbered n ON B.BasketID = n.BasketID
AND B.AsOf = n.AsOf
AND B.ItemValue = n.ItemValue


Obviously, this solution is untested. If you want a tested solution, please
post your sample data as INSERT statement, as well the desired result
given the sample.


--
Erland Sommarskog, SQL Server MVP, esquel.TakeThisOut@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 >> Stay informed about: SQL 2000 Query question 
Back to top
Login to vote
TimXox

External


Since: Jun 12, 2008
Posts: 7



(Msg. 3) Posted: Sat Feb 14, 2009 5:15 am
Post subject: Re: SQL 2000 Query question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thank you, Erland.

"Erland Sommarskog" wrote:

> TimXox ( ) writes:
> > CREATE TABLE Baskets (BasketID INT, ItemID INT, AsOf DATETIME , ItemValue
> > FLOAT, X FLOAT)
> >
> > I need to create groups ( AsOf, BasketID ) and order items (ItemID ) in
> > each group by ItemValue. And calculate X like this:
> > x= 1 if the Item located in first half particular group (COUNT(*) /2)
> > ESLE X= 0
>
> In SQL 2005 this should be doable in one query with help of the
> row_number and ntile functions.
>
> Now we are on SQL 2000, and while it maybe could be one in one query,
> it would not be very efficient.
>
> So:
>
> CREATE TABLE #numbered (ident int IDENTITY,
> no_in_group int NULL,
> <columns from Baskets follow here>)
>
> INSERT #numbered(<columns from Baskets>)
> SELECT BasketID, etc
> FROM Baskets
> ORDER BY AsOf, BasketID, ItemValue
>
>
> UPDATE #numbered
> SET no_in_group = n.ident - m.mindent + 1,
> X = CASE WHEN n.ident - m.mindent + 1 >
> (m.maxident - m.minident + 1 / 2)
> THEN 1
> ELSE 0
> FROM #numbered n
> JOIN (SELECT AsOf, BasketID, minident = MIN(ident), maxident = MAX(ident)
> FROM #numbered
> GROUP BY AsOf, BasketID) AS m ON n.AsOf = m.AsOf
> AND n.BasketID = m.BasketID
>
> UPDATE Baskets
> SET X = n.x
> FROM Baskets B
> JOIN #numbered n ON B.BasketID = n.BasketID
> AND B.AsOf = n.AsOf
> AND B.ItemValue = n.ItemValue
>
>
> Obviously, this solution is untested. If you want a tested solution, please
> post your sample data as INSERT statement, as well the desired result
> given the sample.
>
>
> --
> Erland Sommarskog, SQL Server MVP, esquel DeleteThis @sommarskog.se
>
> Links for SQL Server Books Online:
> SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
> SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
> SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
>
>
 >> Stay informed about: SQL 2000 Query question 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 4) Posted: Sat Feb 14, 2009 10:26 am
Post subject: Re: SQL 2000 Query question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I am glad that you tried to post DDL, but you have a lot of problems.

I will guess that basket_id and the date are the key. What the heck
is "item_id"? We usually have an industry standard like UPC, EAN,
ISBN, CUSIP, etc. The "asOf" date is not a data element name at all
-- where are the noun? What does this record for us? If it was
supposed to be the value of an item, then you would use (start_date,
end_date) to show the duration. Why did you use FLOAT for a
value? I assume it is money and not some other scale, but that might
be wrong. What the hell is X? Why is it a FLOAT when you assign it
integer values? Why do you want to store a calculation? It will
change every time you add an new row! Put it in a query or a VIEW
omstead.

If you will fix the DDL, the DML is much easier, faster, better,.
etc. Here are my guesses that I would not have to make if you had
done your job:

CREATE TABLE Baskets
(basket_id INTEGER NOT NULL,
item_id INTEGER NOT NULL, -- no indusry standards!
basket_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (basket_id, basket_date) -- total guess!
item_value DECIMAL (9,2) NOT NULL -- avoid FP numbers
CHECK (item_value >= 0.00)); --wild guess
--drop computed columns

>> I need to create groups (basket_date, basket_id) and order items (item_id) in each group by item_value and calculate x like this: x= 1 if the Item located in first half particular group (COUNT(*) /2)
ELSE x = 0 <<

Your specs do not work! {1,2,3,4,5} would break down to up = {1,2}
and {4,5} with {3] going to either or neither set. Let's use the
tricotomy law for the groups:

SELECT basket_date, basket_id,
CASE WHEN (x_up < x_down) THEN (+1)
WHEN (x_up > x_down) THEN (-1)
ELSE 0 END AS grp_nbr
FROM
(SELECT basket_date, basket_id,
ROW_NUMBER() OVER (PARTITION BY basket_id ORDER BY item_value
ASC) AS x_up,
ROW_NUMBER() OVER (PARTITION BY basket_id ORDER BY item_value
DESC) AS x_down
FROM Baskets) AS X(basket_date, basket_id, x_up., x_down)

You need the parens on (+1) and (-1) because SQL Server 2008 is
screwed up on unary operators.
 >> Stay informed about: SQL 2000 Query question 
Back to top
Login to vote
TimXox

External


Since: Jun 12, 2008
Posts: 7



(Msg. 5) Posted: Sun Feb 15, 2009 9:32 pm
Post subject: Re: SQL 2000 Query question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thank you, --CELKO--,
It works perfect.
The key is basket_id , item_id and the basket_date.
Probably I have to use this "......PARTITION BY basket_date,basket_id
ORDER BY item_value....."
For simplicity insted of item_id we may say currency_id and for item_value
=> currency_return. I am trying to define for the particular basket/date
"bad" currency (X = -1) or "good" currency (X=1) and X is just one of params
for a function which return a FLOAT estimation.
Tim.

"--CELKO--" wrote:

> I am glad that you tried to post DDL, but you have a lot of problems.
>
> I will guess that basket_id and the date are the key. What the heck
> is "item_id"? We usually have an industry standard like UPC, EAN,
> ISBN, CUSIP, etc. The "asOf" date is not a data element name at all
> -- where are the noun? What does this record for us? If it was
> supposed to be the value of an item, then you would use (start_date,
> end_date) to show the duration. Why did you use FLOAT for a
> value? I assume it is money and not some other scale, but that might
> be wrong. What the hell is X? Why is it a FLOAT when you assign it
> integer values? Why do you want to store a calculation? It will
> change every time you add an new row! Put it in a query or a VIEW
> omstead.
>
> If you will fix the DDL, the DML is much easier, faster, better,.
> etc. Here are my guesses that I would not have to make if you had
> done your job:
>
> CREATE TABLE Baskets
> (basket_id INTEGER NOT NULL,
> item_id INTEGER NOT NULL, -- no indusry standards!
> basket_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
> PRIMARY KEY (basket_id, basket_date) -- total guess!
> item_value DECIMAL (9,2) NOT NULL -- avoid FP numbers
> CHECK (item_value >= 0.00)); --wild guess
> --drop computed columns
>
> >> I need to create groups (basket_date, basket_id) and order items (item_id) in each group by item_value and calculate x like this: x= 1 if the Item located in first half particular group (COUNT(*) /2)
> ELSE x = 0 <<
>
> Your specs do not work! {1,2,3,4,5} would break down to up = {1,2}
> and {4,5} with {3] going to either or neither set. Let's use the
> tricotomy law for the groups:
>
> SELECT basket_date, basket_id,
> CASE WHEN (x_up < x_down) THEN (+1)
> WHEN (x_up > x_down) THEN (-1)
> ELSE 0 END AS grp_nbr
> FROM
> (SELECT basket_date, basket_id,
> ROW_NUMBER() OVER (PARTITION BY basket_id ORDER BY item_value
> ASC) AS x_up,
> ROW_NUMBER() OVER (PARTITION BY basket_id ORDER BY item_value
> DESC) AS x_down
> FROM Baskets) AS X(basket_date, basket_id, x_up., x_down)
>
> You need the parens on (+1) and (-1) because SQL Server 2008 is
> screwed up on unary operators.
>
 >> Stay informed about: SQL 2000 Query question 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
SQL Server 2000 master6.ldf question - Hi, In my SQL Server 2000, I have a master6.ldf. It's size is huge, and it's also growing. I am wondering why it is master6.ldf and not master.ldf. Since it's growing, I am wondering how to make it smaller. Thanks for help. Jason

Question about Microsoft SQL Server 2000, deletion, and lo.. - All, I've been racking my brains trying to solve a deadlocking issue I've encountered and trying to find the simplest way of stating my problem so that others can help - and I think I've figured out the best way to ask. I have it all narrowed down to 2...

A question about keyword search strategy against SQL Serve.. - We have a small web application using Sql Server 2000. We would like to provide a keyword search functionality. The key word can be a location, a string in the description of certain topic or a combination of both. What I have currently is to take the...

Query across servers with SQL 2000 - Hi, I'm trying to query across two different servers but get a syntax error at '-'. Thanks for any help. Line 2: Incorrect syntax near '-'. SELECT shipname, lastname, firstname FROM mysrv-s-d..Northwind.dbo.Orders d JOIN ..

Crosstab query in sql server 2000 - Hi!, I'm trying to make a cross tab query in sql server, I know how to do it, but I need to improve the performance of my query, I'm working with vb.net 2005 and sql server 2000 I need to visualize the information of a different way: My Query: ..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]