Select query, insert into table, and return initial query in postgres - sql

I have a rather complex plpgsql stored procedure and I need to select from multiple tables and insert as well.
This is part of what I currently have.
BEGIN
RETURN query
SELECT domains.id, webpages.id as page_id ...
FROM domains
LEFT JOIN domain_settings
ON domain_settings.domain_id = domains.id
RIGHT JOIN webpages
ON webpages.domain_id = domains.id
LEFT JOIN subscriptions
ON webpages.id = subscriptions.page_id
AND subscriptions.user_id = query_user_id
AND subscriptions.comment_id IS NULL
WHERE domains.domain_address = query_domain_url
IF NOT FOUND THEN ...
END;
$$ language plpgsql;
Now, I would like add an insert query into another table using certain values from the return query before the 'if not found then' statement:
INSERT INTO page_visits (domain_id, page_id)
SELECT id, page_id FROM ?? (return query statement)
And after the insert, I want to return the initial return query values. How do I go about doing this? I tried using WITH AS statements, but I can't seem to get it to work

A set-returning PL/pgSQL function builds the return stack while processing the function body. There is no way to access that return stack from within the same function. You could nest the function. Or use a temporary table.
But using a CTE is probably the simplest way for the cas at hand. Going out on a limb, you may be looking for something like this:
CREATE OR REPLACE FUNCTION demo(query_user_id int, query_domain_url text)
RETURNS TABLE (c1 int, c2 int)
LANGUAGE plpgsql AS
$func$
BEGIN
RETURN QUERY
WITH sel AS (
SELECT d.id, w.id as page_id ...
FROM webpages w
JOIN domains d ON d.id = w.domain_id
LEFT JOIN domain_settings ds ON ds.domain_id = d.id
LEFT JOIN subscriptions s ON s.page_id = w.id
AND s.user_id = query_user_id -- origin?
AND s.comment_id IS NULL
WHERE d.domain_address = query_domain_url -- origin?
)
, ins AS (
INSERT INTO tbl (col1, col2)
SELECT main.id, sel.page_id
FROM (SELECT 'foo') AS main(id)
LEFT JOIN sel USING (id) -- LEFT JOIN ?
)
TABLE sel;
IF NOT FOUND THEN
-- do something
END IF;
END
$func$;
Remember, if the transaction does not commit successfully, the INSERT is also rolled back.
The final TABLE sel is just short syntax for SELECT * FROM sel. See:
Is there a shortcut for SELECT * FROM?

Related

Options for merging two separate complex postgres functions

I have two separate plpgsql functions for my project, that I would like to merge into one, or come up with an alternative solution.
The first function uses a domain address and path, to retrieve the domain_id and webpage_id, along other things (such as whether the domain uses a liking system or voting system or neither for its posts).
And the second function uses the webpage_id from the first function to retrieve appropriate posts for that page and whether to get post likes/votes/neither.
As of now, what I'm doing is to call the following function, first. What it does is that it checks for the domain_id and webpage_id based off the domain_url and slug.
If the values don't exist, it checks whether the domain is registered, and if it is, it makes an insert into the webpages table, else it returns an error.
create or replace function get_domain_details(query_domain_url text, query_slug text) returns table(id bigint, owner_id uuid page_id bigint, post_count int, is_likes boolean, is_votes boolean
)
as $$
DECLARE new_domain_id int;
new_page_id bigint;
BEGIN
RETURN query
WITH domain_row AS (
SELECT domains.id, domains.owner_id, webpages.id as page_id, webpages.post_count, domain_settings.is_likes, domain_settings.is_votes
FROM domains
JOIN domain_settings
ON domain_settings.domain_id = domains.id
RIGHT JOIN webpages
ON webpages.domain_id = domains.id AND webpages.slug = query_slug
WHERE domains.domain_address = query_domain_url),
abc as (
INSERT INTO page_visits (domain_id, page_id)
SELECT domain_row.id, domain_row.page_id FROM domain_row
)
TABLE domain_row;
IF NOT FOUND THEN
IF EXISTS (SELECT domains.id FROM domains WHERE domain_address = query_domain_url) THEN
WITH webpage_rows AS (INSERT INTO webpages (domain_id, slug)
VALUES ((SELECT domains.id FROM domains WHERE domain_address = query_domain_url), query_slug)
RETURNING *
)
SELECT webpage_rows.id, webpage_rows.domain_id
INTO new_page_id, new_domain_id
FROM webpage_rows;
RETURN query
WITH domain_row_2 AS (
SELECT domains.id, domains.owner_id, new_page_id as page_id, 0 as post_count, domain_settings.is_likes, domain_settings.is_votes
FROM domains
JOIN domain_settings
ON domain_settings.domain_id = domains.id
WHERE domains.id = new_domain_id),
abc_2 as (
INSERT INTO page_visits (domain_id, page_id)
SELECT domain_row_2.id, domain_row_2.page_id FROM domain_row_2
)
TABLE domain_row_2;
ELSE
RAISE EXCEPTION 'This is not a valid domain!';
END IF;
END IF;
RETURN;
END;
$$ language plpgsql;
And once I've received the page_id and liking/voting system info from the database, I use that to make another request to the database from my nodejs server to retrieve the posts and its details.
If is_votes is true, it joins with the votes table, else if is_likes is true, it joins with the likes table, else it doesn't join with either.
create or replace function get_page_posts (
query_page_id bigint, is_votes boolean, is_likes boolean
)
returns table (id bigint, full_name text, avatar_url text, author_id uuid, post_text text, created_at timestamptz, comment_count int, vote_count int, like_count int
)
as $$
BEGIN
IF is_votes = true THEN
RETURN query
SELECT posts.id, users.full_name, users.avatar_url, posts.author_id, posts.post_text, posts.created_at, post_data.comment_count, post_data.vote_count, post_data.like_count
FROM posts
LEFT JOIN users
ON users.user_id = posts.author_id
LEFT JOIN post_data
ON post_data.post_id = posts.id
LEFT JOIN votes v1
ON v1.post_id = posts.id
LEFT JOIN webpages
ON webpages.id = posts.page_id
WHERE webpages.id = query_page_id
AND NOT posts.status = 'deleted'
ORDER BY posts.id DESC;
ELSIF is_likes = true THEN
RETURN query
SELECT posts.id, users.full_name, users.avatar_url, posts.author_id, posts.post_text, posts.created_at, post_data.comment_count, post_data.vote_count, post_data.like_count
FROM posts
LEFT JOIN users
ON users.user_id = posts.author_id
LEFT JOIN post_data
ON post_data.post_id = posts.id
LEFT JOIN webpages
ON webpages.id = posts.page_id
LEFT JOIN likes
ON likes.post_id = posts.id
WHERE webpages.id = query_page_id
AND NOT posts.status = 'deleted'
ORDER BY posts.id DESC;
ELSE
RETURN query
SELECT posts.id, users.full_name, users.avatar_url, posts.author_id, posts.comment_text, posts.created_at, post_data.comment_count, post_data.vote_count, post_data.like_count
FROM posts
LEFT JOIN users
ON users.user_id = posts.author_id
LEFT JOIN post_data
ON post_data.post_id = posts.id
LEFT JOIN webpages
ON webpages.id = posts.page_id
WHERE posts.parent_id IS NULL
AND webpages.id = query_page_id
AND NOT posts.status = 'deleted'
ORDER BY posts.id DESC;
END IF;
END;
$$
language plpgsql
Now, the issue that I have is that this is slowing this entire loading process down, so I'm trying to merge both functions together so that I don't have to make two separate requests to the database.
I have currently combined both of them into a new function such that it looks like this.
However, I am unsure if this is a safe and viable approach.
... as $$
BEGIN
RETURN query
WITH abc AS (
SELECT * FROM get_domain_details(query_domain_url, query_slug)
),
def AS (
SELECT * FROM get_page_posts((SELECT abc.page_id FROM abc), (SELECT abc.is_votes FROM abc), (SELECT abc.is_likes FROM abc))
)
SELECT * FROM abc UNION ALL SELECT * FROM def;
END;
$$ language plpgsql;

Rewrite query without using temp table

I have a query that is using a temp table to insert some data then another select from to extract distinct results. That query by it self was fine but now with entity-framework it is causing all kinds of unexpected errors at the wrong time.
Is there any way I can rewrite the query not to use a temp table? When this is converted into a stored procedure and in entity framework the result set is of type int which throws an error:
Could not find an implementation of the query pattern Select not found.
Here is the query
Drop Table IF EXISTS #Temp
SELECT
a.ReceiverID,
a.AntennaID,
a.AntennaName into #Temp
FROM RFIDReceiverAntenna a
full join Station b ON (a.ReceiverID = b.ReceiverID) and (a.AntennaID = b.AntennaID)
where (a.ReceiverID is NULL or b.ReceiverID is NULL)
and (a.AntennaID IS NULL or b.antennaID is NULL)
select distinct r.ReceiverID, r.ReceiverName, r.receiverdescription
from RFIDReceiver r
inner join #Temp t on r.ReceiverID = t.ReceiverID;
No need for anything fancy, you can just replace the reference to #temp with an inner sub-query containing the query that generates #temp e.g.
select distinct r.ReceiverID, r.ReceiverName, r.receiverdescription
from RFIDReceiver r
inner join (
select
a.ReceiverID,
a.AntennaID,
a.AntennaName
from RFIDReceiverAntenna a
full join Station b ON (a.ReceiverID = b.ReceiverID) and (a.AntennaID = b.AntennaID)
where (a.ReceiverID is NULL or b.ReceiverID is NULL)
and (a.AntennaID IS NULL or b.antennaID is NULL)
) t on r.ReceiverID = t.ReceiverID;
PS: I haven't made any effort to improve the query overall like Gordon has but do consider his suggestions.
First, a full join makes no sense in the first query. You are selecting only columns from the first table, so you need that.
Second, you can use a CTE.
Third, you should be able to get rid of the SELECT DISTINCT by using an EXISTS condition.
I would suggest:
WITH ra AS (
SELECT ra.*
FROM RFIDReceiverAntenna ra
Station s
ON s.ReceiverID = ra.ReceiverID AND
s.AntennaID = ra.AntennaID)
WHERE s.ReceiverID is NULL
)
SELECT r.ReceiverID, r.ReceiverName, r.receiverdescription
FROM RFIDReceiver r
WHERE EXISTS (SELECT 1
FROM ra
WHERE r.ReceiverID = ra.ReceiverID
);
You can use CTE instead of the temp table:
WITH
CTE
AS
(
SELECT
a.ReceiverID,
a.AntennaID,
a.AntennaName
FROM
RFIDReceiverAntenna a
full join Station b
ON (a.ReceiverID = b.ReceiverID)
and (a.AntennaID = b.AntennaID)
where
(a.ReceiverID is NULL or b.ReceiverID is NULL)
and (a.AntennaID IS NULL or b.antennaID is NULL)
)
select distinct
r.ReceiverID, r.ReceiverName, r.receiverdescription
from
RFIDReceiver r
inner join CTE t on r.ReceiverID = t.ReceiverID
;
This query will return the same results as your original query with the temp table, but its performance may be quite different; not necessarily slower, it can be faster. Just something that you should be aware about.

SQL with clause in stored procedure

Is it possible to define a with clause in a stored procedure and use it in if else statements because I always get an error?
BEGIN
WITH Test (F, A) AS
(
SELECT FM.ID, FM.Name
FROM [Test.Abc] FM
INNER JOIN [Organization] O on O.ABCID = FM.ID
)
IF(#var = 'case1')
BEGIN
SELECT *
FROM Test F
WHERE NOT F.ID = 'someID'
END
I always get an "Incorrect syntax" error before the if statement
If I move the with clause into the if statement it works fine. But I need the with statement outside to reuse it in different if else statements.
Here's another version of the same answers you're getting:
Your with common table expresson has to be in the same statement as the query that calls it, and it has to be referenced by a query (or other cte) or it is a syntax error.
Reference the documentation Guidelines for Creating and Using Common Table Expressions.
BEGIN -- doing stuff
-- .... doing stuff over here
IF(#var = 'case1')
BEGIN
with Test (F, A) as (
select FM.ID, FM.Name from [Test.Abc] FM
inner join [Organization] O on O.ABCID = FM.ID
)
select * from Test F
where not F.ID = 'someID'
END
-- .... and doing some other stuff over here too
END -- done with this stuff
Just use a temporary table or table variable. The scoping rules of SQL Server ensure that such a table is dropped at the end of the procedure:
BEGIN
select FM.ID, FM.Name
into #test
from [Test.Abc] FM inner join
[Organization] O
on O.ABCID = FM.ID;
IF(#var = 'case1')
BEGIN
select *
from #Test F
where not F.ID = 'someID'
END;
This has the advantage that you can add indexes to the table, and these might improve performance.
WITH is not a standalone, it always a part of a whole statement and only one statement.
It is not recognizable outside the scope ofits statement.
BEGIN
with my_cte (n) as (select 1+1)
select * from my_cte
-- The following statement yields the error "Invalid object name 'my_cte'."
-- select * from my_cte
END

How can I perform the Count function with a where clause?

I have my database setup to allow a user to "Like" or "Dislike" a post. If it is liked, the column isliked = true, false otherwise (null if nothing.)
The problem is, I am trying to create a view that shows all Posts, and also shows a column with how many 'likes' and 'dislikes' each post has. Here is my SQL; I'm not sure where to go from here. It's been a while since I've worked with SQL and everything I've tried so far has not given me what I want.
Perhaps my DB isn't setup properly for this. Here is the SQL:
Select trippin.AccountData.username, trippin.PostData.posttext,
trippin.CategoryData.categoryname, Count(trippin.LikesDislikesData.liked)
as TimesLiked from trippin.PostData
inner join trippin.AccountData on trippin.PostData.accountid = trippin.AccountData.id
inner join trippin.CategoryData on trippin.CategoryData.id = trippin.PostData.categoryid
full outer join trippin.LikesDislikesData on trippin.LikesDislikesData.postid =
trippin.PostData.id
full outer join trippin.LikesDislikesData likes2 on trippin.LikesDislikesData.accountid =
trippin.AccountData.id
Group By (trippin.AccountData.username), (trippin.PostData.posttext), (trippin.categorydata.categoryname);
Here's my table setup (I've only included relevant columns):
LikesDislikesData
isliked(bit) || accountid(string) || postid(string
PostData
id(string) || posttext || accountid(string)
AccountData
id(string) || username(string)
CategoryData
categoryname(string)
Problem 1: FULL OUTER JOIN versus LEFT OUTER JOIN. Full outer joins are seldom what you want, it means you want all data specified on the "left" and all data specified on the "right", that are matched and unmatched. What you want is all the PostData on the "left" and any matching Likes data on the "right". If some right hand side rows don't match something on the left, then you don't care about it. Almost always work from left to right and join results that are relevant.
Problem 2: table alias. Where ever you alias a table name - such as Likes2 - then every instance of that table within the query needs to use that alias. Straight after you declare the alias Likes2, your join condition refers back to trippin.LikesDislikesData, which is the first instance of the table. Given the second one in joining on a different field I suspect that the postid and accountid are being matched on the same row, therefore it should be AND together, not a separate table instance. EDIT reading your schema closer, it seems this wouldn't be needed at all.
Problem 3: to solve you Counts problem separate them using CASE statements. Count will add the number of non NULL values returned for each CASE. If the likes.liked = 1, then return 1 otherwise return NULL. The NULL will be returned if the columns contains a 0 or a NULL.
SELECT trippin.PostData.Id, trippin.AccountData.username, trippin.PostData.posttext,
trippin.CategoryData.categoryname,
SUM(CASE WHEN likes.liked = 1 THEN 1 ELSE 0 END) as TimesLiked,
SUM(CASE WHEN likes.liked = 0 THEN 1 ELSE 0 END) as TimesDisLiked
FROM trippin.PostData
INNER JOIN trippin.AccountData ON trippin.PostData.accountid = trippin.AccountData.id
INNER JOIN trippin.CategoryData ON trippin.CategoryData.id = trippin.PostData.categoryid
LEFT OUTER JOIN trippin.LikesDislikesData likes ON likes.postid = trippin.PostData.id
-- remove AND likes.accountid = trippin.AccountData.id
GROUP BY trippin.PostData.Id, (trippin.AccountData.username), (trippin.PostData.posttext), (trippin.categorydata.categoryname);
Then "hide" the PostId column in the User Interface.
Instead of selecting Count(trippin.LikesDislikesData.liked) you could put in a select statement:
Select AccountData.username, PostData.posttext, CategoryData.categoryname,
(select Count(*)
from LikesDislikesData as likes2
where likes2.postid = postdata.id
and likes2.liked = 'like' ) as TimesLiked
from PostData
inner join AccountData on PostData.accountid = AccountData.id
inner join CategoryData on CategoryData.id = PostData.categoryid
USE AdventureWorksDW2008R2
GO
SET NOCOUNT ON
GO
/*
Default
*/
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
GO
BEGIN TRAN
IF OBJECT_ID('tempdb.dbo.#LikesDislikesData') IS NOT NULL
BEGIN
DROP TABLE #LikesDislikesData
END
CREATE TABLE #LikesDislikesData(
isLiked bit
,accountid VARCHAR(50)
,postid VARCHAR(50)
);
IF OBJECT_ID('tempdb.dbo.#PostData') IS NOT NULL
BEGIN
DROP TABLE #PostData
END
CREATE TABLE #PostData(
postid INT IDENTITY(1,1) NOT NULL
,accountid VARCHAR(50)
,posttext VARCHAR(50)
);
IF OBJECT_ID('tempdb.dbo.#AccountData') IS NOT NULL
BEGIN
DROP TABLE #AccountData
END
CREATE TABLE #AccountData(
accountid INT
,username VARCHAR(50)
);
IF OBJECT_ID('tempdb.dbo.#CategoryData') IS NOT NULL
BEGIN
DROP TABLE #CategoryData
END
CREATE TABLE #CategoryData(
categoryname VARCHAR(50)
);
INSERT INTO #AccountData VALUES ('1', 'user1')
INSERT INTO #PostData VALUES('1','this is a post')
INSERT INTO #LikesDislikesData (isLiked ,accountid, postid)
SELECT '1', P.accountid, P.postid
FROM #PostData P
WHERE P.posttext = 'this is a post'
SELECT *
FROM #PostData
SELECT *
FROM #LikesDislikesData
SELECT *
FROM #AccountData
SELECT COUNT(L.isLiked) 'Likes'
,P.posttext
,A.username
FROM #PostData P
JOIN #LikesDislikesData L
ON P.accountid = L.accountid
AND L.IsLiked = 1
JOIN #AccountData A
ON P.accountid = A.accountid
GROUP BY P.posttext, A.username
SELECT X.likes, Y.dislikes
FROM (
(SELECT COUNT(isliked)as 'likes', accountid
FROM #LikesDislikesData
WHERE isLiked = 1
GROUP BY accountid
) X
JOIN
(SELECT COUNT(isliked)as 'dislikes', accountid
FROM #LikesDislikesData
WHERE isLiked = 0
GROUP BY accountid) Y
ON x.accountid = y.accountid)
IF (XACT_STATE() = 1 AND ERROR_STATE() = 0)
BEGIN
COMMIT TRAN
END
ELSE IF (##TRANCOUNT > 0)
BEGIN
ROLLBACK TRAN
END
How do you think about the solution? We create a new table SummaryReport(PostID,AccountID,NumberOfLikedTime,NumberOfDislikedTimes).
An user clicks on LIKE or DISLIKE button we update the table. After that, you can query as you desire. Another advantage, the table can be served reporting purpose.

DB2 set default when null on join / Open table after FETCH

This is kind of a double question, just thinking of ways to accomplish my problem.
Also, I'm pretty new to DB2 and stored procedures, so bear with me.
I'm creating a stored procedure that gets a value from two tables using a Left Join statement. This will result in some of the values in the second table returning a null value (since they don't exist in tableB).
DECLARE CURSOR C1 WITH RETURN FOR
select a.name, a.title, b.order from tableA a
left outer join tableB b on a.name = b.name;
Now, I need some way to set these null values to a default value of 0.
The program I'm working with can do it ( CAST-IRON ) but if the result set is too large, it slows down the orchestrations and truncates the job log. So I'm trying to figure it out using the stored procedure.
My first thought was to use the FETCH INTO statement and a WHILE loop.
WHILE AT_END = 0 DO
FETCH C1 INTO CHNAME, CHTITLE, CHORDER;
IF CHORDER IS NULL
THEN SET CHORDER = 0;
END IF;
IF SQLCODE = 100
THEN SET AT_END = 1;
END IF;
END WHILE;
But it seems like that would require a temporary table being created, and declaring another cursor with that table, using an insert command after the 'FETCH INTO'. So I was wondering if there were another way to do this, or to automatically set a default in the select statement?
Set a default in the select statement using COALESCE.
DECLARE CURSOR C1 WITH RETURN FOR
select a.name, a.title, COALESCE(b.order,0) as order
from tableA a
left outer join tableB b on a.name = b.name;