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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:24:22+00:00 2026-06-09T19:24:22+00:00

I found this link Pivot tables in SQL Server. A simple sample. and did

  • 0

I found this link Pivot tables in SQL Server. A simple sample. and did a conversion it to temp table. However, I got an error “Incorrect syntax near ‘(‘.” Can you help me on this? Below is the code:

IF OBJECT_ID('tempdb..#DailyIncome') IS NOT NULL
BEGIN
drop table #DailyIncome
END

create table #DailyIncome
       (
         VendorId nvarchar(10)
         , IncomeDay nvarchar(10)
         , IncomeAmount int
       )

insert into #DailyIncome values ('SPIKE', 'FRI', 100)
insert into #DailyIncome values ('SPIKE', 'MON', 300)
insert into #DailyIncome values ('FREDS', 'SUN', 400)
insert into #DailyIncome values ('SPIKE', 'WED', 500)
insert into #DailyIncome values ('SPIKE', 'TUE', 200)
insert into #DailyIncome values ('JOHNS', 'WED', 900)
insert into #DailyIncome values ('SPIKE', 'FRI', 100)
insert into #DailyIncome values ('JOHNS', 'MON', 300)
insert into #DailyIncome values ('SPIKE', 'SUN', 400)
insert into #DailyIncome values ('JOHNS', 'FRI', 300)
insert into #DailyIncome values ('FREDS', 'TUE', 500)
insert into #DailyIncome values ('FREDS', 'TUE', 200)
insert into #DailyIncome values ('SPIKE', 'MON', 900)
insert into #DailyIncome values ('FREDS', 'FRI', 900)
insert into #DailyIncome values ('FREDS', 'MON', 500)
insert into #DailyIncome values ('JOHNS', 'SUN', 600)
insert into #DailyIncome values ('SPIKE', 'FRI', 300)
insert into #DailyIncome values ('SPIKE', 'WED', 500)
insert into #DailyIncome values ('SPIKE', 'FRI', 300)
insert into #DailyIncome values ('JOHNS', 'THU', 800)
insert into #DailyIncome values ('JOHNS', 'SAT', 800)
insert into #DailyIncome values ('SPIKE', 'TUE', 100)
insert into #DailyIncome values ('SPIKE', 'THU', 300)
insert into #DailyIncome values ('FREDS', 'WED', 500)
insert into #DailyIncome values ('SPIKE', 'SAT', 100)
insert into #DailyIncome values ('FREDS', 'SAT', 500)
insert into #DailyIncome values ('FREDS', 'THU', 800)
insert into #DailyIncome values ('JOHNS', 'TUE', 600)

SELECT * FROM #DailyIncome

SELECT *
FROM #DailyIncome
pivot(avg(IncomeAmount) FOR IncomeDay IN (
        [MON]
        ,[TUE]
        ,[WED]
        ,[THU]
        ,[FRI]
        ,[SAT]
        ,[SUN]
        )) AS AvgIncomePerDay

Thanks guys!

[UPDATE]

Based on the comments, the database is created using SQL Server 2000. Is there a workaround for this?

  • 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-09T19:24:23+00:00Added an answer on June 9, 2026 at 7:24 pm

    You can try something like

    SELECT  VendorId,
            AVG(CASE
                WHEN IncomeDay = 'MON'
                    THEN IncomeAmount
                ELSE NULL
            END) [MON],
            AVG(CASE
                WHEN IncomeDay = 'TUE'
                    THEN IncomeAmount
                ELSE NULL
            END) [TUE],
            AVG(CASE
                WHEN IncomeDay = 'WED'
                    THEN IncomeAmount
                ELSE NULL
            END) [WED],
            AVG(CASE
                WHEN IncomeDay = 'THU'
                    THEN IncomeAmount
                ELSE NULL
            END) [THU],
            AVG(CASE
                WHEN IncomeDay = 'FRI'
                    THEN IncomeAmount
                ELSE NULL
            END) [FRI],
            AVG(CASE
                WHEN IncomeDay = 'SAT'
                    THEN IncomeAmount
                ELSE NULL
            END) [SAT],
            AVG(CASE
                WHEN IncomeDay = 'SUN'
                    THEN IncomeAmount
                ELSE NULL
            END) [SUN]
    FROM    #DailyIncome
    GROUP BY VendorId
    

    Please note, the reason why I used NULLS instead of 0 was due to the PIVOT functionality.

    from Using PIVOT and UNPIVOT

    When aggregate functions are used with PIVOT, the presence of any null
    values in the value column are not considered when computing an
    aggregation.

    You will find different results if you were to run the query with 0 instead of NULLs

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

Sidebar

Related Questions

I found this link very useful get selected textbox id jQuery however, what i
I found this link where I got this code: $(document).ready(function(){ //when a link in
I have found this link: Detect Antivirus on Windows using C# However when I
I found this link Link to another page using Twitter Bootstrap jquery modal in
I found this link on how to embed custom fonts in XAML apps. Is
I have found this link: http://www.jfree.org/forum/viewtopic.php?f=3&t=6314 Its back in 2007 where they agree that
I've search for some solution to my problem and I found this link .
Trying to build openssl-fips-2.0 with NDK, before I was lucky found this link and
I need to make rate option in my android app. I found this link
I found from this link that there are several types of XML parsers in

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.