UNION with CASE statement

  • Hi Experts,

    I am getting error in running this query, It has to do with data type conflict. But I am not quite sure about the problem.

    Kindly shed a light on it.

    Thanks!

    Select * from

    (SELECT Dept,IName, ICode, Qty, GrossWeight

    FROM Data_Details

    UNION

    SELECT Dept, ICode,IName, SUM(Qty) AS TotalQ, SUM(GrossWeight) AS GrossWt from Data_Details

    group by ICode,IName,Dept) source

    Order by ICode,

    CASE WHEN Quantity = 'TotalQ'

    then 1 else 0 END, Qty

    error: Msg 245, Level 16, State 1, Line 1

    Conversion failed when converting the varchar value 'TotalQ' to data type int.

  • I don't see where the "Quantity" column comes from. Is that the actual query you're running or a simplified version?

    However, the message indicates that you're comparing the column "Quantity" (which is likely a numeric column) to the string literal 'TotalQ'. For data type precedence, the string literal is converted to numeric to perform the comparison and the conversion fails.

    -- Gianluca Sartori

  • Good spot Twin.Devil


    On two occasions I have been asked, "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    —Charles Babbage, Passages from the Life of a Philosopher

    How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537

  • BWFC (6/28/2016)


    To do this:

    CASE WHEN Quantity = 'TotalQ'

    You need to take the quotes off 'TotalQ', like this:

    CASE WHEN Quantity = TotalQ

    This will not work as in when UNION is used, There is no TotalQ column left. and Quantity Column where is this is coming from ????

    or may be its typeo "Qty" column is refered as "Quantity"????

    Can you please Shed some light on it 1st.

    It would alot easy if you could also share your tables structure & some sample data along with it and for this case Original version of query as well.

  • spaghettidba (6/28/2016)


    I don't see where the "Quantity" column comes from. Is that the actual query you're running or a simplified version?

    However, the message indicates that you're comparing the column "Quantity" (which is likely a numeric column) to the string literal 'TotalQ'. For data type precedence, the string literal is converted to numeric to perform the comparison and the conversion fails.

    +1

    and the Icode and Iname seem to have different order in the two queries, if it is not intentional.

    ----------------------------------------------------------------------------------------------------------------------------------------------------
    Roshan Joe

    Jeff Moden -Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Sorry Quantity is the correct column name, misspelled.:-D

    Select * from

    (SELECT Dept,IName, ICode, Quantity, GrossWeight

    FROM Data_Details

    UNION

    SELECT Zone, ICode,IName, SUM(Quantity) AS TotalQ, SUM(GrossWeight) AS GrossWt from Data_Details

    group by ICode,IName,Dept) source

    Order by ICode,

    CASE WHEN Quantity = TotalQ

    then 1 else 0 END, Quantity

    Error now is: Invalid column name 'TotalQ'. After I removed ' '

  • please read all the comments above your reply and also read this Jeff Moden -Forum Etiquette: How to post data/code on a forum to get the best help[/url]

    and then reply. Thanks

  • The query is more confusing now. Are you trying to compare columns between the two queries in the union? This in not possible.

    ----------------------------------------------------------------------------------------------------------------------------------------------------
    Roshan Joe

    Jeff Moden -Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • maybe.....??

    USE [tempdb]

    GO

    CREATE TABLE [dbo].[Data_Details](

    [ICode] [int] NULL,

    [Quantity] [int] NULL

    )

    GO

    INSERT [dbo].[Data_Details] ([ICode], [Quantity]) VALUES (1, 23)

    INSERT [dbo].[Data_Details] ([ICode], [Quantity]) VALUES (1, -15)

    INSERT [dbo].[Data_Details] ([ICode], [Quantity]) VALUES (1, 17)

    INSERT [dbo].[Data_Details] ([ICode], [Quantity]) VALUES (2, 52)

    INSERT [dbo].[Data_Details] ([ICode], [Quantity]) VALUES (2, -68)

    INSERT [dbo].[Data_Details] ([ICode], [Quantity]) VALUES (2, 100)

    SELECT x.ICode,x.info, x.Quantity FROM (

    SELECT ICode, Quantity, 1 as replevel, 'detail' as info

    FROM Data_Details

    UNION

    SELECT ICode, SUM(Quantity) AS TotalQty, 2 , 'TotalQty'

    FROM Data_Details

    GROUP BY ICode) x

    ORDER BY x.Icode,x.replevel

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • The inner query has errors. Work on it in isolation from the whole query.

    SELECT Dept, IName, ICode, Quantity, GrossWeight

    FROM Data_Details

    UNION

    SELECT Zone, ICode, IName, SUM(Quantity) AS TotalQ, SUM(GrossWeight) AS GrossWt

    from Data_Details

    group by ICode,IName,Dept

    should probably be

    SELECT Dept, IName, ICode, Quantity, GrossWeight

    FROM Data_Details

    UNION ALL

    SELECT Dept, IName, ICode, SUM(Quantity) AS TotalQ, SUM(GrossWeight) AS GrossWt

    from Data_Details

    group by Dept, IName, ICode

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • After changing the order of group by in inner query ,now it is running ok. But it is still showing error: Conversion failed when converting the varchar value 'TotalQ' to data type int. What could be the problem ? :crazy:

  • Newbi (6/28/2016)


    After changing the order of group by in inner query ,now it is running ok. But it is still showing error: Conversion failed when converting the varchar value 'TotalQ' to data type int. What could be the problem ? :crazy:

    Check the column order in the two unioned queries.

    The first query is Dept, IName, ICode

    the second query is Zone, ICode, IName.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Newbi (6/28/2016)


    After changing the order of group by in inner query ,now it is running ok. But it is still showing error: Conversion failed when converting the varchar value 'TotalQ' to data type int. What could be the problem ? :crazy:

    Also, if your query errors, post the query not just the error - so folks don't have to guess what caused the error.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Sorry for all the confusion.:crying:

    This is the final query which I make consistency in naming the columns.

    Select * from

    (SELECT Zone,ItemName, ItemCode, Quantity, GrossWeight

    FROM PickLists_Details

    UNION

    SELECT Zone, ItemCode,ItemName, SUM(Quantity) AS TotalQ, SUM(GrossWeight) AS GrossWt

    from PickLists_Details

    group by Zone,ItemName,ItemCode) source

    Order by ItemCode,

    CASE WHEN Quantity = TotalQ

    then 1 else 0 END, Quantity

  • Newbi (6/28/2016)


    Sorry for all the confusion.:crying:

    This is the final query which I make consistency in naming the columns.

    Select * from

    (SELECT Zone,ItemName, ItemCode, Quantity, GrossWeight

    FROM PickLists_Details

    UNION

    SELECT Zone, ItemCode,ItemName, SUM(Quantity) AS TotalQ, SUM(GrossWeight) AS GrossWt

    from PickLists_Details

    group by Zone,ItemName,ItemCode) source

    Order by ItemCode,

    CASE WHEN Quantity = TotalQ

    then 1 else 0 END, Quantity

    TotalQ does not exist as a column - SUM(Quantity) goes into the Quantity column.

    Do you know the difference between UNION and UNION ALL? If not, look it up.

    The first three columns of the two queries are not in the same order. Are they meant to be?

    I suspect you are completely lost with UNION, it may not even be the best approach for what you are trying to do. If you can provide an explanation then someone will help.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 15 posts - 1 through 15 (of 48 total)

You must be logged in to reply to this topic. Login to reply