Showing posts with label everybody. Show all posts
Showing posts with label everybody. Show all posts

Thursday, March 22, 2012

Calling Functions

Dear Everybody,

Hi again!

Supposing I have an SQL Server Function named Fxn1 which returns a VARCHAR(6) value. I want to use this returned value in another function named Fxn2 but Fxn2 accepts a parameter of type INT. Fxn2 itself returns an INT value. This has to be achieved in a single line.

The following line of code will not work in SQL Server. How should I achieve this?

declare @.Res INT
EXEC @.Res = Fxn2 (CONVERT( INT, Fxn1 ))

Thanx in advance!Just curioius, but have you tried talking to the TA or your instructor? Its really important that you talk to them when you are stumped on homework, they need the feedback as much as you need the help finding the solution.

-PatP|||No thx for the suggestion. :rolleyes:

I always thought that this forum had people who cud come up with suggestions which were technical in nature!|||It does, but we still won't do your homework!

-PatP|||First of all stop calling this homework. You cannot pass judgements like that. To me it was a genuine problem that's why I referred it to this forum. Might be not for u.

If u r not interested, gr8. But keep ur thoughts to urself.|||"This has to be achieved in a single line."?

Are you running out of lines on your computer?

You may just need to create a single function that combines the logic of the two you have.|||I don't have any problem giving legitimate help (read a few thousand of my posts if you'd like to confirm that), but based on the attitude that you've displayed and the grammer and spelling that you've used (or more accurately failed to use), you appear to me to be a rather rude kid that is sqawking because they got caught trying to get someone to do their homework. If I've misinterpreted something, I'm sorry, but that is how you appear to me at the moment.

Based on the way you presented the question in your original posting, it reads like homework. Are you saying that it isn't homework? If not, can you give us a little background in what you are trying to do, and more importantly why it needs to be accomplished in a single line?

-PatP|||Are you running out of lines on your computer?I just hate it when that happens, don't you?

-PatP|||What error are you getting when you execute the statement? Obviously
EXEC @.Res = Fxn2 (CONVERT( INT, Fxn1 ))
...will error out if Fxn1 returns a string that cannot be converted to an INT.
Also, post the actual statement you are sending.|||Dear blindman (although I hate to use that term to address u),

First of all thx for ur concern & help. Can I show the audacity to ask your real name?

Second of all, lets move to my actual problem..

I have several procedures and functions in Oracle which I have to convert to SQL Server syntax. The Fxn1 and Fxn2 names that I had used was just to present the problem to the forum in a simple manner.
In one of the functions named CREATE_TRIPS_SCHED, a SELECT statement has been included (the result of which has been used in another query) which is as follows :

SELECT 'P'||T.T_ID, T.S_TIME, T.END_TIME,
NVL(T.DESIRED_TIME, MIN_TO_AMPM( TO_MINUTES(TO_NUMBER(AMPM2FULLHRS(T.EST_TIME))))),
T.DISP, DECODE(T.WHEEL_CHAIR,'Y',5,2)
FROM DISPATCH_TRIPS T
WHERE T.R_NUM LIKE 'X%'

In the above statement, MIN_TO_AMPM, TO_MINUTES and AMPM2FULLHRS are all UDFs, out of which MIN_TO_AMPM and TO_MINUTES fxns return INTEGER values and AMPM2FULLHRS returns a VARCHAR value.

The conversion for SQL Server that I had done is as under :

SELECT 'P' + T.T_ID, T.S_TIME, T.END_TIME,
(CASE T.DESIRED_TIME WHEN ISNULL THEN MIN_TO_AMPM( TO_MINUTES(CONVERT (INT, AMPM2FULLHRS (T.EST_TIME)))) ELSE T.DESIRED_TIME END),
T.DISP, (CASE T.WHEEL_CHAIR WHEN 'Y' THEN 5 ELSE 2 END)
FROM DISPATCH_TRIPS T
WHERE T.R_NUM LIKE 'X%'

And the error msg that I am getting is this :

Server: Msg 195, Level 15, State 10, Procedure CREATE_TRIPS_SCHED, Line 25
'AMPM2FULLHRS' is not a recognized function name.
Server: Msg 156, Level 15, State 1, Procedure CREATE_TRIPS_SCHED, Line 56
Incorrect syntax near the keyword 'END'.
Server: Msg 156, Level 15, State 1, Procedure CREATE_TRIPS_SCHED, Line 68
Incorrect syntax near the keyword 'END'.

I have to do this conversion without introducing other fxns of mine. So, this is the actual problem. I hope I have been able to get across my problem!

Thx again! :)|||Pat,

At least you had the decency to be 'sorry' even if my turning 'rude' prompted you to do so.

Originally Posted by Pat Phelan
It does, but we still won't do your homework!
Having a few thousand posts does not give you the right to be judgemental. It also does not entitle you to speak on behalf of the whole forum.

P.S. Are u always like that?|||Perhaps adding the owner of the function when calling the function helps (db.owner.function)?|||It appears that my first impression may have been wrong. Based on your last post to blindman, you do seem to have a good grasp of SQL and a practical reason for making your original request.

I think you should have success if you use:SELECT @.Res = dbo.Fxn2 (CONVERT( INT, dbo.Fxn1() ))This ought to safely degenerate into:SELECT @.Res = dbo.Fxn2 (dbo.Fxn1() )In answer to your other question, yes, especially when this forum is barraged with kids looking for someone to do their homework for them, I tend to react with considerable skepticism to posts that are presented that way. My intent is to "red flag" those posts for other forum members, so that they will consider whether they want to spend their time working on a problem that is likely going to be turned in by a kid with no appreciation and no clue. The other forum members can use their own judgement, I'm just trying to make sure that they think before they "burn daylight" answering a post that I perceive as looking for an opportunity to plagerize.

I've repeatedly had my work turned in by students, whose teachers have recognized my style and contacted me. That probably makes me overly sensitive to the issue. I also think that there is a real ethical problem there too, and being in a position where ethical behavior is probably more important than technical expertise, that is very important to me.

Based on the begining of this thread, I had no reason to suspect that you were anything other than an apprentice juvenile delinquent, trolling to find someone that would do their assignment for them. Based on your post to Blindman, it appears that my first impression was wrong, so I responded differently in this post. As I've said before, I have no problem giving legitmate technical assistance, but I outright refuse to allow someone to take advantage of me (or anyone else without their approval).

-PatP|||Knighthassan,

Pat, myself, and others have given literally thousands of bits of advice and help to people who have posted on these boards, all of it free. If Pat made a mistake about your situation then so be it, but he had reasons to be suspicious. People asking for free advice should be less disrespectful, especially of people like Pat who is arguably the most knowledgable SQL Server expert on this forum. The fact that he took considerable time to review your complex code and propose a solution after last few posts you made shows how gracious he is. I hope his solution was helpful.|||blindman and Pat,

Thx to u both my problem is resolved. I also realize that my language has been a bit too harsh at times but something there got to me. Anyways, no hard feelings and thx again.|||blindman and Pat,

Thx to u both my problem is resolved. I also realize that my language has been a bit too harsh at times but something there got to me. Anyways, no hard feelings and thx again.No problem, and I'm glad that your problem has been fixed.

I rarely miss on issues like this, but of course when I do then it's a doozie. Out of several hundred "homework" calls that I've made, this is only the second one that I know where my initial call was wrong.

Right now I'm probably overly sensitive to it, since American schools are just coming back from the Thanksgiving holiday which meant that quite literally tens of thousands of assignments were due last week.

I noticed that you'd dived into some other message threads and contributed to them too. I hope that you decide to stick around and join in the fun! Lord knows that there is no shortage of questions, and it always helps to have a different perspective (although mine is usually quite "different" all by itself!).

-PatP|||I'm glad we (mosty Pat) were able to assist you, and yeah working with databases can be frustrating in itself sometimes, but we try to keep things light on this forum so no hard feelings and post again with both questions and advice.|||Pat, myself, and others have given literally thousands of bits of advice and help to people who have posted on these boards, all of it free.

Thank you

If Pat made a mistake about your situation then so be it, but he had reasons to be suspicious.

Umm, maybe not

People asking for free advice should be less disrespectful

Absolutely...why do people get so worked up? Who cares what soemone says? This always cracks me up.

especially of people like Pat who is arguably the most knowledgable SQL Server expert on this forum.

Well, could be, but I know Nigel posts here from time to time.

The fact that he took considerable time to review your complex code and propose a solution after last few posts you made shows how gracious he is. I hope his solution was helpful.

I think he'd still be having trouble. Post the actuall CREATE FUNCTION statements for both functions...personally they look more like a sproc to me...

And as for B. Lindman, you could probaly say "Yo, Blind dude!"

I always wondered where he got a braille keyboard..|||And as for B. Lindman, you could probaly say "Yo, Blind dude!"

I always wondered where he got a braille keyboard..Ah, Braille keyboards are no biggie to get. The difference is only in a few keys, but the tactile feedback for the indicators is the challenging part.

-PatP|||Wut's tuff iss finding a ceeing I dogg thata caan tipe woof a dam.

Bad dog! Bad!|||Originally Posted by Brett Kaiser
I always wondered where he got a braille keyboard..

A braille display and a braille mouse too! The world gets curiouser and unfathomable each passing day..

:D|||A braille display and a braille mouse too! The world gets curiouser and unfathomable each passing day..You say that as though it were a bad thing! ;)

As a side note, couldn't you get into some kind of trouble for feeling the bumps on a Braille mouse in the office ?

-PatP|||Depends on what you mean by mouse...

Sunday, March 11, 2012

Calling a SP in UDF and Storing Value Return By SP In UDF

Hi EveryBody,
I need to call a Stored Procedure in a UDF.This input of SP would be a string and in turn that output Of SP would also be a string.Also i need to store the output of SP in a Calling UDF.Is it possible if Yes Please tell me How can i make It.Please help.
Thanks!!!
DheerajDECLARE @.outstring varchar(500), @.instring varchar(500)
EXEC dbo.usp_YourStoredProc @.instring,@.outstring OUTPUT
--now you can use the @.outstring in your code as you want.
|||

Hi Thanks for reply,
I have done this also but its giving me Error - "Only functions and extended stored procedures can be executed from within a function." .I think I have to make SP As Extended SP I dont know how to make it can you provide any kind of sample for it.It would get more clear to me.
Thanks,
Dheeraj Verma

|||rather than trying to convert a stored proc to extended stored proc you could convert your stored proc to a function, unless you have some DML statements in your function?

Calling a Stored procedure from a UDF

Hi Everybody,
I was wondering if it is possible to call a Stored Procedure from inside a UDF. Any help is appreciated.
Thanks,
TarunI tried to create a function that called a stored procedure, it won't allow you @. execution time.

Msg 557, Level 16, State 2, Server ATLAS, Procedure fnRevenue, Line 6
Only functions and extended stored procedures can be executed from within a function.

Friday, February 24, 2012

calendar format

Hi, everybody.
I have a problem with the calendar.
I have a parameter from datetime type.
But I want to change this calendar's format.
When I click the calendar button near the parameter textbox and select a date like 14.3.2001, 3.14.2001 is written to the textbox. And this causes to an error.(Because 14 can't be a month number)
How can I change that parameter's format or calendar's format?
Any idea about this?The date supplied to the calendar is based on the accept language for the browser. In IE look at Tools->Internet Options->Languages and make sure the top language is what you expect.|||

Daniel Reib wrote:

The date supplied to the calendar is based on the accept language for the browser. In IE look at Tools->Internet Options->Languages and make sure the top language is what you expect.

Thanks I did what you said but it didn't work.
Because I am not working on IE now .
I am working on Reporting Services side on .NET.
I changed regional settings from control panel too but nothing different happened.
I couldn't see the calendar's properties.
What can I do else?

Sunday, February 19, 2012

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:
>

Friday, February 10, 2012

Calculated values and database design.

Hi everybody,

I need some help on calculated values in my database design.

I'm creating an accounting / business management application with the
usual modules like A/R, A/P, Inventory, etc.

I don't know how to handle calculated values in fields like "Customer
Balance", "Inventory Item Qty on Hand", "Inventory Item Qty Last cost"
and other similar.

I don't want to create fields in the database to store these values
but I want to create them on the fly instead. Another thing I prefer
to do is use code in my application (vb.net) not T-SQL to do this
(stored procedure or views), because this way I'm moving business
logic into SQL; so far all the logic is located in a Business Rules
DLL.

My question is how can I do that and at the same time achieve "easy"
joins.

For example:
Every time I retrieve some customer's info I need his balance. Let's
say if my query is:

SELECT CustomerID, CustomerName, Address from Customers
Where CustomerID = xxxxxx'

How can I retrieve the balance, when is calculated from other fields
in other tables, such as Invoices, Credit Memos and Cash Receipts?

I thought to use views and add a join to the above query. Is that good
or bad, performance wise?

What other options I have?

Looking forward for your answer.

Thanks in advanced.
StefanosBusiness logic belongs server-side if it involves summarizing data.
Otherwise you might as well use a file-server database like Access. You
don't want to have to retrieve all the data just to add it up at the client.

I can only guess at your table structure and keys but maybe this example
will help:

SELECT C.customerid, C.customername, C.address,
SUM(T.trans_amount) AS balance
FROM Customers AS C
JOIN Transactions AS T
ON C.customerid = T.customerid
AND C.customerID = 'xxxxxx'

--
David Portas
SQL Server MVP
--|||OOPS! That wasn't very good!

Correction:

SELECT C.customerid, C.customername, C.address,
SUM(T.trans_amount) AS balance
FROM Customers AS C
JOIN Transactions AS T
ON C.customerid = T.customerid
AND C.customerID = 'xxxxxx'
GROUP BY C.customerid, C.customername, C.address

--
David Portas
SQL Server MVP
--|||David,

Thanks for your reply.

I'm following a "fat-client" implementation.
I agree that business rules belong to the server or in my case the
"fat client".
I don't want to put any logic on the database in the form of sprocs or
similar;
I have enough layers in my design already and I don't want to be tied
in a particular RDBS (currently using MS SQL 2000).

A query like the one you posted is what I've thinking to do, but I
hoped maybe someone has a better solution.
Queries like these are very hard to create in multiple joins.

In your example the table "Transactions" isn't enough.

I have to get totals from:
* InvoiceHeader
* CreaditMemoHeader
* CashReceiptsHeader

Imagine now if I have to get the balance in a query that has another
2-3 joins on it.

Stefanos

"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message news:<OpidnUri8Z4fmRndRVn-sQ@.giganews.com>...
> OOPS! That wasn't very good!
> Correction:
> SELECT C.customerid, C.customername, C.address,
> SUM(T.trans_amount) AS balance
> FROM Customers AS C
> JOIN Transactions AS T
> ON C.customerid = T.customerid
> AND C.customerID = 'xxxxxx'
> GROUP BY C.customerid, C.customername, C.address