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 8862335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:42:15+00:00 2026-06-14T15:42:15+00:00

I have created a temp table, storing the customer details, I want to query

  • 0

I have created a temp table, storing the customer details, I want to query the data which group by the CustomerNo and show the ImagePath if IsDefault is set 1.

CREATE TABLE #Temp
(ID varchar(100), CustomerNo varchar(10), IsDefault int, ImagePath varchar(100), ViewCount int)

INSERT INTO #Temp
SELECT '1', 'AC1234', 1, 'xxx1.jpg', 12 UNION ALL
SELECT '2', 'AC1234', 0, 'xxx2.jpg', 42 UNION ALL
SELECT '3', 'AC1234', 0, 'xxx3.jpg', 15 UNION ALL
SELECT '4', 'AC1235', 0, 'xxx4.jpg', 16 UNION ALL
SELECT '5', 'AC1236', 1, 'xxx5.jpg', 13 UNION ALL
SELECT '6', 'AC1234', 0, 'xxx6.jpg', 56 UNION ALL
SELECT '7', 'AC1235', 1, 'xxx7.jpg', 17

SELECT * FROM #Temp
SELECT CustomerNo, MAX(IsDefault) as IsDefault, Sum(ViewCount) as ViewCount FROM #Temp
GROUP BY CustomerNo

DROP TABLE #Temp

Result:

CustomerNo IsDefault   ViewCount
---------- ----------- -----------
AC1234     1           125
AC1235     1           33
AC1236     1           13

Can I have a result like this (Show the ImagePath if “IsDefault” = 1)?

CustomerNo IsDefault   ViewCount   ImagePath
---------- ----------- ----------- -----------
AC1234     1           125         xxx1.jpg
AC1235     1           33          xxx4.jpg
AC1236     1           13          xxx5.jpg

Thank you so much!!

  • 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-14T15:42:16+00:00Added an answer on June 14, 2026 at 3:42 pm

    Assuming only one record have the IsDefault flag to 1, this will do the trick:

    CREATE TABLE #Temp (
        ID varchar(100)
      , CustomerNo varchar(10)
      , IsDefault int
      , ImagePath varchar(100)
      , ViewCount int
    );
    
    INSERT INTO #Temp
    SELECT '1', 'AC1234', 1, 'xxx1.jpg', 12 UNION ALL
    SELECT '2', 'AC1234', 0, 'xxx2.jpg', 42 UNION ALL
    SELECT '3', 'AC1234', 0, 'xxx3.jpg', 15 UNION ALL
    SELECT '4', 'AC1235', 0, 'xxx4.jpg', 16 UNION ALL
    SELECT '5', 'AC1236', 1, 'xxx5.jpg', 13 UNION ALL
    SELECT '6', 'AC1234', 0, 'xxx6.jpg', 56 UNION ALL
    SELECT '7', 'AC1235', 1, 'xxx7.jpg', 17
    ;
    
    with CustomerReport (CustomerNo, ViewCount) as 
         (SELECT t.CustomerNo, Sum(t.ViewCount) ViewCount 
            FROM #Temp t
           GROUP BY t.CustomerNo)
    Select q1.CustomerNo, q1.ViewCount 
           , (Select ImagePath
                from #Temp
               where #Temp.CustomerNo = q1.CustomerNo
                 and #Temp.IsDefault = 1) ImgPath
      from CustomerReport q1
    ;
    
    drop table #temp
    ;
    

    AFAIK, you need MS-SQL 2008+ to get support for the CTE syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a DB2 query to do the following: Create a temp table
I have created a query block with begin/end and want to run that in
I have temp table #xml that have 2 columns: CREATE TABLE #xml ( id
My requirement is to create temp table then exec a store procedure which will
I have created a local H2 database which I always opened in MySQL mode
I have data from an Excel spreadsheet that I want to import into a
I have a SQL Server 2008-R2 database that has a table that is storing
I want a SQL like query and have results that start with the input
I have query that looks like this declare @tmpTable Table(id int) insert into @tmpTable(id)
I have a temp table with the exact structure of a concrete table T

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.