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
Related
Can someone explain why the below doesn't work?
((SELECT COUNT(*) FROM Table1) - (SELECT Count(Metric) FROM Table1)) as X
Count(*) will give me all the rows in the table and Count(Metric) will give me the non-null values in the Metric column. So the difference between these will give me the number of null values in the Metric column and I have labelled this column X. I just want the difference between the two in Column X but not sure why it isn't working.
By the way, I know I can get it to work via the below:
SELECT COUNT(*) as a, count(metric) as b, COUNT(*)-COUNT(metric) as c
You would need to select the result:
SELECT ((SELECT COUNT(*) FROM Table1) - (SELECT Count(Metric) FROM Table1)) as X
But it is simpler to use conditional aggregation:
SELECT SUM(CASE WHEN Metrics IS NULL THEN 1 ELSE 0 END) X FROM table1
A SELECT query needs to start with SELECT (or WITH or a parenthesis if the query is a compound query with a set operator such as UNION ALL).
One method is:
SELECT ((SELECT COUNT(*) FROM Table1) - (SELECT Count(Metric) FROM Table1)) as X
A better method is:
SELECT COUNT(*) - Count(Metric) as X
FROM Table1
Not sure about amazon-redshift, but in standard SQL I would just count the records where the field is null instead of counting all minus where they are not null.
SELECT COUNT(*) FROM Table1 WHERE Metric IS NULL;
I have a table in which i need to calculate total amount.But there are some rows which are getting repeated and i need to avoid those rows.
eg
Column1 Column2
1 10
2 20
3 30
1 10
2 20
3 30
The problem is i do not know how many times will the values be repeated.
How is it possible?
Use distinct clause in the inner query to avoid duplicate value
and then subquery to get total of column2
Like below -
select sum(column2) as totalamount
from
(
select distinct column1, column2
from tablename
) a
This would do
Select column1,avg(column2)
from tablename
group by column1
You can try:
SELECT
s.column1,
sum(s.column2)
FROM
(
SELECT DISTINCT
INNER.column1,
INNER.coulmn2
FROM <table> INNER
) s
GROUP BY s.column1;
This subquery should help you..
I have two queries that return one result each i.e one number
Select Count(*) as StockCountA from Table_A where dept='AAA'
Results
StockCountA
550
.
Select Count(*) as StockCountB from Table_B where dept='BBB'
Results
StockCountB
450
I wish to join the two results into one row record i.e
| StockCountA | StockCountB
| 550 | 450
You can use:
select
(Select Count(*) as StockCountA from Table_A where dept='AAA') as StockCountA,
(Select Count(*) as StockCountB from Table_B where dept='BBB') as StockCountB
Explanation: you can select single value as a field in a select statement, so you could write something like
select
x.*,
(select Value from Table_Y y) as ValueFromY
from
Table_X x
This will work only with scalar queries, meaning that the sub-query should have exactly 1 column, and at most 1 row. With 0 rows ValueFromY will return NULL and with more than 1 row, the query will fail.
An additional feature of select (in SQL Server, MySQL and probably others) is that you can select just values without specifying a table at all, like this:
Select
3.14 as MoreOrLessPI
You can combine both those facts to combine the two counts into a single result, by writing a query that looks like:
Select
(Select query that returns at most 1 row) as Result1,
(Select another query that returns at most 1 row) as Result2
This should give you the desired result:
SELECT * FROM(
(Select Count(*) as StockCountA from Table_A where dept='AAA') StockCountA ,
(Select Count(*) as StockCountB from Table_B where dept='BBB') StockCountB
);
While not always the best practice, it is possible to do a CROSS JOIN..
SELECT
COUNT(Table_A.SOME_COLUMN) as StockCountA
,COUNT(Table_B.SOME_COLUMN) as StockCountB
FROM Table_A, Table_B WHERE Table_A.dept='AAA' AND Table_B.dept='BBB'
Try below SQL :
select (Select Count(*) as StockCountA from Table_A where dept='AAA') StockCountA,
(Select Count(*) as StockCountB from Table_B where dept='BBB') StockCountB
Hope This Helps :)
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;
I am using a select count distinct to count the number of records in a column. However, I only want to count the records where the value of a different column is 1.
So my table looks a bit like this:
Name------Type
abc---------1
def----------2
ghi----------2
jkl-----------1
mno--------1
and I want the query only to count abc, jkl and mno and thus return '3'.
I wasn't able to do this with the CASE function, because this only seems to work with conditions in the same column.
EDIT: Sorry, I should have added, I want to make a query that counts both types.
So the result should look more like:
1---3
2---2
SELECT COUNT(*)
FROM dbo.[table name]
WHERE [type] = 1;
If you want to return the counts by type:
SELECT [type], COUNT(*)
FROM dbo.[table name]
GROUP BY [type]
ORDER BY [type];
You should avoid using keywords like type as column names - you can avoid a lot of square brackets if you use a more specific, non-reserved word.
I think you'll want (assuming that you wouldn't want to count ('abc',1) twice if it is in your table twice):
select count(distinct name)
from mytable
where type = 1
EDIT: for getting all types
select type, count(distinct name)
from mytable
group by type
order by type
select count(1) from tbl where type = 1
;WITH MyTable (Name, [Type]) AS
(
SELECT 'abc', 1
UNION
SELECT 'def', 2
UNION
SELECT 'ghi', 2
UNION
SELECT 'jkl', 1
UNION
SELECT 'mno', 1
)
SELECT COUNT( DISTINCT Name)
FROM MyTable
WHERE [Type] = 1