I've got a table with 2 columns with keywords, and I need to count the occurrence of them.
I can do that separately, one column at the time, and add the totals later, with a regular count,
select count (id), kw1 from mytable group by kw1
and the same for kw2, but I need to get the info straight from the db.
So the table is something like:
id kw1 kw2
1 a b
2 c d
3 b
4 e a
so the idea is to get how many times has been used each keyword, so the result should be something like:
'a' 2
'b' 2
'c' 1
'd' 1
'e' 1
Thanks in advance
PS: Sorry, I forgot, but just in case, I'm working on Oracle 10g
Try this:
SELECT kw, COUNT(kw)
FROM
(
SELECT "kw1" AS kw FROM table1
UNION ALL
SELECT "kw2" FROM table1
) t
WHERE kw IS NOT NULL
GROUP BY kw
ORDER BY KW;
SQL Fiddle Demo
This will give you:
KW COUNT(KW)
a 2
b 2
c 1
d 1
e 1
It shoud looks something like this.
SELECT kw,SUM(kw)
FROM(
(SELECT kw1 AS kw, COUNT(kw1)
FROM table
WHERE kw1 IS NOT NULL GROUP BY kw1) skw1
UNION ALL
(SELECT kw2, COUNT(kw2)
FROM table
WHERE kw2 IS NOT NULL GROUP BY kw2) skw2
)
GROUP BY kw
ORDER BY kw
Previous answers don't perform a SUM operation after performing the UNION.
use union All to combine two column data into one column & then count occurence
SELECT cnt, COUNT(cnt)
FROM
(
SELECT kw1 AS cnt FROM table
UNION ALL
SELECT kw2 FROM table
) t
WHERE cnt IS NOT NULL
GROUP BY cnt
ORDER BY cnt;
Related
I have a simple SQLite table which has just one ID column.
I have some variable IDs that may be duplicates of each other like: 1,2,3,4,3,1 (These IDs are just examples, there could be hundreds of them).
And I have a simple query as follows:
SELECT ID FROM TABLE WHERE ID in (1,2,3,4,3,1)
In the usual case the answer contains only 4 rows with ids 1,2,3,4. Is there any way to force SQLite to return rows in the order of the request (1,2,3,4,3,1) even if they are duplicates?
I have n IDs in my query and I want n rows in return even if they are duplicates.
Edit: The Table Definition is:
CREATE TABLE TEST(ID TEXT PRIMARY KEY)
You can use left join:
select t.*
from (select 1 as id, 1 as ord union all
select 2 as id, 2 as ord union all
select 3 as id, 3 as ord union all
select 4 as id, 4 as ord union all
select 3 as id, 5 as ord union all
select 1 as id, 6 as ord
) ids left join
t
on t.id = ids.id
order by ids.ord;
I have a table something like this:
ID Marks Weightatthistime
1 5 44
1 5 43
1 5 41
2 4 39
2 4 38
I want the total marks obtained by the IDs, so I want my query to return: 9.
I know I could do a:
select ID, sum(Marks) from table group by 1
but I simply just want the total sum and not another mini table. The table is aggregated at this level and there's no way for me to change it.
Use subquery :
select id, sum(marks)
from (select distinct id, marks
from table t
) t
group by id;
If you want only sum then use only sum(marks) :
select sum(marks)
from (select distinct id, marks
from table t
) t;
use distinct
select sum( distinct Marks) from table
with cte as
(
select 1 id, 5 as m union all
select 1,5 union all
select 1,5 union all
select 2,4 union all
select 2,4
) select sum(distinct m) from cte
but if multiple user have same number then follow subquery method that #Yogesh shown
output
9
I believe this should work with any ANSI SQL engine.
Select ID, Marks, Weightatthistime, (Select Sum(Marks) From Table1) SumAllMarks
From Table1
This will put 9 on each record.
SELECT sum(DISTINCT marks) FROM table;
You can use this query to get the sum of the distinct mark values.
In essence I have one table with two columns
One Two
-----------
A B
B C
C D
and I want to count the number of A's through D.
Resulting
Letter Count
---------------
A 1
B 2
C 2
D 1
My code now is
Select one, count("") from table
group by one
union
Select two, count("*") from table
group by two
Right now I am getting
Letter Count
---------------
A 1
B 1
B 1
C 1
C 1
D 1
How do I fix it?
Try this
SELECT Count(a),
a
FROM (SELECT cola a
FROM table
UNION ALL
SELECT colb a
FROM table) c
GROUP BY a
select letter, sum(total) from
(
Select one as letter, count(1) as total from tablename
group by one
union all
Select two as letter, count(1) as total from tablename
group by two) as t1
group by t1.letter
order by t1.letter asc
there is no reason to group twice.
select letter, count(*) as total_cnt
from
(
Select one as letter from table
union all
Select two as letter from table
)
group by letter;
You can do this
SELECT LETTER,
count(*) AS _count
FROM (
SELECT One AS Letter
FROM Test
UNION ALL
SELECT Two AS Letter
FROM Test
) T1
GROUP BY LETTER
How do I get an output from table of fields A, B, C to display
A | Count(A)
where the number of rows = distinct instances of A and the sum of the values for count(A) equals total number of rows in the database?
SELECT A, COUNT(1) FROM your_table_name GROUP BY A;
You can use the DISTINCT keyword inside the COUNT
Like this:
COUNT(DISTINCT A)
Like AI mentioned a dataset and the final outcome would be helpful. From the small example you gave, i think this might be what you are looking for
SELECT Field_A, Count(*) FROM table_name GROUP BY Field_A
UNION ALL
SELECT Field_B, Count(*) FROM table_name GROUP BY Field_B
UNION ALL
SELECT Field_C, Count(*) FROM table_name GROUP BY Field_C
The output for that would be something similiar to depending on how many unique values you have in Field_A, Field_B, Field_C
Value_In_A1 - 10
Value_In_A2 - 11
Value_In_A3 - 15
Value_In_A4 - 8
Value_In_B1 - 9
Value_In_C1 - 25
Value_In_C2 - 2
Suppose there is a table
_ _
a 1
a 2
b 2
c 3
c 4
c 1
d 2
e 5
e 6
How can I select distinct minimum value of all the rows of each group?
So the expected result here is:
_ _
a 1
b 2
c 1
d 2
e 5
EDIT
My actual table contains more columns and I want to select them all. The rows differ only in the last column (the second one in the example). I'm new to SQL and possibly my question is ill-formed in it initial view.
The actual schema is:
| day | currency ('EUR', 'USD') | diff (integer) | id (foreign key) |
The are duplicate pairs (day, currency) that differ by (diff, id). I want to see a table with uniquer pairs (day, currency) with a minimum diff from the original table.
Thanks!
in your case it's as simple as this:
select column1, min(column2) as column2
from table
group by column1
for more than two columns I can suggest this:
select top 1 with ties
t.column1, t.column2, t.column3
from table as t
order by row_number() over (partition by t.column1 order by t.column2)
take a look at this post https://stackoverflow.com/a/13652861/1744834
You can use the ranking function ROW_NUMBER() to do this with a CTE. Especially, if there are more column other than these two column, it will give the distict values like so:
;WITH RankedCTE
AS
(
SELECT *, ROW_NUMBER() OVER(PARTITION BY column1 ORDER BY Colmn2 ) rownum
FROM Table
)
SELECT column1, column2
FROM RankedCTE
WHERE rownum = 1;
This will give you:
COLUMN1 COLUMN2
a 1
b 2
c 1
d 2
e 5
SQL Fiddle Demo
SELECT ColOne, Min(ColTwo)
FROM Table
GROUP BY ColOne
ORDER BY ColOne
PS: not front of a,machine, but give above a try please.
select MIN(col2),col1
from dbo.Table_1
group by col1