sql query where null results get placed first - sql

select *
from tableA
order by cast(columnA as int), column B.
This is my current query script. There is a scenario where there column A is null. And result rows where column A is null are pushed to the end.
Is there a way such that if columnA is null, put the rows before other rows where columnA is not null?
thanks.

Something along these lines should work if your dbms supports standard SQL.
select (case when columnA is null then 0 else 1 end) as sort_order, *
from tableA
order by sort_order, columnA, columnB;

Try like below... it will help you....
SELECT * FROM tableA ORDER BY (CASE WHEN columnA IS NULL THEN 1 ELSE 0 END) DESC,
CAST(columnA as int), column B
It display the NULL results before NOT NULL Results

I case of Oracle you can use order by columnA NULLS FIRST, columnB

Related

Dividing two counts Oracle SQL

I am trying to write what I thought would be a fairly simple query but is proving to be harder than I thought. I want to divide the results of two select statements:
Select count (*) from tableA where columnA = 'A'
/
Select count (*) from tableA where columnA in ('1','2')
I am using Oracle SQL Developer
Thanks for any guidance
I would recommend conditional aggregation . . . but with sum() not count():
Select (sum(case when columnA = 'A' then 1 else 0 end) /
sum(case when columnA in ('1', '2') then 1 end)
)
from tableA;
Note the lack of else in the denominator. This handles the divide-by-zero case.
You could treat the, both as subqueries within a single main query:
select
(select count (*) from tableA where columnA = 'A')
/
(select count (*) from tableA where columnA in ('1', '2'))
from dual
or if the table and column names are actually the same you could do a single query with conditional counts, something like:
select count (case when columnA = 'A' then columnA end)
/
count (case when columnA in ('1', '2') then columnA end)
from tableA
where columnA in ('A', '1', '2')
I've fixed the single quotes in both, not sure if those were just an issue with posting your question.
You may also need to add logic to handle the second count being zero, if that could happen - as that would cause a runtime error.

Different WHERE clause depending on subquery result

I would like to SELECT WHERE column IS NULL or =value depending on result of subquery.
Here is an example incorrect solution that demonstrates the problem:
SELECT *
FROM table
WHERE column=(
SELECT (CASE WHEN COUNT(*) = COUNT(COLUMN) THEN MIN(column) END)
FROM table
)
When the subquery returns NULL the other query will return nothing because column=NULL is never true. How do I fix this?
(Subquery source: https://stackoverflow.com/a/51341498/7810882)
From your question. just add OR column IS NULL in where clause.
You will get the subquery condition or column IS NULL data.
SELECT *
FROM table
WHERE column= (
SELECT (CASE WHEN COUNT(*) = COUNT(COLUMN) THEN MIN(column) END)
FROM table
) OR column IS NULL
If you are only looking for one row, I would suggest:
select t.*
from table t
order by column nulls first
fetch first 1 row only;

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

Group BY on Condition basis

I have data in following way....
ColumnA ColumnB
7675 22838
7675 24907
7675 NULL
I want the results in following way.....
ColumnA ColumnB
7675 2 (need total count for Not Null value)
7675 0 (need count 0 for NULL value)
SELECT ColumnA, COUNT(ColumnB) ColumnB
FROM YourTable
GROUP BY ColumnA
UNION ALL
SELECT ColumnA, 0
FROM YourTable
WHERE ColumnB IS NULL
GROUP BY ColumnA
You could introduce a calculated column indicating whether ColumnB is null or not and use it as a grouping criterion together with ColumnA:
SELECT
t.ColumnA,
ColumnB = COUNT(t.ColumnB)
FROM
dbo.YourTable AS t
CROSS APPLY
(SELECT CASE WHEN t.ColumnB IS NULL THEN 1 ELSE 0 END) AS x (SubGroup)
GROUP BY
t.ColumnA,
x.SubGroup
ORDER BY
t.ColumnA,
x.SubGroup
;
The COUNT(t.ColumnB) expression would always be NULL for a null subgroup, and for the corresponding non-null subgroup it would return the number of the non-null entries.
select columnA,
count(columnB) as non_null_count,
sum(columnB is null) as null_count
from your_table
group by ColumnA
you could easily do with a count and sum which may be faster if there are a lot of rows rather than selecting all of the rows twice with a UNION
SELECT columna, columnb, SUM(mycount)
FROM
( SELECT *, COUNT(columnb) as mycount
FROM test
GROUP BY columnb
)t
GROUP BY mycount
ORDER BY CASE WHEN mycount = 0 THEN 1 ELSE 2 END DESC;
Fiddle Demo

T-SQL Writing a Count Statement to find two values

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')