i want to to select distinct values from multiple columns of same table with their count
example
and the output should be like this
Since the OP says in his comment that he uses MYSQL this should work
SELECT data,COUNT(data)
FROM
(
SELECT COL1 data
FROM tableso
UNION ALL
SELECT COL2
FROM tableso
UNION ALL
SELECT COL3
FROM tableso
UNION ALL
SELECT COL4
FROM tableso
UNION ALL
SELECT COL6
FROM tableso
UNION ALL
SELECT COL7
FROM tableso
) finaltable group by data;
SQL FIDDLE: http://sqlfiddle.com/#!2/1f8cf/10
I dont think the accepted answer works on MYSQL
UPDATE:
The op has changed his mind about the database(to MSSQL) and the accepted answer has both db versions
Since you are using Sql Server too, will suggest you this way
Use Table valued constructor to convert the columns to rows then count the name
This will avoid multiple table scan's.
SELECT name,
[count]=Count(1)
FROM yourtable
CROSS apply (VALUES (col1),(COL2),(col3),(col4),(COL6),(col7)) cs (name)
GROUP BY name
Note : This will not work in Mysql
SQLFIDDLE DEMO
Get all column values to one row and find the count
SQL SERVER
;WITH CTE AS
(
SELECT COL1 Name
FROM YOURTABLE
UNION ALL
SELECT COL2
FROM YOURTABLE
UNION ALL
SELECT COL3
FROM YOURTABLE
UNION ALL
SELECT COL4
FROM YOURTABLE
UNION ALL
SELECT COL6
FROM YOURTABLE
UNION ALL
SELECT COL7
FROM YOURTABLE
)
SELECT DISTINCT Name,COUNT(Name) OVER(PARTITION BY Name) [COUNT]
FROM CTE
MYSQL
SELECT Name,COUNT(*) [COUNT]
(
SELECT COL1 Name
FROM #TEMP
UNION ALL
SELECT COL2
FROM #TEMP
UNION ALL
SELECT COL3
FROM #TEMP
UNION ALL
SELECT COL4
FROM #TEMP
UNION ALL
SELECT COL6
FROM #TEMP
UNION ALL
SELECT COL7
FROM #TEMP
)TAB
GROUP BY Name
Related
In my MySql database, I want to create select query which should give output like this:
in my select query i want a column output as 1, if the column value present in a list returned by a select query else 0 .
Select col1,col2,
,IF col3 IN
((select col from tabl2 ),1,0)AS col5
from tbl1.
Thanks in Advance
SELECT col1, col2,
IF col3 IN ((select col from tabl2 ),1,0) AS col5
FROM tbl1
Using IF and subquery in MySQL
SELECT table1.column1, table1.column2,
(
SELECT IF (
(SELECT column3 FROM table1 WHERE column3 IN (SELECT column FROM table2)), 1, 0
)
) AS column_output
FROM table1
In general:
SELECT col1,
col2,
CASE WHEN col3 IN (select col from tabl2)
THEN 1
ELSE 0
END AS col5
FROM tbl1
Specific for MySQL:
SELECT col1,
col2,
col3 IN (select col from tabl2) AS col5
FROM tbl1
The database scheme consists:
Table1(code, col1, col2, col3, col4, col5)
What to do is:
For the Table1 with the maximal code value from Table1 table, obtain all its characteristics (except for a code) in two columns:
The name of the characteristic (a name of a corresponding column in the PC table);
Value of the characteristic.
I don't have any idea how to get the table column names in my result column set.
The final result will look like:
chr value
col1 133
col2 80
col3 28
col4 2
col5 50
This is an unpivot operation. The simplest way is using union all. However, the following is generally more efficient:
select (case when n.n = 1 then 'col1'
when n.n = 2 then 'col2'
when n.n = 3 then 'col3'
when n.n = 4 then 'col4'
when n.n = 5 then 'col5'
end) as chr,
(case when n.n = 1 then col1
when n.n = 2 then col2
when n.n = 3 then col3
when n.n = 4 then col4
when n.n = 5 then col5
end) as value
from table t cross join
(select 1 as n union all select 2 union all select 3 union all select 4 union all select 5
) n;
This is more efficient when your table is big or a complicated subquery.
The union all version is:
select 'col1', col1 from table t union all
select 'col2', col2 from table t union all
select 'col3', col3 from table t union all
select 'col4', col4 from table t union all
select 'col5', col5 from table t;
SELECT 'col1', MAX(col1) FROM table1
UNION
SELECT 'col2', MAX(col2) FROM table1
UNION
...
SELECT 'cd' as chr, cd as value
FROM pc
WHERE code = (SELECT max(code) FROM pc)
UNION
SELECT 'model' as chr, cast(model as varchar)as value
FROM pc
WHERE code = (SELECT max(code) FROM pc)
UNION
SELECT 'speed' as chr,cast(speed as varchar) as value
FROM pc
WHERE code = (SELECT max(code) FROM pc)
UNION
SELECT 'ram' as chr, cast(ram as varchar) as value
FROM pc
WHERE code = (SELECT max(code) FROM pc)
UNION
SELECT 'hd' as chr, cast(hd as varchar) as value
FROM pc
WHERE code = (SELECT max(code) FROM pc)
UNION
SELECT 'price' as chr,cast(price as varchar) as value
FROM pc
WHERE code = (SELECT max(code) FROM pc)
Select char, value
From
(Select code,
cast(speed as varchar(20)) speed,
cast(ram as varchar(20)) ram,
cast(hd as varchar(20)) hd,
cast(model as varchar(20)) model,
cast(cd as varchar(20)) cd,
cast(price as varchar(20)) price
FROM pc
) src
UNPIVOT
(value For char in (speed,ram,hd,model,cd,price)
) As unpvt
where
code in (Select max(code) from PC)`
How to obtain a distinct combination of col1 and col2 where former is integer and later is string in mysql
Use GROUP BY
SELECT col1,col2 FROM TABLE
GROUP BY col1,col2
Using distinct
select distinct
col1,col2
from
tbl
;
or
Using group by
select
col1,col2
from
tbl
group by col1, col2
;
I need to using both "union" and "into" in a query. These two versions works ok
SELECT x.* INTO
NewTABLE FROM
(SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2) x
Or
SELECT x.* INTO
NewTABLE FROM
(SELECT val1, val2 FROM TABLE1 UNION SELECT val1, val2 FROM TABLE2) x
But what i need is something like this
SELECT x.* INTO
NewTABLE FROM
(SELECT val1, sum(Iif(val2<0,0,val2)) as PositiveVal2 FROM TABLE1
UNION SELECT val1, sum(Iif(val2<0,0,val2)) as PositiveVal2 FROM TABLE2) x
It looks like the sum and/or Iif is the problem. How should I solve this problem!?
You forgot the GROUP BY in your union selects.
SELECT x.* INTO
NewTABLE FROM
(SELECT val1, sum(Iif(val2<0,0,val2)) as PositiveVal2
FROM TABLE1 GROUP BY Val1
UNION
SELECT val1, sum(Iif(val2<0,0,val2)) as PositiveVal2
FROM TABLE2 GROUP BY Val1) x
You must either aggregate or GROUP BY all select fields in an aggregate query.
I have a table in MySQL:
Col1 | Col2
a A
a B
c C
a B
i want to create a table like this:
col1 | col2 | freq
a A 0.33
a B 0.67
col1 is a specified item in Col1. col2 is distinct item that has occured with the specified item(i.e. a). freq column is the frequency of appearence of item in col2.
Can someone give me a hint of how to create such a query? Thanks a lot.
try this:
Select A.Col1, A.Col2, A.Count1 * 1.0 / B.Count2 As Freq
From (
Select Col1, Col2, Count(*) As Count1
From YourTableName
Group By Col1, Col2
) As A
Inner Join (
Select Col1, Count(*) As Count2
From YourTableName
Group By Col1
) As B
On A.Col1 = B.Col1
You can also use this which is coded in SQL server
DECLARE #Count INT;
SELECT #Count = COUNT(1) FROM YourTableName WHERE Col1 = 'a'
SELECT Col1, Col2, CAST(COUNT(1)* 1.00 /#Count AS DECIMAL(4,2) ) AS Frequency
FROM YourTableName
WHERE Col1 = 'a'
GROUP BY Col1, Col2
this way you have a better performance
With window functions
SELECT Col1, Col2, Count1*1.0 / Count2 AS freq
FROM (
SELECT
Col1,
Col2,
COUNT() OVER(PARTITION BY Col1, Col2) AS Count1,
COUNT() OVER(PARTITION BY Col1) AS Count2
FROM YourTableName
)
GROUP BY Col1, Col2