query to fetch the number of rows fetched by another query - sql

How do I fetch the number of rows fetched by another query in SQL server:
The required value should be:
select count(*) of select * from table

Simply try
SELECT count(*) FROM
(
select * from yourtable
) AS A

Simply Try this Query for the number of rows fetched by another query in SQL server
select temp.TblCount From
(select Count(*) As TblCount from YOURTABLE) As Temp

Why the nested query? If all you need is the count of rows fetched by a query, better replace everything in the SELECT clause of that query with 'SELECT COUNT(*)' thus saving on using nested queries
Query 1:
select COUNT(*) from
(
select col1, col2, col3
From Table1
WHERE <Conditions>
)as x
Query 2:
select COUNT(*)
From Table1
WHERE <Conditions>
Both the Queries should be giving the same Output.

Related

How to write a hive sql that insert into table2 if there's a specific row in table1

I am facing a hive problem.
I will get a 0 or 1 after from sql
"select count(*) from table1 where ..."
If the result is 1, then I will execute the sql
"Insert Into table2 partition(d) (select xxxx from table 1 where ...
group by t)"
Otherwise do nothing.
My question is how can I write these two sql together into one sql. I am only allowed to write a single long sql.
I tried to put the first sql into the where condition in sql2, but it throwed an error said it's not supported to operat on table1 in the subquery (couldn't remember clearly, something like this).
It sounds like a very easy question for experienced programmers, but I just started lerning hive for 2 days.
If select in insert overwrite table partition does not returns rows, nothing is being overwritten.
So, just calculate your count in the same dataset and filter by it, use analytics funtion if you want to aggregate data on different level before insert
Insert Into table2 partition(d)
select col1, col2, sum(col3), etc, etc, partition_col
from
(
select --some columns here,
--Assign the same count to all rows
count(case when your_boolean_condition then 1 else null end) over () as cnt
from table 1
) s
where cnt=1 --If this is not satisfied, no overwrite will happen
AND more_conditions
group by ...
Another approach possible is to use cross-join with your count:
insert Into table2 partition(d)
select xxxx ... ... ..., partition_column as d
from
(
select t.*, c.cnt
table1 t cross join (select count(*) cnt from table1 where condition) c
)s
where cnt=1 <and another_condition>

Group by using a function result SQL Server

How to group the entire SQL query using scalar valued function result.
SELECT Col1,Col2,udf_IsNumaric_Get(Param1,Param2)
FROM Table1
WHERE Col1 LIKE '%123%'
GROUP BY --Function result
ORDER BY --Function result
OR
SELECT Col1,Col2,(SELECT COUNT(*) FROM Table2) AS Count
FROM Table1
WHERE Col1 LIKE '%123%'
GROUP BY --Count
ORDER BY --Count
Is it possible to group the entire SQL query by using the output of the udf_IsNumaric_Get scalar valued function or using the SELECT statement in the 2nd example..
You can achieve it using a subquery:
SELECT t.Col1,t.Col2,t.Count FROM
(SELECT Col1,Col2,(SELECT COUNT(*) FROM Table2) AS Count
FROM Table1
WHERE Col1 LIKE '%123%') as t
GROUP BY Count
ORDER BY Count

Using a value from one query in second query sql

SELECT AS, COUNT(*)
FROM Table1
HAVING COUNT(AS)>1
group BY AS;
This produces the result
AS COUNT
5 2
I then want to use the AS value in another query and only output the end result. Is this possible.i was thinking something like.
SELECT *
FROM
TABLE 2
Where AS =(
SELECT AS, COUNT(*)
FROM Table1
HAVING COUNT(AS)>1
group BY AS;
);
This is called a subquery. To be safe, you would use in instead of = (and as is a bad name for a column, because it is a SQL key word):
SELECT *
FROM TABLE2
WHERE col IN (SELECT col
FROM Table1
GROUP BY col
HAVING COUNT(col) > 1
);
Your first query is also incorrect, because the having clause goes after the group by.
You could use a subquery with the in operator:
SELECT *
FROM table2
WHERE AS IN (SELECT AS
FROM table1
GROUP BY AS
HAVING COUNT(*) > 1)

select value into viarble in Oracle SQL

I need some help with a query.
I want to select row count value from a table, and then to use the value in a different query. For example:
#rowcount = select count(*) from MyTable
select A*#rowcount, B/#rowcount
from MyOtherTable
Can someone show my what is the correct syntax? I need to use #rowcount a lot of times so i prefer to calculate it only once.
In Oracle you can't mix procedural code and regular SQL like that.
But if you use this:
select a / (select count(*) from mytable),
b / (select count(*) from mytable)
from myothertable;
Oracle will evaluate the count(*) only once. There is no need to store the value somewhere to improve performance.
If you want, you could move this into a common table expression:
with row_count as (
select count(*) as numrows
from mytable
)
select a / (select numrows from row_count),
b / (select numrows from row_count)
from myothertable;

Count rows in more than one table with tSQL

I need to count rows in more than one table in SQL Server 2008. I do this:
select count(*) from (select * from tbl1 union all select * from tbl2)
But it gives me an error of incorrect syntax near ). Why?
PS. The actual number of tables can be more than 2.
In case you have different number of columns in your tables try this way
SELECT count(*)
FROM (
SELECT NULL as columnName
FROM tbl1
UNION ALL
SELECT NULL
FROM tbl2
) T
try this:
You have to give a name to your derived table
select count(*) from
(select * from tbl1 union all select * from tbl2)a
I think you have to alias the SELECT in the FROM clause:
select count(*)
from
(
select * from tbl1
union all
select * from tbl2
) AS SUB
You also need to ensure that the * in both tables tbl1 and tbl2 return exactly the same number of columns and they have to be matched in their type.
I don't like doing the union before doing the count. It gives the SQL optimizer an opportunithy to choose to do more work.
AlexK's (deleted) solution is fine. You could also do:
select (select count(*) from tbl1) + (select count(*) from tbl2) as cnt