tsql

Read any JSON into list of key-value pairs (EAV format) in SQL Server

佐手、 提交于 2020-05-15 07:56:33
问题 Looking for a way to read any JSON without knowing the internals with SQL Server's JSON methods, I came up with an approach I'd like to share. Here is the question bringing up this issue. The question is: How can I transform an unknown JSON into a structured EAV format while keeping all the information about sort order and nest levels. The ideal output should carry the original row's id as the entity , the Json's key and value as the attribute and value together with the JsonPath for the

IF EXISTS (SELECT 1…) vs IF EXITS (SELECT TOP 1 1…)

帅比萌擦擦* 提交于 2020-05-13 11:39:27
问题 This is a purely academic question. Are these 2 statements effectively identical? IF EXISTS (SELECT TOP 1 1 FROM Table1) SELECT 1 ELSE SELECT 0 Versus IF EXISTS (SELECT 1 FROM Table1) SELECT 1 ELSE SELECT 0 回答1: If you view the execution plan for these queries you can see that they are identical. Good coding practices would ask that you leave out the "TOP 1" but they should run identical either way. 来源: https://stackoverflow.com/questions/51049683/if-exists-select-1-vs-if-exits-select-top-1-1

SQL SELECT First Day and Last Day of the Month.

雨燕双飞 提交于 2020-05-12 04:39:12
问题 Dearest professionals, I have a query built to get the first and last day of the current month, but I'm having an issue with the time stamp for the First Day of the month. declare @FirstDOM datetime, @LastDOM datetime set @FirstDOM = (select dateadd(dd,-(day(getdate())-1),getdate()) ) set @LastDOM = (select dateadd(s,-1,dateadd(mm,datediff(m,0,getdate())+1,0))) Since it's February of 2015, I would like to get results of: @FirstDOM = 2015-02-01 00:00:00.000 @LastDOM = 2015-02-28 23:59:59.000

CONCAT(column) OVER(PARTITION BY…)? Group-concatentating rows without grouping the result itself

末鹿安然 提交于 2020-05-11 04:46:07
问题 I need a way to make a concatenation of all rows (per group) in a kind of window function like how you can do COUNT(*) OVER(PARTITION BY...) and the aggregate count of all rows per group will repeat across each particular group. I need something similar but a string concatenation of all values per group repeated across each group. Here is some example data and my desired result to better illustrate my problem: grp | val ------------ 1 | a 1 | b 1 | c 1 | d 2 | x 2 | y 2 | z And here is what I

CONCAT(column) OVER(PARTITION BY…)? Group-concatentating rows without grouping the result itself

半城伤御伤魂 提交于 2020-05-11 04:40:49
问题 I need a way to make a concatenation of all rows (per group) in a kind of window function like how you can do COUNT(*) OVER(PARTITION BY...) and the aggregate count of all rows per group will repeat across each particular group. I need something similar but a string concatenation of all values per group repeated across each group. Here is some example data and my desired result to better illustrate my problem: grp | val ------------ 1 | a 1 | b 1 | c 1 | d 2 | x 2 | y 2 | z And here is what I

ORDER/SORT Column mixed number and number prefixed by char

一个人想着一个人 提交于 2020-05-09 02:37:52
问题 I have a varchar column conteining a code, this code could by only numbers, or number prefixed by a char for example i have a column containing this data : +------+ | Code | +------+ | 1 | | C1 | | 2 | | 3 | | C3 | | F3 | | F1 | | F17 | | C9 | | C10 | | C47 | | C100 | | C134 | | A234 | |C1245 | | 10 | | 100 | +------+ And so on ... I want to sort this column by this rules : Only numeric code Prefixed code with the letter part order alfanumerically and numeric part ordered as a number I want

Why does a Recursive CTE in Transact-SQL require a UNION ALL and not a UNION?

我只是一个虾纸丫 提交于 2020-05-07 05:24:07
问题 I get that an anchor is necessary, that makes sense. And I know that a UNION ALL is needed, if your recursive CTE doesn't have one, it just doesn't work... but I can't find a good explanation of why that is the case. All the documentation just states that you need it. Why can't we use a UNION instead of a UNION ALL in a recursive query? It seems like it would be a good idea to not include duplicates upon deeper recursion, doesn't it? Something like that should already be working under the

Why does a Recursive CTE in Transact-SQL require a UNION ALL and not a UNION?

你离开我真会死。 提交于 2020-05-07 05:23:35
问题 I get that an anchor is necessary, that makes sense. And I know that a UNION ALL is needed, if your recursive CTE doesn't have one, it just doesn't work... but I can't find a good explanation of why that is the case. All the documentation just states that you need it. Why can't we use a UNION instead of a UNION ALL in a recursive query? It seems like it would be a good idea to not include duplicates upon deeper recursion, doesn't it? Something like that should already be working under the

Why does a Recursive CTE in Transact-SQL require a UNION ALL and not a UNION?

雨燕双飞 提交于 2020-05-07 05:23:17
问题 I get that an anchor is necessary, that makes sense. And I know that a UNION ALL is needed, if your recursive CTE doesn't have one, it just doesn't work... but I can't find a good explanation of why that is the case. All the documentation just states that you need it. Why can't we use a UNION instead of a UNION ALL in a recursive query? It seems like it would be a good idea to not include duplicates upon deeper recursion, doesn't it? Something like that should already be working under the

Why does a Recursive CTE in Transact-SQL require a UNION ALL and not a UNION?

▼魔方 西西 提交于 2020-05-07 05:23:06
问题 I get that an anchor is necessary, that makes sense. And I know that a UNION ALL is needed, if your recursive CTE doesn't have one, it just doesn't work... but I can't find a good explanation of why that is the case. All the documentation just states that you need it. Why can't we use a UNION instead of a UNION ALL in a recursive query? It seems like it would be a good idea to not include duplicates upon deeper recursion, doesn't it? Something like that should already be working under the