query data in the order of IN clause - sql

select osmid,ST_X(shape),ST_Y(shape)
from osmpoints
where (osmpoints.osmid, osmpoints.osmtimestamp)
IN (select osmid,MAX(osmtimestamp)
from osmPoints
GROUP BY osmid
Having MAX(osmtimestamp) <= '2019-09-16T01:23:55Z'
AND osmid in ('4426786454','1861591896','1861591869','1861591895',
'4426786455','2038185115','1861591853','6797739995',
'2299605892','6797739994','1861591898','2038185111','4426786454'));
when I run this query, I get sorted rows based on osmid column.
but my question is how can I get rows in order of osmid that use in the IN clause?

You can use the same technique as shown here:
with input(idlist) as (
values (
array['4426786454','1861591896','1861591869','1861591895',
'4426786455','2038185115','1861591853','6797739995',
'2299605892','6797739994','1861591898','2038185111','4426786454']::text[])
)
select p.osmid,ST_X(shape),ST_Y(shape)
from osmpoints p
cross join input
where (p.osmid, p.osmtimestamp) IN (select osmid,MAX(osmtimestamp)
from osmPoints
cross join input
GROUP BY osmid
Having MAX(osmtimestamp) <= '2019-09-16T01:23:55Z'
AND osmid = any(idlist))
order by array_position(idlist, osmid);

Related

SQL query to return duplicate rows for certain column, but with unique values for another column

I have written the query shown here that combines three tables and returns rows where the at_ticket_num from appeal_tickets is duplicated but against a different at_sys_ref value
select top 100
t.t_reference, at.at_system_ref, at_ticket_num, a.a_case_ref
from
tickets t, appeal_tickets at, appeals_2 a
where
t.t_reference in ('AB123','AB234') -- filtering on these values so that I can see that its working
and t.t_number = at.at_ticket_num
and at.at_system_ref = a.a_system_ref
and at.at_ticket_num IN (select at_ticket_num
from appeal_tickets
group by at_ticket_num
having count(distinct at_system_ref) > 1)
order by
t.t_reference desc
This is the output:
t_reference at_system_ref at_ticket_num a_case_ref
-------------------------------------------------------
AB123 30838974 23641583 1111979010
AB123 30838976 23641583 1111979010
AB234 30839149 23641520 1111977352
AB234 30839209 23641520 1111988003
I want to modify this so that it only returns records where t_reference is duplicated but against a different a_case_ref. So in above case only records for AB234 would be returned.
Any help would be much appreciated.
You want all ticket appeals that have more than one system reference and more than one case reference it seems. You can join the tables, count the occurrences per ticket and then only keep the tickets that match these criteria.
select *
from
(
select
t.t_reference, at.at_system_ref, at.at_ticket_num, a.a_case_ref,
count(distinct a.a_system_ref) over (partition by at.at_ticket_num) as sysrefs,
count(distinct a.a_case_ref) over (partition by at.at_ticket_num) as caserefs
from tickets t
join appeal_tickets at on at.at_ticket_num = t.t_number
join appeals_2 a on a.a_system_ref = at.at_system_ref
) counted
where sysrefs > 1 and caserefs > 1
order by t.t_reference, at.at_system_ref, at.at_ticket_num, a.a_case_ref;
Correction
It seems that SQL Server still doesn't support COUNT(DISTINCT ...) OVER (...). You can count distinct values in a subquery though. Replace
count(distinct a.a_system_ref) over (partition by at.at_ticket_num) as sysrefs,
by
(
select count(distinct a2.a_system_ref)
from appeal_tickets at2
join appeals_2 a2 on a2.a_system_ref = at2.at_system_ref
where at2.at_ticket_num = t.t_number
) as sysrefs,
An alternative workaround is to use DENSE_RANK in two directions (found here: https://stackoverflow.com/a/53518204/2270762):
dense_rank() over (partition by at.at_ticket_num order by a.a_system_ref) +
dense_rank() over (partition by at.at_ticket_num order by a.a_system_ref desc) -
1 as sysrefs,
with data as (
<your query plus one column>,
case when
min() over (partition by t.t_reference)
<>
max() over (partition by t.t_reference)
then 1 end as dup
)
select * from data where dup = 1

List values with MaxDate

Im trying to create ie query to show itens with MAX DATE, but I donĀ“t know how !
Follow the script and result:
Select
results.severity As "Count_severity",
tasks.name As task,
results.host,
to_timestamp(results.date)::date
From
tasks Inner Join
results On results.task = tasks.id
Where
tasks.name Like '%CORP 0%' And
results.severity >= 7 And
results.qod > 70
I need to show only tasks with the last date of each one.
Can you help me ?
You seem to be using Postgres (as suggested by the use of casting operator ::). If so - and I follow you correctly - you can use distinct on:
select distinct on(t.name)
r.severity, t.name as task, r.host, to_timestamp(r.date::bigint)::date
from tasks t
inner join results r on r.task = t.id
where t.name like '%corp 0%' and r.severity >= 7 and r.qod > 70
order by t.name, to_timestamp(r.date::bigint)::date desc
This guarantees one row per task only; which row is picked is controlled by the order by clause, so the above gets the row with the greatest date (time portion left apart). If there are ties, it is undefined which row is returned. You might want to adapt the order by clause to your exact requirement, if it is different than what I understood.
On the other hand, if you want top ties, then use window functions:
select *
from (
select r.severity, t.name as task, r.host, to_timestamp(r.date::bigint)::date,
rank() over(partition by t.name order by to_timestamp(r.date::bigint)::date desc) rn
from tasks t
inner join results r on r.task = t.id
where t.name like '%corp 0%' and r.severity >= 7 and r.qod > 70
) t
where rn = 1

Multiple results - Need only the latest price

I need to find the latest price for some items
This is my query:
SELECT
MAX("POPORH1"."DATE") as "PO DATE",
"ICSHEH"."DOCNUM",
"ICSHEH"."TRANSDATE",
"ICSHEH"."FISCYEAR",
"ICSHEH"."FISCPERIOD",
"ICSHEH"."REFERENCE",
"ICSHED"."ITEMNO",
"ICSHED"."ITEMDESC",
"ICSHED"."LOCATION",
"ICSHED"."QUANTITY",
"ICSHED"."UNIT",
"POPORL"."UNITCOST"
FROM (("CABDAT"."dbo"."ICSHEH" "ICSHEH"
INNER JOIN
"CABDAT"."dbo"."ICSHED" "ICSHED" ON "ICSHEH"."SEQUENCENO"="ICSHED"."SEQUENCENO")
INNER JOIN "CABDAT"."dbo"."POPORL" "POPORL" ON "ICSHED"."ITEMNO"="POPORL"."ITEMNO")
INNER JOIN "CABDAT"."dbo"."POPORH1" "POPORH1" ON "POPORL"."PORHSEQ"="POPORH1"."PORHSEQ"
WHERE "ICSHED"."SEQUENCENO"=55873
group by
"ICSHEH"."DOCNUM",
"ICSHEH"."TRANSDATE",
"ICSHEH"."FISCYEAR",
"ICSHEH"."FISCPERIOD",
"ICSHEH"."REFERENCE",
"ICSHED"."ITEMNO",
"ICSHED"."ITEMDESC",
"ICSHED"."LOCATION",
"ICSHED"."QUANTITY",
"ICSHED"."UNIT",
"POPORL"."UNITCOST"
This query returns multiple results
These are the results:
"PODATE"='20180405' "ITEMNO"='2944' "UNITCOST"='0.266750'
"PODATE"='20180405' "ITEMNO"='2946' "UNITCOST"='0.266750'
"PODATE"='20170208' "ITEMNO"='2944' "UNITCOST"='0.250780'
"PODATE"='20170208' "ITEMNO"='2944' "UNITCOST"='0.250780'
"PODATE"='20170208' "ITEMNO"='2946' "UNITCOST"='0.250780'
"PODATE"='20170208' "ITEMNO"='2946' "UNITCOST"='0.250780'
I need to have only
"PODATE"='20180405' "ITEMNO"='2944' "UNITCOST"='0.266750'
"PODATE"='20180405' "ITEMNO"='2946' "UNITCOST"='0.266750'
I am learning SQL, so please be patient with my ignorance...
Thanks a lot!
You just need row_number().
WITH cte as (
SELECT *, ROW_NUMBER() OVER (PARTITION BY "ITEMNO" ORDER BY "PODATE" DESC) as rn
FROM "ICSHED" -- or join tables
WHERE "ICSHED"."SEQUENCENO"=55873
)
SELECT *
FROM cte where rn = 1
Or if you only need the highest value without any grouping can use TOP 1
SELECT TOP 1 *
FROM "ICSHED" -- or join tables
WHERE "ICSHED"."SEQUENCENO"=55873
ORDER "PODATE" DESC
By my understanding, you want top 2 rows with recent date. so I try this,
select top 2 * from yourtable order by dateCol desc

Column l' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause - but it is

I have a sql subquery that does the following - it calculates some sums based on a over partition of a column. After that in I want to inner join the table with itself so that I could get the difference between dates.
The problem that it is showing is:
Column 'tblFinansijskiPodaci.DatumVal' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
The problem started when I inserted the date values in the DATEDIFF. When I insert some random date values like '2017/08/25', '2011/08/25' it works fine.
Is maybe my join in the wrong place?
This is my query:
SELECT P2.FinID, P2.Firma, P2.BrojDok, P2.DatumVal,P2.Saldo ,P2.SaldoTotal2,
IIF(P2.SaldoTotal2<0,0,IIF(P2.SaldoTotal2<1,(DATEDIFF(DAY,P2.DatumVal, b.DatumVal)),0))
AS NumberOfDays
FROM
(
SELECT P1.FinID, P1.Firma,P1.BrojDok,P1.DatumVal,P1.Saldo,P1.SaldoTotal,
IIF(P1.SaldoTotal<0,0,IIF(P1.SaldoTotal>1,1,0)) AS SaldoTotal1,
IIF(P1.SaldoTotal<0,0,IIF(P1.SaldoTotal<1,0,1)) AS SaldoTotal2
FROM
(
SELECT P.FinID,P.Firma,P.BrojDok,P.DatumVal,P.Saldo ,
SUM(Saldo) OVER (PARTITION BY BrojDok ORDER BY FinID) AS SaldoTotal
FROM
(
SELECT a.FinID, a.Firma, a.Konto,a.NazivKonta, a.NazFirme, a.BrojDok,
a.DatumVal,a.Valuta,
Sum(IIf(a.[Konto] Like '2%',a.[Duguje] -a.[Potrazuje],a.[Potrazuje] -a.[Duguje]))
AS Saldo
FROM tblFinansijskiPodaci a WHERE a.Firma = 1 AND a.Konto = 2040
AND a.Partner = 1137
GROUP BY a.FinID,a.Firma,a.NazFirme,a.Konto,a.NazivKonta,a.BrojDok,
a.DatumVal,a.Valuta,a.Duguje,a.Potrazuje
) AS P
GROUP BY FinID,Firma,BrojDok,Saldo,DatumVal
) AS P1
GROUP BY P1.FinID,P1.Firma,P1.BrojDok,P1.DatumVal,P1.Saldo,P1.SaldoTotal
) AS P2
INNER JOIN tblFinansijskiPodaci b ON b.BrojDok=P2.BrojDok
GROUP BY P2.FinID,P2.Firma,P2.BrojDok,P2.Saldo,P2.SaldoTotal,P2.SaldoTotal1,P2.SaldoTotal2,P2.DatumVal
ORDER BY BrojDok
Try to add b.DatumVal in your last GROUP BY clause.

SQL Subquery just return one value.How Can make this code efficient?

select count(*) as CountId, [FirstRouteNo],[ThroughRouteSid],[LastRouteNo],
(select top 1 [ThroughRouteJson]
from DirectTransfer as Subquery
where MainQuery.FirstRouteNo=Subquery.FirstRouteNo and
MainQuery.ThroughRouteSid = Subquery.ThroughRouteSid and
MainQuery.LastRouteNo = Subquery.LastRouteNo
) as DetailJson,
(select top 1 RouteMeter
from DirectTransfer as Subquery
where MainQuery.FirstRouteNo = Subquery.FirstRouteNo and
MainQuery.ThroughRouteSid = Subquery.ThroughRouteSid and
MainQuery.LastRouteNo = Subquery.LastRouteNo
) as RouteMeter
from DirectTransfer as MainQuery
group by MainQuery.[FirstRouteNo],MainQuery.[ThroughRouteSid],MainQuery.[LastRouteNo]
order by CountId desc
I want to group by this column [FirstRouteNo],[ThroughRouteSid],[LastRouteNo] then Count How many records.but I also want to show two column values like [ThroughRouteJson] and [RouteMeter] any one of records.Because [ThroughRouteJson] and [RouteMeter] of value has little different.So I can't group by with them.then subquery only return one value.So I write two Subquery to get what I want.because my DB table has More than 100 million records.I want to make efficient. How can I make this code become more efficient then I can get the same result data?
I would suggest you do this as:
select dt.*, dt2.DetailJson, dt2.RouteMeter
from (select count(*) as cnt, dt.FirstRouteNo, dt.ThroughRouteSid, dt.LastRouteNo
from DirectTransfer dt
group by dt.FirstRouteNo, dt.ThroughRouteSid, dt.LastRouteNo
) dt outer apply
(select top 1 ThroughRouteJson as DetailJson, RouteMeter
from DirectTransfer dt2
where dt.FirstRouteNo = dt.FirstRouteNo and
dt.ThroughRouteSid = dt.ThroughRouteSid and
dt.LastRouteNo = dt.LastRouteNo
) dt2
order by CountId desc;
You want indexes on DirectTransfer(FirstRouteNo, ThroughRouteSid, LastRouteNo). There might be other ways to accomplish what you want, but it is a bit unclear what you are trying to do.