I don't know if this is achievable via SQL in Oracle
I have table with 114000 plus records having Customer codes and sales person code.
Now I need to spool this in 10 or 11 separate files so each file contain 1/10 or less records to be grouped on sp code
Able to get first 14000 rec
with tt as (select br,count() cc,ROW_NUMBER() OVER(ORDER BY count() )rn from temp2 group by (br) order by cc)
SELECT cc,count(cc)
FROM
(
SELECT br, cc,
(
SELECT SUM(cc)
FROM tt
WHERE rn <= t.rn ) total
FROM tt t order by cc
) q
WHERE total <= (select count(*)/10 from temp2) group by cc order by cc
Now how to loop
Related
I am trying to select multiple rows of data into one row through multiple columns which will change dynamically.
This is in Oracle database. I want to count repeated work done by the LEAD_TECHNISIAN_ID within a duration. If the difference of last work delivery date and new work receive date is 15 or below 15 then LEAD_TECHNISIAN_ID has one repeated work.
List item
SELECT *
FROM (WITH CTE AS (
SELECT ROW_NUMBER () OVER (ORDER BY ID) AS RW,
RECEIVED_DATE,
DELIVERY_DATE,
SERVICE_NO,
LEAD_TECHNISIAN_ID,
ID,
SERVICE_CENTER
FROM ( SELECT cc.SERVICE_CENTER,
CC.ID,
CC.BARCODE,
TRUNC (cc.CREATED_DATE) RECEIVED_DATE,
TRUNC (CC.DELIVERY_DATE) DELIVERY_DATE,
cc.SERVICE_NO,
CC.LEAD_TECHNISIAN_ID
FROM customer_complains cc
WHERE cc.BARCODE IN (SELECT BARCODE
FROM (SELECT BARCODE,
COUNT (BARCODE)
FROM customer_complains c
WHERE c.BARCODE <> 'UNDEFINE'
AND C.BARCODE = NVL ('351950102757821', BARCODE)
AND c.SEGMENT3 = NVL ('',c.SEGMENT3)
AND c.SEGMENT3 IN (SELECT SEGMENT3
FROM ITEM_MST
WHERE PRODUCT_GROUP = NVL ('',PRODUCT_GROUP))
GROUP BY c.BARCODE
HAVING COUNT (c.BARCODE) >1))
ORDER BY ID DESC)
ORDER BY ID DESC)
SELECT a.id,
a.DELIVERY_DATE,
a.RECEIVED_DATE,
b.RECEIVED_DATE PRE_RCV,
b.DELIVERY_DATE PRE_DEL,
(a.RECEIVED_DATE - b.DELIVERY_DATE) AS DIFF,
a.SERVICE_NO,
a.LEAD_TECHNISIAN_ID,
b.LEAD_TECHNISIAN_ID PRE_TECH --, a.DELIVERY_DATE
FROM CTE a
LEFT JOIN CTE b ON a.RW = b.RW + 1
)
WHERE DIFF <= 15
Here is the output for a specific barcode. but when I try for All the barcode I have in My Customer_complains table. The query provides irrelevant output.
Currently your code is giving numbers 1,2,3,4... to rows irrespective of LEAD_TECHNISIAN_ID and then you are joining it with RW. It will not consider LEAD_TECHNISIAN_ID while giving row numbers.
RW must start with 1 for each LEAD_TECHNISIAN_ID.
You just need to change calculation of RW as following:
ROW_NUMBER () OVER (PARTITION BY LEAD_TECHNISIAN_ID ORDER BY ID) AS RW
Cheers!!
I have a table department with 3 column(department_name , department_id, department_block_number) so I want to fetch the department_block_number in which maximum number of department is located ? I have two department_block_number 303, 202 and each has 4 and 2 departments respectively? how can i do it?
select q1.department_block_number , max(c)
(select department_block_number , count(department_id)as c from department group by department_block_number)q1,
group by department_block_number ;
select q1.department_block_number , max(c)
(select department_block_number , count(department_id)as c from department group by department_block_number)q1,
group by department_block_number ;
Now i want to show only 303 as it is the block number with maximum departments in it but my query is showing both 303, 202 please help me . If you know some other way so that i can fetch the result so please help
In standard SQL if you are looking for one row, you would do:
select d.department_block_number, count(*)
from department d
group by d.department_block_number
order by count(*) desc
fetch first 1 row only;
Some databases spell fetch first 1 row only as limit 1 or select top (1) or in even more arcane ways.
In older versions of Oracle (fetch is supported in 12c+), you can do:
select department_block_number, cnt
from (select d.department_block_number, count(*) as cnt
from department d
group by d.department_block_number
order by count(*) desc
) d
where rownum = 1;
I need to produce a systematic random sample of (approx) 2500 rows from (approx)230,000 rows with a unique auto generated number on each row.
Is this possible using Teradata SQL ast? (The Sample function produces a simple random sample.)
Thank you for your time.
When there's already a gapless unique row number:
select t.*
from mytable as t
cross join
( select random(1,2500) as rnd ) as dt -- random start row
where rownumber mod 2500 = rnd -- every 2500 rows
Otherwise ROW_NUMBER can be used to create it:
select t.*
from mytable as t
cross join
( select random(1,2500) as rnd ) as dt
qualify ROW_NUMBER() OVER (ORDER BY whatever_determines_your_order) mod 2500 = rnd
select rank() over(order by $primary_index_key), t1.*
FROM
(select * from $table_name
sample 2500) t1
Assistant would do that, and so would any other client.
Same approach can be used to generate winning Powerball numbers.
From your comment, I think that you cannot do it on the fly. You need to generate some table first that would contain Step# and step_value, for example:
[1,2500],[2,5000],...[x,x*2500]
Than you should join this table to your query and limit row numbers by the logic: rn = random_seed + step_value.
It will look like this:
select
from (
select t1.*
, row_number() over("ordering logic") as rn
from my_table
) as t1
, (select random(1,2500) as random_seed) as seed --so it will be generated only once
where exists (
select 1 from sampling_table as t2
where t1.rn = t2.step_value + seed.random_seed
)
I have a SQL query that looks like this:
SELECT foo "c0",
bar "c1",
baz "c2",
...
FROM some_table
WHERE ...
In order to apply a limit, and only return a subset of records from this query, I use the following wrapper SQL:
SELECT *
FROM (
SELECT t.*,
ROW_NUMBER() OVER (ORDER BY ...) rnum
FROM (
... original SQL goes here ...
) t
)
WHERE rnum BETWEEN 1 AND 10
My problem is that the original query is selecting over 1000 columns across a large number of joins to other tables. Oracle has an internal limit of 1000 columns per table or view, and apparently the wrapper SQL I'm using to limit the result set is creating a temporary view to which this limit is applied, causing the whole thing to fail.
Is there another method of pagination that doesn't create such a view, or wouldn't otherwise be affected by the 1000 column limit?
I'm not interested in suggestions to break the work up into chunks, not select > 1000 columns, etc., as I'm already fully aware of all of these methods.
Okay, this will perform worse than what you were planning, but my point is that you could try pagination this way:
WITH CTE AS
(
... original SQL goes here ...
)
SELECT A.*
FROM CTE A
INNER JOIN (SELECT YourKey,
ROW_NUMBER() OVER (ORDER BY ...) rnum
FROM CTE) B
ON A.YourKey = B.YourKey
WHERE rnum BETWEEN 1 AND 10;
you cant have a view with 1000+ columns, so cheat a little.
select *
from foo f, foo2 f2
where (f.rowid, f2.rowid) in (select r, r2
from (select r, r2, rownum rn
from (select /*+ first_rows */ f.rowid r, f2.rowid r2
from foo f, foo2 f2
where f.c1 = f2.a1
and f.c2 = '1'
order by f.c1))
where rn >= AAA
and rownum <= BBB)
order by whatever;
now put any where clauses in the innermost bit (eg i put f.c1 = '1').
BBB = pagesize.
AAA = start point
Is the problem pagination or just returning the first 10 rows? If it is the latter, you can do:
SELECT foo "c0",
bar "c1",
baz "c2",
...
FROM some_table
WHERE ... and
rownum <= 10
I have a procedure that returns multiple rows on some criteria and in specific order. These rows are separated into few pages (50 rows per page).
How can I retrieve all rows from page having some specific row.
I've created a query the query that do this work, but it is not optimized and have huge impact on performance. Help me please to optimize it or give an alternative to it:
select *
from
(
select file_id, row_number() over (order by rownum) rn
from my_table
)
where trunc(rn/50) = (
select trunc(rn/50) from
(select t.*, rownum rn from my_table t)
where file_id = 29987);
You have to adjust it a little...
with tab as
(
-- the
-- row_number() over (order by rownum) rn
-- should be here
select level + 1000 as val
, level/50 as rn_50
from dual
connect by
level < 140
)
, val as
(
select rn_50
from tab
where val = 1004 -- pg 1
--where val = 1051 -- pg 2
--where val = 1101 -- pg 3
)
select *
from tab t
where rn_50 >= (select floor(rn_50) from val)
and rn_50 <= (select ceil (rn_50) from val)
;