Group by using a function result SQL Server - sql

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

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>

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)

query to fetch the number of rows fetched by another query

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.

DB2 AS/400 iseries Use alias in where clause

I'm trying to use the alias of a column in the where clause. for example:
SELECT col1 AS alias1, col2 + col3 as sums
FROM my_table
WHERE sums > 10
But then I get an error message saying:
Column sums not in specified tables.
Is there anyway I can do this?
If you really want to use alias in WHERE, not the column itself, you can do it with derived table :
SELECT a.*
FROM
(
SELECT lmITNO AS Item_Number, lmBANO AS Lot, lmSTAS AS Status,
lmRORN AS Order_Number
FROM MVXJDTA.MILOMA
) a
WHERE a.Status = 'CCC'
....
The where-clause is processed before the select-clause in a statement:
The WHERE clause specifies an intermediate result table that consists of those rows of R for which the search-condition is true. R is the result of the FROM clause of the statement.
Re-write the where-clause to reference the actual column name:
...
WHERE A.lmSTAS = 'CCC'
...
A common-table-expression can be used to pre-process the select-clause. For example:
WITH A AS (SELECT
lmITNO AS Item_Number,
lmBANO AS Lot,
lmSTAS AS Status,
lmRORN AS Order_Number
FROM MVXJDTA.MILOMA)
SELECT A.* FROM A WHERE A.Status = 'CCC'
FETCH FIRST 1000 ROWS ONLY
The columns in a CTE can also be renamed by listing them after the table-identifier. For example:
WITH A (Item_Number, Lot, Status, Order_Number)
AS (SELECT
lmITNO,
lmBANO,
lmSTAS,
lmRORN
FROM MVXJDTA.MILOMA)
SELECT A.* FROM A WHERE A.Status = 'CCC'
FETCH FIRST 1000 ROWS ONLY

SQL IN Operator with Select

I want to use the IN Operator in SQL with a select statement but I get the following error "Cannot perform an aggregate function on an expression containing an aggregate or a subquery."
The code that I am using looks as follows:
select * from Table where ID in (select Units from Table2)
You have use WHEN instead of WHERE
SELECT * FROM `Table` WHERE `ID` IN (SELECT `Units` FROM `Table2`)
You should use where in place of when in your query. The correct query will be:
select * from Table where ID in (select Units from Table2)
You should try to select specific ID columns in your second query, something like this:
Select*from Table where ID IN (Select ID from Table2)