Skip to main content
Commonmark migration
Source Link

Formatting

#Formatting# YouYou use a mixture of lowercase and UPPERCASE keywords, you should stick to one or the other. It makes it much easier to read the query if you do. I am going to stick with UPPERCASE as it is the convention.

An alternate approach

#An alternate approach# MightMight I suggest storing the minimum SortSequence value in a variable and then using that to filter out records you don't need.

Here is what I came up with.

DECLARE @Minimum_Sort_Seq INT = 
(
    SELECT MIN(SortSequence)
    FROM WebcastChannelWebcastEventLink
)

SELECT wcwel.WebcastChannelId,wcwel.WebcastEventId
FROM WebcastChannelWebcastEventLink wcwel
WHERE wcwel.SortSequence = @Minimum_Sort_Seq

This query returned the same results in my testing. You can run this to check yourself:

CREATE TABLE #WebcastChannelWebcastEventLink
(
    WebcastChannelId INT,
    WebcastEventId INT,
    SortSequence INT
) 

INSERT INTO #WebcastChannelWebcastEventLink
SELECT 1,2,1 UNION ALL
SELECT 1,5,2 UNION ALL
SELECT 1,3,3 UNION ALL
SELECT 2,7,2 UNION ALL
SELECT 2,8,1

SELECT wcwel.WebcastChannelId, wcwel.WebcastEventId
FROM #WebcastChannelWebcastEventLink wcwel WITH (NOLOCK)
INNER JOIN #WebcastChannelWebcastEventLink wcwel1 WITH (NOLOCK) 
    ON wcwel1.WebcastChannelId = wcwel.WebcastChannelId
GROUP BY
wcwel.WebcastChannelId,
wcwel.WebcastEventId,
wcwel.SortSequence
HAVING
wcwel.SortSequence = MIN(wcwel1.SortSequence)

DECLARE @Minimum_Sort_Seq INT = 
(
    SELECT MIN(SortSequence)
    FROM WebcastChannelWebcastEventLink
)

SELECT wcwel.WebcastChannelId,wcwel.WebcastEventId
FROM WebcastChannelWebcastEventLink wcwel
WHERE wcwel.SortSequence = @Minimum_Sort_Seq

DROP TABLE #WebcastChannelWebcastEventLink

#Formatting# You use a mixture of lowercase and UPPERCASE keywords, you should stick to one or the other. It makes it much easier to read the query if you do. I am going to stick with UPPERCASE as it is the convention.

#An alternate approach# Might I suggest storing the minimum SortSequence value in a variable and then using that to filter out records you don't need.

Here is what I came up with.

DECLARE @Minimum_Sort_Seq INT = 
(
    SELECT MIN(SortSequence)
    FROM WebcastChannelWebcastEventLink
)

SELECT wcwel.WebcastChannelId,wcwel.WebcastEventId
FROM WebcastChannelWebcastEventLink wcwel
WHERE wcwel.SortSequence = @Minimum_Sort_Seq

This query returned the same results in my testing. You can run this to check yourself:

CREATE TABLE #WebcastChannelWebcastEventLink
(
    WebcastChannelId INT,
    WebcastEventId INT,
    SortSequence INT
) 

INSERT INTO #WebcastChannelWebcastEventLink
SELECT 1,2,1 UNION ALL
SELECT 1,5,2 UNION ALL
SELECT 1,3,3 UNION ALL
SELECT 2,7,2 UNION ALL
SELECT 2,8,1

SELECT wcwel.WebcastChannelId, wcwel.WebcastEventId
FROM #WebcastChannelWebcastEventLink wcwel WITH (NOLOCK)
INNER JOIN #WebcastChannelWebcastEventLink wcwel1 WITH (NOLOCK) 
    ON wcwel1.WebcastChannelId = wcwel.WebcastChannelId
GROUP BY
wcwel.WebcastChannelId,
wcwel.WebcastEventId,
wcwel.SortSequence
HAVING
wcwel.SortSequence = MIN(wcwel1.SortSequence)

DECLARE @Minimum_Sort_Seq INT = 
(
    SELECT MIN(SortSequence)
    FROM WebcastChannelWebcastEventLink
)

SELECT wcwel.WebcastChannelId,wcwel.WebcastEventId
FROM WebcastChannelWebcastEventLink wcwel
WHERE wcwel.SortSequence = @Minimum_Sort_Seq

DROP TABLE #WebcastChannelWebcastEventLink

Formatting

You use a mixture of lowercase and UPPERCASE keywords, you should stick to one or the other. It makes it much easier to read the query if you do. I am going to stick with UPPERCASE as it is the convention.

An alternate approach

Might I suggest storing the minimum SortSequence value in a variable and then using that to filter out records you don't need.

Here is what I came up with.

DECLARE @Minimum_Sort_Seq INT = 
(
    SELECT MIN(SortSequence)
    FROM WebcastChannelWebcastEventLink
)

SELECT wcwel.WebcastChannelId,wcwel.WebcastEventId
FROM WebcastChannelWebcastEventLink wcwel
WHERE wcwel.SortSequence = @Minimum_Sort_Seq

This query returned the same results in my testing. You can run this to check yourself:

CREATE TABLE #WebcastChannelWebcastEventLink
(
    WebcastChannelId INT,
    WebcastEventId INT,
    SortSequence INT
) 

INSERT INTO #WebcastChannelWebcastEventLink
SELECT 1,2,1 UNION ALL
SELECT 1,5,2 UNION ALL
SELECT 1,3,3 UNION ALL
SELECT 2,7,2 UNION ALL
SELECT 2,8,1

SELECT wcwel.WebcastChannelId, wcwel.WebcastEventId
FROM #WebcastChannelWebcastEventLink wcwel WITH (NOLOCK)
INNER JOIN #WebcastChannelWebcastEventLink wcwel1 WITH (NOLOCK) 
    ON wcwel1.WebcastChannelId = wcwel.WebcastChannelId
GROUP BY
wcwel.WebcastChannelId,
wcwel.WebcastEventId,
wcwel.SortSequence
HAVING
wcwel.SortSequence = MIN(wcwel1.SortSequence)

DECLARE @Minimum_Sort_Seq INT = 
(
    SELECT MIN(SortSequence)
    FROM WebcastChannelWebcastEventLink
)

SELECT wcwel.WebcastChannelId,wcwel.WebcastEventId
FROM WebcastChannelWebcastEventLink wcwel
WHERE wcwel.SortSequence = @Minimum_Sort_Seq

DROP TABLE #WebcastChannelWebcastEventLink
Source Link
PenutReaper
  • 1.3k
  • 10
  • 8

#Formatting# You use a mixture of lowercase and UPPERCASE keywords, you should stick to one or the other. It makes it much easier to read the query if you do. I am going to stick with UPPERCASE as it is the convention.

#An alternate approach# Might I suggest storing the minimum SortSequence value in a variable and then using that to filter out records you don't need.

Here is what I came up with.

DECLARE @Minimum_Sort_Seq INT = 
(
    SELECT MIN(SortSequence)
    FROM WebcastChannelWebcastEventLink
)

SELECT wcwel.WebcastChannelId,wcwel.WebcastEventId
FROM WebcastChannelWebcastEventLink wcwel
WHERE wcwel.SortSequence = @Minimum_Sort_Seq

This query returned the same results in my testing. You can run this to check yourself:

CREATE TABLE #WebcastChannelWebcastEventLink
(
    WebcastChannelId INT,
    WebcastEventId INT,
    SortSequence INT
) 

INSERT INTO #WebcastChannelWebcastEventLink
SELECT 1,2,1 UNION ALL
SELECT 1,5,2 UNION ALL
SELECT 1,3,3 UNION ALL
SELECT 2,7,2 UNION ALL
SELECT 2,8,1

SELECT wcwel.WebcastChannelId, wcwel.WebcastEventId
FROM #WebcastChannelWebcastEventLink wcwel WITH (NOLOCK)
INNER JOIN #WebcastChannelWebcastEventLink wcwel1 WITH (NOLOCK) 
    ON wcwel1.WebcastChannelId = wcwel.WebcastChannelId
GROUP BY
wcwel.WebcastChannelId,
wcwel.WebcastEventId,
wcwel.SortSequence
HAVING
wcwel.SortSequence = MIN(wcwel1.SortSequence)

DECLARE @Minimum_Sort_Seq INT = 
(
    SELECT MIN(SortSequence)
    FROM WebcastChannelWebcastEventLink
)

SELECT wcwel.WebcastChannelId,wcwel.WebcastEventId
FROM WebcastChannelWebcastEventLink wcwel
WHERE wcwel.SortSequence = @Minimum_Sort_Seq

DROP TABLE #WebcastChannelWebcastEventLink