How to populate data from a table, depending upon another table - sql

My question is super straight,
I have a table naming FAQ_MASTER having data of faqs
I have another table naming FAQ_OVERRIDE having data of overrided faqs with foreign key company_id from different table
Now i want to populate data from FAQ_MASTER, if FAQ_OVERRIDE is not populating data depending upon the company_id fetched from session variable
if FAQ_OVERRIDE is populating data, then populating data from FAQ_MASTER is not required.
i want to run the query in postreSQL
What i have tried :
select
*
from
(
SELECT
distinct fm.faq_uid as faqUid,
case
when fm.qa_text is not null then fm.qa_text
else fm.qa_text
end as qaText,
case
when fm.sort_order is not null then fm.sort_order
else fm.sort_order
end as sortOrder,
fm.end_date as endDate
FROM rnr.faq_master fm
) faq_qry
left outer join rnr.company_faq_override fo on faq_qry.faqUid = fo.faq_override_uid and fo.mp_company_uid = 734
WHERE
faq_qry.endDate is null
AND
(
faq_qry.mp_company_uid is null or fo.mp_company_uid = 734
)
ORDER BY
faq_qry.sortOrder

Related

How do I flatten an array in SQL

I am trying to extract custom_entry(s) from the table t_Open_Issues and they are being returned as an Array. They are user defined fields that can be created for andy departments issues. Using SQL I am trying to find the code that will allow me to return them as part of a single table and have them not be an array inside the primary table. I am looking for the SQL statement(s) that will allow me to flatten the array custom_entry and return .entry_name and .entry.value as unique columns in the table t_Open_Issues.
The SQL Statement below captures what I need, but the custom entries are returned as an array, instead of columns in t_Open_Issues.
CREATE OR REPLACE TABLE.t_Open_Issues AS SELECT summary,
custom_entry.entry_name,
custom_entry.value
issue_num,
status,
createddate,
modifieddate
FROM t_Issue_Table
WHERE (
custom_entry.entry_name = 'Audit Number:' OR
custom_entry.entry_name = 'Drawings attached:' OR
custom_entry.entry_name = 'Checklist Complete:'
)
AND department_num = '123456'
AND (
status = 'ASSIGNED' OR
status = 'COMPLETE' OR
status = 'PROCESSED'
)

I want update from one table to another table based on true or false condition

I have one table that contains several tasks which is belongs to some categories and one column called 'completed' which tells particular task is completed or not, based on this status I want to update as 'completed' for particular category in another table which contains categories. How to do in MS SQL query? Here, for 'completed' column I am using bit as datatype.
Update table category set complete = True where name In
(
select distinct t.category
from task t
where task.complete = true And not exists (
select t1.category
from task t1
where t1.category = t.category And t1.complete = false
)
)
You may create a AFTER INSERT TRIGGER on your first table thus you can update your second table column value referencing your status column value in the earlier.

Insert data from one table to other using select statement and avoid duplicate data

Database: Oracle
I want to insert data from table 1 to table 2 but the catch is, primary key of table 2 is the combination of first 4 letters and last 4 numbers of the primary key of table 1.
For example:
Table 1 - primary key : abcd12349887/abcd22339887/abcder019987
In this case even if the primary key of table 1 is different, but when I extract the 1st 4 and last 4 chars, the output will be same abcd9887
So, when I use select to insert data, I get error of duplicate PK in table 2.
What I want is if the data of the PK is already present then don't add that record.
Here's my complete stored procedure:
INSERT INTO CPIPRODUCTFAMILIE
(productfamilieid, rapport, mesh, mesh_uitbreiding, productlabelid)
(SELECT DISTINCT (CONCAT(SUBSTR(p.productnummer,1,4),SUBSTR(p.productnummer,8,4)))
productnummer,
ps.rapport, ps.mesh, ps.mesh_uitbreiding, ps.productlabelid
FROM productspecificatie ps, productgroep pg,
product p left join cpiproductfamilie cpf
on (CONCAT(SUBSTR(p.productnummer,1,4),SUBSTR(p.productnummer,8,4))) = cpf.productfamilieid
WHERE p.productnummer = ps.productnummer
AND p.productgroepid = pg.productgroepid
AND cpf.productfamilieid IS NULL
AND pg.productietype = 'P'
**AND p.ROWID IN (SELECT MAX(ROWID) FROM product
GROUP BY (CONCAT(SUBSTR(productnummer,1,4),SUBSTR(productnummer,8,4))))**
AND (CONCAT(SUBSTR(p.productnummer,1,2),SUBSTR(p.productnummer,8,4))) not in
(select productfamilieid from cpiproductfamilie));
The highlighted section seems to be wrong, and because of this the data is not picking up.
Please help
Try using this.
p.productnummer IN (SELECT MAX(productnummer) FROM product
GROUP BY (CONCAT(SUBSTR(productnummer,1,4),SUBSTR(productnummer,8,4))))

Find which column differs between 2 rows?

We are using audit tables for each operational table, which stores the previous value of its operational equivalent plus change date, change type (UPDATE or DELETE) and its own auto incremental Primary Key.
So, for a table Users with columns UserID, Name, Email there would be a table xUsers with columns ID, OpererationType, OperationDate, UserID, Name, Email.
See that the xTable contains every column that its 'parent' does with 3 extra fields. This pattern is repeated for all tables used by our system.
table Users:
UserID int
Name nvarchar
Email nvarchar
table xUsers:
xUserID int
OpererationType int
OperationDate datetime
UserID int
Name nvarchar
Email nvarchar
Now, my question:
If I have a certain UserID, for which there is 2 entries in the xUsers table when the email was changed twice,
how would I construct a query that identifies which columns (can be more than 1) differ between the two rows in the audit table?
If I'm understanding this correctly, you'd like to create a query passing in the UserID as a parameter, which I'll call #UserID for the following example.
This query will select all rows from xUsers joined onto itself where there is a difference in a non-UserID column, using a series of case statements (one per column) to pull out specifically which columns differ.
SELECT *
, CASE
WHEN a.OperationType <> b.OperationType
THEN 1
ELSE 0
END AS OperationTypeDiffers
, CASE
WHEN a.OperationDate <> b.OperationDate
THEN 1
ELSE 0
END AS OperationDateDiffers
FROM xUsers a
JOIN xUsers b
ON a.xUserID < b.xUserID
AND a.UserID = b.UserID
AND (a.OperationType <> b.OperationType
OR a.OperationDate <> b.OperationDate) -- etc.
WHERE a.UserID = #UserID
You can put the rows of xUsers in a temporary table and then make a while cycle to go for each one and compare the results.
OR
You can do some dynamic SQL and use sysobjects and syscolumns tables to compare each result. It would be more dynamic and then it would be easy to implement for other tables.

How to get one common value from Database using UNION

2 records in above image are from Db, in above table Constraint are (SID and LINE_ITEM_ID),
SID and LINE_ITEM_ID both column are used to find a unique record.
My issues :
I am looking for a query it should fetch the recored from DB depending on conditions
if i search for PART_NUMBER = 'PAU43-IMB-P6'
1. it should fetch one record from DB if search for PART_NUMBER = 'PAU43-IMB-P6', no mater to which SID that item belong to if there is only one recored either under SID =1 or SID = 2.
2. it should fetch one record which is under SID = 2 only, from DB on search for PART_NUMBER = 'PAU43-IMB-P6', if there are 2 items one in SID=1 and other in SID=2.
i am looking for a query which will search for a given part_number depending on Both SID 1 and 2, and it should return value under SID =2 and it can return value under SID=1 only if the there are no records under SID=2 (query has to withstand a load of Million record search).
Thank you
Select *
from Table
where SID||LINE_ITEM_ID = (
select Max(SID)||Max(LINE_ITEM_ID)
from table
where PART_NUMBER = 'PAU43-IMB-P6'
);
If I understand correctly, for each considered LINE_ITEM_ID you want to return only the one with the largest value for SID. This is a common requirement and, as with most things in SQL, can be written in many different ways; the best performing will depend on many factors, not least of which is the SQL product you are using.
Here's one possible approach:
SELECT DISTINCT * -- use a column list
FROM YourTable AS T1
INNER JOIN (
SELECT T2.LINE_ITEM_ID,
MAX(T2.SID) AS max_SID
FROM YourTable AS T2
GROUP
BY T2.LINE_ITEM_ID
) AS DT1 (LINE_ITEM_ID, max_SID)
ON T1.LINE_ITEM_ID = DT1.LINE_ITEM_ID
AND T1.SID = DT1.max_SID;
That said, I don't recall seeing one that relies on the UNION relational operator. You could easily rewrite the above using the INTERSECT relational operator but it would be more verbose.
Well in my case it worked something like this:
select LINE_ITEM_ID,SID,price_1,part_number from (
(select LINE_ITEM_ID,SID,price_1,part_number from Table where SID = 2)
UNION
(select LINE_ITEM_ID,SID,price_1,part_number from Table SID = 1 and line_item_id NOT IN (select LINE_ITEM_ID,SID,price_1,part_number from Table SID = 2)))
This query solved my issue..........