select *
from amazon_shipment, customer
where amazon_shipment.customer_id = customer.customer_id
and amazon_shipment.customer_id in
(select top(1) amazon_shipment.customer_id
from amazon_shipment
group by amazon_shipment.customer_id
order by count(*) desc);
I am trying to select all the customers with the most order, however, I get an error:
FROM keyword not found were expected
TOP(1) isn't available in Oracle.
In Oracle 11gR2 and lower, you can use WHERE ROWNUM < 2
select *
from EMPLOYEES
where rownum < 2
order by SALARY desc;
In Oracle 12c and higher, you can use FETCH FIRST 1 ROWS ONLY
select *
from EMPLOYEES
order by SALARY desc
fetch first 1 rows only;
Related
SELECT partner_id
FROM trip_delivery_sales ts
WHERE ts.route_id='152'
GROUP BY ts.partner_id
From the query we can get the partners id.Using that partner id we want check in trip delicery sales lines table and want to find each customer last two sale product quantity sum. If last two sale have product qty as 2 & 5 want result as partner_id | count as Mn2333 - 7
here fore example i take partner id as 34806. But i want to check all partner_id obtained from last query
SELECT product_qty
FROM trip_delivery_sales_lines td
WHERE td.partner_id='34806'
AND td.route_id='152'
AND td.product_id='432'
ORDER BY td.order_date DESC
LIMIT 2
You can run this query
SELECT td.partner_id,sum(product_qty)
FROM trip_delivery_sales_lines td,
(SELECT partner_id FROM trip_delivery_sales ts WHERE ts.route_id='152') as ts
WHERE td.partner_id=ts.partner_id
AND td.product_id='432'
GROUP BY td.partner_id
ORDER BY td.order_date DESC
LIMIT 2
Or this one
with ts as (SELECT distinct partner_id FROM trip_delivery_sales WHERE route_id='152')
SELECT td.partner_id,sum(product_qty)
FROM trip_delivery_sales_lines td,ts
WHERE td.partner_id=ts.partner_id
AND td.product_id='432'
GROUP BY td.partner_id
ORDER BY td.order_date DESC
LIMIT 2
You might be looking for
SELECT DISTINCT ts.partner_id, ARRAY(
SELECT product_qty
FROM trip_delivery_sales_lines td
WHERE td.partner_id=ts.partner_id
AND td.product_id='432'
ORDER BY td.order_date DESC
LIMIT 2
) AS product_qty_arr
FROM trip_delivery_sales ts
WHERE ts.route_id='152'
or just
SELECT
partner_id,
array_agg(product_qty ORDER BY order_date DESC) as product_qty_arr
FROM (
SELECT
td.partner_id,
td.product_qty,
td.order_date,
row_number() OVER (PARTITION BY td.partner_id ORDER BY td.order_date DESC)
FROM trip_delivery_sales_lines td
JOIN trip_delivery_sales ts USING (partner_id)
WHERE ts.route_id='152'
AND td.product_id='432'
) AS enumerated
WHERE row_number <= 2
GROUP BY partner_id
See also PostgreSQL: top n entries per item in same table or Optimize GROUP BY query to retrieve latest row per user
This question already has answers here:
SQL update from one Table to another based on a ID match IN db2
(6 answers)
Closed 3 years ago.
I’m new in DB2 but not new in SQL and new a DB2 SQL update correlating with subquery in DB2 LUW V9.5. My Table is like below:
In rows 3 and 7 I need to update ACTIVE = 0 and VALID_TO = VALID_FROM from Rows 4 and 8 (SCD). I select data using below Statement:
SELECT T2.COL1, T2.COL1_HIST, T2.ACTIVE, T2.VALID_FROM, T2.VALID_TO, T3.VALID_FROM
FROM TMP.TABLE01 T2 INNER JOIN
(
SELECT COL1, MAX(COL1_HIST) MAX_HIST FROM TMP.TABLE01 WHERE
ACTIVE = 1
GROUP BY COL1
HAVING Count(*)>1
)T1 ON T2.COL1 = T1.COL1 AND T2.COL1_HIST < T1.MAX_HIST
INNER JOIN TMP.TABLE01 T3 ON T1.COL1 = T3.COL1 AND T1.MAX_HIST = T3.COL1_HIST
WHERE T2.ACTIVE = 1
To update in SQL world, I can use below statement:
UPDATE T2 SET T2.ACTIVE = 0,T2.VALID_TO=T3.VALID_FROM
FROM TMP.TABLE01 T2 INNER JOIN
(
SELECT COL1, MAX(COL1_HIST) MAX_HIST FROM TMP.TABLE01 WHERE
ACTIVE = 1
GROUP BY COL1
HAVING Count(*)>1
)T1 ON T2.COL1 = T1.COL1 AND T2.COL1_HIST < T1.MAX_HIST
INNER JOIN TMP.TABLE01 T3 ON T1.COL1 = T3.COL1 AND T1.MAX_HIST = T3.COL1_HIST
WHERE T2.ACTIVE = 1
How could this be in DB2 (DB2 LUW V9.5) world?
I know the similar Question in Stack Overflow, but it does not help me :( link
Many thanks in advance
Try this:
merge into table01 a using
(
select rownumber() over(partition by col1 order by col1_hist desc) rn_, t.*
from table01 t
where active=1
) m on m.rn_=1 and a.active=1 and a.col1=m.col1 and a.col1_hist<>m.col1_hist
when matched then update set active=0, valid_to=m.valid_from;
rn_ column of the inner subselect has value 1 for each row with the highest col1_hist in a group of records with the same col1.
We update all other active rows except these ones with correct valid_from value for each group.
I'm having trouble with paginating with joined tables in DB2. I want to return rows 10-30 of a query that contains an INNER JOIN.
This works:
SELECT *
FROM (
SELECT row_number() OVER (ORDER BY U4SLSMN.SLNAME) AS ID,
U4SLSMN.SLNO, U4SLSMN.SLNAME, U4SLSMN.SLLC
FROM U4SLSMN) AS P
WHERE P.ID BETWEEN 10 AND 30
This does not work:
SELECT *
FROM (
SELECT row_number() OVER (ORDER BY U4SLSMN.SLNAME) AS ID,
U4SLSMN.SLNO, U4SLSMN.SLNAME, U4SLSMN.SLLC, U4CONST.C4NAME
FROM U4SLSMN INNER JOIN U4CONST ON U4SLSMN.SLNO = U4CONST.C4NAME
) AS P
WHERE P.ID BETWEEN 10 AND 30
The error I get is:
Selection error involving field *N.
Note that the JOIN query works correctly by itself, just not when it's run as a subquery.
How do I perform a join inside a subquery in DB2?
Works fine for me on v7.1 TR9
Here's what I actually ran:
select *
from ( select rownumber() over (order by vvname) as ID, idescr, vvname
from olsdta.ioritemmst
inner join olsdta.vorvendmst on ivndno = vvndno
) as P
where p.id between 10 and 30;
I much prefer the CTE version however:
with p as
( select rownumber() over (order by vvname) as ID, idescr, vvname
from olsdta.ioritemmst
inner join olsdta.vorvendmst on ivndno = vvndno
)
select *
from p
where p.id between 10 and 30;
Finally, note that at 7.1 TR11 (7.2 TR3), IBM added support of the LIMIT and OFFSET clauses. Your query could be re-done as follows:
SELECT
U4SLSMN.SLNO, U4SLSMN.SLNAME, U4SLSMN.SLLC, U4CONST.C4NAME
FROM U4SLSMN INNER JOIN U4CONST ON U4SLSMN.SLNO = U4CONST.C4NAME
ORDER BY U4SLSMN.SLNAME
LIMIT 20 OFFSET 9;
However, note that the LIMIT & OFFSET clauses are only supported in prepared or embedded SQL. You can't use them in STRSQL or STRQMQRY. I believe the "Run SQL Scripts" GUI interface does support them. Here's an article about LIMIT & OFFSET
I have a query like the one shown below:
SELECT
SUM (LIMIT_AMOUNT)
FROM
(SELECT
CTR_NO
, REPORT_DATE
, LIMIT_AMOUNT
, ROW_NUMBER()
OVER (PARTITION BY CTR_NO, REPORT_DATE ORDER BY REPORT_DATE) rn
FROM LOD_CONTRACT
WHERE
br_cst_code='3432434'
AND REPORT_DATE BETWEEN '20-FEB-15' AND '28-FEB-15') b
WHERE
b.rn=1;
How can I build a SQL query for a JPA-managed entity like:
SELECT
SUM (o.limit_amount)
FROM
(SELECT
o.ctr_no
, o.rpt_dt
, o.limit_amount
, ROW_NUMBER()
OVER (PARTITION BY o.ctr_no, o.rpt_dt ORDER BY o.rpt_dt) rn
FROM LOD_CONTRACT o
WHERE
o.br_cst_code='3432434'
AND o.rpt_dt BETWEEN '20-FEB-15' AND '28-FEB-15') b
WHERE
b.rn=1;
JPQL doesn't currently support Oracle partition tables. To use this feature you will have to stick to Native queries.
My SQL is a bit bad. I got a query that when I run it I return for example 10 rows but there are 15 in my where clause, how do I identify those 5 that I can't find?
Off course I can dump it in MS Excel but how do I use SQL?
Its a simple query:
select * from customers where username in ("21051", "21052"...
Nothing else in the where clause, it return 10 rows but there are 15 usernames in there where clause.
To identify the missing rows:
SELECT *
FROM (SELECT '21051' username
UNION ALL SELECT '21052'
UNION ALL SELECT '21053'
UNION ALL SELECT '21054') u
WHERE u.username NOT IN (SELECT c.username FROM customers c)