SQL Server CE 4.0选择连续数字字段
我回来尝试让这个工作(几年后)。它适用于SQL Server CE 4.0 下面是一些任何人都可以复制并运行的测试脚本SQL Server CE 4.0选择连续数字字段,sql,tsql,sql-server-ce,Sql,Tsql,Sql Server Ce,我回来尝试让这个工作(几年后)。它适用于SQL Server CE 4.0 下面是一些任何人都可以复制并运行的测试脚本 @关键字表将是用户输入的内容 这些将是歌曲ID的。每个节目都有歌曲ID和SongOrder栏 SongOrder必须是连续的 要测试这一点,请更改@Keywords的顺序,ShowId将保留,并且不应保留 必须找到一个特定顺序的特定歌曲列表的节目 因此,用户正在寻找具有特定歌曲顺序的节目 在测试脚本中创建的每个表参数都会镜像实际数据库 SELECT应该只返回1546,但它不是很
@关键字表将是用户输入的内容
这些将是歌曲ID的。每个节目都有歌曲ID和SongOrder栏
SongOrder
必须是连续的
要测试这一点,请更改@Keywords
的顺序,ShowId
将保留,并且不应保留
必须找到一个特定顺序的特定歌曲列表的节目
因此,用户正在寻找具有特定歌曲顺序的节目
在测试脚本中创建的每个表参数都会镜像实际数据库
SELECT
应该只返回1546,但它不是很好
--Test two shows, years apart
DECLARE @Shows TABLE (ID INT)
INSERT INTO @Shows (ID) VALUES (1)
INSERT INTO @Shows (ID) VALUES (1546)
--Add a few songs
DECLARE @Songs TABLE (ID INT, Name VARCHAR(256))
INSERT INTO @Songs (ID, Name) VALUES (1, 'Song 1')
INSERT INTO @Songs (ID, Name) VALUES (2, 'Song 2')
INSERT INTO @Songs (ID, Name) VALUES (3, 'Song 3')
INSERT INTO @Songs (ID, Name) VALUES (654, 'Song 4')
INSERT INTO @Songs (ID, Name) VALUES (321, 'Song 5')
INSERT INTO @Songs (ID, Name) VALUES (322, 'Song 6')
--Where the shows and songs get their reference
DECLARE @ShowSongRef TABLE (ID INT, ShowID INT, SongID INT, SongOrder INT, PRIMARY KEY (ID))
--Show 1
INSERT INTO @ShowSongRef (ID, ShowID, SongID, SongOrder) VALUES (1, 1, 1, 1)
INSERT INTO @ShowSongRef (ID, ShowID, SongID, SongOrder) VALUES (2, 1, 2, 2)
INSERT INTO @ShowSongRef (ID, ShowID, SongID, SongOrder) VALUES (3, 1, 321, 3)
INSERT INTO @ShowSongRef (ID, ShowID, SongID, SongOrder) VALUES (4, 1, 3, 4)
--Show 1546
INSERT INTO @ShowSongRef (ID, ShowID, SongID, SongOrder) VALUES (5, 1546, 3, 1)
INSERT INTO @ShowSongRef (ID, ShowID, SongID, SongOrder) VALUES (6, 1546, 1, 2)
INSERT INTO @ShowSongRef (ID, ShowID, SongID, SongOrder) VALUES (7, 1546, 654, 3)
INSERT INTO @ShowSongRef (ID, ShowID, SongID, SongOrder) VALUES (8, 1546, 321, 4)
INSERT INTO @ShowSongRef (ID, ShowID, SongID, SongOrder) VALUES (9, 1546, 322, 5)
--The song order we are looking for
--Doesn't matter where in the show the order occurs
DECLARE @Keyword TABLE (ID INT)
INSERT INTO @Keyword (ID) VALUES ('654')
INSERT INTO @Keyword (ID) VALUES ('321')
INSERT INTO @Keyword (ID) VALUES ('322')
--This should only return 1546
SELECT DISTINCT sh.ID AS ShowID--, s.Name
FROM @Songs s
INNER JOIN @ShowSongRef ref ON ref.SongID = s.ID
--Comment out this join or remove the +1 and it works but in any order
INNER JOIN @ShowSongRef ref2 ON ref2.SongID = s.ID
AND ref2.SongOrder = ref.SongOrder + 1 --remove the +1 and it works but are they in order? Not always
AND ref2.ShowID = ref.ShowID
INNER JOIN @Shows sh ON sh.ID = ref.ShowID
WHERE ref.SongID IN (SELECT ID FROM @Keyword)
GROUP BY sh.ID
HAVING COUNT(sh.ID) = (SELECT COUNT(ID) FROM @Keyword)
--ORDER BY ref2.SongOrder
--Couldn't get this to work either
--SELECT
-- ShowID
--FROM
-- @ShowSongRef m
--CROSS JOIN
-- (SELECT COUNT(ID) AS consec FROM @Keyword) x
--WHERE
-- EXISTS
-- (SELECT 1 FROM @ShowSongRef ref
-- INNER JOIN @Shows sh ON sh.ID = ref.ShowID
-- WHERE ref.SongID IN (SELECT ID FROM @Keyword)
-- AND ref.SongOrder = m.SongOrder - x.consec + 1
-- )
也许:
SELECT TOP (1) WITH TIES
showsong.ShowID, Showsong.SongId, showsong.SongOrder, song.*
FROM @Shows AS show
JOIN @ShowSongRef AS showsong ON show.ID = showsong.ShowID
JOIN @Songs AS song ON showsong.SongID = song.ID
JOIN @Keyword AS kw ON song.ID = kw.ID
ORDER BY -COUNT(*) OVER (PARTITION BY showsong.ShowID);
或者,如果您不能在…
上使用窗口聚合函数(例如,COUNT(*),则:
WITH correctId AS
(
SELECT TOP (1) ShowID
FROM
(
SELECT showsong.ShowID, ttl = COUNT(*)
FROM @Shows AS show
JOIN @ShowSongRef AS showsong ON show.ID = showsong.ShowID
JOIN @Songs AS song ON showsong.SongID = song.ID
JOIN @Keyword AS kw ON song.ID = kw.ID
GROUP BY showsong.ShowID
) AS x
ORDER BY -ttl
)
SELECT
showsong.ShowID, Showsong.SongId, showsong.SongOrder, song.*
FROM @Shows AS show
JOIN @ShowSongRef AS showsong ON show.ID = showsong.ShowID
JOIN @Songs AS song ON showsong.SongID = song.ID
JOIN @Keyword AS kw ON song.ID = kw.ID
CROSS JOIN correctId
WHERE showsong.ShowID = (SELECT showId FROM correctId);
原件:
也许是这样的:
SELECT TOP (1) WITH TIES *,
-- if this number does not chnage then the song order is consecutive
Songordercheck = showsong.songorder -
ROW_NUMBER() OVER (PARTITION BY show.ID ORDER BY showsong.songorder)
FROM @Shows AS show
JOIN @ShowSongRef AS showsong ON show.ID = showsong.ShowID
JOIN @Songs AS song ON showsong.SongID = song.ID
JOIN @Keyword AS kw ON song.ID = kw.ID
ORDER BY -COUNT(*) OVER (PARTITION BY show.ID)
如果我没有使用SQLCe,那么我就不会有这个问题。SQLCe不支持ROW_NUMBER(),我只是编辑了我的答案…如果有帮助,请告诉我