I'm extracting data from a Salesforce REST endpoint with the following set of queries:
SELECT COUNT(Id) FROM Table1__c WHERE CreatedDate < 2017-10-18T16:16:03Z
This returns a result of: 216
SELECT Id FROM Table1__c WHERE CreatedDate < 2017-10-18T16:16:03Z ORDER BY CreatedDate ASC LIMIT 100 OFFSET 0
This returns the desired 100 results which I format as follows for the next query:
'result1', 'result2',...,'result100'
This query however is only returning 87 of the desired 100 records:
SELECT
Id, CreatedDate, A whole lotta fields,
(SELECT Name, more fields FROM Table2__r),
(SELECT Name, Even more fields FROM Table3__r),
(SELECT Name, Yeah, more fields FROM Table4__r),
(SELECT Name, You guessed it! more fields FROM Table5__r),
(SELECT Name, finally, the last fields FROM Table6__r)
FROM Table1__c WHERE Id IN (previous formatted result)
So my desire is for the query (ignoring the where-clause for a moment) to perform a left-outer join on Table1__c with the other tables and limit the results to only the Ids from the previous query. However, the where-clause seems to be forcing it into a left-inner join between Table1__c and the other tales? I'm not entirely sure. I'm querying against the Salesforce v39 REST interface.
nextRecordsUrl is getting populated due to the size of the response and needs to be followed.
I'm using Postgresql 9.2 and have a simple students table as follow
id | proj_id | mark | name | test_date
I have 2 queries which is described below
select * from (select distinct on (proj_id) proj_id , mark, name,
test_date from students )
t
where t.mark <= 1000
VS
select distinct on (proj_id) proj_id , mark, name, test_date from
students where mark <= 1000
when I run each query for more than 10000 records each query returns different result especially result count although for less than 3000 records the result would be the same.
is this postgresql 9.2 bug or I'm missing something ?
Your queries are producing two different sets of results because they are applying the logic differently.
The first query is getting a distinct set of results, and then applying the 'mark' filter.
The second query is applying the 'mark' filter, and then getting a distinct set of results.
As you don't have any ordering applied the first query could potential return a different number of rows each time it is run - as the mark field could contain any of the values that relate to the proj_id.
i Have two data sets. First one dataset shows Total counts against each person and the Second one Dataset shows SLA breached counts against respective person. As before i used two charts for for the above datasets and result will be achieved but now i want these two datasets result in just one chart. kindly tell me how to do this to achieve desired result.
Dataset 1 (Total Counts query is):
Select Count(I.Id) AS TotalId,
U.Firstname AS Username
From I
Group U.Firstname
Dataset 2 (SLA Breached query is):
Select Count(I.Id) AS BreachedId,
U.Firstname AS Username
From I
***Where I.ResolvedDate > I. ResolvedByDate***
Group U.Firstname
The highlighted query is just used in dataset 2 which shows SLA Breached counts. Rest of the query in both dataset are same. so now i don`t know how show toghether. I used union all function but it add both TotalId and BreachedId.
Thanks
AhsanMisbah
Do you want both aggregates to appear in the same query? if so change your select to
select U.Firstname AS Username, Count(I.Id) AS TotalId, nullif(SUM(case when I.ResolvedDate > I. ResolvedByDate then 1 else 0 end ),0) as Breached
May I know is there any solution to get the result without ordering in Oracle? It is because when I execute the query as follows, it seems to automatically helps me to sort it by ID field.
SELECT ID FROM USER WHERE ID IN (5004, 5003, 5005, 5002, 5008);
Actual results Expected results
---5002 ---5004
---5003 ---5003
---5004 ---5005
---5005 ---5002
---5008 ---5008
Million thanks if you guys have solutions on this.
SELECT statements return the rows of their result sets in an unpredictable order unless you give an ORDER BY clause.
Certain DBMS products give the illusion that their result sets are in a predictable order. But if you rely on that you're bound to be disappointed.
This is one way I've seen in the past using INSTR:
SELECT *
FROM YourTable
WHERE ID IN (5004, 5003, 5005, 5002, 5008)
ORDER BY INSTR ('5004,5003,5005,5002,5008', id)
SQL Fiddle Demo
I've also seen use of CASE like this:
ORDER BY
CASE ID
WHEN 5004 THEN 1
WHEN 5003 THEN 2
WHEN 5005 THEN 3
WHEN 5002 THEN 4
WHEN 5008 THEN 5
END
if you want to keep the order as your in list, you can do something like this:
SQL> create type user_va as varray(1000) of number;
2 /
Type created.
SQL> with users as (select /*+ cardinality(a, 10) */ rownum r, a.column_value user_id
2 from table(user_va(11, 0, 19, 5)) a)
3 select d.user_id, d.username
4 from dba_users d
5 inner join users u
6 on u.user_id = d.user_id
7 order by u.r
8 /
USER_ID USERNAME
---------- ------------------------------
11 OUTLN
0 SYS
19 DIP
5 SYSTEM
i.e we put the elements into a varray and assign a rownum prior to merging the set. we can then order by that r to maintain the order of our in list. The cardinality hint just tells the optimizer how many rows are in the array (doesn't have to be dead on, just in the ballpark..as without this, it will assume 8k rows and may prefer a full scan over an index approach)
if you don't have privs to create a type and this is just some adhoc thing, there's a few public ones:
select owner, type_name, upper_bound max_elements, length max_size, elem_type_name
from all_Coll_types
where coll_type = 'VARYING ARRAY'
and elem_type_name in ('INTEGER', 'NUMBER');
There is no guarantee of sort order without an ORDER BY clause.
If your question is about why the ordering occurs then the answer is: Do you have an index or primary key defined on the column ID? If yes the database responds to your query with an index scan. That is: it looks up the IDs in the IN clause not in the table itself but in the index defined on your ID-column. Within the index the values are ordered.
To get more information about the execution of your query try Oracle's explain plan feature.
To get the values in a certain order you have to add an ORDER BY clause. One way of doing this would be
select ID
from USER
where ID in (5004, 5003, 5005, 5002, 5008)
order by
case ID
when 5004 then 1
when 5003 then 2
...
end;
A more general way would be to add an ORDERING column to your table:
select ID
from USER
where ID in (5004, 5003, 5005, 5002, 5008)
order by
ORDERING;
Another solution that I found here.
select ID
from USER
where ID in (5004, 5003, 5005, 5002, 5008)
order by decode(ID, 5002, 1, 5003, 2, 5004, 3, 5005, 4, 5008, 5);
order by decode(COLUMN NAME, VALUE, POSITION)
*Note: Only need to repeat the VALUE and POSITION
And yah, thanks for all the responds! I am really appreciate it.
booking id booked_journey_info_id PNR
1 1000 PNR5000
1 1001 PNR5000
1 1002 PNR7000
1 1003 PNR7000
In above table I want to retrieve exactly one record for each PNR. So for above case, for PNR - PNR5000 query should retrieve exact one booked_journey_info_id(1000 or 10001) and for PNR - PNR7000 query should retrieve exact one booked_journey_info_id(1002 or 10003).
I need only 1 record per PNR.
If it does not matter which booked_journey_info_id you get from the group, you can use the max() aggregate function to pull the max id:
SELECT MAX(booked_journey_info_id)
FROM myTbl GROUP BY PNR
Basically the aggregate function tells which value to pull from the group.
Simpler than Shredder's answer is
select * from tablename group by PNR
This selects an arbitrary row, as (seemingly) desired.
It sounds like you want complete rows.
The following should work in any database.
select t.*
from t join
(select pnr, max(booked_journey_info_id) as maxid
from t
group by pnr
) tpnr
on tpnr.maxid = t.booked_journey_info_id