I have two tables, one that represents stock trades:
Blotter
TradeDate Symbol Shares Price
2014-09-02 ABC 100 157.79
2014-09-10 ABC 200 72.50
2014-09-16 ABC 100 36.82
and one that stores a history of stock splits for all symbols:
Splits
SplitDate Symbol Factor
2014-09-08 ABC 2
2014-09-15 ABC 2
2014-09-20 DEF 2
I am trying to write a report that reflects trades and includes what their current split adjustment factor should be. For these table values, I would expect the report to look like:
TradeDate Symbol Shares Price Factor
2014-09-02 ABC 100 157.79 4
2014-09-10 ABC 200 72.50 2
2014-09-16 ABC 100 36.82 1
The first columns are taken straight from Blotter - the Factor should represent the split adjustments that have taken place since the trade occurred (the Price is not split-adjusted).
Complicating matters is that each symbol could have multiple splits, which means I can't just OUTER JOIN the Splits table or I will start duplicating rows.
I have a subquery that I adapted from https://stackoverflow.com/a/3912258/3063706 to allow me to calculate the product of rows, grouped by symbol, but how do I only return the product of all Splits records with SplitDates occurring after the TradeDate?
A query like the following
SELECT tb.TradeDate, tb.Symbol, tb.Shares, tb.Price, ISNULL(s.Factor, 1) AS Factor
FROM Blotter tb
LEFT OUTER JOIN (
SELECT Symbol, EXP(Factor) AS Factor
FROM
(SELECT Symbol, SUM(LOG(ABS(NULLIF(Factor, 0)))) AS Factor
FROM Splits s
WHERE s.SplitDate > tb.TradeDate -- tb is unknown here
GROUP BY Symbol
) splits) s
ON s.Symbol = tb.Symbol
returns the error "Msg 4104, Level 16, State 1, Line 1 The multi-part identifier "tb.TradeDate" could not be bound."
Without the inner WHERE clause I get results like:
TradeDate Symbol Shares Price Factor
2014-09-02 ABC 100 157.79 4
2014-09-10 ABC 200 72.50 4
2014-09-16 ABC 100 36.82 4
Update The trade rows in Blotter are not guaranteed to be unique, so I think that rules out one suggested solution using a GROUP BY.
One way without changing the logic too much is to put the factor calculation into a table valued function:
create function dbo.FactorForDate(
#Symbol char(4), #TradeDate datetime
) returns table as
return (
select
exp(Factor) as Factor
from (
select
sum(log(abs(nullif(Factor, 0)))) as Factor
from
Splits s
where
s.SplitDate > #TradeDate and
s.Symbold = #Symbol
) splits
);
select
tb.TradeDate,
tb.Symbol,
tb.Shares,
tb.Price,
isnull(s.Factor, 1) as Factor
from
Blotter tb
outer apply
dbo.FactorForDate(tb.Symbol, tb.TradeDate) s;
To do it in a single statement is going to be something like:
select
tb.TradeDate,
tb.Symbol,
tb.Shares,
tb.Price,
isnull(exp(sum(log(abs(nullif(factor, 0))))), 1) as Factor
from
Blotter tb
left outer join
Symbol s
on s.Symbol = tb.Symbol and s.SplitDate > tb.TradeDate
group by
tb.TradeDate,
tb.Symbol,
tb.Shares,
tb.Price;
This will probably perform better if you can get it to work.
Apologies for any syntax errors, don't have access to SQL at the moment.
Related
I am working on a report that gives me a Monthly Rate for each Generator we have based on the GenAmps and GenVolts. If My Pricing table has the GenAmps # and GenVolt size then it will populate a Monthly Rate. There are cases as you can see below when there is a GenAmps # and GenVolts value that are not found in my Pricing table. When this occurs i want the MonthlyRate to pull from the closest GenAmps # where the GenVolts value equal, ex(GenAmps 15, GenVolts KW) which is not found in the pricing table, so i want it to pull the MonthlyRate from the (GenAmps 20, GenVolts KW)
GenAmps GenVolts PricingGenAmp PricingGenVolt MonthRate(160hr)
10 KW NULL NULL NULL
15 KVA NULL NULL NULL
This is what my report looks like if they cannot find the GenAmps # and GenVolts value in the pricing table.
GenAmps GenVolts MonthRate(160hr)
25 KVA 1251
20 KW 1251
(Seen above is from the pricing table). Since these are not in the pricing table i want the 10KW to pull the Monthly rate from the 20 KW and the 15 KVA from the 25 KVA. So it would look like this:
GenAmps GenVolts PricingGenAmp PricingGenVolt MonthRate(160hr)
10 KW 20 KW 1251
15 KVA 25 KVA 1251
It should look like the data above when the query is working correctly.
There are GenAmps # that range from 10 - 900 that are not found in the pricing table.
this is the Query that i currently have, which needs to be revised so i can make the data look the way i want it to look.
SELECT TOP (100) PERCENT ContractID, SaleRep, GenAmps, GenVolts, GenPhase, EstRunningHours, MonthlyRate, DeliveryRate, PickupRate, FuelRate, DropCharge, HourlyServiceRate,
CASE WHEN gr.PricingGenAmps IS NULL THEN
(SELECT MAX(GenAmps) AS a1
FROM dbo.GeneratorSizePrices AS a
WHERE (GenAmps < gr.GenAmps) AND a.GenVolts = gr.GenVolts) ELSE gr.PricingGenAmps END AS PricingGenAmp, ISNULL([MonthRate(160hr)],
(SELECT [MonthRate(160hr)]
FROM dbo.GeneratorSizePrices AS b
WHERE (GenAmps =
(SELECT MAX(GenAmps) AS a
FROM dbo.GeneratorSizePrices
WHERE (GenAmps < gr.GenAmps) AND (GenVolts = gr.GenVolts))))) AS Month
FROM dbo.Elliott_GensRentalRate AS gr
GROUP BY ContractID, SaleRep, GenAmps, GenVolts, GenPhase, EstRunningHours, MonthlyRate, DeliveryRate, PickupRate, FuelRate, DropCharge, HourlyServiceRate, PricingGenAmps, [MonthRate(160hr)]
ORDER BY PricingGenAmps, GenAmps
I'm not really sure about what's going on in the rest of the query. But the part of the problem that involves matching up a row with the nearest row in another table can be approached this way.
select *
from
dbo.Elliott_GensRentalRate as gr cross apply
(
select gsp.*, row_number() over
(order by abs(gr.GenAmps - gsp.GenAmps), sign(gr.GenAmps - gsp.GenAmps)) as rnk
from dbo.GeneratorSizePrices as gsp
where gsp.GenVolts = gr.GenVolts
) as gspr /* GeneratorSizePricesRanked */
where gspr.rnk = 1
The cross apply lets us look at the value of GenAmps from the gr table that's outside the query. There are other ways to accomplish this when cross apply isn't available but this is very likely to be more efficient. The key then is to rank the potential matches by the absolute value of the differences and then keep only the one that came out on top.
Since you should probably handle ties I used the sign() function to determine whether the higher or lower match is the preferred one. As written it would favor rounding up. You could also use outer apply if there are cases where a match might not be found at all for some reason.
This should get you started. It's hard to determine without all of the data though. Cheers!
IF OBJECT_ID('tempdb..#gens') IS NOT NULL DROP TABLE #gens
IF OBJECT_ID('tempdb..#genPrice') IS NOT NULL DROP TABLE #genPrice
CREATE TABLE #gens(
GenAmps INT,
GenVolts VARCHAR (3),
PricingGenAmp INT null,
PricingGenVolt VARCHAR (3) null,
MonthRate DECIMAL (6,2) null)
CREATE TABLE #genPrice(
GenAmps INT,
GenVolts VARCHAR (3),
MonthRate DECIMAL (6,2))
INSERT INTO #gens
VALUES(10,'KW',null,null,null),(15,'KVA',null,null,null)
INSERT INTO #genPrice
VALUES(25,'KVA',1251.00),(20,'KW',1151.00)
SELECT
g.GenAmps,
g.GenVolts,
ISNULL(g.PricingGenAmp,(SELECT MIN(gp.GenAmps) FROM #genPrice gp WHERE g.GenVolts = gp.GenVolts and gp.GenAmps > g.GenAmps)) AS PricingGenAmp,
ISNULL(g.PricingGenVolt,(SELECT GenVolts FROM #genPrice gp2 WHERE gp2.GenAmps = (SELECT MIN(gp.GenAmps) FROM #genPrice gp WHERE g.GenVolts = gp.GenVolts and gp.GenAmps > g.GenAmps))) as PricingGenVolt,
ISNULL(g.MonthRate,(SELECT MonthRate FROM #genPrice gp3 WHERE gp3.GenAmps = (SELECT MIN(gp.GenAmps) FROM #genPrice gp WHERE g.GenVolts = gp.GenVolts and gp.GenAmps > g.GenAmps))) as MonthRate
FROM
#gens g
There is one scheme and different items inside it, so the scenario is that if user send SchemeID to the procedure then it should return the SchemeName(once) and all items inside a scheme i.e. DescriptionOfitem, Quantity, Rate, Amount... in this format
SchemeName DescriptionOfItems Quantity Unit Rate Amount
Scheme01 Bulbs 2 M2 200 400
Titles 10 M3 300 3000
SolarPanels 2 M2 1000 2000
Bricks 50 M9 50 2500
Total 7900
My try, it works but it also repeats the SchemeName for each row and can't find total
Select
Schemes.SchemeName,
ContractorsWorkDetails.ContractorsWorkDetailsItemDescription,
ContractorsWorkDetails.ContractorsWorkDetailsUnit,
ContractorsWorkDetails.ContractorsWorkDetailsItemQuantity,
ontractorsWorkDetails.ContractorsWorkDetailsItemRate,
ContractorsWorkDetails.ContractorsWorkDetailsAmount
From ContractorsWorkDetails
Inner Join Schemes
ON Schemes.pk_Schemes_SchemeID= ContractorsWorkDetails.fk_Schemes_ContractorsWorkDetails_SchemeID
Where ContractorsWorkDetails.fk_Schemes_ContractorsWorkDetails_SchemeID= 2
Update:
I tested the query as suggested below but it gives this kinda result
You can get the total using grouping sets. I would advise you to keep the schema name on each row. If you want it filtered out on certain rows, then do that at the application layer.
Now, having said that, I think this will do what you want in SQL:
Select (case when GROUPING(cwd.ContractorsWorkDetailsItemDescription) = 0
then 'Total'
when row_number() over (partition by s.SchemeName
order by cwd.ContractorsWorkDetailsItemDescription
) = 1
then s.SchemeName else ''
end) as SchemeName,
cwd.ContractorsWorkDetailsItemDescription,
cwd.ContractorsWorkDetailsUnit,
cwd.ContractorsWorkDetailsItemQuantity,
cwd.ContractorsWorkDetailsItemRate,
SUM(cwd.ContractorsWorkDetailsAmount) as ContractorsWorkDetailsAmount
From ContractorsWorkDetails cwd Inner Join
Schemes s
ON s.pk_Schemes_SchemeID = cwd.fk_Schemes_ContractorsWorkDetails_SchemeID
Where cwd.fk_Schemes_ContractorsWorkDetails_SchemeID = 2
group by GROUPING SETS ((s.SchemeName,
cwd.ContractorsWorkDetailsItemDescription,
cwd.ContractorsWorkDetailsUnit,
cwd.ContractorsWorkDetailsItemQuantity,
cwd.ContractorsWorkDetailsItemRate
), s.SchemeName)
Order By GROUPING(cwd.ContractorsWorkDetailsItemDescription),
s.SchemeName, cwd.ContractorsWorkDetailsItemDescription;
The reason you don't want to do this in SQL is because the result set no longer has a relational structure: the ordering of the rows is important.
In my table, I have data that looks like this:
CODE DATE PRICE
100 1/1/13 $500
100 2/1/13 $521
100 3/3/13 $530
100 5/9/13 $542
222 3/3/13 $20
350 1/1/13 $200
350 3/1/13 $225
Is it possible to create query to pull out the TWO most recent records by DATE? AND only if there are 2+ dates for a specific code. So the result would be:
CODE DATE PRICE
100 5/9/13 $542
100 3/3/13 $530
350 3/1/13 $225
350 1/1/13 $200
Bonus points if you can put both prices/dates on the same line, like this:
CODE OLD_DATE OLD_PRICE NEW_DATE NEW_PRICE
100 3/3/13 $530 5/9/13 $542
350 1/1/13 $200 3/1/13 $225
Thank you!!!
I managed to solve it with 5 sub-queries and 1 rollup query.
First we have a subquery that gives us the MAX date for each code.
Next, we do the same subquery, except we exclude our previous results.
We assume that your data is already rolled up and you won't have duplicate dates for the same code.
Next we bring in the appropriate Code / Price for the latest and 2nd latest date. If a code doesn't exist in the 2nd Max query - then we don't include it at all.
In the union query we're combining the results of both. In the Rollup Query, we're sorting and removing null values generated in the union.
Results:
CODE MaxOfOLDDATE MaxOfOLDPRICE MaxOfNEWDATE MaxOfNEWPRICE
100 2013-03-03 $530.00 2013-05-09 542
350 2013-01-01 $200.00 2013-03-01 225
Using your Data in a table called "Table", create the following queries:
SUB_2ndMaxDatesPerCode:
SELECT Table.CODE, Max(Table.Date) AS MaxOfDATE1
FROM SUB_MaxDatesPerCode RIGHT JOIN [Table] ON (SUB_MaxDatesPerCode.MaxOfDATE = Table.DATE) AND (SUB_MaxDatesPerCode.CODE = Table.CODE)
GROUP BY Table.CODE, SUB_MaxDatesPerCode.CODE
HAVING (((SUB_MaxDatesPerCode.CODE) Is Null));
SUB_MaxDatesPerCode:
SELECT Table.CODE, Max(Table.Date) AS MaxOfDATE
FROM [Table]
GROUP BY Table.CODE;
SUB_2ndMaxData:
SELECT Table.CODE, Table.Date, Table.PRICE
FROM [Table] INNER JOIN SUB_2ndMaxDatesPerCode ON (Table.DATE = SUB_2ndMaxDatesPerCode.MaxOfDATE1) AND (Table.CODE = SUB_2ndMaxDatesPerCode.Table.CODE);
SUB_MaxData:
SELECT Table.CODE, Table.Date, Table.PRICE
FROM ([Table] INNER JOIN SUB_MaxDatesPerCode ON (Table.DATE = SUB_MaxDatesPerCode.MaxOfDATE) AND (Table.CODE = SUB_MaxDatesPerCode.CODE)) INNER JOIN SUB_2ndMaxDatesPerCode ON Table.CODE = SUB_2ndMaxDatesPerCode.Table.CODE;
SUB_Data:
SELECT CODE, DATE AS OLDDATE, PRICE AS OLDPRICE, NULL AS NEWDATE, NULL AS NEWPRICE FROM SUB_2ndMaxData;
UNION ALL SELECT CODE, NULL AS OLDDATE, NULL AS OLDPRICE, DATE AS NEWDATE, PRICE AS NEWPRICE FROM SUB_MaxData;
Data (Rollup):
SELECT SUB_Data.CODE, Max(SUB_Data.OLDDATE) AS MaxOfOLDDATE, Max(SUB_Data.OLDPRICE) AS MaxOfOLDPRICE, Max(SUB_Data.NEWDATE) AS MaxOfNEWDATE, Max(SUB_Data.NEWPRICE) AS MaxOfNEWPRICE
FROM SUB_Data
GROUP BY SUB_Data.CODE
ORDER BY SUB_Data.CODE;
There you go - thanks for the challenge.
Accessing the recent data
To access the recent data, you use TOP 2. Such as you inverse the data from the table, then select the top 2. Just as you start ABC from ZYX and select the TOP 2 which would provide you with ZY.
SELECT TOP 2 * FROM table_name ORDER BY column_time DESC;
This way, you reverse the table, and then select the most recent two from the top.
Joining the Tables
To join the two columns and create a result from there quest you can use JOIN (INNER JOIN; I prefer this) such as:
SELECT TOP 2 * FROM table_name INNER JOIN table_name.column_name ON
table_name.column_name2
This way, you will join both the tables where a value in one column matches the value from the other column in both tables.
You can use a for loop for this to select the value for them, or you can use this inside the foreach loop to take out the values for them.
My suggestion
My best method would be to, first just select the data that was ordered using the date.
Then inside the foreach() loop where you will write the data for that select the remaining data for that time. And write it inside that loop.
Code (column_name) won't bother you
And when you will reference the query using ORDER By Time Desc you won't be using the CODE anymore such as WHERE Code = value. And you will get the code for the most recent ones. If you really need the code column, you can filter it out using and if else block.
Reference:
http://technet.microsoft.com/en-us/library/ms190014(v=sql.105).aspx (Inner join)
http://www.w3schools.com/sql/sql_func_first.asp (top; check the Sql Server query)
I have a query similar to the following:
SELECT CASE WHEN (GROUPING(Name) = 1) THEN 'All' ELSE Name END AS Name,
CASE WHEN (GROUPING(Type) = 1) THEN 'All' ELSE Type END AS Type,
sum(quantity) AS [Quantity],
CAST(sum(quantity) * (SELECT QuantityMultiplier FROM QuantityMultipliers WHERE a = t.b) AS DECIMAL(18,2)) AS Multiplied Quantity
FROM #Table t
GROUP BY Name, Type WITH ROLLUP
I'm trying to return a list of Names, Types, a summed Quantity and a summed quantity multiplied by an arbitrary number. All fine so far. I also need to return a sub-total row per Name and per Type, such as the following
Name Type Quantity Multiplied Quantity
------- --------- ----------- -------------------
a 1 2 4
a 2 3 3
a ALL 5 7
b 1 6 12
b 2 1 1
b ALL 7 13
ALL ALL 24 40
The first 3 columns are fine. I'm getting null values in the rollup rows for the multiplied quantity though. The only reason I can think this is happening is because SQL doesn't recognize the last column as an aggregate now that I've multiplied it by something.
Can I somehow work around this without things getting too convoluted?
I will be falling back onto temporary tables if this can't be done.
In your sub-query to acquire the multiplier, you have WHERE a=b. Are either a or b from the tables in your main query?
If these values are static (nothing to do with the main query), it looks like it should be fine...
If the a or b values are the name or type field, they can be NULL for the rollup records. If so, you can change to something similiar to...
CAST(sum(quantity * (<multiplie_query>)) AS DECIMAL(18,2)).
If a or b are other field from your main query, you'd be getting multiple records back, not just a single multiplier. You could change to something like...
CAST(sum(quantity) * (SELECT MAX(multiplier) FROM ...)) AS DECIMAL(18,2))
I need to show more than one result from each field in a table. I need to do this with only one SQL sentence, I donĀ“t want to use a Cursor.
This seems silly, but the number of rows may vary for each item. I need this to print afterwards this information as a Crystal Report detail.
Suppose I have this table:
idItem Cantidad <more fields>
-------- -----------
1000 3
2000 2
3000 5
4000 1
I need this result, using one only SQL Sentence:
1000
1000
1000
2000
2000
3000
3000
3000
3000
3000
4000
where each idItem has Cantidad rows.
Any ideas?
It seems like something that should be handled in the UI (or the report). I don't know Crystal Reports well enough to make a suggestion there. If you really, truly need to do it in SQL, then you can use a Numbers table (or something similar):
SELECT
idItem
FROM
Some_Table ST
INNER JOIN Numbers N ON
N.number > 0 AND
N.number <= ST.cantidad
You can replace the Numbers table with a subquery or function or whatever other method you want to generate a result set of numbers that is at least large enough to cover your largest cantidad.
Check out UNPIVOT (MSDN)
Another example
If you use a "numbers" table that is useful for this and many similar purposes, you can use the following SQL:
select t.idItem
from myTable t
join numbers n on n.num between 1 and t.Cantidad
order by t.idTtem
The numbers table should just contain all integer numbers from 0 or 1 up to a number big enough so that Cantidad never exceeds it.
As others have said, you need a Numbers or Tally table which is just a sequential list of integers. However, if you knew that Cantidad was never going to be larger than five for example, you can do something like:
Select idItem
From Table
Join (
Select 1 As Value
Union All Select 2
Union All Select 3
Union All Select 4
Union All Select 5
) As Numbers
On Numbers.Value <= Table.Cantidad
If you are using SQL Server 2005, you can use a CTE to do:
With Numbers As
(
Select 1 As Value
Union All
Select N.Value + 1
From Numbers As N
)
Select idItem
From Table
Join Numbers As N
On N.Value <= Table.Cantidad
Option (MaxRecursion 0);