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;
Related
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;
I have some E_ids which are linked to a couple of d_ids and with o_count in any of (1,0,null).
So if any of the E_IDs have an O_count = 1, I have to club it into one row and write the O_count = 1 for that E_ID else 0.
But when I do the below, I get all the rows without the grouping, i.e, I get two rows of the same e_ids. Is there any other way to do the same?
SELECT DISTINCT E_ID, status
(CASE WHEN o_count = any(1) THEN 1
WHEN o_count = any(0) THEN 0
ELSE null END
) Ocount
FROM (SELECT e_id, status, o_count FROM A)
GROUP BY e_id, status, o_count
Yes, just wrap it with MAX() :
SELECT E_ID,status
MAX(case when o_count = 1 then 1 ELSE 0 END) as Ocount
FROM A
GROUP BY e_id,status
Also , the sub query was unnecessary , you are not doing any logic in there.
First of all you group by e_id, status, o_count. That means you aggregate your data such that you get one row for each such combination. Don't you rather want to get one result row per e_id alone or maybe e_id plus status?
Then you have that case construct not containing any aggregate function, but only the o_count which is part of your group by clause. So you are looking at one row where you want o_count = any(1) which is exactly the same as o_count = 1 of course, because there is only one value in the specified set. You can replace the complete case expression with a mere o_count.
Then you apply distinct. But there can be no duplicates, as you are grouping by all columns used. So distinct doesn't do anything here.
Selecting from a subquery without any where clause or aggregation is also superfluous and you can select from table a directly.
Your query can be re-written as
select distinct e_id, status, o_count
from a;
I suppose you want something like this instead:
select e_id, status, max(o_count)
from a
group by e_id, status;
Or this:
select e_id, max(status), max(o_count)
from a
group by e_id;
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;
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
;
I am working with two sql queries that I would like to combine as one so the result of the first query would be in the first column and the result of the second one in column two. How can I acheive this?
I tried union, but it will put the results in two rows.. thats not what I want...
select count(*) as ColumnA from Inventory i, Sale s where i.vin=s.vin and i.condition='new'
select count(*) as ColumnB from Inventory i, Sale s where i.vin=s.vin and i.condition='used' order by 1 desc;
You can use combine two sub-queries in one query like this:
select
(select count(*) from Inventory i, Sale s where i.vin=s.vin and i.condition='new') as New,
(select count(*) from Inventory i, Sale s where i.vin=s.vin and i.condition='used') as Used
What were you trying to achieve with order by statement?
You can obtain both counts at the same time with a little bit different query, which will be slightly more efficient than combining two queries:
SELECT
SUM(CASE WHEN i.condition = 'new' THEN 1 ELSE 0 END),
SUM(CASE WHEN i.condition = 'used' THEN 1 ELSE 0 END)
FROM
Inventory i
JOIN
Sale s ON i.vin = s.vin
A simple way would be to:
select
(select count(*) as ColumnA from Inventory i, Sale s where i.vin=s.vin and i.condition='new') as newCount,
(select count(*) as ColumnB from Inventory i, Sale s where i.vin=s.vin and i.condition='used') as usedCount
You can use another SELECT to combine results:
SELECT
(select count(*) from Inventory i, Sale s where i.vin=s.vin and i.condition='new') as ColumnA,
(select count(*) from Inventory i, Sale s where i.vin=s.vin and i.condition='used') as ColumnB