Right Outer Join Issue in a CTE - sql-server

Ultimately I'd like my end result to look like the following:
ReportingDate FundCode FundName AssetClass Rank Percentage
-------------------------------------------------------------------------
30/11/2012 1 Fund1 Bond 1 50
30/11/2012 1 Fund1 Equity 2 30
30/11/2012 1 Fund1 Balanced 3 0
30/11/2012 1 Fund1 Other 4 20
30/11/2012 2 Fund2 Equity 1 60
30/11/2012 2 Fund2 Bond 2 20
.......
Basically if there is no data for say Balanced like in the above example I would still like this to be returned in the data but with a percentage of 0.
To get this I created a table called #AssetClass and RIGHT OUTER JOIN this to my work table so that I could get all the AssetClass's returned even without data.
My script looks like this:
;;WITH CTE AS
(
SELECT
CASE
WHEN ReportingDate IS NULL THEN MAX(ReportingDate) OVER (PARTITION BY (SELECT 1))
ELSE ReportingDate
END AS ReportingDate
, CASE
WHEN PortfolioID IS NULL THEN MAX(PortfolioID) OVER (PARTITION BY (SELECT 1))
ELSE PortfolioID
END AS PortfolioID
, CASE
WHEN PortfolioNme IS NULL THEN MAX(PortfolioNme) OVER (PARTITION BY (SELECT 1))
ELSE PortfolioNme
END AS PortfolioNme
, AC.AssetClass AS AssetClass
, CASE
WHEN AC.AssetClass = 'No Asset Class' THEN 3
WHEN AC.AssetClass = 'Other' THEN 2
ELSE 1
END AS [Rank]
, CAST(SUM(ISNULL(Percentage, 0)) AS DECIMAL(22,1)) AS [Weight]
FROM #Worktable as WT
RIGHT OUTER JOIN #AssetClass AS AC
ON RTRIM(WT.AssetClass) = RTRIM(AC.AssetClass)
GROUP BY WT.ReportingDate, WT.PortfolioID, WT.PortfolioNme, AC.AssetClass
)
SELECT
CONVERT(VARCHAR, ReportingDate, 103) AS ReportingDate
, PortfolioID AS FundCode
, PortfolioNme AS FundName
, AssetClass
, RANK() OVER ( PARTITION BY PortfolioID
ORDER BY [Rank], [Weight] DESC) AS [Rank]
, [Weight] AS Percentage
FROM CTE
ORDER BY ReportingDate, PortfolioID, [Rank], [Weight] DESC
My problem is, when I run this for one portfolio this works perfectly. When I run this for multiple portfolio's it seems to exclude in the final select anything where there is no data, so in the above example the Balanced row is not returned.
Is there an issue with my script or how I've right outer joined to #AssetClass? Is there something I'm missing or something I can improve upon in my script?

Possible this can help you
UPDATE 03.01.2013
;WITH CTE AS
(
SELECT DISTINCT WT.ReportingDate, WT.PortfolioID, WT.PortfolioNme,
AC.AssetClass,
CASE WHEN AC.AssetClass = 'No Asset Class' THEN 3
WHEN AC.AssetClass = 'Other' THEN 2
ELSE 1 END AS [Rank],
SUM(CASE WHEN AC.PortfolioID IS NULL THEN 0.00 ELSE WT.Percentage END)
OVER(PARTITION BY WT.ReportingDate, WT.PortfolioID, AC.AssetClass) AS [Weight]
FROM Worktable WT CROSS APPLY (
SELECT AC2.AssetClass, WT2.ReportingDate, WT2.PortfolioID,
WT2.AssetClass AS AssetClass2
FROM AssetClass AC2 LEFT JOIN Worktable WT2
ON RTRIM(AC2.AssetClass) = RTRIM(WT2.AssetClass)
AND WT2.PortfolioID = WT.PortfolioID
) AC
WHERE (WT.ReportingDate = AC.ReportingDate AND WT.PortfolioID = AC.PortfolioID AND WT.AssetClass = AC.AssetClass)
OR (AC.AssetClass2 IS NULL)
GROUP BY WT.ReportingDate, WT.PortfolioID, WT.PortfolioNme,
AC.AssetClass, AC.PortfolioID, WT.Percentage
)
SELECT CONVERT(VARCHAR, ReportingDate, 103) AS ReportingDate,
PortfolioID AS FundCode,
PortfolioNme AS FundName,
AssetClass,
RANK() OVER (PARTITION BY PortfolioID
ORDER BY [Rank], [Weight] DESC) AS [Rank],
[Weight] AS Percentage
FROM CTE
ORDER BY ReportingDate, PortfolioID, [Rank], [Weight] DESC

Related

Average day gap in between a repeat order for each product

Can someone please help me to find the average time between first and second purchase on a product level.
This is what I have written -
Select A.CustomerId,A.ProductId , A.OrderSequence, (Case WHEN OrderSequence = 1 THEN OrderDate END) AS First_Order_Date,
MAX(Case WHEN OrderSequence = 2 THEN OrderDate END) AS Second_Order_Date
From
(
Select t.CustomerId, t.ProductId, t.OrderDate,
Dense_RANK() OVER (PARTITION BY t.CustomerId, t.ProductId ORDER BY OrderDate Asc) as OrderSequence
From Transactions t (NOLOCK)
Where t.SiteKey = 01
Group by t.CustomerId, t.ProductId, t.OrderDate)
A
Where A.OrderSequence IN (1,2)
Group By A.Customer_Id, A.ProductId, A.OrderSequence, A.OrderDate
Sample Data:
It looks like row-numbering and LEAD should do the trick for you here.
Don't use NOLOCK unless you really know what you're doing
It's unclear if you want the results to be partitioned by CustomerId also. If not, you can remove it everywhere in the query
SELECT
A.CustomerId,
A.ProductId,
AVG(DATEDIFF(day, OrderDate, NextOrderDate))
FROM
(
SELECT
t.CustomerId,
t.ProductId,
t.OrderDate,
ROW_NUMBER() OVER (PARTITION BY t.CustomerId, t.ProductId ORDER BY OrderDate) AS rn,
LEAD(OrderDate) OVER (PARTITION BY t.CustomerId, t.ProductId ORDER BY OrderDate) AS NextOrderDate
FROM Transactions t
WHERE t.SiteKey = '01'
) t
WHERE t.rn = 1
GROUP BY
t.Customer_Id,
t.ProductId;

SQL Server: when Else is case it is showing 0 as default in CASE clause

This query is run on AdventureWorks2014.Sales.SalesOrderDetail table.
My query: in the CASE clause I have clearly stated to show - when it is the ELSE case. This query removes the repeating SalesOrderID and after 1st time shown it should be replaced with "-" this character.
SELECT
CASE WHEN ROW_NUMBER() OVER(PARTITION BY SalesOrderID ORDER BY SalesOrderID ASC) = 1 THEN SalesOrderID ELSE '-' END AS SalesOrderID,
R.ProductID,
R.OrderQty,
R.UnitPrice,
R.LineTotal
FROM AdventureWorks2014.Sales.SalesOrderDetail AS R
currently showing 0 even though i said in the ELSE to show me -
Below query will solve your issue
SELECT
CASE WHEN ROW_NUMBER() OVER(PARTITION BY SalesOrderID ORDER BY SalesOrderID ASC) = 1 THEN CONVERT(varchar(10),SalesOrderID) ELSE '-' END AS SalesOrderID,
R.ProductID,
R.OrderQty,
R.UnitPrice,
R.LineTotal
FROM AdventureWorks2014.Sales.SalesOrderDetail AS R
Thanks for your input i made it work
SELECT CASE
WHEN ROW_NUMBER() OVER(PARTITION BY R.SalesOrderID ORDER BY SalesOrderID ASC) = 1 THEN SalesOrderID
ELSE ' ' END AS SO_ID,
R.ProductID,
R.OrderQty,
R.UnitPrice,
R.LineTotal
FROM(SELECT
CONVERT(VARCHAR(15),S.SalesOrderID) AS SalesOrderID,
S.ProductID,
S.OrderQty,
S.UnitPrice,
S.LineTotal
FROM AdventureWorks2014.Sales.SalesOrderDetail AS S) AS R
Sample data
I think your are getting 0 because data type of SalesOrderId is int. If it will be varchar then it will come -. Try this below query.
Create table Orders (SalesOrderId Varchar(5), ProductId int, OrderQty int, UnitPrice decimal,
LineTotal decimal)
insert into Orders
values
(43659, 776, 1, 2024.94, 2024.99),
(43659, 777, 3, 2024.94, 2024.99),
(43659, 778, 1, 2024.94, 2024.99)
SELECT
CASE WHEN ROW_NUMBER() OVER(PARTITION BY SalesOrderID ORDER BY SalesOrderID ASC) = '1'
THEN SalesOrderID
ELSE '-' END AS SalesOrderID,
R.ProductID,
R.OrderQty,
R.UnitPrice,
R.LineTotal
FROM Orders AS R
You can find the live demo Live Demo Here in both format of datatype int and varchar.
Without changing datatype you can achieve it using query as shown below
SELECT
CASE WHEN ROW_NUMBER() OVER(PARTITION BY SalesOrderID ORDER BY SalesOrderID ASC) = '1'
THEN Convert(Varchar(5), SalesOrderID)
ELSE '-' END AS SalesOrderID,
R.ProductID,
R.OrderQty,
R.UnitPrice,
R.LineTotal
FROM OrdersInt AS R

Is there a way to pivot a customer ID and a their most recent order dates?

I have a query that gives me all customer's and their last three order dates.
EX:
CustomerId DateOrdered
167 2006-09-16 01:25:38.060
167 2006-09-21 13:11:53.530
171 2006-08-31 15:19:22.543
171 2006-09-01 13:30:54.013
171 2006-09-01 13:34:36.483
178 2006-09-04 11:36:19.983
186 2006-09-05 12:50:27.153
186 2006-09-05 12:51:08.513
I want to know if there is a way for me to pivot it to display like this:
[CustomerId] [Most Recent] [Middle] [Oldest]
'167' '2006-09-21 13:11:53.530' '2006-09-16 01:25:38.060' 'NULL'
'171' '2006-09-01 13:34:36.483' '2006-09-01 13:30:54.013' '2006-08-31 15:19:22.543'
'178' '2006-09-04 11:36:19.983' NULL NULL
'186' '2006-09-05 12:51:08.513' '2006-09-05 12:50:27.153' NULL
;WITH YourQuery As
(
SELECT CustomerId, DateOrdered,
ROW_NUMBER() OVER (PARTITION BY CustomerId ORDER BY DateOrdered DESC) AS RN
FROM Orders
)
select [CustomerId],
MAX(CASE WHEN RN=1 THEN DateOrdered END) AS [Most Recent] ,
MAX(CASE WHEN RN=2 THEN DateOrdered END) AS [Middle] ,
MAX(CASE WHEN RN=3 THEN DateOrdered END) AS [Oldest]
FROM YourQuery
WHERE RN<=3
GROUP BY [CustomerId]
Warning: Not Tested
I think you're looking for something like this. The specific joins and where clause may need some work, but basically your just joining the table back on itself to get one date each time.
SELECT C.CUSTOMERID, C.DATEORDERED, C2.DATEORDERED, C3.DATEORDERED
FROM CUSTOMER C
INNER JOIN CUSTOMER C2 ON C.CUSTOMERID = C2.CUSTOMERID
INNER JOIN CUSTOMER C3 ON C.CUSTOMERID = C3.CUSTOMERID
WHERE C.DATEORDERED = MAX(C.DATEORDERED)
AND C2.DATEORDERED < C.DATEORDERED AND
(C3.DATEORDERED IS NULL OR C2.DATEORDERED > C3.DATEORDERED)

PIVOT on Common Table Expression

I have a CTE as follows
WITH details
AS ( SELECT FldId
,Rev
,Words
,row_number() OVER ( PARTITION BY FldId ORDER BY Rev DESC ) AS rn
FROM WorkItemLongTexts
WHERE ID = 2855
)
SELECT f.ReferenceName
,d.FldId
,Rev
,Words
FROM details AS d
INNER JOIN Fields AS f ON f.FldId = d.FldId
WHERE d.rn = 1 ;
The above returns the following output
ReferenceName | FldId | Rev | Words
Description 52 2 Description here
Objectives 10257 2 Objectives here
Specification 10258 6 Specification here
Requirements 10259 6 Requirements here
I want to apply PIVOT (or whatever is the best option) so that i can get output as follows
Description | Objectives | Specification | Requirements
Description here Objectives here Specification here Requirements here
Pls. suggest.
Thanks
You do this:
SELECT
FldId,
[Description],
[Objectives],
[Specification],
[Requirements]
FROM (
SELECT
ReferenceName,
FldId,
REV,
Words
FROM CTE
WHERE RowNumber = 1
) t
PIVOT (
MIN(Words)
FOR ReferenceName IN ([Description], [Objectives], [Specification], [Requirements])
) PIV
Or you can add it to your CTE, like this:
;WITH CTE2 AS (
SELECT
FldId,
REV,
[Description],
[Objectives],
[Specification],
[Requirements],
ROW_NUMBER() OVER (PARTITION BY FldId ORDER BY REV DESC) AS RowNumber
FROM TBL
PIVOT (
MIN(Words)
FOR ReferenceName IN ([Description], [Objectives], [Specification], [Requirements])
) PIV
)
SELECT
FldId,
REV,
[Description],
[Objectives],
[Specification],
[Requirements]
FROM CTE2
WHERE RowNumber = 1
WITH details
AS ( SELECT FldId
,Rev
,Words
,row_number() OVER ( PARTITION BY FldId ORDER BY Rev DESC ) AS rn
FROM WorkItemLongTexts
WHERE ID = 2855
),
cte_1
AS ( SELECT f.ReferenceName
,d.FldId
,Rev
,Words
FROM details AS d
INNER JOIN Fields AS f ON f.FldId = d.FldId
WHERE d.rn = 1
)
SELECT max(case [ReferenceName] WHEN 'Descripton' THEN [Words] ELSE NULL END) AS [Descripton]
,max(case [ReferenceName] WHEN 'Objectives' THEN [Words] ELSE NULL END) AS [Objectives]
,max(case [ReferenceName] WHEN 'Specification' THEN [Words] ELSE NULL END) AS [Specification]
,max(case [ReferenceName] WHEN 'Requirements' THEN [Words] ELSE NULL END) AS [Requirements]
FROM cte_1 ;
OR:
-- cte here as above
SELECT Description
,Objectives
,Specification
,Requirements
FROM cte_1 PIVOT ( max(Words) FOR ReferenceName IN ( Description,
Objectives,
Specification,
Requirements ) ) AS PivotTable
Do something like:
with details as (...)
, unpivotted as (select f.ReferenceName, Words
from details as d
inner join Fields as f
on f.FldId=d.FldId
where d.rn =1)
Select *
from unpivotted
pivot
(max(Words) for Description in ([Objectives],[Specification],[Requirements]) p
;

How to query Open-high-low-close (OHLC) data from SQL Server

I'm trying to retrieve data for a Open-high-low-close (OHLC) chart directly from the database, it's the kind of chart you see of stocks. Is this possible, and if, how?
I have a table like this (simplified):
Date | Price | PriceType
A record is created for each day, I will report per month / year, not per day as used for stocks.
I would like to query something like this:
SELECT PriceType, MAX(Price) as High, MIN(Price) as Low, [Price of first item of month] as Open, [Price of last item of month] as Close GROUP BY PriceType, Year(Date), Month(Date)
To access the SQL Server I use LLBLGen, so an anwser based on that technology would be great, a generic SQL server will do too!
It's SQL 2005, but 2008 is also an option.
Thanks.
This appears to work. There may well be a less verbose way to do it.
--create test data
CREATE TABLE #t
(priceDate DATETIME
,price MONEY
,priceType CHAR(1)
)
INSERT #t
SELECT '20090101',100,'A'
UNION SELECT '20090102',500,'A'
UNION SELECT '20090103',20 ,'A'
UNION SELECT '20090104',25 ,'A'
UNION SELECT '20090105',28 ,'A'
UNION SELECT '20090131',150,'A'
UNION SELECT '20090201',501,'A'
UNION SELECT '20090203',21 ,'A'
UNION SELECT '20090204',26 ,'A'
UNION SELECT '20090205',29 ,'A'
UNION SELECT '20090228',151,'A'
UNION SELECT '20090101',100,'B'
UNION SELECT '20090102',500,'B'
UNION SELECT '20090103',20 ,'B'
UNION SELECT '20090104',25 ,'B'
UNION SELECT '20090105',28 ,'B'
UNION SELECT '20090131',150,'B'
UNION SELECT '20090201',501,'B'
UNION SELECT '20090203',21 ,'B'
UNION SELECT '20090204',26 ,'B'
UNION SELECT '20090205',29 ,'B'
UNION SELECT '20090228',151,'B'
--query
;WITH rangeCTE
AS
(
SELECT MIN(priceDate) minDate
,MAX(priceDate) maxDate
FROM #t
)
,datelistCTE
AS
(
SELECT CAST(CONVERT(CHAR(6),minDate,112) + '01' AS DATETIME) AS monthStart
,DATEADD(mm,1,CAST(CONVERT(CHAR(6),minDate,112) + '01' AS DATETIME)) -1 AS monthEnd
,1 AS monthID
FROM rangeCTE
UNION ALL
SELECT DATEADD(mm,1,monthStart)
,DATEADD(mm,2,monthStart) - 1
,monthID + 1
FROM datelistCTE
WHERE monthStart <= (SELECT maxDate FROM rangeCTE)
)
,priceOrderCTE
AS
(
SELECT *
,ROW_NUMBER() OVER (PARTITION BY monthID, priceType
ORDER BY priceDate
) AS rn1
,ROW_NUMBER() OVER (PARTITION BY monthID, priceType
ORDER BY priceDate DESC
) AS rn2
,ROW_NUMBER() OVER (PARTITION BY monthID, priceType
ORDER BY price DESC
) AS rn3
,ROW_NUMBER() OVER (PARTITION BY monthID, priceType
ORDER BY price
) AS rn4
FROM datelistCTE AS d
JOIN #t AS t
ON t.priceDate BETWEEN d.monthStart AND d.monthEnd
WHERE monthStart <= (SELECT maxDate FROM rangeCTE)
)
SELECT o.MonthStart
,o.priceType
,o.Price AS opening
,c.price AS closing
,h.price AS high
,l.price AS low
FROM priceOrderCTE AS o
JOIN priceOrderCTE AS c
ON c.priceType = o.PriceType
AND c.monthID = o.MonthID
JOIN priceOrderCTE AS h
ON h.priceType = o.PriceType
AND h.monthID = o.MonthID
JOIN priceOrderCTE AS l
ON l.priceType = o.PriceType
AND l.monthID = o.MonthID
WHERE o.rn1 = 1
AND c.rn2 = 1
AND h.rn3 = 1
AND l.rn4 = 1
This is a little query I wrote that seems to work nicely for one time span at a time. All you need to do is comment the select DATEPARTS in order to get to the timespan you are looking for. Or you could just make multiple views for different timespans. Also the underlying data table uses Bid Ask tick style data. If you are using mids or last prices you could eliminate the case statements from the selects.
Select
tmp.num,
rf.CurveName,
rf.Period as Period,
CASE WHEN (tmp2.Bid is null or tmp2.Ask is null) then isnull(tmp2.Bid,0)+isnull(tmp2.Ask,0) else (tmp2.Bid+tmp2.Ask)/2 end as [Open],
tmp.Hi,
tmp.Lo,
CASE WHEN (rf.Bid is null or Rf.Ask is null) then isnull(rf.Bid,0)+isnull(rf.Ask,0) else (rf.Bid+rf.Ask)/2 end as [Close],
tmp.OpenDate,
tmp.CloseDate,
tmp.yr,
tmp.mth,
tmp.wk,
tmp.dy,
tmp.hr
from BidAsk rf inner join
(SELECT count(CurveName)as num,CurveName,
Period,
max(CASE WHEN (Bid is null or Ask is null) then isnull(Bid,0)+isnull(Ask,0) else (Bid+Ask)/2 end) as Hi,
min(CASE WHEN (Bid is null or Ask is null) then isnull(Bid,0)+isnull(Ask,0) else (Bid+Ask)/2 end) as Lo,
max(CurveDateTime) as CloseDate, min(CurveDateTime) as OpenDate,
DATEPART(year, CurveDateTime) As yr,
DATEPART(month, CurveDateTime) As mth,
DATEPART(week, CurveDateTime) As wk,
DATEPART(Day, CurveDateTime) as dy,
DATEPART(Hour, CurveDateTime) as hr
--DATEPART(minute, CurveDateTime) as mnt
FROM
BidAsk
GROUP BY
CurveName,Period,
DATEPART(year, CurveDateTime),
DATEPART(month, CurveDateTime),
DATEPART(week, CurveDateTime),
DATEPART(Day, CurveDateTime) ,
DATEPART(Hour, CurveDateTime)
--DATEPART(minute, CurveDateTime)
) tmp on
tmp.CurveName=rf.CurveName and
tmp.CloseDate=rf.CurveDateTime and
tmp.Period=rf.Period
inner join BidAsk tmp2 on
tmp2.CurveName=rf.CurveName and
tmp2.CurveDateTime=tmp.Opendate and
tmp2.Period=rf.Period
ORDER BY
CurveName,Period,tmp.yr,tmp.mth
--DATEPART(year, CurveDateTime),
--DATEPART(month, CurveDateTime)
--DATEPART(day, CurveDateTime),
--DATEPART(Hour, CurveDateTime),
--DATEPART(minute, CurveDateTime) )

Resources