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;
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'm writing a SQL statement for my MS Access database, and the purpose is to count values from 3 different queries, so I tried this way:
SELECT(query1 + query2 + query3) AS Qtd
Each query returns an unique value from an aggregate function count, i.e, query1 = SELECT Count(something) FROM Table WHERE...
Everything should work fine, but MS Access demands a FROM clause. When I put a table in that query (not changing the SELECT statement above), I end up with tones of rows and each row the result expected from Qtd column.
So is there any way to skip the FROM Clause or the only option to work around is write TOP 1 (or DISTINCT) to not get tones of duplicated rows because of the unnecessary table in FROM clause?
You could union all the queries and then sum all the results:
SELECT SUM(cnt)
FROM (SELECT COUNT(*) AS cnt FROM table1 WHERE ...
UNION ALL
SELECT COUNT(*) AS cnt FROM table2 WHERE ...
-- Etc..
) t
Consider the cross join (comma separated tables) of the aggregate queries:
SELECT (query1.CntColumn + query2.CntColumn + query3.CntColumn) AS Qtd
FROM query1, query2, query3
I need to sum the result of count of a column in one query.
Is it possible to have like this query?
SELECT sum(count(pro_id)) from jalasat group by pro_id
You have not mentioned which SQL database you are using so you may modify this slightly to fit it to what you are using:
SELECT SUM(cnt) FROM (SELECT COUNT(pro_id) as cnt
FROM jalasat
GROUP BY continent) as t1
In HIVE, I tried getting the count of distinct rows in 2 methods,
SELECT COUNT (*) FROM (SELECT DISTINCT columns FROM table);
SELECT COUNT (DISTINCT columns) FROM table;
Both are yielding DIFFERENT RESULTS.
The count for the first query is greater than the second query.
How are they working differently?
Thanks in advance.
Do a slight change to your query, ie name your sub query for eg:
SELECT COUNT (*) FROM (SELECT DISTINCT columns FROM table) myquery;
Try with this in hive:
SELECT COUNT (DISTINCT nvl(columns,'NA')) FROM table;
or:
SELECT COUNT (DISTINCT coalesce(columns,'NA')) FROM table;
Above query output will be same as below:
SELECT COUNT (*) FROM (SELECT DISTINCT columns FROM table);
If i perform a standard query in SQLite:
SELECT * FROM my_table
I get all records in my table as expected. If i perform following query:
SELECT *, 1 FROM my_table
I get all records as expected with rightmost column holding '1' in all records. But if i perform the query:
SELECT *, COUNT(*) FROM my_table
I get only ONE row (with rightmost column is a correct count).
Why is such results? I'm not very good in SQL, maybe such behavior is expected? It seems very strange and unlogical to me :(.
SELECT *, COUNT(*) FROM my_table is not what you want, and it's not really valid SQL, you have to group by all the columns that's not an aggregate.
You'd want something like
SELECT somecolumn,someothercolumn, COUNT(*)
FROM my_table
GROUP BY somecolumn,someothercolumn
If you want to count the number of records in your table, simply run:
SELECT COUNT(*) FROM your_table;
count(*) is an aggregate function. Aggregate functions need to be grouped for a meaningful results. You can read: count columns group by
If what you want is the total number of records in the table appended to each row you can do something like
SELECT *
FROM my_table
CROSS JOIN (SELECT COUNT(*) AS COUNT_OF_RECS_IN_MY_TABLE
FROM MY_TABLE)