How to improve Teradata query for pagination? - teradata

Hi Guys i'm trying to use pagination for my teradata query as below:
SELECT RANK() OVER (ORDER BY id,firstname,lastname,grade,gender,age,profession) as row_num, ORDER BY id,firstname,lastname,grade,gender,age,profession FROM table-1 QUALIFY row_num BETWEEN 0 and 1000 ;
the query works fine. However, i'm trying to see if there is an other way to query the table based on * instead of stating all table columns in query 2 times.
Apprecite your input on this request.

Related

Get top records by latest date in Azure CosmosDB

I have a table like this:
Assume there are lots of Names (i.e., E,F,G,H,I etc.,) and their respective Date and Produced Items in this table. It's a massive table, so I'd want to write an optimised query.
In this, I want to query the latest A,B,C,D records.
I was using the following query:
SELECT * FROM c WHERE c.Name IN ('A','B','C','D') ORDER BY c.Date DESC OFFSET 0 LIMIT 4
But the problem with this query is, since I'm ordering by Date, the latest 4 records I'm getting are:
I want to get this result:
Please help me in modifying the query. Thanks.

PeopleSoft Query - finding people without leave type

I'm working with PeopleSoft's query manager and I'm having trouble creating a report that will find all active employees who do not have a certain leave type.
I have the two tables (Employees - Non terminated Employees and Leave_Accrual-EE). They are left outer joined. The field in question is PLAN_TYPE. Now, I've tried creating a filter to pull in all employees who do not have plan type 54. The criteria is B.PLAN_TYPE not equal to 54, but that still brings up everyone, it just doesn't bring up the row for 54.
I feel like I'm missing something obvious - maybe I have to create a subquery? If so, I have never done this in PeopleSoft.
Anyone have any advice?
Original SQL screenshot.
UPDATED
This is less of a PeopleSoft question, and more of a SQL question.
The problem you have been running into is that you are doing a per-row filter and excluding only rows that have the undesirable code.
What you need to do instead is exclude all rows for a user that has the undesirable code in any row.
This can be done with a NOT IN or NOT EXISTS query.
e.g.
SELECT EMPLID
FROM TABLE1
WHERE
EMPLID NOT IN
(
SELECT EMPLID
FROM TABLE1
WHERE CODE = 123
)
/
alternately
SELECT A.EMPLID
FROM TABLE1 A
WHERE
NOT EXISTS
(
SELECT B.EMPLID
FROM TABLE1 B
WHERE
B.CODE = 123
AND B.EMPLID = A.EMPLID
)
/
See this SQL Fiddle example to test out the SQL:
http://sqlfiddle.com/#!4/2b0f6/7
To do this in PS Query, you could do this by adding a criteria with a subquery on the right side of the equivalence.
Here is some documentation:
Home > PeopleSoft PeopleTools 8.53 > PeopleSoft Query > Working with Subqueries

How to get number of rows updated by a query in .net

In my database, i have a trigger which insert the change log entries when a row in Table tblA is updated.
Now, in my code i have to update it through a plain Sql query like
int count = DBContext.ExecuteStoreCommand("<sql query to update records>");
This count variable contains the number of rows affected(no of rows updated + no of rows inserted) due to query.
So my question is, How do i can get only the number of updated rows?
Currently i'm using Entity framework 4. I have looked for solution through connected or disconnected model but couldn't help myself.
int count = DBContext.ExecuteStoreCommand("");
I think you hv to change this to return Select result set
then do this,
<sql query to update>
Select ##RowCount rowcountAffected
Or
suppose your update is
update table1 set col1='foo' where id=2
select count(*) rowcountAffected from table1 where id=2
The most efficient way to return row affected can be
i) Assuming you only update (don't refresh any record after that)
Put Set Nocount ON
Declare #Output parameter inside proc

Sqlite fetch a particular value not present within the specified limit

I have a table Customer which has the following columns,
user_name,current_id,id,params,display,store.
I am writing a query like this,
SELECT * FROM Customer WHERE user_name='Mike' AND current_id='9845' AND id='Get_Owner' AND params='owner=1' order by(display) limit 6 offset 0
Now there are times when I want to fetch a particular value which is not there in the first six and I want to fetch that particular value and rest 5 values in the same way like above how can I do that?
For example I want something like this
SELECT * FROM Customer WHERE user_name='Mike' AND current_id='9845' and id='Get_Owner' AND params='owner=1' AND stored='Shelly.Am'
I want Shelly.Am and other 5 value like my first query
You can combine two queries by using a compound query.
The ORDER BY/LIMIT clauses would apply to the entire compound query, so the second query must be moved into a subquery:
SELECT *
FROM Customer
WHERE user_name='Mike'
AND current_id='9845'
AND id='Get_Owner'
AND params='owner=1'
AND stored='Shelly.Am'
UNION ALL
SELECT *
FROM (SELECT *
FROM Customer
WHERE user_name='Mike'
AND current_id='9845'
AND id='Get_Owner'
AND params='owner=1'
AND stored!='Shelly.Am'
ORDER BY display
LIMIT 5);

Get the number of records from MDX query with Subcubes

I'm developing a system for generate mdx queries from entity "FilterCriterias" and related info like the number of records of a query, so I need a generic way to get the number of records of a mdx query than use subcubes. In a normal query I do something like:
WITH
MEMBER [MyCount] AS
Count([Date].[Date].MEMBERS)
SELECT
{[MyCount]} ON 0
FROM [Adventure Works];
But I have problems when use this way in queries a little more complexes like that
WITH
MEMBER [MyCount] AS
Count([Date].[Date].MEMBERS)
SELECT
{[MyCount]} ON 0
FROM
(
SELECT
{[Measures].[Sales Amount]} ON 0
,{[Date].[Date].&[20050701] : [Date].[Date].&[20051231]} ON 1
FROM
(
SELECT
{[Sales Channel].[Sales Channel].&[Internet]} ON 0
FROM [Adventure Works]
)
);
I guess the logic response could be the number of records of [Date].[Members] left in the subcube, but I get a result without columns and rows. I'm newbie in mdx language and I don't understand this behavior. Exists some generic way to get the number of records from a "base" query just like SELECT COUNT(*) FROM () in plain SQL?
The structure is quite different to a ralational SELECT COUNT(*) FROM ().
I believe that the structure of a sub-select will be very similar to that of a sub-cube and reading through this definition from MSDN (https://msdn.microsoft.com/en-us/library/ms144774.aspx) of what a sub-cube contains tells us that it isn't a straight filter like in a relational query:
Admittedly I still find this behaviour rather "enigmatic" (a polite way of saying "I do not understand it")
Is there a workaround?

Resources