Sqlite union and sort order - sql

select 'none', '0'
union
select * from category where id='2'
Can I retrieve the output of above sqlite query as 'none' should always be the first item ?, ie I want to block from a combined sort of two resultsets. plz help...

select * from
(
select 'none' as col1, '0' as col2 union select * from category where id='2'
) t
order by case when col1='none' then 0 else 1 end

I'd avoid using SELECT * in a union query
SELECT 'none', '0', 0 AS sort
UNION
SELECT col1, col2, 1 AS sort
FROM category
WHERE id = '2'
ORDER BY sort ASC

Related

Distinct values in cte table

I'm doing a query in SQL like this:
with cte as
(
select
t.ktokk, t.txt30,
case
when t.spras = 'P' then '1'
when t.spras = 'E' then '2'
else '3'
end as ord_ktokk
from
t077y t
order by
ord_ktokk
)
select *
from cte
and the result is that:
[Image]
Now I want to select only the distinct values in ktokk column. How can i do that?
thanks for the help

Count distinct letters in a string in bigquery

I have a string column in Biquery like:
select 'A'
union all (select 'ab')
union all (select 'abc')
union all (select 'aa')
union all (select 'aab')
I would like to count the number of distinct characters in every row of the column, in this case the results would be:
1
2
3
1
2
Can this be done in BigQuery? How?
How about this (assuming you don't want to differentiate between uppercase and lowercase)...
WITH data AS (select 'A' AS `val`
union all (select 'ab')
union all (select 'abc')
union all (select 'aa')
union all (select 'aab'))
SELECT `val`, 26 - LENGTH(REGEXP_REPLACE('abcdefghijklmnopqrstuvwxyz', '['||LOWER(`val`)||']', ''))
FROM `data`;
A simple approach is to use the SPLIT to convert your string to an array and UNNEST to convert the resulting array to a table. You may then use COUNT and DISTINCT to determine the number of unique characters as shown below:
with my_data AS (
select 'A' as col
union all (select 'ab')
union all (select 'abc')
union all (select 'aa')
union all (select 'aab')
)
select col, (SELECT COUNT(*) FROM (
SELECT DISTINCT element FROM UNNEST(SPLIT(col,'')) as element
)) n from my_data;
or simply
WITH my_data AS (
SELECT 'A' as col UNION ALL
SELECT 'ab' UNION ALL
SELECT 'abc' UNION ALL
SELECT 'aa' UNION ALL
SELECT 'aab'
)
SELECT
col,
(
SELECT
COUNT(DISTINCT element)
FROM
UNNEST(SPLIT(col,'')) as element
) cnt
FROM
my_data;
Like previous but using COUNT with DISTINCT
with my_data AS (
select 'A' as col
union all (select 'ab')
union all (select 'abc')
union all (select 'aa')
union all (select 'aab')
)
select col, COUNT(DISTINCT element) FROM
my_data,UNNEST(SPLIT(col,'')) as element
GROUP BY col
If the data is not quite huge, I would rather go with the user-defined functions to ease up the string manipulation across different columns
CREATE TEMP FUNCTION
get_unique_char_count(x STRING)
RETURNS INT64
LANGUAGE js AS r"""
str_split = new Set(x.split(""));
return str_split.size;
""";
WITH
result AS (
SELECT
'A' AS val
UNION ALL (
SELECT
'ab')
UNION ALL (
SELECT
'abc')
UNION ALL (
SELECT
'aa')
UNION ALL (
SELECT
'aab') )
SELECT
val,
get_unique_char_count(val) unique_char_count
FROM
result
RESULT:

Selecting distinct values within a a group

I want to select distinct values of one variable within a group defined by another variable. What is the easiest way?
My first thought was to combine group by and distinct but it does not work. I tried something like:
select distinct col2, col1 from myTable
group by col1
I have looked at this one here but can't seem to solve my problem
Using DISTINCT along with GROUP BY in SQL Server_
Table example
If your requirement is to pick distinct combinations if col1 and COL2 then no need to group by just use
SELECT DISTINCT COL1, COL2 FROM TABLE1;
But if you want to group by then automatically one record per group is displayed by then you have to use aggregate function of one of the columns i.e.
SELECT COL1, COUNT(COL2)
FROM TABLE1 GROUP BY COL1;
no need group by just use distinct
select distinct col2, col1 from myTable
create table t as
with inputs(val, id) as
(
select 'A', 1 from dual union all
select 'A', 1 from dual union all
select 'A', 2 from dual union all
select 'B', 1 from dual union all
select 'B', 2 from dual union all
select 'C', 3 from dual
)
select * from inputs;
The above creates your table and the below is the solution (12c and later):
select * from t
match_recognize
(
partition by val
order by id
all rows per match
pattern ( a {- b* -} )
define b as val = a.val and id = a.id
);
Output:
Regards,
Ranagal

Strange sort behaviour for numeric values in Varchar column in SQL Server

Can someone please explain this strange behaviour:
select a from (
select '1' as a
union all
select '2' as a
union all
select '-3' as a
) as b
order by a desc
select a from (
select '4' as a
union all
select '5' as a
union all
select '-3' as a
) as b
order by a desc
Result for query 1:
-3
2
1
Result for query 2:
5
4
-3
It looks like - character is ignored. I though, that SQL Server orders varchars based on ASCII code.
So expected result would be:
2
1
-3 //ascii - is before 0-9
and:
5
4
-3 //ascii - is before 0-9
I get same result if I add a letter before number:
select a from (
select 'a1' as a
union all
select 'a2' as a
union all
select '-a3' as a
) as b
order by a desc
select a from (
select 'a4' as a
union all
select 'a5' as a
union all
select '-a3' as a
) as b
order by a desc
Actual sort order in SQL Server depends totally on the active collation (either the default one, or a collation that is specified explicitly).
If e.g. you use a binary collation, you'll get what you were expecting for this case:
select a from (
select '1' as a
union all
select '2' as a
union all
select '-3' as a
) as b
order by a COLLATE Latin1_General_BIN desc
/* Result: 2, 1, -3 */
select a from (
select '4' as a
union all
select '5' as a
union all
select '-3' as a
) as b
order by a COLLATE Latin1_General_BIN desc
/* Result: 5, 4, -3 */
To see all collations, run this:
select * from sys.fn_helpcollations()
You should set the collation to Latin1_General_BIN like this :
select a from (
select '1' as a
union all
select '0' as a
union all
select '-1' as a
) as b
order by a COLLATE Latin1_General_BIN desc
If you use numbers instead of strings ...
select a from (
select 1 as a
union all
select 2 as a
union all
select -3 as a
) as b
order by a desc
... then the numbers are sorted as expected:
2
1
-3

Get Max for field in SQL according to the datatype

I am trying to get the MAX of a field in SQL. The field takes numeric and non numeric characters. If the field has the values A, B, C the result should be C. But when the field has for example A, B, C, 1, 2 the result should be 2.
Can someone please assist me on how I can achieve this??
Thank You
See Image
This works
SELECT TOP 1 IDValue
FROM
(
SELECT 'A' as IDValue, ISNUMERIC('A') tag
UNION
SELECT 'C', ISNUMERIC('C')
UNION
SELECT 'B', ISNUMERIC('B')
UNION
SELECT '1',ISNUMERIC('1')
UNION
SELECT '2',ISNUMERIC('2')
)tmp
ORDER BY tag DESC,IDValue DESC
Output
2
ISNUMERIC() returns 1 if expression is number else 0 . You can use it in ORDER BY to fetch max value.
Try this : May solve your problem at some level
DECLARE #T TABLE(data VARCHAR(2))
INSERT INTO #T VALUES('A'),('B'),('C'),('1'),('2')
SELECT TOP(1) data
FROM #T
ORDER BY CASE WHEN ISNUMERIC(data)=1
THEN 'Z'+data
ELSE data
END desc
here's the syntax for finding max value with SQL
SELECT MAX(column_name) FROM table_name;
you could refer to http://www.w3schools.com/sql/sql_func_max.asp for further reading and code testing
Do conditional ordering
select n from
(
select '1' as n union all
select '2' as n union all
select 'A' as n union all
select 'B' as n union all
select 'C' as n
) t
order by case when isnumeric(n)=1 then 1 else 2 end,n desc
select n from
(
select 'A' as n union all
select 'B' as n union all
select 'C' as n
) t
order by case when isnumeric(n)=1 then 1 else 2 end,n desc