T-SQL - GROUP BY with LIKE - is this possible?

后端 未结 8 1138
失恋的感觉
失恋的感觉 2020-12-10 03:55

Is there a way to include a LIKE expression in a GROUP BY query? For example:

SELECT Count(*) 
FROM tblWhatever
GROUP BY column_x [LIKE %Fall-2009%]


        
相关标签:
8条回答
  • 2020-12-10 04:24

    No, the LIKE function is not supported in the GROUP BY clause. You'd need to use:

      SELECT x.term,
             COUNT(*)
        FROM (SELECT CASE
                       WHEN CHARINDEX('Fall_2009', t.column) > 0 THEN
                         SUBSTRING(t.column, CHARINDEX('Fall_2009', t.column), LEN(t.column))
                       WHEN CHARINDEX('Spring_2009', t.column) > 0 THEN
                         SUBSTRING(t.column, CHARINDEX('Spring_2009', t.column), LEN(t.column))
                       ELSE
                         NULL
                     END as TERM
                FROM TABLE t) x
    GROUP BY x.term
    
    0 讨论(0)
  • 2020-12-10 04:24

    How about this:

    SELECT MAX(column_x) AS column_x 
    FROM (
        SELECT column_x 
        FROM tblWhatever 
        WHERE (UPPER(column_x) LIKE  '%Fall-2009%')
    ) AS derivedtbl_1 GROUP BY column_x
    
    0 讨论(0)
  • 2020-12-10 04:31

    If your courses always take five letters, you can keep it really simple:

    SELECT substring(column_x,5,100), count(*)
    FROM YourTable
    GROUP BY substring(column_x,5,100)
    

    Otherwise, check Peters or Rexem's answer.

    0 讨论(0)
  • 2020-12-10 04:32

    You need an expression that returns "Fall_2009" or "Spring_2009", and then group on that expression. eg:

    -- identify each pattern individually w/ a case statement
    SELECT
      CASE
        WHEN column_x LIKE '%Fall[_]2009'   THEN 'Fall 2009'
        WHEN column_x LIKE '%Spring[_]2009' THEN 'Spring 2009'
      END AS group_by_value
    , COUNT(*) AS group_by_count
    FROM Table1 a
    GROUP BY 
      CASE
        WHEN column_x LIKE '%Fall[_]2009'   THEN 'Fall 2009'
        WHEN column_x LIKE '%Spring[_]2009' THEN 'Spring 2009'
      END
    

    or

    -- strip all characters up to the first space or dash
    SELECT 
      STUFF(column_x,1,PATINDEX('%[- ]%',column_x),'') AS group_by_value
    , COUNT(*) as group_by_count
    FROM Table1 a
    GROUP BY 
      STUFF(column_x,1,PATINDEX('%[- ]%',column_x),'')
    

    or

    -- join to a (pseudo) table of pattern masks
    SELECT b.Label, COUNT(*)
    FROM Table1 a
    JOIN (
      SELECT '%Fall[_]2009'  , 'Fall, 2009' UNION ALL
      SELECT '%Spring[_]2009', 'Spring, 2009'
      ) b (Mask, Label) ON a.column_x LIKE b.Mask
    GROUP BY b.Label
    
    0 讨论(0)
  • 2020-12-10 04:32

    Unfortunately, you have a badly structured database, having combined SUBJECT and TERM into the same column. When you use GROUP BY it treats each unique value in the column as a group in the result set. You'd be best advised to restructure the database if at all possible — you probably want three columns here (SUBJECT, TERM, SCHOOL_YEAR) but two might possibly be appropriate.

    If you can't restructure the database you'll need to parse the column to extract the term. Rexem showed you one way to do this, you could also use a stored procedure.

    0 讨论(0)
  • 2020-12-10 04:37

    LIKE does not make sense in your context, as it either matches or it does not, but it does not establish groups. You will have to use string functions that parse the column values into what makes sense for your data.

    0 讨论(0)
提交回复
热议问题