COL1
COL2
COL3
A
B
A
C
D
C
for example lets say I have a dataset like this. I want to count the values, each value in multiple columns and same rows. As a result it has to say the count of the values I put into.
2A 1B and
2C 1D
Anyone can help?
You don't count values in a row, you count values in a column. So, you use sql to reformat your data in to a single column, then count the values in the usual way.
SELECT
column_value,
COUNT(*)
FROM
(
SELECT col1 AS column_value FROM your_table
UNION ALL
SELECT col2 AS column_value FROM your_table
UNION ALL
SELECT col3 AS column_value FROM your_table
)
AS pivoted
GROUP BY
column_value
ORDER BY
column_value
As I commented, here is the code for the same. Can be optimized a bit by removing multiple select statements.
Declare #val Varchar(MAX);
Select #val=COALESCE(#val + ' ' + result, result)
FROM (
SELECT CONCAT(cnt,col) result FROM (
SELECT col, count(*)cnt FROM (
SELECT col
FROM
(
SELECT col1, col2, col3
from #temp
) AS cp
UNPIVOT
(
col FOR cols IN (col1, col2, col3 )
) AS up)t
group by col)t)t2
select #val
Related
Here is a mock table
MYTABLE ROWS
PKEY 1,2,3,4,5,6
COL1 a,b,b,c,d,d
COL2 55,44,33,88,22,33
I want to know which rows have duplicated COL1 values:
select col1, count(*)
from MYTABLE
group by col1
having count(*) > 1
This returns :
b,2
d,2
I now want all the rows that contain b and d. Normally, I would use where in stmt, but with the count column, not certain what type of statement I should use?
maybe you need
select * from MYTABLE
where col1 in
(
select col1
from MYTABLE
group by col1
having count(*) > 1
)
Use a CTE and a windowed aggregate:
WITH CTE AS(
SELECT Pkey,
Col1,
Col2,
COUNT(1) OVER (PARTITION BY Col1) AS C
FROM dbo.YourTable)
SELECT PKey,
Col1,
Col2
FROM CTE
WHERE C > 1;
Lots of ways to solve this here's another
select * from MYTABLE
join
(
select col1 ,count(*)
from MYTABLE
group by col1
having count(*) > 1
) s on s.col1 = mytable.col1;
I have a table similar to this
Can I get help writing up a query which will join col1, col2 & col3 and give me a result as below
I've spent an hour trying to figure it out with my mediocre skills and have got to some point.
select col1, count(*)
from tableName
group by col1
But I can't figure out how to join all three cols.
try this one
select
col,
count(*)
from
(select
id,
col1 as col
from
<table_name>
union all
select
id,
col2
from
<table_name>
union all
select
id,
col3
from
<table_name>)
group by
col
You need to group by col of the union of the 3 columns:
select t.col, count(*)
from (
select col1 col from tablename
union all
select col2 from tablename
union all
select col3 from tablename
) t
group by t.col
You should use UNION to group values from all columns to one column. After that, you can count values
SELECT
col,
count(*) as cnt
FROM
(SELECT col1 as col FROM table1
UNION ALL
SELECT col2 as col FROM table1
UNION ALL
SELECT col2 as col FROM table1) as t
GROUP BY col
COUNT(DATECREATED)
88708
26625
17092
how to create a select query for viewing this three values in single column as different column names like
COUNT(DATECREATED) COUNT(DATECREATED) COUNT(DATECREATED)
88708 26625 17092
88708
88708
If you just want 1 row with 3 rows as columns, then use this. Here col is the alias for your count statement.
with tbl as
(select rownum as rno,col from
(your existing query) t
)
select (select col from tbl where rno=1) col1 ,
(select col from tbl where rno=2) col2,
(select col from tbl where rno=3 ) col3
from dual
If there are more rows and you want to use them as column, then read about Dynamic pivot in Oracle and you shall get your answer.
Finally i did like this.. Thanks all for quick reply
select sum(colname) as aliasname, sum(colname) as aliasname, sum(colname) as aliasname from
(select count(colname) as colname,0 as colname,0 as colname from cof
union all
select 0,count(colname),0 from cof where colname is not null
union all
select 0,0,count(colname) from cof where colname is not null);
I am trying to figure out how to use the values from a table as a select statement for example
table1 contains:
column- cl1
Value - numb
table2:
column - numb
values 1,2,3,4
i want to select cl1 (value: numb) and then use it to run a statement
select numb from table2
so far i have used
select (select cl1 from table1) from table2;
this returns numb 4 times but i want the actual values
the output that i expect is
1,2,3,4.
I want the query to select from table 1 which will return the field name and then use that field name(numb) as part of the select statement so expecting the end sql to look like:
select numb from table2;
However numb will be whatever is in table1;
You CAN do it, but you need to expand out your table to include by the list of possible columns that you are selecting with one row per column per source row. IF this is a large list of possibilities or a large data set....well, it ain't gonna be pretty.
For example:
With thedata as (
select 1 row_id, 11 col1, 12 col2, 13 col3 from dual union all
select 2 row_id, 21 col1, 22 col2, 23 col3 from dual union all
select 3 row_id, 31 col1, 32 col2, 33 col3 from dual union all
select 4 row_id, 41 col1, 42 col2, 43 col3 from dual )
, col_list as (
select 1 col_id, 'col1' col from dual union all
select 2 col_id, 'col2' col from dual union all
select 3 col_id, 'col3' col from dual )
select row_id, coldata
FROM (
-- here's where I have to mulitply the source data, generating one row for each possible column, and hard-coding that column to join to
SELECT row_id, 'col1' as col, col1 as coldata from thedata
union all
SELECT row_id, 'col2' as col, col2 as coldata from thedata
union all
SELECT row_id, 'col3' as col, col3 as coldata from thedata
) expanded_Data
JOIN col_list
on col_list.col = expanded_data.col
where col_id = :your_id;
Set the id to 2 and get:
ROW_ID COLDATA
1 12
2 22
3 32
4 42
So yes it can be done, but not truly dynamically as you need to be fully aware before-hand and hard-code the possible column name values that you are pulling from your table. If you need a truly dynamic select that may pick any column, or from any table, then you need to build your query dynamically and EXECUTE IMMEDIATE.
Edit - Add this caveat:
I should add also that this only works if all of the possible columns grabbed are of the same datatype, or you will need to cast them all to a common data type.
You can use a variable to store the value of numb, then reuse it in your SELECT statement, like this:
DECLARE #numb int
SET #numb = (SELECT cl1 from table2)
SELECT * from stat1 s WHERE s.numb = #numb
I am looking for a query where I can get unique values(5) in a table. For example.
The table consists of more 100+ columns. Is there any way I can get unique values.
I am using google big query and tried this option
select col1 col2 ... coln
from tablename
where col1 is not null and col2 is not null
group by col1,col2... coln
order by col1, col2... coln
limit 5
But problem is it gives zero records if all the column are null
Thanks
R
I think you might be able to do this in Google bigquery, assuming that the types for the columns are compatible:
select colname, colval
from (select 'col1' as colname, col1 as colvalue
from t
where col1 is not null
group by col1
limit 5
),
(select 'col2' as colname, col2 as colvalue
from t
where col2 is not null
group by col2
limit 5
),
. . .
For those not familiar with the syntax, a comas in the from clause means union all, not cross join in this dialect. Why did they have to change this?
Try This one, i hope it works
;With CTE as (
select * ,ROW_NUMBER () over (partition by isnull(col1,''),isnull(col2,'')... isnull(coln,'') order by isnull(col1,'')) row_id
from tablename
) select * from CTE where row_id =1