SQL Case MAX WHEN LIKE

John

I would like to create a case statement or a rank over statement for a particular outlier case I have.

I am not sure how to write a case statement utilizing CASE (pseudo code)

WHEN MAX Total_Revenue COMPANY like 'ABC%'
else COMPANY

i have tried rank over, but it is not working:

,RANK() OVER(COMPANY LIKE 'DEF%' ORDER BY  Total_Revenue DESC) AS grp

Current table:

Company    Total_Revenue
ABC        10
DEF1       25 --This row will NOT be selected as its less than
DEF2       35 -- this row will be kept    
GHI3       65
JKL9       100  

Ouput table:

Company    Total_Revenue
ABC        10
DEF2       35     --kept 
GHI3       65
JKL9       100  
JNevill

There's quite a few ways to do what it seems like you are after:

Using a subquery to find max revenue for each comp:

SELECT Company, Total_Revenue
FROM myTable
  INNER JOIN 
    (
      SELECT Left(Company, 3) as leftcomp, 
        max(Total_Revenue) as maxTotalRevenue
      FROM mytable
      GROUP BY Left(Company, 3) 
    ) mt
    ON Left(myTable.Company, 3) = mt.leftcomp
    AND myTable.Total_Revenue = mt.maxTotalRevenue;

Window function that is later filtered by Where:

SELECT *
FROM
  (
    SELECT Company, 
      Total_Revenue,
      MAX(Total_Revenue) OVER (PARTITION BY Left(Company, 3)) as maxTotalRevenue
    FROM myTable
  ) mt
 WHERE Total_Revenue = maxTotalRevenue;

Correlated subquery in the WHERE clause:

 SELECT *
 FROM myTable mt1
 Where Total_Revenue =
   (
     SELECT max(total_revenue)
     FROM myTable
     WHERE Left(myTable.Company, 3) = Left(mt1.Company, 3)
   );

SQLFiddle here

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to select a unique row derived of multiple max(case when) without using distinct in Oracle SQL

分類Dev

SPARK SQL - case when then

分類Dev

CASE WHEN "__" LIKE "__" ELSE "__" MySQLエラー

分類Dev

SQL Server:LIKEでCASEを使用する

分類Dev

Multiple SQL MAX when items are not in order

分類Dev

SQL - how to count unique within case when?

分類Dev

Selbstreferenzielle CASE WHEN-Klausel in SQL

分類Dev

sql nested "sum (case when) or" select query

分類Dev

INSERT INTO statement after CASE WHEN in Oracle SQL

分類Dev

SQL Multiple Case When and mulitple results

分類Dev

teradata SQL CASE WHEN - SELECT Failed. 3771: Illegal expression in WHEN clause of CASE expression

分類Dev

base :: max()とdplyr :: case_whenの間の相互作用

分類Dev

CASEおよびMAX値を使用したSQLクエリ

分類Dev

Using Case results in where clause - i would like to use the results of TorL - SQL Server

分類Dev

mysql like query to get max like

分類Dev

Case when a value is different to other value, SQL Server

分類Dev

Invalid false result expression in CASE WHEN T-SQL

分類Dev

Proc SQL:名前が必要、Case When、End AS

分類Dev

Where and how to use CASE WHEN SQL multiple sub queries

分類Dev

Convert SUM / CASE WHEN / GROUP BY SQL query into LINQ

分類Dev

SQL:where [columnName] = CASE WHEN(condition)THEN NULL ELSE [columnName] END

分類Dev

SQL min()、max()

分類Dev

SQL Max (Quantity*ProductPrice)

分類Dev

SQL count(max())

分類Dev

select max count in SQL

分類Dev

MAX関数SQL

分類Dev

Why does my sql not work with the Abs function when it works with the Max function?

分類Dev

Why does my sql not work with the Abs function when it works with the Max function?

分類Dev

Is There Anything Like a Templatized Case-Statement

Related 関連記事

  1. 1

    How to select a unique row derived of multiple max(case when) without using distinct in Oracle SQL

  2. 2

    SPARK SQL - case when then

  3. 3

    CASE WHEN "__" LIKE "__" ELSE "__" MySQLエラー

  4. 4

    SQL Server:LIKEでCASEを使用する

  5. 5

    Multiple SQL MAX when items are not in order

  6. 6

    SQL - how to count unique within case when?

  7. 7

    Selbstreferenzielle CASE WHEN-Klausel in SQL

  8. 8

    sql nested "sum (case when) or" select query

  9. 9

    INSERT INTO statement after CASE WHEN in Oracle SQL

  10. 10

    SQL Multiple Case When and mulitple results

  11. 11

    teradata SQL CASE WHEN - SELECT Failed. 3771: Illegal expression in WHEN clause of CASE expression

  12. 12

    base :: max()とdplyr :: case_whenの間の相互作用

  13. 13

    CASEおよびMAX値を使用したSQLクエリ

  14. 14

    Using Case results in where clause - i would like to use the results of TorL - SQL Server

  15. 15

    mysql like query to get max like

  16. 16

    Case when a value is different to other value, SQL Server

  17. 17

    Invalid false result expression in CASE WHEN T-SQL

  18. 18

    Proc SQL:名前が必要、Case When、End AS

  19. 19

    Where and how to use CASE WHEN SQL multiple sub queries

  20. 20

    Convert SUM / CASE WHEN / GROUP BY SQL query into LINQ

  21. 21

    SQL:where [columnName] = CASE WHEN(condition)THEN NULL ELSE [columnName] END

  22. 22

    SQL min()、max()

  23. 23

    SQL Max (Quantity*ProductPrice)

  24. 24

    SQL count(max())

  25. 25

    select max count in SQL

  26. 26

    MAX関数SQL

  27. 27

    Why does my sql not work with the Abs function when it works with the Max function?

  28. 28

    Why does my sql not work with the Abs function when it works with the Max function?

  29. 29

    Is There Anything Like a Templatized Case-Statement

ホットタグ

アーカイブ