Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8383909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:17:08+00:00 2026-06-09T17:17:08+00:00

The SELECT statement below returns the correct data. SELECT stu.sc, stu.sn, COUNT(*) AS Total,

  • 0

The SELECT statement below returns the correct data.

SELECT stu.sc, stu.sn, COUNT(*) AS Total,
    CASE
      WHEN COUNT(*) = 3 Then 'Letter 1'
      WHEN COUNT(*) = 4 Then 'Letter 2'
      WHEN COUNT(*) = 5 Then 'Letter 3'
      ELSE 'SARB'
    END AS Letter  
FROM STU  join att ON (stu.SC = att.SC and stu.SN = att.SN)
WHERE att.al in ('c','t','u')
GROUP by stu.sc, stu.sn
HAVING COUNT(*) >= 3

I need to perform an UPDATE based on that query, and I can’t seem to figure it out. I’ve looked at multiple examples online that are similar to what I need, but I can’t get this to work. I need to be able to do something like this…

UPDATE stu
SET stu.tru = 
   CASE
     When COUNT(*) = 3 Then 'Letter 1'
     When COUNT(*) = 4 Then 'Letter 2'
     When COUNT(*) = 5 Then 'Letter 3'
     ELSE 'SARB'
   END  
FROM stu  JOIN att
   on (stu.sc = att.sc and stu.sn = att.sn)
WHERE ATT.AL in ('c','t','u')
GROUP BY stu.sc, stu.sn
HAVING COUNT(*) >= 3

I know I can’t use Group By and Having in an Update statement directly, but I’ve seen examples where they use a Select with Group By before the Set and Join. I just can’t make it work.

Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:17:09+00:00Added an answer on June 9, 2026 at 5:17 pm
    WITH CTE AS (
      SELECT stu.sc, stu.sn, COUNT(*) AS Total,
          CASE
            WHEN COUNT(*) = 3 Then 'Letter 1'
            WHEN COUNT(*) = 4 Then 'Letter 2'
            WHEN COUNT(*) = 5 Then 'Letter 3'
            ELSE 'SARB'
          END AS Letter  
      FROM STU  join att ON (stu.SC = att.SC and stu.SN = att.SN)
      WHERE att.al in ('c','t','u')
      GROUP by stu.sc, stu.sn
      HAVING COUNT(*) >= 3
    )
    UPDATE stu
    SET stu.tru = cte.Letter
    FROM stu  JOIN cte
       on (stu.sc = cte.sc and stu.sn = cte.sn)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SELECT statement similar to the one below which returns several counts
How to get a correct result for my select statement below SELECT * FROM
Please review the select statement below: SELECT fc.flavorid, fc.flavorname, CASE WHEN sa.flavorid IS NULL
I want to write an SQL statement like below: select * from tbl where
I have created a procedure and used the below statement inside that . select
I'm trying to do the below sql statement in GORM select * from table1
I have a select statement running which pulls data into $row. $result = mysql_query(Select
I have this ugly SELECT statement : Select 'Urbain' as Véhicule, Count( DISTINCT Plaintes.IDEvenements)
I have a SELECT statement like this: SELECT T1.COD, T1.NAME, (SELECT MAX(T2.DATA) FROM dbo.TAB2
The following query returns data right away: SELECT time, value from data order by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.