Join Two Count(*) Tables with No Relation in SQL Server - sql

I am trying to combine the results of a count(*) statement and a count(*) with a where clause on a SQL Server Table into a single table.
I have a union statement that bring together the two queries one of top of another.
SELECT count(*) FROM [dbo].asma a
where [MLR] in ('y')) l
union
SELECT count (*) as 'Total' FROM [dbo].asma]
This post of solutions I looked at, but couldn't piece together a solution that would present these side by side. How would you do this?
What I need is this output:

You can do conditional aggregation instead :
select sum(case when MLR = 'y' then 1 else 0 end) as Active, count(*) as Total
from dbo.asma a;

Related

select two counts from a table with 1 pass using SQL?

Say if we do such query:
select
(select count(*) as count_ca from employees where state = 'CA'),
(select count(*) as count_all from employees);
is it true that the SQL statement will need to go through the table twice, even when the table has 20 million rows? If we write a program we can keep 2 counts and just make 1 pass through the table records. Does the SQL statement do 2 passes and is it possible to make it 1 pass?
Use conditional aggregation:
select count(*), sum(case when state = 'CA' then 1 else 0 end)
from employees;

SQL combine 2 queries to one where 2 queries are from different database

I'm trying to combine two query results to one where both the tables are present in different databases like below:
select
(select COUNT(DISTINCT BaseVehicleID) as BVOld
from BaseVehicle) Old,
(select COUNT(DISTINCT BaseVehicleID) as BVNew
from [EnhancedStandard_VCDB_Exported_PRD_3006].BaseVehicle) New
Here [EnhancedStandard_VCDB_Exported_PRD_3006] is a different database.
So that I need to validate the count of records in both the database.
I'm able to combine the records among queries from same database.
Can someone please tell how to combine the result from 2 queries from 2 database.
Are you looking for 3-part naming? If so, this will probably work:
select (select COUNT(DISTINCT BaseVehicleID)
from BaseVehicle
) as Old,
(Select COUNT(DISTINCT BaseVehicleID)
from [EnhancedStandard_VCDB_Exported_PRD_3006].dbo.BaseVehicle
) New
You can use UNION ALL to combine the result of both the queries together in one result set.
Considering you're referring the database hosted on same SQL Server instance, If not you need to refer the the table on remote server using a Linked Server, like LinkedServerName.DatabasName.SchemaName.TableName.
If you've both the databases on same server you can use following query, alert, I'm considering your table is under default schema i.e. dbo.
Select COUNT(DISTINCT BaseVehicleID) as BVOldCount
from BaseVehicle
UNION ALL
Select COUNT(DISTINCT BaseVehicleID) as BVNewCount
from [EnhancedStandard_VCDB_Exported_PRD_3006].dbo.BaseVehicle;
Or
Select COUNT(DISTINCT BaseVehicleID) as BVOldCount, 'BVOldCount' as Type
from BaseVehicle
UNION ALL
Select COUNT(DISTINCT BaseVehicleID) as BVNewCount, 'BVNewCount' as Type
from [EnhancedStandard_VCDB_Exported_PRD_3006].dbo.BaseVehicle;
Try This:
SELECT COUNT(DISTINCT Base.BaseVehicleID) AS BVNew ,
Old.BVOld
FROM [EnhancedStandard_VCDB_Exported_PRD_3006].BaseVehicle AS Base
CROSS APPLY ( SELECT COUNT(DISTINCT B2.BaseVehicleID) AS BVOld
FROM BaseVehicle AS B2
) Old
GROUP BY Old.BVOld
If your other database is in other server, you need to create linked server and follow below query:
SELECT (SELECT count(*) FROM [serverName].[DatabaseName].dbo.TableName)
+
(SELECT count(*) FROM [serverName].[DatabaseName].dbo.TableName)

Combine multiple select queries result in one table or query

hello I have 3 select queries
I want to combine these queries and get result into columns not in rows.
If I use union it will show the output in rows
I don't want these result in column not in row.
queries
You can use this skeleton:
SELECT
Count(*) AS FirstCount,
(Select Count(*) As SecondCount From SecondTable) AS SecondCount,
(Select Count(*) As ThirdCount From ThirdTable) AS ThirdCount,
FROM
FirstTable;

Subsets count on Hive using CTE

I want to count the rows in a Hive table and at the same time, count the subsets (based on certain conditions in WHERE clause) in a single query. I came across CTE in this post, which I think applies to non-Hive SQL. I've researched a bit and found out that Hive has CTE. However this form does not work in Hive when I tried:
WITH MY_TABLE AS (
SELECT *
FROM orig_table
WHERE base_condition
)
SELECT
(SELECT COUNT(*) FROM MY_TABLE) AS total,
(SELECT COUNT(*) FROM MY_TABLE WHERE cond_1) AS subset_1,
...
(SELECT COUNT(*) FROM MY_TABLE WHERE cond_n) AS subset_n;
Does anyone have a workaround or similar working idea for Hive?
No need for Common table expressions. Use case when clauses to sum over conditions:
select count(1) as total
, sum(case when cond_1 then 1 else 0 end) as subset_1
--...
, sum(case when cond_n then 1 else 0 end) as subset_n
from orig_table
where base_cond
;

SQL Server "cannot perform an aggregate function on an expression containing an aggregate or a subquery", but Sybase can

This issue has been discussed before, but none of the answers address my specific problem because I am dealing with different where clauses in the inner and outer selects. This query executed just fine under Sybase, but gives the error in the title of this post when executed under SQL Server. The query is complicated, but the general outline of the query is:
select sum ( t.graduates -
( select sum ( t1.graduates )
from table as t1
where t1.id = t.id and t1.group_code not in ('total', 'others' ) ) )
from table as t
where t.group_code = 'total'
The following describes the situation I am trying to resolve:
all group codes represent races except for 'total' and 'others'
group code 'total' represents the total graduates of all races
however, multi-race is missing, so the race graduate counts may not add up to the total graduate counts
this missing data is what needs to be calculated
Is there anyway to rewrite this using derived tables or joins to get the same results?
Update: I created sample data and 3 solutions to my specific problem (2 influenced by sgeddes). The one that I added involves moving the correlated subquery to a derived table in the FROM clause. Thanks for the help guys!
One option is to put the subquery in a LEFT JOIN:
select sum ( t.graduates ) - t1.summedGraduates
from table as t
left join
(
select sum ( graduates ) summedGraduates, id
from table
where group_code not in ('total', 'others' )
group by id
) t1 on t.id = t1.id
where t.group_code = 'total'
group by t1.summedGraduates
Perhaps a better option would be to use SUM with CASE:
select sum(case when group_code = 'total' then graduates end) -
sum(case when group_code not in ('total','others') then graduates end)
from yourtable
SQL Fiddle Demo with both