{"id":1391,"date":"2015-04-14T07:18:13","date_gmt":"2015-04-14T07:18:13","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1391"},"modified":"2015-08-06T12:20:35","modified_gmt":"2015-08-06T12:20:35","slug":"is-it-possible-in-sql-to-return-a-first-row-that-has-attribute-names-and-then-values","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/is-it-possible-in-sql-to-return-a-first-row-that-has-attribute-names-and-then-values\/","title":{"rendered":"Is it possible in SQL to return a first row that has attribute names and then values"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[1391]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>My <a href=\"http:\/\/stackoverflow.com\/questions\/7258119\/is-it-possible-in-sql-to-return-a-first-row-that-has-attribute-names-and-then-va\">question<\/a> was:<\/p>\n<p style=\"color: #222222;\">I am wondering if there is a way to write the SQL so that it would return me a result as usual but now, on the first row that would return also the attribute names.<\/p>\n<p style=\"color: #222222;\">To explain what I mean:<\/p>\n<p style=\"color: #222222;\">say you have a table &#8220;test&#8221; which has 2 attributes &#8220;id&#8221; and &#8220;name&#8221;:<\/p>\n<pre class=\"lang:default decode:true\">id name\r\n1  nik\r\n2  tst<\/pre>\n<p style=\"color: #222222;\">query:<\/p>\n<pre class=\"lang:default decode:true\">SELECT*FROM test;<\/pre>\n<p style=\"color: #222222;\">produces:<\/p>\n<pre class=\"lang:default decode:true\">1 nik\r\n2 tst<\/pre>\n<p style=\"color: #222222;\">but what I want it to return is this:<\/p>\n<pre class=\"lang:default decode:true\">id name\r\n1 nik\r\n2 tst<\/pre>\n<p style=\"color: #222222;\">Is this possible?<\/p>\n<p style=\"color: #222222;\"><strong>edit:<\/strong>\u00a0I am using PostreSQL<\/p>\n<p style=\"color: #222222;\">\u00a0The answer, by <a href=\"http:\/\/stackoverflow.com\/users\/330315\/a-horse-with-no-name\">a_horse_with_no_name<\/a>,\u00a0was:<\/p>\n<blockquote>\n<p style=\"color: #222222;\">You cannot return the names and the actual column values in a\u00a0<strong>single<\/strong>\u00a0result unless you give up on the real datatypes (which is probably not what you want).<\/p>\n<p style=\"color: #222222;\">Your example mixes character data and numeric data in the id column and Postgres will (rightfully) refuse to return such a result set.<\/p>\n<p style=\"color: #222222;\"><strong>Edit<\/strong>:<br \/>\nI tested the &#8220;union&#8221; solution given e.g. by JNK and it fails (as expected) on Postgres, Oracle and SQL Server precisely because of the non-matching datatypes. MySQL follows it&#8217;s usual habits of not throwing errors and simply converts everything to characters.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1391","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1391"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1391\/revisions"}],"predecessor-version":[{"id":1684,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1391\/revisions\/1684"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}