Unexpected Result SQL ORDER BY The Next CEO of Stack OverflowHow can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerInserting multiple rows in a single SQL query?Insert results of a stored procedure into a temporary tableHow do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableWhat are the options for storing hierarchical data in a relational database?How to order by with unionHow to import an SQL file using the command line in MySQL?Convert INT to VARCHAR SQL

Audio Conversion With ADS1243

What difference does it make using sed with/without whitespaces?

Does the Idaho Potato Commission associate potato skins with healthy eating?

Towers in the ocean; How deep can they be built?

Physiological effects of huge anime eyes

Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact

Why did early computer designers eschew integers?

TikZ: How to fill area with a special pattern?

Purpose of level-shifter with same in and out voltages

Scary film where a woman has vaginal teeth

free fall ellipse or parabola?

How to get the last not-null value in an ordered column of a huge table?

Which one is the true statement?

Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico

Is dried pee considered dirt?

Airplane gently rocking its wings during whole flight

Is there an equivalent of cd - for cp or mv

Is a distribution that is normal, but highly skewed, considered Gaussian?

Could a dragon use its wings to swim?

Can Sneak Attack be used when hitting with an improvised weapon?

Can I board the first leg of the flight without having final country's visa?

Are the names of these months realistic?

Decide between Polyglossia and Babel for LuaLaTeX in 2019

Is it professional to write unrelated content in an almost-empty email?



Unexpected Result SQL ORDER BY



The Next CEO of Stack OverflowHow can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerInserting multiple rows in a single SQL query?Insert results of a stored procedure into a temporary tableHow do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableWhat are the options for storing hierarchical data in a relational database?How to order by with unionHow to import an SQL file using the command line in MySQL?Convert INT to VARCHAR SQL










0















I have a scenario that I dont know how to get the correct results due to my lack of knowledge with SQL and need assistance.



I understand what the ORDER BY command does; I just dont know how to manipulate the data returned from the query to keep the data in the order I need it to be in.



Below is the SQL String:



SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000714'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000713'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000715'
ORDER BY "CIF Number"


As you can see The "CIF Numbers" are out of order, which is the way they need to be and with the ORDER BY the data is ordered by the CIF#, but I need the data to actually be out of order.



Below is how the current string returns the data:



T000713 | ROGER RABBITT 
T000714 | JESSICA RABBIT
T000715 | JAMES KIRK


Below is how I need the data returned:



T000714 | JESSICA RABBITT
T000713 | ROGER RABBITT
T000715 | JAMES KIRK


When I remove the ORDER BY the result returned is this:



T000714 | JAMES KIRK
T000713 | JESSICA RABBITT
T000715 | ROGER RABBITT









share|improve this question

















  • 1





    The name/number combinations change without the ORDER BY? That seems unlikely.

    – Eric Brandt
    Mar 8 at 17:34















0















I have a scenario that I dont know how to get the correct results due to my lack of knowledge with SQL and need assistance.



I understand what the ORDER BY command does; I just dont know how to manipulate the data returned from the query to keep the data in the order I need it to be in.



Below is the SQL String:



SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000714'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000713'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000715'
ORDER BY "CIF Number"


As you can see The "CIF Numbers" are out of order, which is the way they need to be and with the ORDER BY the data is ordered by the CIF#, but I need the data to actually be out of order.



Below is how the current string returns the data:



T000713 | ROGER RABBITT 
T000714 | JESSICA RABBIT
T000715 | JAMES KIRK


Below is how I need the data returned:



T000714 | JESSICA RABBITT
T000713 | ROGER RABBITT
T000715 | JAMES KIRK


When I remove the ORDER BY the result returned is this:



T000714 | JAMES KIRK
T000713 | JESSICA RABBITT
T000715 | ROGER RABBITT









share|improve this question

















  • 1





    The name/number combinations change without the ORDER BY? That seems unlikely.

    – Eric Brandt
    Mar 8 at 17:34













0












0








0








I have a scenario that I dont know how to get the correct results due to my lack of knowledge with SQL and need assistance.



I understand what the ORDER BY command does; I just dont know how to manipulate the data returned from the query to keep the data in the order I need it to be in.



Below is the SQL String:



SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000714'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000713'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000715'
ORDER BY "CIF Number"


As you can see The "CIF Numbers" are out of order, which is the way they need to be and with the ORDER BY the data is ordered by the CIF#, but I need the data to actually be out of order.



Below is how the current string returns the data:



T000713 | ROGER RABBITT 
T000714 | JESSICA RABBIT
T000715 | JAMES KIRK


Below is how I need the data returned:



T000714 | JESSICA RABBITT
T000713 | ROGER RABBITT
T000715 | JAMES KIRK


When I remove the ORDER BY the result returned is this:



T000714 | JAMES KIRK
T000713 | JESSICA RABBITT
T000715 | ROGER RABBITT









share|improve this question














I have a scenario that I dont know how to get the correct results due to my lack of knowledge with SQL and need assistance.



I understand what the ORDER BY command does; I just dont know how to manipulate the data returned from the query to keep the data in the order I need it to be in.



Below is the SQL String:



SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000714'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000713'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000715'
ORDER BY "CIF Number"


As you can see The "CIF Numbers" are out of order, which is the way they need to be and with the ORDER BY the data is ordered by the CIF#, but I need the data to actually be out of order.



Below is how the current string returns the data:



T000713 | ROGER RABBITT 
T000714 | JESSICA RABBIT
T000715 | JAMES KIRK


Below is how I need the data returned:



T000714 | JESSICA RABBITT
T000713 | ROGER RABBITT
T000715 | JAMES KIRK


When I remove the ORDER BY the result returned is this:



T000714 | JAMES KIRK
T000713 | JESSICA RABBITT
T000715 | ROGER RABBITT






sql db2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 17:28









Zack EZack E

394115




394115







  • 1





    The name/number combinations change without the ORDER BY? That seems unlikely.

    – Eric Brandt
    Mar 8 at 17:34












  • 1





    The name/number combinations change without the ORDER BY? That seems unlikely.

    – Eric Brandt
    Mar 8 at 17:34







1




1





The name/number combinations change without the ORDER BY? That seems unlikely.

– Eric Brandt
Mar 8 at 17:34





The name/number combinations change without the ORDER BY? That seems unlikely.

– Eric Brandt
Mar 8 at 17:34












2 Answers
2






active

oldest

votes


















3














I don't understand why you use UNION. You can do it like this:



SELECT 
cfcif# AS "CIF Number",
cfna1 AS "Customer Name"
FROM cncttp08.jhadat842.cfmast cfmast
WHERE cfcif# IN ('T000714', 'T000713', 'T000715')
ORDER BY
CASE cfcif#
WHEN 'T000714' THEN 1
WHEN 'T000713' THEN 2
WHEN 'T000715' THEN 3
END





share|improve this answer























  • Thank you. This is exactly what i was needing and a great learning tool for me to use as I am still really new to SQL and its functionality. Much appreciated!.

    – Zack E
    Mar 8 at 17:54


















0














Simple!



Remove quotes from the order by clause. It should work. Your code should be like below.



`SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM 
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000714'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000713'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000715'
ORDER BY CIF Number`





share|improve this answer


















  • 2





    That won't work. If a column name is mixed case, or has a space in it, you will need to put it in double quotes

    – Paul Vernon
    Mar 8 at 18:47











  • agreed with your comment.

    – Sathvik Reddy
    Mar 8 at 22:07











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068172%2funexpected-result-sql-order-by%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














I don't understand why you use UNION. You can do it like this:



SELECT 
cfcif# AS "CIF Number",
cfna1 AS "Customer Name"
FROM cncttp08.jhadat842.cfmast cfmast
WHERE cfcif# IN ('T000714', 'T000713', 'T000715')
ORDER BY
CASE cfcif#
WHEN 'T000714' THEN 1
WHEN 'T000713' THEN 2
WHEN 'T000715' THEN 3
END





share|improve this answer























  • Thank you. This is exactly what i was needing and a great learning tool for me to use as I am still really new to SQL and its functionality. Much appreciated!.

    – Zack E
    Mar 8 at 17:54















3














I don't understand why you use UNION. You can do it like this:



SELECT 
cfcif# AS "CIF Number",
cfna1 AS "Customer Name"
FROM cncttp08.jhadat842.cfmast cfmast
WHERE cfcif# IN ('T000714', 'T000713', 'T000715')
ORDER BY
CASE cfcif#
WHEN 'T000714' THEN 1
WHEN 'T000713' THEN 2
WHEN 'T000715' THEN 3
END





share|improve this answer























  • Thank you. This is exactly what i was needing and a great learning tool for me to use as I am still really new to SQL and its functionality. Much appreciated!.

    – Zack E
    Mar 8 at 17:54













3












3








3







I don't understand why you use UNION. You can do it like this:



SELECT 
cfcif# AS "CIF Number",
cfna1 AS "Customer Name"
FROM cncttp08.jhadat842.cfmast cfmast
WHERE cfcif# IN ('T000714', 'T000713', 'T000715')
ORDER BY
CASE cfcif#
WHEN 'T000714' THEN 1
WHEN 'T000713' THEN 2
WHEN 'T000715' THEN 3
END





share|improve this answer













I don't understand why you use UNION. You can do it like this:



SELECT 
cfcif# AS "CIF Number",
cfna1 AS "Customer Name"
FROM cncttp08.jhadat842.cfmast cfmast
WHERE cfcif# IN ('T000714', 'T000713', 'T000715')
ORDER BY
CASE cfcif#
WHEN 'T000714' THEN 1
WHEN 'T000713' THEN 2
WHEN 'T000715' THEN 3
END






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 17:36









forpasforpas

19.1k3828




19.1k3828












  • Thank you. This is exactly what i was needing and a great learning tool for me to use as I am still really new to SQL and its functionality. Much appreciated!.

    – Zack E
    Mar 8 at 17:54

















  • Thank you. This is exactly what i was needing and a great learning tool for me to use as I am still really new to SQL and its functionality. Much appreciated!.

    – Zack E
    Mar 8 at 17:54
















Thank you. This is exactly what i was needing and a great learning tool for me to use as I am still really new to SQL and its functionality. Much appreciated!.

– Zack E
Mar 8 at 17:54





Thank you. This is exactly what i was needing and a great learning tool for me to use as I am still really new to SQL and its functionality. Much appreciated!.

– Zack E
Mar 8 at 17:54













0














Simple!



Remove quotes from the order by clause. It should work. Your code should be like below.



`SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM 
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000714'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000713'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000715'
ORDER BY CIF Number`





share|improve this answer


















  • 2





    That won't work. If a column name is mixed case, or has a space in it, you will need to put it in double quotes

    – Paul Vernon
    Mar 8 at 18:47











  • agreed with your comment.

    – Sathvik Reddy
    Mar 8 at 22:07















0














Simple!



Remove quotes from the order by clause. It should work. Your code should be like below.



`SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM 
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000714'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000713'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000715'
ORDER BY CIF Number`





share|improve this answer


















  • 2





    That won't work. If a column name is mixed case, or has a space in it, you will need to put it in double quotes

    – Paul Vernon
    Mar 8 at 18:47











  • agreed with your comment.

    – Sathvik Reddy
    Mar 8 at 22:07













0












0








0







Simple!



Remove quotes from the order by clause. It should work. Your code should be like below.



`SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM 
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000714'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000713'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000715'
ORDER BY CIF Number`





share|improve this answer













Simple!



Remove quotes from the order by clause. It should work. Your code should be like below.



`SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM 
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000714'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000713'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000715'
ORDER BY CIF Number`






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 18:02









Sathvik ReddySathvik Reddy

265




265







  • 2





    That won't work. If a column name is mixed case, or has a space in it, you will need to put it in double quotes

    – Paul Vernon
    Mar 8 at 18:47











  • agreed with your comment.

    – Sathvik Reddy
    Mar 8 at 22:07












  • 2





    That won't work. If a column name is mixed case, or has a space in it, you will need to put it in double quotes

    – Paul Vernon
    Mar 8 at 18:47











  • agreed with your comment.

    – Sathvik Reddy
    Mar 8 at 22:07







2




2





That won't work. If a column name is mixed case, or has a space in it, you will need to put it in double quotes

– Paul Vernon
Mar 8 at 18:47





That won't work. If a column name is mixed case, or has a space in it, you will need to put it in double quotes

– Paul Vernon
Mar 8 at 18:47













agreed with your comment.

– Sathvik Reddy
Mar 8 at 22:07





agreed with your comment.

– Sathvik Reddy
Mar 8 at 22:07

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068172%2funexpected-result-sql-order-by%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page