Showing posts with label sale. Show all posts
Showing posts with label sale. Show all posts

Sunday, March 25, 2012

Calling SQL Gurus (query help needed!)

Hey guys, iam having major problems with this query:

"average percent increase in price of work between initial sale price and last sale price"?

if any one can find a solution it would be GREATLY appreciated.

CREATE TABLE [dbo].[Sales](
[SalesID] [int] IDENTITY(1,1) NOT NULL,
[LocationID] [smallint] NOT NULL,
[VendorID] [smallint] NOT NULL,
[WorkID] [int] NOT NULL,
[Sale_Status] [char](8)
[Previous_Sale_Price] [money] NULL,
[Sale_Price] [money] NULL,
[Commission] [float] NULL,
[Date_Up_For_Sales] [datetime] NULL,
[Date_Withdrawn_From_Sale] [datetime] NULL,
[Sale_Date] [datetime] NULL,
CONSTRAINT [pk_Sales] PRIMARY KEY CLUSTERED

CREATE TABLE [dbo].[Work](
[WorkID] [int] IDENTITY(1,1) NOT NULL,
[Work_TypeID] [tinyint] NOT NULL,
[Work_Title] [varchar](40) NOT NULL,
[Creation_Date] [datetime] NOT NULL,
CONSTRAINT [pk_Work] PRIMARY KEY CLUSTEREDnot enough info, even for a homework assignment

surely the assignment gives a hint about what constitutes the initial and the final sale price? how do these relate to the columns Sale_Price and Previous_Sale_Price? what is Sale_Status and how does it apply?|||I always find it amusing that these students think an "SQL Guru" is required to help them cheat on their simple homework assignments.|||items are put up for sale, these items can be sold many times, if an item is sold or put up for sale it is marked accordingly in the sale status for eg "for sale" "sold" etc...

i need to no the average increase in price for an item between its original sale date and its last sale date.

So i assume the query will go something like this: filter by status to find all the items that have been involved in a sale, "sold" items, and then find first date it was put up for sale "Min" date_up_for_sale: then find its last sale date "max" sale_date: the average Previous_Sale_Price and Sale_Price|||Use a common table expression to find the first price for an item, a second CTE for the last price, then calculate the average over the difference of both prices. Instead of CTEs, you can use subselects. No big deal...|||if you want the average price.. then presumably you want to look at the AVERAGE of all sales, how you calculate that is up to you.. do you want a simple AVG or a weighted one.. where you give weight to number of items sold at that price
say you had 2 sales one for 10 items at 15.25, one at 14.00, one at 14.25
a simple average would be (15.25+14.00+14.25)/3= 14.50, whereas the weighted average would be (15.25*10 + 14 + 14.25)/12 =15.06

BTW have you investigated what functions are available as part of SQL?
I wonder if SQL has an AVG or even AVERAGE function that may help?|||SQL has an AVG() aggregate function (aka column function), of course.

Sunday, February 19, 2012

calculation question

Hi,

how can write MDX in calculation for this case I highlighted in red?

product Sale contribution

-

a | 30.00 30/80 = 0.375

b | 50.00 50/80 =0.625

total | 80.00

-

[measures].[sale]/ sum ([measures].[sale] in current level ) ?

Thanks in advance.

Dear Friend,

try this and tell me if works:

Code Snippet

WITH
SET [InterestingProduts] AS
'{[Product].&[2],
[Product].&[1]}'

MEMBER [Product].[TotalOfInterestingProducts] AS
'SUM([InterestingProduts])'
MEMBER [MEASURES].[Percentage] AS
'[Measures].[Sales]/([Measures].[Sales],[Product].[TotalOfInterestingProducts])',

SOLVE_ORDER=5

SELECT {[Measures].[Sales],[MEASURES].[Percentage]} ON COLUMNS,
{[Product].members,[Product].[TotalOfInterestingProducts]} ON ROWS
FROM [YourCube]

Is more completed... but try if works!

regards!

|||

Hi PedroCGD,

Thanks for your code Smile

It works perfectly.

And I have another challenge that if I want to view percentage in each level (please see the example below) then how should I define for this scnario?

Currently, it only works in product a / b level. If I drill down and see the percentage then it calculates like this below:

i.g. product a-1 : 10/100 --> want to correct 10/30

product a-2: 20/ 100 > want to correct 20/30

percentage

product a

product a-1 10 10/30

product a-2 20 20/30

sub total 30 30/100

product b

product b-1 20 20/70

product b-2 50 50/70

sub total 70 70/100

Total 100

Here is the calculation script below:

CREATE MEMBER CURRENTCUBE.[Product].[Div-Quad Hierarchy].[Total] AS 'SUM([ALL])'; /* I may change this but not clear now */

CREATE MEMBER CURRENTCUBE.[MEASURES].[% Contrib Sls $]

AS

'[Measures].[Net Sale Dollars_Prod]/([Measures].[Net Sale Dollars_Prod], [product].[Div-Quad Hierarchy].[Total])',

SOLVE_ORDER=2,

FORMAT_STRING = "Percent",

VISIBLE = 1 ;

Please give me any comments.

Thanks in advance Smile

|||

Dear abc,

I'm happy you get it!! :-)

I will try to help you in your new requirement more late (few hours), ok? At the moment I'm very, very busy! :-(

Regards!!!

|||

Hi Pedro,

It seems you are busy Sad

If I run the mdx query,I can get a right result for each product level if I change this area that I highlighted in yellow.

But if I add this member in the calculation script then I don't know how to apply this for each particular product level.

FYI, I have product hierarchy (division > Q > Q name > ...) and we want to see the contribution percent in the total amount of the each product level not grand total of total products.

MEMBER [Product].[Div-Quad Hierarchy].[Total] AS

'SUM([Product].[Div-Quad Hierarchy].[division].&[interesting product name ])'

MEMBER [MEASURES].[Percentage] AS

'[Measures].[Net Purch Dollars_prod]/([Measures].[Net Purch Dollars_prod], [product].[Div-Quad Hierarchy].[Total])',

SOLVE_ORDER=2,

FORMAT_STRING="percent"

Please give me some comments.

Thanks in advance.

calculation question

Hi,

how can write MDX in calculation for this case I highlighted in red?

product Sale contribution

-

a | 30.00 30/80 = 0.375

b | 50.00 50/80 =0.625

total | 80.00

-

[measures].[sale]/ sum ([measures].[sale] in current level ) ?

Thanks in advance.

Dear Friend,

try this and tell me if works:

Code Snippet

WITH
SET [InterestingProduts] AS
'{[Product].&[2],
[Product].&[1]}'

MEMBER [Product].[TotalOfInterestingProducts] AS
'SUM([InterestingProduts])'
MEMBER [MEASURES].[Percentage] AS
'[Measures].[Sales]/([Measures].[Sales],[Product].[TotalOfInterestingProducts])',

SOLVE_ORDER=5

SELECT {[Measures].[Sales],[MEASURES].[Percentage]} ON COLUMNS,
{[Product].members,[Product].[TotalOfInterestingProducts]} ON ROWS
FROM [YourCube]

Is more completed... but try if works!

regards!

|||

Hi PedroCGD,

Thanks for your code Smile

It works perfectly.

And I have another challenge that if I want to view percentage in each level (please see the example below) then how should I define for this scnario?

Currently, it only works in product a / b level. If I drill down and see the percentage then it calculates like this below:

i.g. product a-1 : 10/100 --> want to correct 10/30

product a-2: 20/ 100 > want to correct 20/30

percentage

product a

product a-1 10 10/30

product a-2 20 20/30

sub total 30 30/100

product b

product b-1 20 20/70

product b-2 50 50/70

sub total 70 70/100

Total 100

Here is the calculation script below:

CREATE MEMBER CURRENTCUBE.[Product].[Div-Quad Hierarchy].[Total] AS 'SUM([ALL])'; /* I may change this but not clear now */

CREATE MEMBER CURRENTCUBE.[MEASURES].[% Contrib Sls $]

AS

'[Measures].[Net Sale Dollars_Prod]/([Measures].[Net Sale Dollars_Prod], [product].[Div-Quad Hierarchy].[Total])',

SOLVE_ORDER=2,

FORMAT_STRING = "Percent",

VISIBLE = 1 ;

Please give me any comments.

Thanks in advance Smile

|||

Dear abc,

I'm happy you get it!! :-)

I will try to help you in your new requirement more late (few hours), ok? At the moment I'm very, very busy! :-(

Regards!!!

|||

Hi Pedro,

It seems you are busy Sad

If I run the mdx query,I can get a right result for each product level if I change this area that I highlighted in yellow.

But if I add this member in the calculation script then I don't know how to apply this for each particular product level.

FYI, I have product hierarchy (division > Q > Q name > ...) and we want to see the contribution percent in the total amount of the each product level not grand total of total products.

MEMBER [Product].[Div-Quad Hierarchy].[Total] AS

'SUM([Product].[Div-Quad Hierarchy].[division].&[interesting product name ])'

MEMBER [MEASURES].[Percentage] AS

'[Measures].[Net Purch Dollars_prod]/([Measures].[Net Purch Dollars_prod], [product].[Div-Quad Hierarchy].[Total])',

SOLVE_ORDER=2,

FORMAT_STRING="percent"

Please give me some comments.

Thanks in advance.

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 member with measure and dimension

Hi,

We have a little problem with our calculated member... in our table we have the total amount of a sale in 2 different currencies (US and CAN).

In another field we have the currency (1 being can, 2 being US)

Now I need to create a measure that will give me all the amount in CAN money and one in US money (depending on how users want to view the cube) but it's not working since my Exchange rate is not a measure and the type of currency isn't either, so the system isn't entering in my case.

I tried to do something like this :

case [Probill General].[Payeur Fund type]

when 1

then ([Measures].[Probill Total] * [Probill General].[Us Exch Rate])

when 2

then ([Measures].[Probill Total])

end

But it doesn't enter in the cases and it gives me an empty data field when I put it in the cube.

Any idea on how to bypass that problem?

Thanks,

Jason

Hello. One problem with your Case statement is that you do no write this in the TSQL way.

Instead try something like:

Case When [Probill General].[Payeur Fund type] = 1

Then [Measures].[Probill Total] * [Probill General].[Us Exch Rate])

Else [Measures].[Probill Total]

End

A good Blog post about currency conversion you can find here: http://blogs.conchango.com/christianwade/archive/2006/08/24/Currency-Conversion-in-Analysis-Services-2005.aspx

HTH

Thomas Ivarsson

|||

Didn't work...

I went to that page and tried to create something based on that which didn't really solved my problem.

I think the real problem in my cube lies in the fact that my table has total that are both in canadian funds and us funds... all the solution on the web are to convert a full us fund (or other) to another currency but I can't seem to find anything where I can just transform or not an amount into a currency based on a dimension field...

I talked to my boss and she wants everything in Canadian funds, which means that IF my "[Probill General].[Payeur Fund type] = 1" then "[Measures].[Probill Total]" stays the same, but if it's "[Probill General].[Payeur Fund type] = 2" then it has to be "[Measures].[Probill Total] / [Probill General].[Us Exch Rate]" .

Any idea on how you can apply that in a cube?

Thanks

|||

With only two currencies the simple solution is to solve this problem in the ETL-process(not inte cube), when you load your data mart/data warehouse.

You will need a currency table with dates and currencies. If you have the currency code in the fact table when you load data, you join the date and the currency with your currency table.

If the requirement is that all values should be i CAD you only need to keep the exchange rate for CAD against your incoming currencies.

HTH

Thomas Ivarsson

Calculated member with measure and dimension

Hi,

We have a little problem with our calculated member... in our table we have the total amount of a sale in 2 different currencies (US and CAN).

In another field we have the currency (1 being can, 2 being US)

Now I need to create a measure that will give me all the amount in CAN money and one in US money (depending on how users want to view the cube) but it's not working since my Exchange rate is not a measure and the type of currency isn't either, so the system isn't entering in my case.

I tried to do something like this :

case [Probill General].[Payeur Fund type]

when 1

then ([Measures].[Probill Total] * [Probill General].[Us Exch Rate])

when 2

then ([Measures].[Probill Total])

end

But it doesn't enter in the cases and it gives me an empty data field when I put it in the cube.

Any idea on how to bypass that problem?

Thanks,

Jason

Hello. One problem with your Case statement is that you do no write this in the TSQL way.

Instead try something like:

Case When [Probill General].[Payeur Fund type] = 1

Then [Measures].[Probill Total] * [Probill General].[Us Exch Rate])

Else [Measures].[Probill Total]

End

A good Blog post about currency conversion you can find here: http://blogs.conchango.com/christianwade/archive/2006/08/24/Currency-Conversion-in-Analysis-Services-2005.aspx

HTH

Thomas Ivarsson

|||

Didn't work...

I went to that page and tried to create something based on that which didn't really solved my problem.

I think the real problem in my cube lies in the fact that my table has total that are both in canadian funds and us funds... all the solution on the web are to convert a full us fund (or other) to another currency but I can't seem to find anything where I can just transform or not an amount into a currency based on a dimension field...

I talked to my boss and she wants everything in Canadian funds, which means that IF my "[Probill General].[Payeur Fund type] = 1" then "[Measures].[Probill Total]" stays the same, but if it's "[Probill General].[Payeur Fund type] = 2" then it has to be "[Measures].[Probill Total] / [Probill General].[Us Exch Rate]" .

Any idea on how you can apply that in a cube?

Thanks

|||

With only two currencies the simple solution is to solve this problem in the ETL-process(not inte cube), when you load your data mart/data warehouse.

You will need a currency table with dates and currencies. If you have the currency code in the fact table when you load data, you join the date and the currency with your currency table.

If the requirement is that all values should be i CAD you only need to keep the exchange rate for CAD against your incoming currencies.

HTH

Thomas Ivarsson