T-SQL Writing a Count Statement to find two values - sql

I am trying to get two columns to appear. I have made a union of number of tables together. These tables then appear in one table now.
After this table I know need to do a summary count of one column.
This column contains two values. So i require to get count on text value 1 and text value 2 in the column.
select count (column_name) as column_name
FROM table name
where column_name = 'value1'
But i am not sure how to add value 2 into this statement? Any help be great. Much appreciated.

You can use pivot, but I think conditional aggregation is easier in this case:
select sum(case when column_name = 'value1' then 1 else 0 end) as value1,
sum(case when column_name = 'value2' then 1 else 0 end) as value2
from table name;
If you can live with the values on two rows instead of in two columns, use group by:
select column_name, count(*)
from table name
group by column_name;

I not sure what you want but whatever I understand, I think this will help you -
select
Sum ( case when column_name = 'value1' then 1 else 0 end) as CountValue1,
Sum ( case when column_name = 'value2' then 1 else 0 end) as CountValue2
FROM table name

select column_name, count (*)
FROM
(
select column_name from table1
union all
select column_name from table2
) src
group by column_name
where column_name in ( 'value1' ,'value2')

Related

Redshift: Analysis of all columns with null values in all the tables in a database

Need to find the counts of the null and not null values in all columns in all the tables in my database. With below query I can get the info for a single table.
SELECT
'A' as col_name,
COUNT(*) - COUNT(A) as nul_val,
COUNT(A) as nnul_val
FROM table1
UNION
SELECT
'B' as col_name,
COUNT(*) - COUNT(B) as nul_val,
COUNT(B) as nnul_val
FROM table1
I can query information_schema to get list of column names and tables names.
SELECT column_name, table_name from information_schema.columns;
How do I pass the column_name and table_name values from here to my main query? My database is Redshift and it has no provision for variables. Would most likely need to use python UDFs but I'm not sure how to write them for my case.
Python UDFs won't help you - they can only return a single value.
You will need to write a program external to Redshift to collect a list of tables and their columns, then run a query against each table and the table's specific columns.
By the way, there might be a slightly easier way to count the nulls, like this:
SELECT
SUM(CASE WHEN column1 IS NULL THEN 1 END) as column1_null_count,
SUM(CASE WHEN column1 IS NOT NULL THEN 1 END) as column1_not_null_count,
SUM(CASE WHEN column2 IS NULL THEN 1 END) as column2_null_count,
SUM(CASE WHEN column2 IS NOT NULL THEN 1 END) as column2_not_null_count
FROM table

SQL SELECT from one of two columns where column value is not equal to something

if I have a table with two columns, and the value for one of them will be known, is there a way to SELECT the value that is not equal to the known value and cast it as another column name?
For instance
columna columnb
1 5
3 1
4 1
1 7
I want to query both columns in the table above for all values not equal to 1, and return the list in a single column called column (or similar), i.e. the resultant table should be:
column
3
4
5
7
I think you just want:
select distinct col
from t cross join
(values (columna), (columnb)) v(col)
where col <> 1;
This will capture situations where both columns are not "1".
If your intention is something along the lines of "the other talker" in a chat, then:
select t.*, (case when columna <> 1 then columna else columnb end) as col
from t
where 1 in (columna, columnb);
You are looking for:
Field alias.
Where clause.
Your query:
SELECT
columnb as [column] --here the alias
FROM
yourTable
WHERE
columnb <> 1 or columnea <>1 --here the where clause
Notice: you can use and or or operator in where clause.
Quoting t-sql select docs:
column_ alias
Is an alternative name to replace the column name in the query result set. For example, an alias such as Quantity, or Quantity to Date, or Qty can be specified for a column named quantity.
Aliases are used also to specify names for the results of expressions, for example:
SELECT AVG(UnitPrice) AS [Average Price]
FROM Sales.SalesOrderDetail;
Edited due to OP comments:
To get values from both columns, the easiest way for you is a UNION:
SELECT
columnb as [column] --here the alias
FROM
yourTable
WHERE
columnb <> 1 --here the where clause
UNION ALL
SELECT
columna as [column] --here the alias
FROM
yourTable
WHERE
columna <> 1 --here the where clause

How to fetch column names count from Tables based on columns passed into IN clause

I am trying to fetch column count from all_tab_columns for particular columns passed into IN clause like:
select count(COLUMN_NAME)
from ALL_TAB_COLUMNS
WHERE owner='SA' and COLUMN_NAME IN ('CASE_REPORTER2SITE', 'PRIMARY2BUS_ORG')
GROUP BY COLUMN_NAME
HAVING COLUMN_NAME IN ('CASE_REPORTER2SITE', 'PRIMARY2BUS_ORG')
ORDER BY DECODE (COLUMN_NAME, 'CASE_REPORTER2SITE', 1 'PRIMARY2BUS_ORG', 2)
Here when both the columns exist in the database, It gives me count of both columns in 2 rows.
RESULT:
COUNT(COLUMN_NAME)
------------------
2
4
But, when I pass one existing and 1 non existing column like:
select count(COLUMN_NAME)
from ALL_TAB_COLUMNS
WHERE owner='SA' and COLUMN_NAME IN ('CASE_XYZ', 'PRIMARY2BUS_ORG')
GROUP BY COLUMN_NAME
HAVING COLUMN_NAME IN ('CASE_XYZ', 'PRIMARY2BUS_ORG')
ORDER BY DECODE (COLUMN_NAME, 'CASE_XYZ', 1 'PRIMARY2BUS_ORG', 2)
(Assume CASE_XYZ does not exist).
It gives me count result in 1 row.
RESULT:
COUNT(COLUMN_NAME)
------------------
4
Expected Result:
COUNT(COLUMN_NAME)
------------------
0
4
How to get the count as 0 for the particular column if it does not exist?
You can solve this using left join. With a smart subquery, this eliminates the in and the decode() in the order by as well.
I would phrase the query as:
with cols as (
select 'CASE_REPORTER2SITE' as col, 1 as ordering from dual union all
select 'PRIMARY2BUS_ORG', 2 as ordering from dual
)
select cols.col, count(atc.col_name)
from cols left join
all_tab_columns atc
on cols.col = atc.col_name and atc.owner = 'SA'
group by cols.col
order by max(cols.ordering);
Note: I also included the column name in the output, because I think that is a good practice.
Mmmm, the only way I can think of is joining the all_Tab_columns table with a self created from dual, left outer join to keep those who are null, and put 0 when null like this:
select t.column_name,case when count is null then 0 else count end from (
SELECT 'CASE_XYZ' as colname from dual
union
SELECT 'PRIMARY2BUS_ORG' from dual) t
left outer join (
select column_name, count(COLUMN_NAME) as count from ALL_TAB_COLUMNS
WHERE owner='SA' and COLUMN_NAME IN ('CASE_XYZ', 'PRIMARY2BUS_ORG')
GROUP BY COLUMN_NAME HAVING COLUMN_NAME IN ('CASE_XYZ', 'PRIMARY2BUS_ORG')
ORDER BY DECODE (COLUMN_NAME, 'CASE_XYZ', 1 'PRIMARY2BUS_ORG', 2)) s
on t.colname = s.column_name

SQL query to determine that values in a column are unique

How to write a query to just determine that the values in a column are unique?
Try this:
SELECT CASE WHEN count(distinct col1)= count(col1)
THEN 'column values are unique' ELSE 'column values are NOT unique' END
FROM tbl_name;
Note: This only works if 'col1' does not have the data type 'ntext' or 'text'. If you have one of these data types, use 'distinct CAST(col1 AS nvarchar(4000))' (or similar) instead of 'distinct col1'.
select count(distinct column_name), count(column_name)
from table_name;
If the # of unique values is equal to the total # of values, then all values are unique.
IF NOT EXISTS (
SELECT
column_name
FROM
your_table
GROUP BY
column_name
HAVING
COUNT(*)>1
)
PRINT 'All are unique'
ELSE
PRINT 'Some are not unique'
If you want to list those that aren't unique, just take the inner query and run it. HTH.
With this following query, you have the advantage of not only seeing if your columns are unique, but you can also see which combination is most non-unique. Furthermore, because you still see frequency 1 is your key is unique, you know your results are good, and not for example simply missing; something is less clear when using a HAVING clause.
SELECT Col1, Col2, COUNT(*) AS Freq
FROM Table
GROUP BY Col1, Col2
ORDER BY Freq DESC
Are you trying to return only distinct values of a column? If so, you can use the DISTINCT keyword. The syntax is:
SELECT DISTINCT column_name,column_name
FROM table_name;
If you want to check if all the values are unique and you care about NULL values, then do something like this:
select (case when count(distinct column_name) = count(column_name) and
(count(column_name) = count(*) or count(column_name) = count(*) - 1)
then 'All Unique'
else 'Duplicates'
end)
from table t;
select (case when count(distinct column1 ) = count(column1)
then 'Unique'
else 'Duplicates'
end)
from table_name
By my understanding you want to know which values are unique in a column. Therefore, using select distinct to do so doesn't solve the problem, because only lists the value as if they are unique, but they may not.
A simple solution as follows:
SELECT COUNT(column_name), column_name
FROM table_name
GROUP BY column_name
HAVING COUNT(column_name) = 1;
this code return distinct value
SELECT code FROM #test
group by code
having count(distinct code)= count(code)
return 14 that is just unique value
Use the DISTINCT keyword inside a COUNT aggregate function as shown below:
SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name
The above query will give you the count of distinct values in that column.

Gathering Database table information?

I am trying to find below information from the table in tabular format . I can get Rows Count, Column Name and Attribute (DataType)
No.Of columns
No.Of Rows Count
Column name
Attribute (DataType)
Min Value
Max Value
Non null count
Distinct count of the column
Any idea?
Many of these items can be found in the INFORMATION_SCHEMA.COLUMNS view, and the rest can be found by querying the table itself. You say you want this data in a tabular format, but many of the items do not 'fit' together. Can you provide a sample of what the result set should look like?
-- No.Of columns
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'your_table'
-- No.Of Rows Count
SELECT COUNT(*)
FROM your_table
--Column name
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'your_table'
--Attribute (DataType)
SELECT DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'your_table'
--Min Value
SELECT MIN(column_1)
FROM your_table
--Max Value
SELECT MAX(column_1)
FROM your_table
--Non null count
SELECT SUM(CASE WHEN column_1 IS NOT NULL THEN 1 ELSE 0 END) AS not_null_count
FROM your_table
--Distinct count of the column
SELECT COUNT(*)
FROM your_table
GROUP BY column_1