1"use strict";(self.webpackChunkdocumentation=self.webpackChunkdocumentation||[]).push([["2545"],{50047:function(e,n,s){s.r(n),s.d(n,{frontMatter:()=>l,toc:()=>o,default:()=>a,metadata:()=>t,assets:()=>c,contentTitle:()=>r});var t=JSON.parse('{"id":"query/sql/case","title":"CASE keyword","description":"CASE SQL keyword reference documentation.","source":"@site/documentation/query/sql/case.md","sourceDirName":"query/sql","slug":"/query/sql/case","permalink":"/docs/query/sql/case","draft":false,"unlisted":false,"editUrl":"https://github.com/questdb/documentation/edit/main/documentation/query/sql/case.md","tags":[],"version":"current","frontMatter":{"title":"CASE keyword","sidebar_label":"CASE","description":"CASE SQL keyword reference documentation."},"sidebar":"docs","previous":{"title":"ASOF JOIN","permalink":"/docs/query/sql/asof-join"},"next":{"title":"CAST","permalink":"/docs/query/sql/cast"}}'),d=s(85893),i=s(50065);let l={title:"CASE keyword",sidebar_label:"CASE",description:"CASE SQL keyword reference documentation."},r=void 0,c={},o=[{value:"Syntax",id:"syntax",level:2},{value:"Description",id:"description",level:2},{value:"Examples",id:"examples",level:2}];function h(e){let n={code:"code",h2:"h2",p:"p",pre:"pre",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",...(0,i.a)(),...e.components};return(0,d.jsxs)(d.Fragment,{children:[(0,d.jsx)(n.h2,{id:"syntax",children:"Syntax"}),"\n",(0,d.jsx)(n.pre,{children:(0,d.jsx)(n.code,{className:"language-questdb-sql",children:"CASE\n WHEN condition THEN value\n [WHEN condition THEN value ...]\n [ELSE value]\nEND\n"})}),"\n",(0,d.jsx)(n.h2,{id:"description",children:"Description"}),"\n",(0,d.jsxs)(n.p,{children:[(0,d.jsx)(n.code,{children:"CASE"})," goes through a set of conditions and returns a value corresponding to the\nfirst condition met. Each new condition follows the ",(0,d.jsx)(n.code,{children:"WHEN condition THEN value"}),"\nsyntax. The user can define a return value when no condition is met using\n",(0,d.jsx)(n.code,{children:"ELSE"}),". If ",(0,d.jsx)(n.code,{children:"ELSE"})," is not defined and no conditions are met, then case returns\n",(0,d.jsx)(n.code,{children:"null"}),"."]}),"\n",(0,d.jsx)(n.h2,{id:"examples",children:"Examples"}),"\n",(0,d.jsxs)(n.p,{children:["Tag each trade as bullish or bearish based on its side, using ",(0,d.jsx)(n.code,{children:"ELSE"})," as the\nfallback:"]}),"\n",(0,d.jsx)(n.pre,{children:(0,d.jsx)(n.code,{className:"language-questdb-sql",metastring:'title="CASE with ELSE" demo',children:"SELECT symbol, side,\n CASE\n WHEN side = 'buy' THEN 'bullish'\n ELSE 'bearish'\n END AS sentiment\nFROM trades\nLIMIT -40;\n"})}),"\n",(0,d.jsxs)(n.table,{children:[(0,d.jsx)(n.thead,{children:(0,d.jsxs)(n.tr,{children:[(0,d.jsx)(n.th,{children:"symbol"}),(0,d.jsx)(n.th,{children:"side"}),(0,d.jsx)(n.th,{children:"sentiment"})]})}),(0,d.jsxs)(n.tbody,{children:[(0,d.jsxs)(n.tr,{children:[(0,d.jsx)(n.td,{children:"BTC-USD"}),(0,d.jsx)(n.td,{children:"buy"}),(0,d.jsx)(n.td,{children:"bullish"})]}),(0,d.jsxs)(n.tr,{children:[(0,d.jsx)(n.td,{children:"ETH-USD"}),(0,d.jsx)(n.td,{children:"sell"}),(0,d.jsx)(n.td,{children:"bearish"})]}),(0,d.jsxs)(n.tr,{children:[(0,d.jsx)(n.td,{children:"BTC-USD"}),(0,d.jsx)(n.td,{children:"buy"}),(0,d.jsx)(n.td,{children:"bullish"})]}),(0,d.jsxs)(n.tr,{children:[(0,d.jsx)(n.td,{children:"SOL-USD"}),(0,d.jsx)(n.td,{children:"sell"}),(0,d.jsx)(n.td,{children:"bearish"})]})]})]}),"\n",(0,d.jsxs)(n.p,{children:["Without ",(0,d.jsx)(n.code,{children:"ELSE"}),", unmatched rows produce ",(0,d.jsx)(n.code,{children:"null"}),":"]}),"\n",(0,d.jsx)(n.pre,{children:(0,d.jsx)(n.code,{className:"language-questdb-sql",metastring:'title="CASE without ELSE" demo',children:"SELECT symbol, side,\n CASE\n WHEN side = 'buy' THEN 'bullish'\n END AS sentiment\nFROM trades\nLIMIT -40;\n"})}),"\n",(0,d.jsxs)(n.table,{children:[(0,d.jsx)(n.thead,{children:(0,d.jsxs)(n.tr,{children:[(0,d.jsx)(n.th,{children:"symbol"}),(0,d.jsx)(n.th,{children:"side"}),(0,d.jsx)(n.th,{children:"sentiment"})]})}),(0,d.jsxs)(n.tbody,{children:[(0,d.jsxs)(n.tr,{children:[(0,d.jsx)(n.td,{children:"BTC-USD"}),(0,d.jsx)(n.td,{children:"buy"}),(0,d.jsx)(n.td,{children:"bullish"})]}),(0,d.jsxs)(n.tr,{children:[(0,d.jsx)(n.td,{children:"ETH-USD"}),(0,d.jsx)(n.td,{children:"sell"}),(0,d.jsx)(n.td,{children:"null"})]}),(0,d.jsxs)(n.tr,{children:[(0,d.jsx)(n.td,{children:"BTC-USD"}),(0,d.jsx)(n.td,{children:"buy"}),(0,d.jsx)(n.td,{children:"bullish"})]}),(0,d.jsxs)(n.tr,{children:[(0,d.jsx)(n.td,{children:"SOL-USD"}),(0,d.jsx)(n.td,{children:"sell"}),(0,d.jsx)(n.td,{children:"null"})]})]})]})]})}function a(e={}){let{wrapper:n}={...(0,i.a)(),...e.components};return n?(0,d.jsx)(n,{...e,children:(0,d.jsx)(h,{...e})}):h(e)}},50065:function(e,n,s){s.d(n,{Z:()=>r,a:()=>l});var t=s(67294);let d={},i=t.createContext(d);function l(e){let n=t.useContext(i);return t.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function r(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(d):e.components||d:l(e.components),t.createElement(i.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.