PeopleSoft Query Union to Existing Query - sql

In PS Query, is it possible to create a union with an existing query? Basically I need to take a query, union it with a duplicate (with a minor change). Unfortunately, they query is quite complex, with tons of expressions.
I have full access to the database, so I'm not limited to the query tool (for building - this is something that a basic user will run from query viewer)

As a direct answer to your question, no, that isn't possible in ps qry.
What I would do is, since you have full access to the database, create the query you want to connect to as a view, and then use a union w/ that view instead of the query, ie.
(the query you're working on)
union
select * from name_of_new_vw

Related

Query with subquery is not updateable. Is there a work-around?

I'm working with a database in Microsoft Access, and this is the scenario I'm trying to find a solution for:
Companies send in applications to apply for more capacity. The application can contain multiple smaller capacities which add up to the total capacity of the application. The applications are stored in the table tApplication and the capacities associated with the application are stored in tCapacity.
I wish to present information about the application including the total capacity. Therefore I have a form with the following Data Source:
SELECT tApplication.ID,tApplication.CompanyID, tApplication.statusID,
(SELECT SUM(tCapacity.Capacity) FROM tCapacity WHERE tApplication.ID = tCapacity.ApplicationID) AS TotalCapacity
FROM tApplication;
This query gives me the information I need, but it hinders me from edit anything in the recordset (Using subqueries apparently makes your query not updateable in Microsoft Access). Is there a way around this? I've been playing around with the idea to store the applications and capacities in the same table, but I'm not sure it's a good design choice.
I used DSUM() which worked great. Thank you to June7.
SELECT tApplication.ID,tApplication.CompanyID, tApplication.statusID,
DSUM( "tCapacity.Capacity" , "tCapacity", "tCapacity.ApplicationID = " & tApplication.ID) AS TotalCapacity
FROM tApplication;
Try this two options:
Make Table Queries
Make-Table queries are just like Select queries except their results are put into a new table rather than a datasheet view. You specify the table name and it is created. If the table exists, it is replaced. To create a Make-Table query, open the query (qryCustomerSales) in design mode, and choose Make-Table Query from the Query menu
Use Append Queries Rather than Make-Table Queries
An alternative to Make-Table queries is an Append query. Append queries let you insert records from a query into an existing table. If you just have a one step process, there usually is not much difference. However, if you have multiple steps, Append queries have a clear advantage.
More information:
https://www.fmsinc.com/MicrosoftAccess/query/non-updateable/index.html#Example2

Iterative union SQL query

I'm working with CA (Broadcom) UIM. I want the most efficient method of pulling distinct values from several views. I have views that start with "V_" for every QOS that exists in the S_QOS_DATA table. I specifically want to pull data for any view that starts with "V_QOS_XENDESKTOP."
The inefficient method that gave me quick results was the following:
select * from s_qos_data where qos like 'QOS_XENDESKTOP%';
Take that data and put it in Excel.
Use CONCAT to turn just the qos names into queries such as:
SELECT DISTINCT samplevalue, 'QOS_XENDESKTOP_SITE_CONTROLLER_STATE' AS qos
FROM V_QOS_XENDESKTOP_SITE_CONTROLLER_STATE union
Copy the formula cell down for all rows and remove Union from the last query as well
as add a semicolon.
This worked, I got the output, but there has to be a more elegant solution. Most of the answers I've found related to iterating through SQL uses numbers or doesn't seem quite what I'm looking for. Examples: Multiple select queries using while loop in a single table? Is it Possible? and Syntax of for-loop in SQL Server
The most efficient method to do what you want to do is to do something like what CA's scripts do (the ones you linked to). That is, use dynamic SQL: create a string containing the SQL you want from system tables, and execute it.
A more efficient method would be to write a different query based on the underlying tables, mimicking the criteria in the views you care about.
Unless your view definitions are changing frequently, though, I recommend against dynamic SQL. (I doubt they change frequently. You regenerate the views no more frequently than you get a new script, right? CA isn't adding tables willy nilly.) AFAICT, that's basically what you're doing already.
Get yourself a list of the view names, and write your query against a union of them, explicitly. Job done: easy to understand, not much work to modify, and you give the server its best opportunity to optimize.
I can imagine that it's frustrating and error-prone not to be able to put all that work into your own view, and query against it at your convenience. It's too bad most organizations don't let users write their own views and procedures (owned by their own accounts, not dbo). The best I can offer is to save what would be the view body to a file, and insert it into a WITH clause in your queries
WITH (... query ...) as V select ... from V

How do I query across many tables in redshift without UNION ALL?

I'm looking for a dumb way to write the same select query across all tables. For example in Google Bigquery I can query like this using wild cards
select COMPLICATED QUERY HERE from `myproject:mytable_2017_1_*`;
How can I do the equivalent in redshift?
The wildcard syntax is not available for Amazon Redshift. Each query must specifically reference the table(s) it wishes to use.
You could create a VIEW that does the UNION ALL for you, and then you could just query the view.

Retrieving data from multiple schemas in a single database

I'm having more than one schemas inside a single database.
Say,
DBTEST (Database)
SCH001 (schema1)
SCH002 (schema2)
Similarly, I'm having a table called Tbl which is present in the both schemas given above.
Now i had retrieved datum from the tables SCH001.tbl and SCH002.tbl by using the union all key word like this below,
select * From SCH001.tbl union all select * From SCH002.tbl
The above query is working with out any issue, But my boss is asking me to use some other technique to achieve the same(with out union all / Union). So what i am trying to ask is,
Is there any remedy available for Union all to get the same result in my case ? If not then, kindly tell the reason that why Union all cannot be replaced in this case.?
If your boss thinks its ugly to use this union all (all) the time, why not make a view of it?
just reference all the fields in both union parts, and present this as combined view
(i say this cause its generally bad to use * in views)
i do not know if you could index such a view, however its properly worth checking out,
i believe you have to use one schema only when doing indexed views, but i'm unsure of this
(however if possible, this would be a better solution than "just" the union)
(new to that area)
:EDIT: you cannot (in Microsoft sql) make the materialized views on a union, and/or thereby on different schemas, (they are also schema bound)
i would personally use the view anyway, its just a nice way to give a programmatic interface to your data, that you can replace one day!.
:EDIT:
but fact is... its still a union under the hood

Why is UNION faster than an OR statement [duplicate]

This question already has answers here:
UNION ALL vs OR condition in sql server query
(3 answers)
Closed 9 years ago.
I have a problem where I need to find records that either have a measurement that matches a value, or do not have that measurement at all. I solved that problem with three or four different approaches, using JOINs, using NOT IN and using NOT EXISTS. However, the query ended up being extremely slow every time. I then tried splitting the query in two, and they both run very fast (three seconds). But combining the queries using OR takes more than five minutes.
Reading on SO I tried UNION, which is very fast, but very inconvenient for the script I am using.
So two questions:
Why is UNION so much faster? (Or why is OR so slow)?
Is there any way I can force MSSQL to use a different approach for the OR statement that is fast?
The reason is that using OR in a query will often cause the Query Optimizer to abandon use of index seeks and revert to scans. If you look at the execution plans for your two queries, you'll most likely see scans where you are using the OR and seeks where you are using the UNION. Without seeing your query it's not really possible to give you any ideas on how you might be able to restructure the OR condition. But you may find that inserting the rows into a temporary table and joining on to it may yield a positive result.
Also, it is generally best to use UNION ALL rather than UNION if you want all results, as you remove the cost of row-matching.
There is currently no way in SQL Server to force a UNION execution plan if no UNION statement was used. If the only difference between the two parts is the WHERE clause, create a view with the complex query. The UNION query then becomes very simple:
SELECT * FROM dbo.MyView WHERE <cond1>
UNION ALL
SELECT * FROM dbo.MyView WHERE <cond2>
It is important to use UNION ALL in this context when ever possible. If you just use UNION SQL Server has to filter out duplicate rows, which requires an expensive sort operation in most cases.