delete tables from log table - postgresql

I need to create drop statements from the log table below.
example of the log_table (4 rows)
1 2022-03-01 16:24:12 CREATE TABLE a.test1
2 2022-04-01 16:27:55 CREATE TABLE b.test2
3 2022-05-01 17:01:06 CREATE TABLE c.test3
4 2022-06-01 17:12:59 CREATE TABLE d.test4
Not my strongest area hence seeking help. Thanks

Related

PowerBI: Appending tables with calculated columns

i have uploaded into Power BI 4 Tables: 1) Exchange Rates 2) Ledger Tree 3) Actuals results 4) Forecast.
Tables 3 and 4 had different columns or information (eg. 3 showed values by ledger in usd and 4 in local currencies; table 3) is not showing the ledger currency while Table 4 does), so I had to add in both several calculated columns (including looking values from tables 1 and 2) to make them look alike.
I would like now to put table 3 and 4 above one another one but I am not sure how to do it?
Any help is much appreciated
In Power Query to Union tables use append query
When selecting this, you can add to the existing query, or create a new table from the appended tables. If you do append you can select the old tables no longer used, and turn off 'Enable Load' so the reference table isn't loaded into the Power Pivot
In DAX, you can union tables using the Union function

How do I select rows from one table that do not exist in another table with a specific value

I know this sounds simple and I have found many similar examples but I can't make sense of it to match my specific problem.
I have tried some nested selects and some left joins but they didn't work.
I have a Building table and a ComplianceItems Table
A Building can have many ComplianceItems.
I also have a ComplianceItemType table that contains all the possible types.
I want to find all the Buildings that don't have a ComplianceItem of Type 17 or 18.
I see lots of examples that select the parent record when there are no child records.
But I want to select all the parent records that don't have a Compliance Item of type 17 or 18
How can I add this condition to my query?
select OC.lOwnersCorporationID
From Strata.dbo.OwnersCorporation OC
Left Join ComplianceDEMO.dbo.ComplianceItem CI on OC.lOwnersCorporationID = CI.OwnersCorporationID
Where CI.OwnersCorporationID IS NULL
AND OC.bManaged = 'Y'
BTW I don't care about performance as this query will only be run once and used as part of an insert statement to create missing records.
UPDATE
Obviously my data is more complex than this but this should give you an idea.
Table Definition
Ownerscorporation Table
OwnersCorporationID INT PK
PlanNumber Varchar(10)
ComplianceItem Table
ComplianceItemID INT PK
ComplianceTypeID INT FK
OwnersCorporationID INT FK
ComplianceType Table
ComplianceTypeID INT PK
Name varchar(50)
Test Data
Owners Corporation Table
ID PlanNumber
===============================================
1 1001
2 1002
3 1003
Compliance Item Table
ComplianceItemID ComplianceTypeID OwnersCorporationID
==================================================================
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 1 2
7 2 2
8 4 2
9 1 3
10 2 3
11 3 3
Compliance Type Table
==================================================================
ComplianceTypeID Name
1 Asbestos Report
2 Capital Works Fund
3 Anchor Point Compliance
4 Window Lock Compliance
5 Pool Compliance
The Problem
=========================
Probably easier in two parts.
Find all the OwnersCorporations That Don't have a Compliance type of 4
Find all the OwnersCorporations That Don't have a Compliance type of 5
Expected Results
=========================
Part 1
OwnersCorporationID
3
Part 2
OwnersCorporationID
2
3
UPDATE 2
It looks like Matt's answer in the comments worked on the test data.
Select OwnersCorporationID
From OwnersCorporationTable
Where OwnersCorporationID Not In
(
Select OwnersCorporationID
From ComplianceItemTable
Where (ComplianceTypeID =5 )
Returned 2 and 3
And
Select OwnersCorporationID
From OwnersCorporationTable
Where OwnersCorporationID Not In
(
Select OwnersCorporationID
From ComplianceItemTable
Where (ComplianceTypeID =4 )
Returned 3
I will need to run it in two parts when I use it to create the missing records.
I'll try and convert it to run on my real data. Hopefully I haven't simplified it too much :)
Thanks for your help Matt. If you would like the points please post this as an answer.
You may be looking for this
SELECT OC.lOwnersCorporationID
From Strata.dbo.OwnersCorporation OC
INNER JOIN ComplianceDEMO.dbo.ComplianceItem CI on OC.lOwnersCorporationID = CI.OwnersCorporationID AND
CI.ComplianceTypeID NOT IN (17,18) AND
OC.bManaged = 'Y'

SQL (Redshift) to get the intersect of multiple tables

I'm using Redshift and have 6 tables of IDs in. I want to get the intersect between each of the tables.
So my final output would look something like this:
Table 1 & Table 2 have 10% common IDs
Table 1 & Table 3 have 50% common IDs
.....
.....
Table 6 & Table 4 have 20% common IDs
Table 6 & Table 5 have 3% common IDs
I can easily get the data, but it would be a lot of repeating the same SQL, so I've tried to create some tables of all the IDs and tables they are in but I'm stuck as to what to get the data in one or two SQL's.
Any ideas welcome!
you could try to full join all these tables by ID in a subquery and then use conditional aggregate so that Table 1 & Table 2 have 10% common IDs would be expressed as
100.0*sum(case when id1 is not null and id2 is not null then 1 end)/count(id1)
(taking Table 1 row count as denominator)

How to convert rows into column while inserting into temp table?

I want to convert rows into column while inserting into temp table. I read somewhere about crosstab but not exactly sure how to use it for my purpose.
So suppose firstly I query like
select cat_id, category_name from category_tab;
cat_id category_name
111 books
112 pen
113 copy
Now I want a temp table which should be like
s_no books pen copy
1
2
3
s_no will be serial number and rows of previous query becomes the column in new temp table.
How can I achieve this task in postgres ?

Adding standard metadata to a table - Postgresql

I worked with the ESRI geodb where you can insert (into the "property" of a table) some metadata (also compliant to some International standard), like creation date, organization, source, copyright info etc.
Is there something similar in Postgres, for metadata of a table as a whole? I know only COMMENT but it seems too poor for my purposes.
Here is a very naive example of how you can save your wanted "metadata"
lest assume you have two tables you want to have data about:
t=# create table so66(i int, t text);
CREATE TABLE
Time: 5.431 ms
t=# create table so67(i int, t text);
CREATE TABLE
Time: 4.797 ms
and a "metadata" holder table:
t=# create table metadata(tname text, created timestamptz, details json);
CREATE TABLE
Time: 6.814 ms
t=# insert into metadata select 'so66',now(),'{"organization":"n/a","source":"manual","catalog":false}';
INSERT 0 1
Time: 3.144 ms
t=# insert into metadata select 'so76',now(),'{"organization":"home","source":"manual","catalog":true}';
INSERT 0 1
Time: 0.907 ms
t=# select * from metadata ;
tname | created | details
-------+-------------------------------+----------------------------------------------------------
so66 | 2017-04-21 09:24:08.233346+00 | {"organization":"n/a","source":"manual","catalog":false}
so76 | 2017-04-21 09:24:26.641526+00 | {"organization":"home","source":"manual","catalog":true}
(2 rows)
Time: 0.253 ms
I used json to save arbitrary details. Of course you can add columns with special data types for your needs. Also you might want to use oids instead of table names or do some logic on insert/update of it.