Showing posts with label order. Show all posts
Showing posts with label order. Show all posts

Sunday, March 25, 2012

Calling sp_helptext from ASPNET

Hi,

I am executing a call to "sp_helptext" from a web service, in order to
obtain the text of a stored procedure. The call itself seems to be ok but
eventually it throws an exception saying that the specified object does not
exist in the database - which is a false statement, i've checked carefully.

I probably do not have enough rights to access something from the ASPNET
account.
Do i have to login as "sa"? Is this the only solution available?

Bogdan.Bogdan Rechi (bogdan_rechi@.hotmail.com) writes:
> I am executing a call to "sp_helptext" from a web service, in order to
> obtain the text of a stored procedure. The call itself seems to be ok
> but eventually it throws an exception saying that the specified object
> does not exist in the database - which is a false statement, i've
> checked carefully.

What sort of object is it? Not all object types have text.

> I probably do not have enough rights to access something from the ASPNET
> account.

Is this account owner of the database? If not, how do you specify the
object names? Do you include owner, as in

exec sp_helptext 'dho.my_proc'

In SQL 2000, access rights are not likely to be an issue, as all users
have rights to see metadata. SQL2005 is very different in that regard.

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

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Monday, March 19, 2012

Calling All T-SQL Gods & Goddesses

Hi

I'm having trouble with the script below that it just won't do a
correct ORDER BY for a date field.

When executing the two SELECT TOP statements on their own the records
are sorted correctly but when wrapped in the main statement, the ORDER
BY is just ignored. Tried to play around with the script but with no
luck. Script is for paging, in this case, select all records meeting
criteria, order and select Top 25 to display on first page. Any
questions, please don't hesitate contacting me.

Thanks very much to anyone who's having a look at this!

Martin

SELECT COALESCE((i2b_contact.Firstname + CHAR(32) +
i2b_contact.Lastname),i2b_company.CompanyName) AS CName,
i2b_keytransactionlog.KeyTransactionLogID, i2b_key.KeyCode,
(i2b_address.Address1) AS PropertyAddress,
A.ProgUserName AS ProgUserName,
CONVERT (varchar(10), i2b_keytransactionlog.TransactionDate, 104 ) AS
TransactionDate,
CONVERT(varchar(10),i2b_keytransactionlog.ReturnBy Date,104) AS
ReturnByDate
FROM i2b_keytransactionlog
LEFT JOIN i2b_contact ON (i2b_keytransactionlog.EntityID =
i2b_contact.ContactID AND i2b_keytransactionlog.IsContact = 1)
LEFT JOIN i2b_company ON (i2b_keytransactionlog.EntityID =
i2b_company.CompanyID AND i2b_keytransactionlog.IsContact = 0)
JOIN i2b_key ON (i2b_keytransactionlog.KeyID = i2b_key.KeyID)
JOIN i2b_property on (i2b_key.PropertyID = i2b_property.PropertyID)
JOIN i2b_address ON (i2b_property.AddressID = i2b_address.AddressID)
JOIN i2b_proguser AS A ON (i2b_keytransactionlog.ProgUserID =
A.ProgUserID)
WHERE KeyTransactionLogID IN(SELECT TOP 25 KeyTransactionLogID FROM
i2b_keytransactionlog
LEFT JOIN i2b_contact ON (i2b_keytransactionlog.EntityID =
i2b_contact.ContactID AND i2b_keytransactionlog.IsContact = 1)
LEFT JOIN i2b_company ON (i2b_keytransactionlog.EntityID =
i2b_company.CompanyID AND i2b_keytransactionlog.IsContact = 0)
JOIN i2b_key ON (i2b_keytransactionlog.KeyID = i2b_key.KeyID)
JOIN i2b_property on (i2b_key.PropertyID = i2b_property.PropertyID)
JOIN i2b_address ON (i2b_property.AddressID = i2b_address.AddressID)
JOIN i2b_proguser AS A ON (i2b_keytransactionlog.ProgUserID =
A.ProgUserID)
WHERE i2b_keytransactionlog.ProgClientID = 1 AND
i2b_keytransactionlog.ProgUserID = 3 AND KeyTransactionLogID NOT IN
(SELECT TOP 0 KeyTransactionLogID FROM i2b_keytransactionlog
LEFT JOIN i2b_contact ON (i2b_keytransactionlog.EntityID =
i2b_contact.ContactID AND i2b_keytransactionlog.IsContact = 1)
LEFT JOIN i2b_company ON (i2b_keytransactionlog.EntityID =
i2b_company.CompanyID AND i2b_keytransactionlog.IsContact = 0)
JOIN i2b_key ON (i2b_keytransactionlog.KeyID = i2b_key.KeyID)
JOIN i2b_property on (i2b_key.PropertyID = i2b_property.PropertyID)
JOIN i2b_address ON (i2b_property.AddressID = i2b_address.AddressID)
JOIN i2b_proguser AS A ON (i2b_keytransactionlog.ProgUserID =
A.ProgUserID)
WHERE i2b_keytransactionlog.ProgClientID = 1 AND
i2b_keytransactionlog.ProgUserID = 3 ORDER BY ReturnByDate DESC)
ORDER BY ReturnByDate DESC)
ORDER BY ReturnByDate DESCOn 29 May 2004 12:19:20 -0700, Martin wrote:

>Hi
>I'm having trouble with the script below that it just won't do a
>correct ORDER BY for a date field.
>When executing the two SELECT TOP statements on their own the records
>are sorted correctly but when wrapped in the main statement, the ORDER
>BY is just ignored. Tried to play around with the script but with no
>luck. Script is for paging, in this case, select all records meeting
>criteria, order and select Top 25 to display on first page. Any
>questions, please don't hesitate contacting me.
>Thanks very much to anyone who's having a look at this!
>Martin
>SELECT COALESCE((i2b_contact.Firstname + CHAR(32) +
>i2b_contact.Lastname),i2b_company.CompanyName) AS CName,
>i2b_keytransactionlog.KeyTransactionLogID, i2b_key.KeyCode,
>(i2b_address.Address1) AS PropertyAddress,
>A.ProgUserName AS ProgUserName,
>CONVERT (varchar(10), i2b_keytransactionlog.TransactionDate, 104 ) AS
>TransactionDate,
>CONVERT(varchar(10),i2b_keytransactionlog.ReturnBy Date,104) AS
>ReturnByDate
> FROM i2b_keytransactionlog
>LEFT JOIN i2b_contact ON (i2b_keytransactionlog.EntityID =
>i2b_contact.ContactID AND i2b_keytransactionlog.IsContact = 1)
>LEFT JOIN i2b_company ON (i2b_keytransactionlog.EntityID =
>i2b_company.CompanyID AND i2b_keytransactionlog.IsContact = 0)
>JOIN i2b_key ON (i2b_keytransactionlog.KeyID = i2b_key.KeyID)
>JOIN i2b_property on (i2b_key.PropertyID = i2b_property.PropertyID)
>JOIN i2b_address ON (i2b_property.AddressID = i2b_address.AddressID)
>JOIN i2b_proguser AS A ON (i2b_keytransactionlog.ProgUserID =
>A.ProgUserID)
>WHERE KeyTransactionLogID IN(SELECT TOP 25 KeyTransactionLogID FROM
>i2b_keytransactionlog
>LEFT JOIN i2b_contact ON (i2b_keytransactionlog.EntityID =
>i2b_contact.ContactID AND i2b_keytransactionlog.IsContact = 1)
>LEFT JOIN i2b_company ON (i2b_keytransactionlog.EntityID =
>i2b_company.CompanyID AND i2b_keytransactionlog.IsContact = 0)
>JOIN i2b_key ON (i2b_keytransactionlog.KeyID = i2b_key.KeyID)
>JOIN i2b_property on (i2b_key.PropertyID = i2b_property.PropertyID)
>JOIN i2b_address ON (i2b_property.AddressID = i2b_address.AddressID)
>JOIN i2b_proguser AS A ON (i2b_keytransactionlog.ProgUserID =
>A.ProgUserID)
>WHERE i2b_keytransactionlog.ProgClientID = 1 AND
>i2b_keytransactionlog.ProgUserID = 3 AND KeyTransactionLogID NOT IN
> (SELECT TOP 0 KeyTransactionLogID FROM i2b_keytransactionlog
>LEFT JOIN i2b_contact ON (i2b_keytransactionlog.EntityID =
>i2b_contact.ContactID AND i2b_keytransactionlog.IsContact = 1)
>LEFT JOIN i2b_company ON (i2b_keytransactionlog.EntityID =
>i2b_company.CompanyID AND i2b_keytransactionlog.IsContact = 0)
>JOIN i2b_key ON (i2b_keytransactionlog.KeyID = i2b_key.KeyID)
>JOIN i2b_property on (i2b_key.PropertyID = i2b_property.PropertyID)
>JOIN i2b_address ON (i2b_property.AddressID = i2b_address.AddressID)
>JOIN i2b_proguser AS A ON (i2b_keytransactionlog.ProgUserID =
>A.ProgUserID)
>WHERE i2b_keytransactionlog.ProgClientID = 1 AND
>i2b_keytransactionlog.ProgUserID = 3 ORDER BY ReturnByDate DESC)
> ORDER BY ReturnByDate DESC)
> ORDER BY ReturnByDate DESC

Hi Martin,

I'm not entirely sure why you would use TOP 0 for the NOT IN subquery, but
that's not the cause of the problem you post about.

The ANSI standard for SQL allows only columns appearing in the SELECT list
in the ORDER BY clause. The rationale being that first, the query gets
executed and the result set is determined; the sorting is a seperate,
final stage. ANSI SQL allows things as ORDER BY 3 (3 being the third
column in the SELECT LIST) or SELECT MyColumn AS ColumnAlias ... ORDER BY
ColumnAlias.

MS SQL Server expands on this standard. With MS SQL Server, you can ORDER
BY on a column not in the SELECT list. If you use a column name that is
not an alias in the column list, SQL Server will try to locate a column
with that name in one of the tables used in the query and use that for
sorting.

Before I get back to your problem, run the following script. It will
clearly show that SQL Server will first attempt to resolve names in the
ORDER BY clause following ANSI standard before looking at the columns that
make up the data.

CREATE TABLE testtab (a int, b int)
go
INSERT testtab (a, b) SELECT 1, 20
INSERT testtab (a, b) SELECT 2, 10
go
SELECT b
FROM testtab
ORDER BY a
go
SELECT b AS a
FROM testtab
ORDER BY a
go
SELECT b AS a, a AS b
FROM testtab
ORDER BY a
go
DROP TABLE testtab
go

In the first query, no column "a" is in the SELECT list, so SQL Server
will sort on the column a in the table. In the second query, the SELECT
list has a column aliased as "a", so this is used for sorting. In the
third query, the column a itself is used in the select list; aliases are
disregarded and sorting is done on the contents of a.

Now back to your real problem. I'll show you the parts that cause the
unexpected ordering:

>SELECT (...)
> (...)
>CONVERT(varchar(10),i2b_keytransactionlog.ReturnBy Date,104) AS
>ReturnByDate
> (...)
> ORDER BY ReturnByDate DESC

The column ReturnByDate is not used in the SELECT list (the results of a
function are not cosidered equal to the column itself). There is however
an alias ReturnByDate, so there's no need to get back to the data in the
table either - the aliased column in the result set is used. This column
is the output of a CONVERT function call. It's varchar(10), therefor the
sorting will be done by alphabet, descending. "23-04-2003" will go before
"06-05-2004".

The fix? Either use something else as an alias ([Return By Date], for
example) or use ORDER BY CONVERT(datetime, ReturnByDate).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Calling a Stored Procedure to list Data

I need to call a stored procedure in order to list products by either a text box based search or by clicking on a category.

I am very new to this and would really appreciate any knowledge anyone could share.

A quick example:

protected void Button1_Click(object sender, EventArgs e)
{
string connectionString = @."Data Source=Confute;Initial Catalog=AdventureWorks;Integrated Security=SSPI";

using (SqlConnection connection = new SqlConnection(connectionString))
{

SqlCommand cmd = new SqlCommand("sp_SearchByCategory", connection);
cmd.Parameters.Add("@.category", txtBox_Name.Text);

cmd.CommandType = CommandType.StoredProcedure;
connection.Open();
cmd.ExecuteNonQuery();

SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
DataGrid dg = new DataGrid();
dg.DataSource = dt;
txtBox_Name.Text = dt.Rows[0][0].ToString();
}

}

Wednesday, March 7, 2012

call order

If a VIEW and a STORED PROCEDURE exist and have the same identical name -
is there a particular call sequence that SQL SERVER uses -
in other words if the particular name is called up in code - does SQL pull
the view first or the SP first.Not possible. Try below:
USE tempdb
GO
CREATE PROC xyz AS SELECT 1 AS s
GO
CREATE VIEW xyz AS SELECT 1 AS s
Object names need to be unique. And even if SQL Server would allow it, there
would not be any
ambiguity. You don't use a stored procedure and a view in the same way.
You execute a stored procedure. You don't execute a view.
You select data from a view. You don't select data from a stored procedure.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"jduran" <jduran@.discussions.microsoft.com> wrote in message
news:A0D23A66-F34E-4177-9C79-54D92ABB216B@.microsoft.com...
> If a VIEW and a STORED PROCEDURE exist and have the same identical name -
> is there a particular call sequence that SQL SERVER uses -
> in other words if the particular name is called up in code - does SQL pul
l
> the view first or the SP first.|||Hi
This should not occur as the names must be unique
CREATE PROCEDURE MyProc
AS
SELECT * FROM SYSOBJECTS
CREATE VIEW MyProc AS SELECT * FROM SYSOBJECTS
Server: Msg 2714, Level 16, State 5, Procedure MyProc, Line 1
There is already an object named 'MyProc' in the database.
Using a strict naming convention policy to distinguish objects is also a
good idea.
John
"jduran" wrote:

> If a VIEW and a STORED PROCEDURE exist and have the same identical name -
> is there a particular call sequence that SQL SERVER uses -
> in other words if the particular name is called up in code - does SQL pul
l
> the view first or the SP first.|||You can not create two objects with the same name. Even if you could, you ca
n
not select from an sp or execute a view.
Example:
use northwind
go
create procedure proc1
as
return 0
go
-- this will give an error
create view proc1
as
select * from orders
go
drop procedure proc1
go
AMB
"jduran" wrote:

> If a VIEW and a STORED PROCEDURE exist and have the same identical name -
> is there a particular call sequence that SQL SERVER uses -
> in other words if the particular name is called up in code - does SQL pul
l
> the view first or the SP first.

Friday, February 24, 2012

Calculations Script Order and Formatting

I have a parent-child dimension for Accounts with a calculated member drapped off of the top level that figures Net Earnings % using a measure called Amount. As described by the name of the member, I need this expressed as a percentage. I am using this calc in the same view as a (Shell/Utility) Time Calculations dim that figures Year-to-date, Prior Year-to-date, YTD % Change, etc.... Depending on the order of the calculations in the calculations script I get different results. If I put the formula for the Net Earnings % before the MDX for the Time Calculations the results comes out correct but the formating gets overidden by the formating applied when the Time Calulations are applied. So the Net Earnings % for [YTD] and [YTD PY] are expressed as decimals and [YTD % Change] is expressed as a percentage. If I flip the order in the calculations script the formating gets corrected because the formatting of the Net Earnings % get applied last but my results for Net Earnings % are incorrect for [YTD % Change] because they were applied after the Time Calculations and end up applying the formula using the reults (the percentages) of the [YTD % Change] calc instead of the numbers used to make up that percentage.

Is there a way to put the Net Earnings % MDX before the Time Calculations MDX so that the results are correct for [YTD % Change] and reapply the formating of percent to [YTD] and [YTD PY] after the formatting of the Time Calculations get applied?

Nevermind, I figured it out. I placed the Net Earnings % MDX before the Time Calculations MDX and placed the following MDX after the Time Calculations MDX:

Code Snippet

Scope({[Account].[Account Hierarchy].[Net Earnings%]});

FORMAT_STRING(THIS) = "Percent";

End Scope;

|||

Nevermind, I figured it out. I placed the Net Earnings % MDX before the Time Calculations MDX and placed the following MDX after the Time Calculations MDX:

Code Snippet

Scope({[Account].[Account Hierarchy].[Net Earnings%]});

FORMAT_STRING(THIS) = "Percent";

End Scope;

Sunday, February 19, 2012

calculation problem

Hello,

We have time dimension (month, week)

and fact table, total_revenue, no_of_work_day, order, order type....

We added a calculated member : Ave revenue/work_day
Expression: "[Measures].[Tot Revenue]/[Measures].[No Of Work Day]"
in BI studio, but result is wrong.

Something wrong with 'no_of_work_day' , but how to correct it?

Thank you very much for any help.

In spite of using calculated member inside cube, try to create a named calculation inside datasourceview!?

In the table of datasourceview, right-click and add a named calculation a insert your formula TotRevenue/NoOfWorkDay...

Regards!

|||

Can you show us your mdx query?

You try using the SUM(...) function on the measures members and dividing with it...

I would also like to say that you can create your calculated member in the cube...


Regards,

|||Thank you very much PedroCGD and Luis for the advice,

I tried using named calculation, it works. Great!

What is different between named calculation and calculate command inside cube?

I really appreciated your help.
|||

Dear Olap_user,

Always try to use named calculations... only when you need to calculate inside aggregations you use calculated member. The big advantage of using named calculation is that the system spend time to do calculates only once, and with calculated member each time you query the cube. So:

Named Calculation > each time your process cube

Calculated member > each time you query the cube!

Helped you?

Regards!!!

|||Thank you very much for the detail explanation, using Named Calculation is better for the performance.

Your explanation is very helpful.

Thanks a lot.

Calculation for FIFO

Hi Everybody,
I've provide a sample Purchase Order with some data. I need to maintain a FI
FO (first-in-first-out) for the the sale of item and at the end of the day,
will generate a report which are the Purchase Order with item still unsold.
Example, sold 150 items (80 from id=1, 23 from id=2, 43 from id=3 and 4 from
id=4) , so the query need to display id 4 (still remain with 26) and id 5 (
still remain with 60)
Can someone help me on this query?
Sample Purchase Order table
id date item_code quantity
-- -- -- --
1 2005-01-02 item_A 80
2 2005-01-15 item_A 23
3 2005-02-04 item_A 43
4 2005-02-23 item_A 30
5 2005-03-13 item_A 60
Thanks in advance,
KristeKriste
See, if this helps you
CREATE TABLE #Sales
(
SaleId INT NOT NULL PRIMARY KEY,
SaleDate DATETIME NOT NULL,
ItemCode CHAR(6) NOT NULL,
Quantity INT
)
INSERT INTO #Sales VALUES (1,'2005-01-02', 'item_A',80)
INSERT INTO #Sales VALUES (2,'2005-01-15', 'item_A',23)
INSERT INTO #Sales VALUES (3,'2005-02-04', 'item_A',43)
INSERT INTO #Sales VALUES (4,'2005-02-23', 'item_A',30)
INSERT INTO VALUES (5,'2005-03-13', 'item_A',60)
SELECT * FROM #Sales S
WHERE (
SELECT COUNT(*) FROM #Sales
WHERE ItemCode = S.ItemCode
AND SaleId > S.SaleId
) <= 1
ORDER BY SaleId ASC
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:%23zGe4WGVFHA.3076@.T
K2MSFTNGP12.phx.gbl...
Hi Everybody,
I've provide a sample Purchase Order with some data. I need to maintain a FI
FO (first-in-first-out) for the the sale of item and at the end of the day,
will generate a report which are the Purchase Order with item still unsold.
Example, sold 150 items (80 from id=1, 23 from id=2, 43 from id=3 and 4 from
id=4) , so the query need to display id 4 (still remain with 26) and id 5 (
still remain with 60)
Can someone help me on this query?
Sample Purchase Order table
id date item_code quantity
-- -- -- --
1 2005-01-02 item_A 80
2 2005-01-15 item_A 23
3 2005-02-04 item_A 43
4 2005-02-23 item_A 30
5 2005-03-13 item_A 60
Thanks in advance,
Kriste|||Can u explain how does this query works? I don't quite understand.
If now the sold quantity = 100, how will it return the id=2 to 5?
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:OHfl5gGVFHA.4092@.TK2MS
FTNGP12.phx.gbl...
Kriste
See, if this helps you
CREATE TABLE #Sales
(
SaleId INT NOT NULL PRIMARY KEY,
SaleDate DATETIME NOT NULL,
ItemCode CHAR(6) NOT NULL,
Quantity INT
)
INSERT INTO #Sales VALUES (1,'2005-01-02', 'item_A',80)
INSERT INTO #Sales VALUES (2,'2005-01-15', 'item_A',23)
INSERT INTO #Sales VALUES (3,'2005-02-04', 'item_A',43)
INSERT INTO #Sales VALUES (4,'2005-02-23', 'item_A',30)
INSERT INTO VALUES (5,'2005-03-13', 'item_A',60)
SELECT * FROM #Sales S
WHERE (
SELECT COUNT(*) FROM #Sales
WHERE ItemCode = S.ItemCode
AND SaleId > S.SaleId
) <= 1
ORDER BY SaleId ASC
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:%23zGe4WGVFHA.3076@.T
K2MSFTNGP12.phx.gbl...
Hi Everybody,
I've provide a sample Purchase Order with some data. I need to maintain a FI
FO (first-in-first-out) for the the sale of item and at the end of the day,
will generate a report which are the Purchase Order with item still unsold.
Example, sold 150 items (80 from id=1, 23 from id=2, 43 from id=3 and 4 from
id=4) , so the query need to display id 4 (still remain with 26) and id 5 (
still remain with 60)
Can someone help me on this query?
Sample Purchase Order table
id date item_code quantity
-- -- -- --
1 2005-01-02 item_A 80
2 2005-01-15 item_A 23
3 2005-02-04 item_A 43
4 2005-02-23 item_A 30
5 2005-03-13 item_A 60
Thanks in advance,
Kriste|||Kriste
Can you explain if i add INSERT INTO #Sales VALUES (6,'2005-03-13', 'item_A'
,100) why do I should get back from 2 to 5?
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:eEYiF4HVFHA.1404@.TK2
MSFTNGP09.phx.gbl...
Can u explain how does this query works? I don't quite understand.
If now the sold quantity = 100, how will it return the id=2 to 5?
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:OHfl5gGVFHA.4092@.TK2MS
FTNGP12.phx.gbl...
Kriste
See, if this helps you
CREATE TABLE #Sales
(
SaleId INT NOT NULL PRIMARY KEY,
SaleDate DATETIME NOT NULL,
ItemCode CHAR(6) NOT NULL,
Quantity INT
)
INSERT INTO #Sales VALUES (1,'2005-01-02', 'item_A',80)
INSERT INTO #Sales VALUES (2,'2005-01-15', 'item_A',23)
INSERT INTO #Sales VALUES (3,'2005-02-04', 'item_A',43)
INSERT INTO #Sales VALUES (4,'2005-02-23', 'item_A',30)
INSERT INTO VALUES (5,'2005-03-13', 'item_A',60)
SELECT * FROM #Sales S
WHERE (
SELECT COUNT(*) FROM #Sales
WHERE ItemCode = S.ItemCode
AND SaleId > S.SaleId
) <= 1
ORDER BY SaleId ASC
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:%23zGe4WGVFHA.3076@.T
K2MSFTNGP12.phx.gbl...
Hi Everybody,
I've provide a sample Purchase Order with some data. I need to maintain a FI
FO (first-in-first-out) for the the sale of item and at the end of the day,
will generate a report which are the Purchase Order with item still unsold.
Example, sold 150 items (80 from id=1, 23 from id=2, 43 from id=3 and 4 from
id=4) , so the query need to display id 4 (still remain with 26) and id 5 (
still remain with 60)
Can someone help me on this query?
Sample Purchase Order table
id date item_code quantity
-- -- -- --
1 2005-01-02 item_A 80
2 2005-01-15 item_A 23
3 2005-02-04 item_A 43
4 2005-02-23 item_A 30
5 2005-03-13 item_A 60
Thanks in advance,
Kriste|||Hi Uri,
I think you didn't get my question right. The data here is of Purchase Order
. I have a accumulating sales quantity.
As the sales quantity increase, I need to find out which Purchase Order quan
ity is still unsold.
Eg.
Sold sales quantity = 50, so display Purchase Order 1 to 5
Sold sales quantity = 100, so display Purchase Order 2 to 5
Sold sales quantity = 150, so display Purchase Order 4 to 5
Sample Purchase Order table
id date item_code quantity accumulated inventory
-- -- -- -- --
1 2005-01-02 item_A 80 + 80 = 80
2 2005-01-15 item_A 23 + 23 = 103
3 2005-02-04 item_A 43 + 43 = 146
4 2005-02-23 item_A 30 + 30 = 176
5 2005-03-13 item_A 60 + 60 = 236
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:%23rT3DHIVFHA.3176@.TK2
MSFTNGP12.phx.gbl...
Kriste
Can you explain if i add INSERT INTO #Sales VALUES (6,'2005-03-13', 'item_A'
,100) why do I should get back from 2 to 5?
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:eEYiF4HVFHA.1404@.TK2
MSFTNGP09.phx.gbl...
Can u explain how does this query works? I don't quite understand.
If now the sold quantity = 100, how will it return the id=2 to 5?
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:OHfl5gGVFHA.4092@.TK2MS
FTNGP12.phx.gbl...
Kriste
See, if this helps you
CREATE TABLE #Sales
(
SaleId INT NOT NULL PRIMARY KEY,
SaleDate DATETIME NOT NULL,
ItemCode CHAR(6) NOT NULL,
Quantity INT
)
INSERT INTO #Sales VALUES (1,'2005-01-02', 'item_A',80)
INSERT INTO #Sales VALUES (2,'2005-01-15', 'item_A',23)
INSERT INTO #Sales VALUES (3,'2005-02-04', 'item_A',43)
INSERT INTO #Sales VALUES (4,'2005-02-23', 'item_A',30)
INSERT INTO VALUES (5,'2005-03-13', 'item_A',60)
SELECT * FROM #Sales S
WHERE (
SELECT COUNT(*) FROM #Sales
WHERE ItemCode = S.ItemCode
AND SaleId > S.SaleId
) <= 1
ORDER BY SaleId ASC
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:%23zGe4WGVFHA.3076@.T
K2MSFTNGP12.phx.gbl...
Hi Everybody,
I've provide a sample Purchase Order with some data. I need to maintain a FI
FO (first-in-first-out) for the the sale of item and at the end of the day,
will generate a report which are the Purchase Order with item still unsold.
Example, sold 150 items (80 from id=1, 23 from id=2, 43 from id=3 and 4 from
id=4) , so the query need to display id 4 (still remain with 26) and id 5 (
still remain with 60)
Can someone help me on this query?
Sample Purchase Order table
id date item_code quantity
-- -- -- --
1 2005-01-02 item_A 80
2 2005-01-15 item_A 23
3 2005-02-04 item_A 43
4 2005-02-23 item_A 30
5 2005-03-13 item_A 60
Thanks in advance,
Kriste|||Kriste
Now , you are explained the problem by posting DDL
CREATE TABLE #Sales
(
SaleId INT NOT NULL PRIMARY KEY,
SaleDate DATETIME NOT NULL,
ItemCode CHAR(6) NOT NULL,
Quantity INT
)
INSERT INTO #Sales VALUES (1,'2005-01-02', 'item_A',80)
INSERT INTO #Sales VALUES (2,'2005-01-15', 'item_A',23)
INSERT INTO #Sales VALUES (3,'2005-02-04', 'item_A',43)
INSERT INTO #Sales VALUES (4,'2005-02-23', 'item_A',30)
INSERT INTO #Sales VALUES (5,'2005-03-13', 'item_A',60)
Declare @.Q INT
SET @.Q=100
SELECT * FROM
(
SELECT Saleid,(SELECT SUM(Quantity) FROM #Sales S
WHERE #Sales.Saleid>=S.Saleid )AS Ac
FROM #Sales
) AS DER WHERE Ac>=@.q
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:%23gB%23ePIVFHA.3544
@.TK2MSFTNGP12.phx.gbl...
Hi Uri,
I think you didn't get my question right. The data here is of Purchase Order
. I have a accumulating sales quantity.
As the sales quantity increase, I need to find out which Purchase Order quan
ity is still unsold.
Eg.
Sold sales quantity = 50, so display Purchase Order 1 to 5
Sold sales quantity = 100, so display Purchase Order 2 to 5
Sold sales quantity = 150, so display Purchase Order 4 to 5
Sample Purchase Order table
id date item_code quantity accumulated inventory
-- -- -- -- --
1 2005-01-02 item_A 80 + 80 = 80
2 2005-01-15 item_A 23 + 23 = 103
3 2005-02-04 item_A 43 + 43 = 146
4 2005-02-23 item_A 30 + 30 = 176
5 2005-03-13 item_A 60 + 60 = 236
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:%23rT3DHIVFHA.3176@.TK2
MSFTNGP12.phx.gbl...
Kriste
Can you explain if i add INSERT INTO #Sales VALUES (6,'2005-03-13', 'item_A'
,100) why do I should get back from 2 to 5?
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:eEYiF4HVFHA.1404@.TK2
MSFTNGP09.phx.gbl...
Can u explain how does this query works? I don't quite understand.
If now the sold quantity = 100, how will it return the id=2 to 5?
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:OHfl5gGVFHA.4092@.TK2MS
FTNGP12.phx.gbl...
Kriste
See, if this helps you
CREATE TABLE #Sales
(
SaleId INT NOT NULL PRIMARY KEY,
SaleDate DATETIME NOT NULL,
ItemCode CHAR(6) NOT NULL,
Quantity INT
)
INSERT INTO #Sales VALUES (1,'2005-01-02', 'item_A',80)
INSERT INTO #Sales VALUES (2,'2005-01-15', 'item_A',23)
INSERT INTO #Sales VALUES (3,'2005-02-04', 'item_A',43)
INSERT INTO #Sales VALUES (4,'2005-02-23', 'item_A',30)
INSERT INTO VALUES (5,'2005-03-13', 'item_A',60)
SELECT * FROM #Sales S
WHERE (
SELECT COUNT(*) FROM #Sales
WHERE ItemCode = S.ItemCode
AND SaleId > S.SaleId
) <= 1
ORDER BY SaleId ASC
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:%23zGe4WGVFHA.3076@.T
K2MSFTNGP12.phx.gbl...
Hi Everybody,
I've provide a sample Purchase Order with some data. I need to maintain a FI
FO (first-in-first-out) for the the sale of item and at the end of the day,
will generate a report which are the Purchase Order with item still unsold.
Example, sold 150 items (80 from id=1, 23 from id=2, 43 from id=3 and 4 from
id=4) , so the query need to display id 4 (still remain with 26) and id 5 (
still remain with 60)
Can someone help me on this query?
Sample Purchase Order table
id date item_code quantity
-- -- -- --
1 2005-01-02 item_A 80
2 2005-01-15 item_A 23
3 2005-02-04 item_A 43
4 2005-02-23 item_A 30
5 2005-03-13 item_A 60
Thanks in advance,
Kriste|||Thank Uri, it works.
Btw, the DDL has already been included in my 1st post.
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:OPJNzbIVFHA.1604@.TK2MS
FTNGP10.phx.gbl...
Kriste
Now , you are explained the problem by posting DDL
CREATE TABLE #Sales
(
SaleId INT NOT NULL PRIMARY KEY,
SaleDate DATETIME NOT NULL,
ItemCode CHAR(6) NOT NULL,
Quantity INT
)
INSERT INTO #Sales VALUES (1,'2005-01-02', 'item_A',80)
INSERT INTO #Sales VALUES (2,'2005-01-15', 'item_A',23)
INSERT INTO #Sales VALUES (3,'2005-02-04', 'item_A',43)
INSERT INTO #Sales VALUES (4,'2005-02-23', 'item_A',30)
INSERT INTO #Sales VALUES (5,'2005-03-13', 'item_A',60)
Declare @.Q INT
SET @.Q=100
SELECT * FROM
(
SELECT Saleid,(SELECT SUM(Quantity) FROM #Sales S
WHERE #Sales.Saleid>=S.Saleid )AS Ac
FROM #Sales
) AS DER WHERE Ac>=@.q
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:%23gB%23ePIVFHA.3544
@.TK2MSFTNGP12.phx.gbl...
Hi Uri,
I think you didn't get my question right. The data here is of Purchase Order
. I have a accumulating sales quantity.
As the sales quantity increase, I need to find out which Purchase Order quan
ity is still unsold.
Eg.
Sold sales quantity = 50, so display Purchase Order 1 to 5
Sold sales quantity = 100, so display Purchase Order 2 to 5
Sold sales quantity = 150, so display Purchase Order 4 to 5
Sample Purchase Order table
id date item_code quantity accumulated inventory
-- -- -- -- --
1 2005-01-02 item_A 80 + 80 = 80
2 2005-01-15 item_A 23 + 23 = 103
3 2005-02-04 item_A 43 + 43 = 146
4 2005-02-23 item_A 30 + 30 = 176
5 2005-03-13 item_A 60 + 60 = 236
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:%23rT3DHIVFHA.3176@.TK2
MSFTNGP12.phx.gbl...
Kriste
Can you explain if i add INSERT INTO #Sales VALUES (6,'2005-03-13', 'item_A'
,100) why do I should get back from 2 to 5?
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:eEYiF4HVFHA.1404@.TK2
MSFTNGP09.phx.gbl...
Can u explain how does this query works? I don't quite understand.
If now the sold quantity = 100, how will it return the id=2 to 5?
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:OHfl5gGVFHA.4092@.TK2MS
FTNGP12.phx.gbl...
Kriste
See, if this helps you
CREATE TABLE #Sales
(
SaleId INT NOT NULL PRIMARY KEY,
SaleDate DATETIME NOT NULL,
ItemCode CHAR(6) NOT NULL,
Quantity INT
)
INSERT INTO #Sales VALUES (1,'2005-01-02', 'item_A',80)
INSERT INTO #Sales VALUES (2,'2005-01-15', 'item_A',23)
INSERT INTO #Sales VALUES (3,'2005-02-04', 'item_A',43)
INSERT INTO #Sales VALUES (4,'2005-02-23', 'item_A',30)
INSERT INTO VALUES (5,'2005-03-13', 'item_A',60)
SELECT * FROM #Sales S
WHERE (
SELECT COUNT(*) FROM #Sales
WHERE ItemCode = S.ItemCode
AND SaleId > S.SaleId
) <= 1
ORDER BY SaleId ASC
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:%23zGe4WGVFHA.3076@.T
K2MSFTNGP12.phx.gbl...
Hi Everybody,
I've provide a sample Purchase Order with some data. I need to maintain a FI
FO (first-in-first-out) for the the sale of item and at the end of the day,
will generate a report which are the Purchase Order with item still unsold.
Example, sold 150 items (80 from id=1, 23 from id=2, 43 from id=3 and 4 from
id=4) , so the query need to display id 4 (still remain with 26) and id 5 (
still remain with 60)
Can someone help me on this query?
Sample Purchase Order table
id date item_code quantity
-- -- -- --
1 2005-01-02 item_A 80
2 2005-01-15 item_A 23
3 2005-02-04 item_A 43
4 2005-02-23 item_A 30
5 2005-03-13 item_A 60
Thanks in advance,
Kriste|||Right ,but without accumulated column.
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:%2353RfjIVFHA.3312@.T
K2MSFTNGP09.phx.gbl...
Thank Uri, it works.
Btw, the DDL has already been included in my 1st post.
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:OPJNzbIVFHA.1604@.TK2MS
FTNGP10.phx.gbl...
Kriste
Now , you are explained the problem by posting DDL
CREATE TABLE #Sales
(
SaleId INT NOT NULL PRIMARY KEY,
SaleDate DATETIME NOT NULL,
ItemCode CHAR(6) NOT NULL,
Quantity INT
)
INSERT INTO #Sales VALUES (1,'2005-01-02', 'item_A',80)
INSERT INTO #Sales VALUES (2,'2005-01-15', 'item_A',23)
INSERT INTO #Sales VALUES (3,'2005-02-04', 'item_A',43)
INSERT INTO #Sales VALUES (4,'2005-02-23', 'item_A',30)
INSERT INTO #Sales VALUES (5,'2005-03-13', 'item_A',60)
Declare @.Q INT
SET @.Q=100
SELECT * FROM
(
SELECT Saleid,(SELECT SUM(Quantity) FROM #Sales S
WHERE #Sales.Saleid>=S.Saleid )AS Ac
FROM #Sales
) AS DER WHERE Ac>=@.q
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:%23gB%23ePIVFHA.3544
@.TK2MSFTNGP12.phx.gbl...
Hi Uri,
I think you didn't get my question right. The data here is of Purchase Order
. I have a accumulating sales quantity.
As the sales quantity increase, I need to find out which Purchase Order quan
ity is still unsold.
Eg.
Sold sales quantity = 50, so display Purchase Order 1 to 5
Sold sales quantity = 100, so display Purchase Order 2 to 5
Sold sales quantity = 150, so display Purchase Order 4 to 5
Sample Purchase Order table
id date item_code quantity accumulated inventory
-- -- -- -- --
1 2005-01-02 item_A 80 + 80 = 80
2 2005-01-15 item_A 23 + 23 = 103
3 2005-02-04 item_A 43 + 43 = 146
4 2005-02-23 item_A 30 + 30 = 176
5 2005-03-13 item_A 60 + 60 = 236
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:%23rT3DHIVFHA.3176@.TK2
MSFTNGP12.phx.gbl...
Kriste
Can you explain if i add INSERT INTO #Sales VALUES (6,'2005-03-13', 'item_A'
,100) why do I should get back from 2 to 5?
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:eEYiF4HVFHA.1404@.TK2
MSFTNGP09.phx.gbl...
Can u explain how does this query works? I don't quite understand.
If now the sold quantity = 100, how will it return the id=2 to 5?
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:OHfl5gGVFHA.4092@.TK2MS
FTNGP12.phx.gbl...
Kriste
See, if this helps you
CREATE TABLE #Sales
(
SaleId INT NOT NULL PRIMARY KEY,
SaleDate DATETIME NOT NULL,
ItemCode CHAR(6) NOT NULL,
Quantity INT
)
INSERT INTO #Sales VALUES (1,'2005-01-02', 'item_A',80)
INSERT INTO #Sales VALUES (2,'2005-01-15', 'item_A',23)
INSERT INTO #Sales VALUES (3,'2005-02-04', 'item_A',43)
INSERT INTO #Sales VALUES (4,'2005-02-23', 'item_A',30)
INSERT INTO VALUES (5,'2005-03-13', 'item_A',60)
SELECT * FROM #Sales S
WHERE (
SELECT COUNT(*) FROM #Sales
WHERE ItemCode = S.ItemCode
AND SaleId > S.SaleId
) <= 1
ORDER BY SaleId ASC
"Kriste L" <whiteegg@.hotmail.com> wrote in message news:%23zGe4WGVFHA.3076@.T
K2MSFTNGP12.phx.gbl...
Hi Everybody,
I've provide a sample Purchase Order with some data. I need to maintain a FI
FO (first-in-first-out) for the the sale of item and at the end of the day,
will generate a report which are the Purchase Order with item still unsold.
Example, sold 150 items (80 from id=1, 23 from id=2, 43 from id=3 and 4 from
id=4) , so the query need to display id 4 (still remain with 26) and id 5 (
still remain with 60)
Can someone help me on this query?
Sample Purchase Order table
id date item_code quantity
-- -- -- --
1 2005-01-02 item_A 80
2 2005-01-15 item_A 23
3 2005-02-04 item_A 43
4 2005-02-23 item_A 30
5 2005-03-13 item_A 60
Thanks in advance,
Kriste|||hi
you can easly turn it to a sp
declare @.salessum int
declare @.id int,@.quantity int
declare unsold cursor for
select id, quantity
from porders
order by id
open unsold
declare @.sum int
set @.sum=0
set @.salessum=150
fetch next from unsold
into @.id,@.quantity
WHILE @.sum<=@.salessum
BEGIN
set @.sum=@.sum+@.quantity
FETCH NEXT FROM unsold
INTO @.id,@.quantity
END
CLOSE unsold
DEALLOCATE unsold
set @.sum=@.sum-@.salessum
set @.id=@.id-1
select id,date,item_code,@.sum as remain from porders where id=@.id
union all
select id,date,item_code,quantity as remain from porders where id>@.id
"Kriste L" wrote:

> Hi Everybody,
> I've provide a sample Purchase Order with some data. I need to maintain a
FIFO (first-in-first-out) for the the sale of item and at the end of the day
, will generate a report which are the Purchase Order with item still unsold
.
> Example, sold 150 items (80 from id=1, 23 from id=2, 43 from id=3 and 4 fr
om id=4) , so the query need to display id 4 (still remain with 26) and id 5
(still remain with 60)
> Can someone help me on this query?
> Sample Purchase Order table
> id date item_code quantity
> -- -- -- --
> 1 2005-01-02 item_A 80
> 2 2005-01-15 item_A 23
> 3 2005-02-04 item_A 43
> 4 2005-02-23 item_A 30
> 5 2005-03-13 item_A 60
> Thanks in advance,
> Kriste
>|||previous code i send may couse problems sorry:(
this is much better and safe:)
declare @.salessum int
declare @.id int,@.quantity int
declare unsold cursor for
select id, quantity
from porders
order by id
open unsold
declare @.sum int
set @.sum=0
set @.salessum=150
fetch next from unsold
into @.id,@.quantity
WHILE @.@.fetch_status=0
BEGIN
set @.sum=@.sum+@.quantity
if @.sum>@.salessum
begin
set @.sum=@.sum-@.salessum
select id,date,item_code,@.sum as remain from porders where id=@.id
union all
select id,date,item_code,quantity as remain from porders where id>@.id
end
else
FETCH NEXT FROM unsold
INTO @.id,@.quantity
END
CLOSE unsold
DEALLOCATE unsold
"POKEMON" wrote:
> hi
> you can easly turn it to a sp
> declare @.salessum int
> declare @.id int,@.quantity int
> declare unsold cursor for
> select id, quantity
> from porders
> order by id
> open unsold
> declare @.sum int
> set @.sum=0
> set @.salessum=150
> fetch next from unsold
> into @.id,@.quantity
> WHILE @.sum<=@.salessum
> BEGIN
> set @.sum=@.sum+@.quantity
> FETCH NEXT FROM unsold
> INTO @.id,@.quantity
> END
> CLOSE unsold
> DEALLOCATE unsold
>
> set @.sum=@.sum-@.salessum
> set @.id=@.id-1
> select id,date,item_code,@.sum as remain from porders where id=@.id
> union all
> select id,date,item_code,quantity as remain from porders where id>@.id
> "Kriste L" wrote:
>