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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:45:32+00:00 2026-05-29T05:45:32+00:00

SQL Returns the following results from table: CowTracking ID cow_id barn_id — —— ——-

  • 0

SQL Returns the following results from table: CowTracking

    ID  cow_id  barn_id
    --  ------  -------
    19    5       3
    20    5       2
    21    5       9
    22    5       1

I am trying to get the following results with a PIVOT in SQL

     cow_id  barn1  barn2  barn3  barn4
     ------  -----  -----  -----  -----
       5       3      2      9      1

This is the code I have so far.

    SELECT *
    FROM 
    (
        SELECT TOP 4 *
        FROM CowTracking
            WHERE cow_id = 5
    ) AS DataTable
    PIVOT
    (
        MIN(barn_id) **IDK what function to use and which column to use it on**
        FOR ID  ??<---**NOT SURE**
        IN 
        (
        [barn1], [barn2], [barn3], [barn4]
        )
    ) AS PivotTable


    ERRORS: Error converting data type nvarchar to int
            The incorrect value "barn1" is supplied in the PIVOT operator

NOTE: The barn_id is a varchar. It will not be possible to change the datatype.

I am not trying to add/multiply/aggregate or whatever. I am simply trying to move the row to a column

How would I go about doing this?
Is this the correct thought process?

Do I even need to use PIVOT?

  • 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-05-29T05:45:33+00:00Added an answer on May 29, 2026 at 5:45 am

    As there is no barn1..4 in your tables, you somehow have to replace the ID‘s with their corresponding barns.

    One solution using PIVOT might be like this

    SELECT  cow_id
            , [19] as [barn1]
            , [20] as [barn2]
            , [21] as [barn3]
            , [22] as [barn4]
    FROM    (       
                SELECT  *
                FROM    DataTable
                PIVOT   (   MIN(barn_id)
                            FOR ID IN ([19], [20], [21], [22])
                        ) AS PivotTable
            ) q                 
    

    another solution using CASE and GROUP BY could be

    SELECT  cow_id
            , [barn1] = SUM(CASE WHEN ID = 19 THEN barn_id END)
            , [barn2] = SUM(CASE WHEN ID = 20 THEN barn_id END)
            , [barn3] = SUM(CASE WHEN ID = 21 THEN barn_id END)
            , [barn4] = SUM(CASE WHEN ID = 22 THEN barn_id END)
    FROM    DataTable
    GROUP BY
            cow_id
    

    but in essence, this all boils down to hardcoding an ID to a barn.


    Edit

    If you always return a fixed number of records, and using SQL Server you might make this a bit more robust by

    • adding a ROW_NUMBER to each result
    • pivot on this upfront known number

    SQL Statement

    SELECT  cow_id  
            , [barn1] = SUM(CASE WHEN rn = 1 THEN barn_id END)
            , [barn2] = SUM(CASE WHEN rn = 2 THEN barn_id END)
            , [barn3] = SUM(CASE WHEN rn = 3 THEN barn_id END)
            , [barn4] = SUM(CASE WHEN rn = 4 THEN barn_id END)
    FROM    (
                SELECT  cow_id
                        , rn = ROW_NUMBER() OVER (ORDER BY ID)
                        , barn_id
                FROM    DataTable       
            ) q
    GROUP BY
            cow_id
    

    Test script

    ;WITH DataTable (ID, cow_id, barn_id) AS (
        SELECT * FROM (VALUES 
            (19, 5, 3)
            , (20, 5, 2)
            , (21, 5, 9)
            , (22, 5, 1)
        ) AS q (a, b, c)        
    )
    SELECT  cow_id  
            , [barn1] = SUM(CASE WHEN rn = 1 THEN barn_id END)
            , [barn2] = SUM(CASE WHEN rn = 2 THEN barn_id END)
            , [barn3] = SUM(CASE WHEN rn = 3 THEN barn_id END)
            , [barn4] = SUM(CASE WHEN rn = 4 THEN barn_id END)
    FROM    (
                SELECT  cow_id
                        , rn = ROW_NUMBER() OVER (ORDER BY ID)
                        , barn_id
                FROM    DataTable       
            ) q
    GROUP BY
            cow_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following SQL query that selects some results from my table: select avg(c3),
I have the following Sql Query that returns the type of results that I
The following PL/SQL will not execute and simply returns 'Invalid Argument' as an error.
Is the following possible in SQL Server 2000? CREATE FUNCTION getItemType (@code varchar(18)) RETURNS
I need a SQL query that returns ContactDate, SortName, City, ContactType, and Summary from
I have the following SQL statement: USE ws_results_db_2011_09_11_09_06_24;SELECT table_name FROM INFORMATION_SCHEMA.Tables WHERE table_name like
I have a Premiums table that I attempting to query using the following LINQ-to-SQL:
How come the following doesn't work? CREATE FUNCTION Test (@top integer) RETURNS TABLE AS
The following query returns strange results for me: SELECT `Statistics`.`StatisticID`, COUNT(`Votes`.`StatisticID`) AS `Score`, COUNT(`Views`.`StatisticID`)
I have tried the following query in phpmyadmin and it returns the correct results,

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.