tsql 'in' statement to table valued function - tsql

I have simple table valued function returning one column table
ALTER FUNCTION [dbo].[fnGetProducersByWheelType]
(
#wheeltype int
)
RETURNS TABLE
AS
RETURN
(
SELECT ProducerId
FROM ProducersByWheelTypes
WHERE WheelType=#wheeltype
)
and what i need is to know if i can call the way like :
select *
from producers
where producerid in ( fnGetProducersByWheelType(2))
not
select *
from producers
where producerid in (
select productId
from fnGetProducersByWheelType(2)
)
i.e. not mention single column productId 'select productId from'

No. As the documentation for in shows you may have either a subquery or list of expressions. A TVF is not an expression.

You can do INNER JOIN instead:
select p.*
from producers p
join fnGetProducersByWheelType(2) f
on p.producerid = f.producerid

Related

Postgres Crosstab query with CTE (with clause)

Recently started working on Postgres and need to pivot data.
I wrote the following query:
select *
from crosstab (
$$
with tmp_kv as (
select distinct pat_id
,col.name as key, replace(replace(replace(value, '[',''), ']', ''),'"','') as value
from (
select p.Id as pat_id, nullif(kv.key,'undefined')::int as key, trim(kv.value::text,'"') as value
from pat_table p
left join e_table e on e.pat_id = p.id and e.id is null
,jsonb_each_text(p.data) as kv
) t
left join lateral (
select name::text as name from public.config_fields fld
where id = t.key
) col on true
)
select pat_id, key, value
from tmp_kv
where nullif(trim(key),'') is not null
order by pat_id, key
$$,$$
select distinct key from tmp_kv -- (Get error "relation "tmp_kv" does not exist" )
where nullif(trim(key),'') is not null
order by 1
$$
) as (
pat_id bigint
...
...
);
Query works if I take the WITH clause out into temporary table. But will be deploying it to production with read replicas, so need it to be working with a CTE. Is there a way?
The two queries passed as strings to the crosstab() function are separate queries.
A CTE can only be attached to a single query.
What you ask for is strictly impossible.
Since you have to spell out the (static) return type for crosstab() anyway, and the result of the query in the 2nd parameter has to match that, it's pointless to use a query with a dynamic result as 2nd parameter to begin with.

access subquery return value in union to other table postgres

Given the following query:
select parent_id from (
select parent_id, (most_recent_sit(date, child_id, key)).*
from links
where child_id = ANY(source_ids)
group by parent_id
) as subquery
union select * from link_traversal(
array(select parent_id from subquery),
);
Postgres throws the following error for the link_traversal function call:
relation "subquery" does not exist
link_traversal() and most_recent_sit() are custom functions I've created. I'd like to use the two functions and union the results together, where I reference the results of most_recent_sit() as the input for link_traversal().
Is it possible to reference a subquery select like so in a subsequent function call?

Postgres join involving tables having join condition defined on an text array

I have two tables in postgresql
One table is of the form
Create table table1(
ID serial PRIMARY KEY,
Type []Text
)
Create table table2(
type text,
sellerID int
)
Now i want to get all the rows from table1 which are having type same that in table2 but the problem is that in table1 the type is an array.
In case the type in the table has an identifiable delimiter like ',' ,';' etc. you can rewrite the query as regexp_split_to_table(type,',') or versions later than 9.5 unnest function can be use too.
For eg.,
select * from
( select id ,regexp_split_to_table(type,',') from table1)table1
inner join
select * from table2
on trim(table1.type) = trim(table2.type)
Another good example can be found - https://www.dbrnd.com/2017/03/postgresql-regexp_split_to_array-to-split-string-using-different-delimiters/
SELECT
a[1] AS DiskInfo
,a[2] AS DiskNumber
,a[3] AS MessageKeyword
FROM (
SELECT regexp_split_to_array('Postgres Disk information , disk 2 , failed', ',')
) AS dt(a)
You can use the ANY operator in the JOIN condition:
select *
from table1 t1
join table2 t2 on t2.type = any (t1.type);
Note that if the types in the table1 match multiple rows in table2, you would get duplicates (from table1) because that's how a join works. Maybe you want an EXISTS condition instead:
select *
from table1 t1
where exists (select *
from table2 t2
where t2.type = any(t1.type));

Trying to use a CTE calculation to update a column

I am trying to update column3, based on a calculation which is happening between column1 and column2. The theory is relatively simple, however I seem to be struggling with CTE's. If column1 is not null, then column1 * AVG(column2) gets put in column3.
I have searched the forums and tried a few different methods, including CTE and standard UPDATE queries. I seem to be making a mistake.
WITH cte_avg1 AS (
SELECT "column1" * AVG("column2") AS avg
FROM table1
)
UPDATE table1
SET "column3" = cte_avg1.avg
FROM cte_avg1
WHERE "column1" IS NOT NULL;
The error message which I am getting is as follows;
ERROR: column must appear in the GROUP BY clause or be used in an aggregate function
LINE 5: SELECT "column1" * AVG("column2"...
In an aggregating query all columns after SELECT must either be in the GROUP BY clause or a parameter to an aggregation function. Move the multiplication out of the CTE.
WITH cte_avg1
AS
(
SELECT avg(column2) avg
FROM table1
)
UPDATE table1
SET column3 = column1 * cte_avg1.avg
FROM cte_avg1
WHERE column1 IS NOT NULL;

tsql - using internal stored procedure as parameter is where clause

I'm trying to build a stored procedure that makes use of another stored procedure. Taking its result and using it as part of its where clause, from some reason I receive an error:
Invalid object name 'dbo.GetSuitableCategories'.
Here is a copy of the code:
select distinct top 6 * from
(
SELECT TOP 100 *
FROM [dbo].[products] products
where products.categoryId in
(select top 10 categories.categoryid from
[dbo].[GetSuitableCategories]
(
-- #Age
-- ,#Sex
-- ,#Event
1,
1,
1
) categories
ORDER BY NEWID()
)
--and products.Price <=#priceRange
ORDER BY NEWID()
)as d
union
select * from
(
select TOP 1 * FROM [dbo].[products] competingproducts
where competingproducts.categoryId =-2
--and competingproducts.Price <=#priceRange
ORDER BY NEWID()
) as d
and here is [dbo].[GetSuitableCategories] :
if (#gender =0)
begin
select * from categoryTable categories
where categories.gender =3
end
else
begin
select * from categoryTable categories
where categories.gender = #gender
or categories.gender =3
end
I would use an inline table valued user defined function. Or simply code it inline is no re-use is required
CREATE dbo.GetSuitableCategories
(
--parameters
)
RETURNS TABLE
AS
RETURN (
select * from categoryTable categories
where categories.gender IN (3, #gender)
)
Some points though:
I assume categoryTable has no gender = 0
Do you have 3 genders in your categoryTable? :-)
Why do pass in 3 parameters but only use 1? See below please
Does #sex map to #gender?
If you have extra processing on the 3 parameters, then you'll need a multi statement table valued functions but beware these can be slow
You can't use the results of a stored procedure directly in a select statement
You'll either have to output the results into a temp table, or make the sproc into a table valued function to do what you doing.
I think this is valid, but I'm doing this from memory
create table #tmp (blah, blah)
Insert into #tmp
exec dbo.sprocName