我有以下专栏:
╔════════════════════════════╗
║ 'Symbol' ║
╠════════════════════════════╣
║ RELIANCE14JULFUT ║
║ RELIANCE14JUL900CE ║
║ RELIANCE14JUL1040CE ║
║ RELIANCE14JUL980CE ║
║ RELIANCE14JUL1020PE ║
║ RELIANCE14JUL1040PE ║
║ RELIANCE14JUL980PE ║
║ RELIANCE14JUL1140PE ║
║ NIFTY14JULFUT ║
╚════════════════════════════╝
我的代码是:
DECLARE @Symbol NVARCHAR (50)
DECLARE @SpFrom NVARCHAR (50)
DECLARE @SpTo NVARCHAR (50)
SET @Symbol = 'RELIANCE14JUL'
SET @SpFrom = 980
SET @SpTo = 1100
SELECT Replace(symbol, @Symbol, '')AS St
FROM symbol
WHERE ( symbol LIKE @Symbol + '%'
AND Replace(symbol, @Symbol, '') = 'FUT' )
OR ( symbol LIKE @Symbol + '%'
AND Cast(LEFT(Replace(symbol, @Symbol, ''),
Len(Replace(symbol, @Symbol, '')) - 2)
AS INT) BETWEEN @SpFrom AND @SpTo )
GROUP BY symbol
ORDER BY CASE
WHEN symbol LIKE '%FUT' THEN 1
WHEN symbol LIKE '%CE' THEN 2
WHEN symbol LIKE '%PE' THEN 3
ELSE 4
END,
symbol
我正在
╔═══════════╗
║ ST ║
╠═══════════╣
║ FUT ║
║ 1020CE ║
║ 1040CE ║
║ 980CE ║
║ 1020PE ║
║ 1040PE ║
║ 980PE ║
╚═══════════╝
没有正确订购,我需要
╔═════════════╗
║ ST ║
╠═════════════╣
║ FUT ║
║ 980CE ║
║ 1020CE ║
║ 1040CE ║
║ 980PE ║
║ 1020PE ║
║ 1040PE ║
╚═════════════╝
上的示例
答案 0 :(得分:1)
在玩了一下这个例子之后,我创建了以下解决方案:
DECLARE @Date DATETIME
DECLARE @Symbol NVARCHAR (50)
DECLARE @SpFrom NVARCHAR (50)
DECLARE @SpTo NVARCHAR (50)
SET @Date = '07/23/2014'
SET @Symbol = 'RELIANCE14JUL'
SET @SpFrom = 980 -------- use 1000
SET @SpTo = 1100
SELECT Replace(symbol, @Symbol, '')AS St
FROM opt
WHERE ( symbol LIKE @Symbol + '%'
AND Replace(symbol, @Symbol, '') = 'FUT' )
OR ( symbol LIKE @Symbol + '%'
AND Cast(LEFT(Replace(symbol, @Symbol, ''),
Len(Replace(symbol, @Symbol, '')) - 2)
AS INT) BETWEEN @SpFrom AND @SpTo )
GROUP BY symbol
ORDER BY CASE
WHEN symbol LIKE '%FUT' THEN 1
WHEN symbol LIKE '%CE' THEN 2
WHEN symbol LIKE '%PE' THEN 3
ELSE 4
END,
Cast(CASE
WHEN symbol LIKE '%FUT' THEN 0
ELSE LEFT(RIGHT(symbol, Len(symbol) - 13),
Len(RIGHT(symbol, Len(symbol) - 13)) - 2)
END AS INT)
看一下 SQL fiddle 示例