I have columns a,b in table x.And i want to change this columns data into rows.
it is possible to have duplicate vales in table but in columns to row change only distinct values should come.
E.G:
a b
1 2
1 11
3 4
5 6
7 8
9 10
......etc
the result 1 (query 1) should be 1-2,1-11,3-4,5-6,7-8,9-10.....etc
The result 2 (query 2) should b 1,3,5,7,9....etc(only one 1 must come as we have duplicate data for column a)
how can i achieve this in oracle SQL.
Please help.
For Oracle 11 use function listagg() and in first query concatenate columns, in second - select distinct values at first.
Query 1:
select listagg(a||'-'||b, ',') within group (order by a, b) result from t
RESULT
------------------------------
1-2,1-11,3-4,5-6,7-8,9-10
Query 2:
select listagg(a, ',') within group (order by a) result
from (select distinct a from t)
RESULT
------------------------------
1,3,5,7,9
For older versions you can use wmsys.wm_concat.
Related
I have a use case where I need to take max value from different columns from a table in sparksql.
Below is a sample table -
I want to take the max of values from columns a, b and c without using the union clause.
Below is the SL query I executed -
SELECT (
SELECT MAX(myval)
FROM (VALUES (a),(b),(c)) AS temp(myval)
) AS MaxOfColumns
FROM
table
But this is throwing an error - "cannot evaluate expression outer() in inline table definition; line 3 pos 16"
Could you please help me with this?
array_max
with t(id,a,b,c) as (select stack(2 ,100,1,2,3 ,200,5,6,4))
select *, array_max(array(a,b,c)) as MaxOfColumns
from t
id
a
b
c
MaxOfColumns
100
1
2
3
3
200
5
6
4
6
S.No Name Path
1 a a/b/c
2 b x/y/z
3 a a/b/c
4 c t/y/z
5 b x/y/z
in my sql to find repeated values
select Name,Path,Count(Name), group_concat(S.No) as Concatlist from tab
group by Name, Path
OutPut will be
a a/b/c 2 1,3
b x/y/z 2 2,5
c t/y/z 1 4
Same query i want in ms sql server..
Please notice concatlist S.No column not using in group by....
You can use string_agg() in the more recent versions of SQL Server:
select name, path, count(*),
string_agg(s_no order by s_no) as s_nos
from t
group by name, path;
What is the result of:
WITH Tbl AS (SELECT 5 AS A UNION SELECT 6 AS A)
SELECT COUNT(*) AS Tbl FROM Tbl AS A, Tbl AS B, Tbl AS C;
I know the result is supposed to be 8 but I don't know why. Also when I change both values (the 5 or 6) to the same thing it returns a table with the value 1 instead of 8 but all other instances it returns 8 no matter what numbers if they are different. I tested it out with an online sql executor.
Here is what the query does:
the common table expression (the subquery within the with clause) generates a derived table made of two rows
then, in the outer query, the from clause generates a cartesian product of this resultset twice: that's a total of 8 rows (2 * 2 * 2)
the select clause counts the number of rows - that's 8
The content of the rows in the with clause does not matter: this 5 and 6 could very well be foo and bar, or null and null, the result would be the same.
What makes a difference is the number of rows that the with clause generates. If it was generating just one row, you would get 1 as a result (1 * 1 * 1). If it was generating 3 rows, you would get 27 - and so on.
This expression:
WITH Tbl AS (SELECT 5 AS A UNION SELECT 6 AS A)
creates a (derived) table with two rows.
This expression:
WITH Tbl AS (SELECT 5 AS A UNION SELECT 5 AS A)
creates a (derived) table with one row, because UNION removes duplicates.
The rest of the query just counts the number of rows in the 3-way Cartesian product, which is either 111 or 222.
I am using T-SQL.
Say if I have the following
Value Nbr
----- ---
one 6
one 7
one 8
two 6
two 7
three 5
three 3
three 2
In the above table, I need to find which group does not have 6 in it.
In this case, it is three as it does not have 6 in it.
What would be the best approach to do this?
I tried:
select Value from tbl1
where nbr <> 6
group by Value
but did not get the intended result.
select distinct value
from tbl1
where value not in
(
select distinct value
from tbl1
where nbr = 6
)
I'm trying to count the number of rows that have distinct values in both of the columns "a" and "b" in my Sybase ISQL 9 database.
What I means is, the following dataset will produce the answer "4":
a b
1 9
2 9
3 8
3 7
2 9
3 7
Something like the following syntax would be nice:
SELECT COUNT(DISTINCT a, b) FROM MyTable
But this doesn't work.
I do have a solution:
SELECT COUNT(*) FROM
(SELECT a, b
FROM MyTable
WHERE c = 'foo'
GROUP BY a, b) SubTable
But I was wondering if there is a neater way of constructing this query?
How about:
SELECT COUNT(*)
FROM (SELECT DISTINCT a, b FROM MyTable)
For more information on why this can't be done in a simpler way (besides concatenating strings as noted in a different answer), you can refer to the this Google Answers post: Sql Distinct Count.
You could concatenate a and b together into 1 string like this (TSQL, hopefully something very similar in Sybase:
SELECT COUNT(DISTINCT(STR(a) + ',' + STR(b)))
FROM #YourTable