How do I flatten an array in SQL - 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'
)

Related

setting up a hierarchy for a query in SQL

I am attempting to set up a query that will return records following a certain hierarchy. My code thus far returns one record from groups where the count =2 where my water value is not equal to 7. If there are no records that match this criteria, I need it to look in the 'earth' field, and return values within the same table that = 'Y'. I'm relatively new to SQL, so any help you guys can give is appreciated
select collection, id
from TABLE a
where a.collection in (
select collection
from TABLE a
where to_number(substr(collection 10,3)) =2
group by collection
having max(water) <> min(water)
and min(water) = '01')
and water <> '07';

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.

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

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

SQL server query for pulling data from child table

I have a table called Identifier which has identifierType, identifierValue and foreignkey to patient table.
One patient can have multiple identifiers so for a given patient there will be multiple rows in identifier table.
I want to pull value of patientforeign key from this table which meets given criteria,
one example is I want to find
patientId where identifierType = 'PatientFirst"
and identifierValue = 'sally'
and identifierType= 'patientFirst'
and identifier value = 'sally'.
what will be sql statement to pull this result in sqlserver
References : ( http://sqlfiddle.com/#!3/33fc6/2/0 )
Seems a bit easy for this website, no? :)
SELECT fk_patientID
FROM identifier
WHERE IdentifierType = 'PatientFirst'
AND IdentifierValue = 'sally'
Pivot table may be useful to you as well if you'd like to flatten certain properties into a single row per patient ID:
;with PatientFullName( fk_patientId, PatientLast, PatientFirst )
as
(
select
fk_patientId
, pt.PatientLast
, pt.PatientFirst
from
Identifier
pivot
(
MAX(identifierValue)
for identifierType in ( [PatientLast], [PatientFirst] )
) as pt
)
select
*
from
PatientFullName pfn
where
pfn.PatientLast = 'Doe'
and pfn.PatientFirst = 'Sally'

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.