Skip to main content
edited tags
Source Link
200_success
  • 145.7k
  • 22
  • 192
  • 481

Rewriting SQL IN statement with long list of parameters

These IDs seem like they should be a table as, there are several queries that use the same list of IDs. The following needs to be refactored.

SELECT SUM(CASE WHEN s.TypeID IN(1,2,3,4,7,8,10) THEN 1 ELSE 0 END) FROM Sales s

The problem is a subquery can't be used in an aggregate function. The following I'd like to write it as follows, but it causes an error:

SELECT SUM(CASE WHEN s.TypeID IN(SELECT ID FROM SaleTypes WHERE SaleType.Desc = 'US') THEN 1 ELSE 0 END) FROM Sales s

SQL IN statement with long list of parameters

These IDs seem like they should be a table as, there are several queries that use the same list of IDs. The following needs to be refactored.

SELECT SUM(CASE WHEN s.TypeID IN(1,2,3,4,7,8,10) THEN 1 ELSE 0 END) FROM Sales s

The problem is a subquery can't be used in an aggregate function. The following causes an error:

SELECT SUM(CASE WHEN s.TypeID IN(SELECT ID FROM SaleTypes WHERE SaleType.Desc = 'US') THEN 1 ELSE 0 END) FROM Sales s

Rewriting SQL IN statement with long list of parameters

These IDs seem like they should be a table as, there are several queries that use the same list of IDs. The following needs to be refactored.

SELECT SUM(CASE WHEN s.TypeID IN(1,2,3,4,7,8,10) THEN 1 ELSE 0 END) FROM Sales s

The problem is a subquery can't be used in an aggregate function. I'd like to write it as follows, but it causes an error:

SELECT SUM(CASE WHEN s.TypeID IN(SELECT ID FROM SaleTypes WHERE SaleType.Desc = 'US') THEN 1 ELSE 0 END) FROM Sales s
Rollback to Revision 2
Source Link
Mathieu Guindon
  • 75.6k
  • 18
  • 195
  • 469

Nitpicks

SQL code (like pretty much all code) reads a lot easier when using line breaks and indentation.


s is not These IDs seem like they should be a very good alias. You want your aliases to say something about what it meanstable as, not just shortenthere are several queries that use the codesame list of IDs. In this case I would not even use one, I feel Sales is plenty short The following needs to be refactored.


Your code so far:

SELECT SUM(
    CASE WHEN Saless.TypeID IN(1,2,3,4,7,8,10) THEN 1 
    ELSE 0 
    END
 ) 
 FROM Sales;Sales s

 

Common Table Expression

You could organize the code a little better by using a CTE, although it The problem is a bit more verbosesubquery can't be used in an aggregate function. The following causes an error:

WITH SalesInUS AS(
    SELECT ID
    FROM SalesType
    WHERE SalesType.Desc = 'US'
),

SELECT SUM(
    CASE WHEN Saless.TypeID IN SalesInUS THEN 1 
    ELSE 0 
    END
) 
FROM Sales;

But if this is often referenced, you are correct: a table would work better.

Example:

INSERT INTO #SalesInUS
    (SELECT ID
    FROM SalesType
   SaleTypes WHERE SalesTypeSaleType.Desc = 'US'

Then you just join that table when you need it. Note I changed SUM to COUNT as well.

SELECT COUNT(Sales.TypeID)
FROM Sales 
INNER JOIN #SalesInUS
ONTHEN Sales.TypeID1 =ELSE #SalesInUS.ID

Alternatively, you could also use a simple JOIN

SELECT0 COUNT(Sales.TypeIDEND)
FROM Sales 
INNER JOIN SalesType
ONFROM Sales.TypeID = SalesType.ID
AND SalesType.Desc = 'US's

Nitpicks

SQL code (like pretty much all code) reads a lot easier when using line breaks and indentation.


s is not a very good alias. You want your aliases to say something about what it means, not just shorten the code. In this case I would not even use one, I feel Sales is plenty short.


Your code so far:

SELECT SUM(
    CASE WHEN Sales.TypeID IN(1,2,3,4,7,8,10) THEN 1 
    ELSE 0 
    END
 ) 
 FROM Sales;

 

Common Table Expression

You could organize the code a little better by using a CTE, although it is a bit more verbose:

WITH SalesInUS AS(
    SELECT ID
    FROM SalesType
    WHERE SalesType.Desc = 'US'
),

SELECT SUM(
    CASE WHEN Sales.TypeID IN SalesInUS THEN 1 
    ELSE 0 
    END
) 
FROM Sales;

But if this is often referenced, you are correct: a table would work better.

Example:

INSERT INTO #SalesInUS
    SELECT ID
    FROM SalesType
    WHERE SalesType.Desc = 'US'

Then you just join that table when you need it. Note I changed SUM to COUNT as well.

SELECT COUNT(Sales.TypeID)
FROM Sales 
INNER JOIN #SalesInUS
ON Sales.TypeID = #SalesInUS.ID

Alternatively, you could also use a simple JOIN

SELECT COUNT(Sales.TypeID)
FROM Sales 
INNER JOIN SalesType
ON Sales.TypeID = SalesType.ID
AND SalesType.Desc = 'US'

These IDs seem like they should be a table as, there are several queries that use the same list of IDs. The following needs to be refactored.

SELECT SUM(CASE WHEN s.TypeID IN(1,2,3,4,7,8,10) THEN 1 ELSE 0 END) FROM Sales s

The problem is a subquery can't be used in an aggregate function. The following causes an error:

SELECT SUM(CASE WHEN s.TypeID IN(SELECT ID FROM SaleTypes WHERE SaleType.Desc = 'US') THEN 1 ELSE 0 END) FROM Sales s
I had misread the question. I revised my answer.
Source Link
Phrancis
  • 20.5k
  • 6
  • 70
  • 155

These IDs seem like they should beNitpicks

SQL code (like pretty much all code) reads a table aslot easier when using line breaks and indentation.


s is not a very good alias. You want your aliases to say something about what it means, there are several queries that usenot just shorten the same list of IDscode. The following needs to be refactoredIn this case I would not even use one, I feel Sales is plenty short.


Your code so far:

SELECT SUM(
    CASE WHEN sSales.TypeID IN(1,2,3,4,7,8,10) THEN 1 
    ELSE 0 
    END 
)  
FROM Sales sSales;
 

The problemCommon Table Expression

You could organize the code a little better by using a CTE, although it is a subquery can't be used in an aggregate function. The following causes an errorbit more verbose:

WITH SalesInUS AS(
    SELECT ID
    FROM SalesType
    WHERE SalesType.Desc = 'US'
),

SELECT SUM(
    CASE WHEN sSales.TypeID IN( SalesInUS THEN 1 
    ELSE 0 
    END
) 
FROM Sales;

But if this is often referenced, you are correct: a table would work better.

Example:

INSERT INTO #SalesInUS
    SELECT ID
    FROM SaleTypesSalesType
    WHERE SaleTypeSalesType.Desc = 'US'

Then you just join that table when you need it. Note I changed SUM to COUNT as well.

SELECT COUNT(Sales.TypeID)
FROM THENSales 1
INNER ELSEJOIN 0#SalesInUS
ON END)Sales.TypeID = #SalesInUS.ID

Alternatively, you could also use a simple JOIN

SELECT COUNT(Sales.TypeID)
FROM Sales s
INNER JOIN SalesType
ON Sales.TypeID = SalesType.ID
AND SalesType.Desc = 'US'

These IDs seem like they should be a table as, there are several queries that use the same list of IDs. The following needs to be refactored.

SELECT SUM(CASE WHEN s.TypeID IN(1,2,3,4,7,8,10) THEN 1 ELSE 0 END) FROM Sales s

The problem is a subquery can't be used in an aggregate function. The following causes an error:

SELECT SUM(CASE WHEN s.TypeID IN(SELECT ID FROM SaleTypes WHERE SaleType.Desc = 'US') THEN 1 ELSE 0 END) FROM Sales s

Nitpicks

SQL code (like pretty much all code) reads a lot easier when using line breaks and indentation.


s is not a very good alias. You want your aliases to say something about what it means, not just shorten the code. In this case I would not even use one, I feel Sales is plenty short.


Your code so far:

SELECT SUM(
    CASE WHEN Sales.TypeID IN(1,2,3,4,7,8,10) THEN 1 
    ELSE 0 
    END 
)  
FROM Sales;
 

Common Table Expression

You could organize the code a little better by using a CTE, although it is a bit more verbose:

WITH SalesInUS AS(
    SELECT ID
    FROM SalesType
    WHERE SalesType.Desc = 'US'
),

SELECT SUM(
    CASE WHEN Sales.TypeID IN SalesInUS THEN 1 
    ELSE 0 
    END
) 
FROM Sales;

But if this is often referenced, you are correct: a table would work better.

Example:

INSERT INTO #SalesInUS
    SELECT ID
    FROM SalesType
    WHERE SalesType.Desc = 'US'

Then you just join that table when you need it. Note I changed SUM to COUNT as well.

SELECT COUNT(Sales.TypeID)
FROM Sales 
INNER JOIN #SalesInUS
ON Sales.TypeID = #SalesInUS.ID

Alternatively, you could also use a simple JOIN

SELECT COUNT(Sales.TypeID)
FROM Sales 
INNER JOIN SalesType
ON Sales.TypeID = SalesType.ID
AND SalesType.Desc = 'US'
Modified title
Source Link
Phrancis
  • 20.5k
  • 6
  • 70
  • 155
Loading
Source Link
user30586
  • 237
  • 1
  • 8
Loading