Need help in the sql query
i have a table
source table :
create table order(name_a varchar2(100),intid number);
insert into order values('a',1);
insert into order values('b',1);
insert into order values('c',1);
insert into order values('d',1);
insert into order values('e',2);
insert into order values('f',2);
insert into order values('g',2);
i need a query , which result in the below output :
if i look for intid=1 the query should give a,b,c,d
if i look for intid=2 the query should give f,g
Thanks
Hi,
781649 wrote:
i have a table
source table :
create table order(name_a varchar2(100),intid number);
insert into order values('a',1);
insert into order values('b',1);
insert into order values('c',1);
insert into order values('d',1);
insert into order values('e',2);
insert into order values('f',2);
insert into order values('g',2);Thanks for posting the CREATE TABLE and INSERT statements; it's very helpful.
i need a query , which result in the below output :
if i look for intid=1 the query should give a,b,c,d
if i look for intid=2 the query should give f,gDid you mean <b>e</b>,f,g ?
That's called String Aggregation , and how to do it depends on your version of Oracle, and your exact requirements.
See thie following page for several techniques:
http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php
If you're using Oracle 10 (or higher), and it's important that the name_a's be in order in the output, then you can do this:
WITH got_r_num AS
SELECT name_a
, intid
, ROW_NUMBER () OVER ( PARTITION BY intid
ORDER BY name_a
) AS r_num
FROM order_table -- ORDER is not a good table name
WHERE intid IN (1) -- Optional
SELECT intid
, SUBSTR ( SYS_CONNECT_BY_PATH (name_a, ',')
, 2
) AS name_a_list
FROM got_r_num
WHERE CONNECT_BY_ISLEAF = 1
START WITH r_num = 1
CONNECT BY r_num = PRIOR r_num + 1
AND intid = PRIOR intid
;Starting in Oracle 11.2, LISTAGG is better.
This does not assume that you are getting the output only for a single intid at a time. You can get any number of them in a suingle query. Of course, that number can be 1 if that's what you want.
Edited by: Frank Kulash on Mar 24, 2011 12:19 PM
Similar Messages
-
Need help on the below query.
Hi All,
I've a query given below..
SELECT W.WONUM,
W.STATUS,
WS.CHANGEDATE,
EH.OLD_RATE
FROM
WORKORDER W,
WOSTATUS WS,
ESTIMATE_HEADER@GQMFOF EH
WHERE WS.CHANGEDATE BETWEEN '01-Oct-2009' AND '1-Nov-2009'
AND W.WONUM = WS.WONUM
AND EH.OLD_RATE = 'N'
AND WS.WOSTATUS = 'CLOSE';
I would like to get All the data which status =closed in the month of Oct for Old rate,
So for this i am writing the query above. But not getting the o/p.
It is giving me that " Table/View doesn't exist.
There 2 schemas MAXIMO,GQMMGR..
DBlinks are GQMFOF,MAXFOFNC..
Can anyone help me while writing the above query...
Regards,
gr.A question was asked in your other thread. But the problem was you dint care to give an answer.
Dont open duplicate post.
I need help on the below problem.. -
Hi All,
I need to prepare a report having monthly data. For this i am using query to pull data from the table having monthly data. Below are the complete details.
Table Details:
column_name data_type
status varchar2(5)
description varchar2(35)
report_date date
member_count number
It have the sample data like below:
status Description Report_date member_count
0 Voluntary Disenrollment 7/1/2012
1 Regular 7/1/2012 96540
5 HOLD - Medi-Cal Ineligibility 7/1/2012 57
55 Unmet Share of Cost 7/1/2012 6074
59 HOLD - HCP Coverage Limits 7/1/2012 699
P4 Pending Enrollment 7/1/2012
S0 Retro - Voluntary Disenrollment 7/1/2012
S1 Retro - Add 7/1/2012
S9 Retro - Mandatory Disenrollment 7/1/2012
similarly it have data for different report_date's. I need to pull data from this table like below.
Status Description 7/1/2012 6/1/2012 5/1/2012 4/1/2012
00 Voluntary Disenrollment 1 3
01 Regular 96,540 96,620 96,500 96,429
05 HOLD - Medi-Cal Ineligibility 57
55 Unmet Share of Cost 6,074 6,652 6,899 6,922
59 HOLD - HCP Coverage Limits 699
P4 Pending Enrollment 1
S0 Retro - Voluntary Disenrollment 14 13 24
S1 Retro - Add 4,587 7,200 8,288
S9 Retro - Mandatory Disenrollment 2 4 1
i need to get all the details like above from 07/01/2012 to 07/01/2011. Similarly if i pull the data next month it should have all details form 08/01/2012 to 08/01/2011.
I am using below query i am not able to get the result as intended.
select status,description,
max(decode(TO_CHAR(report_date,'MON'),'JAN',member_count,0)) jan,
max(decode(TO_CHAR(report_date,'MON'),'FEB',member_count,0)) feb,
max(decode(TO_CHAR(report_date,'MON'),'MAR',member_count,0)) mar,
max(decode(TO_CHAR(report_date,'MON'),'APR',member_count,0)) apr,
max(decode(TO_CHAR(report_date,'MON'),'JUN',member_count,0)) may,
max(decode(TO_CHAR(report_date,'MON'),'JUL',member_count,0)) jun,
max(decode(TO_CHAR(report_date,'MON'),'AUG',member_count,0)) jul,
max(decode(TO_CHAR(report_date,'MON'),'SEP',member_count,0)) aug
from MONTHLY_MEMBERSHIP_REPORT_834
where report_date >=to_date('2012/01/01','yyyy/dd/mm')
group by description,status
order by status;
Can some one help me on this..?
Edited by: 948188 on Jul 23, 2012 11:36 AM
Edited by: 948188 on Jul 23, 2012 11:36 AMHi,
Are the results you posted really what you want given the sample data you posted? If looks like all the report_dates in the sample data are in July, 2011, so I don't see how you get non-0 values in the other columns.
To automatically get the most recent 13 months, you can do something like this:
WITH got_month_num AS
SELECT status, description, member_count
, MONTHS_BETWEEN ( TRUNC (SYSDATE, 'MONTH')
, TRUNC (report_date, 'MONTH')
) AS month_num
FROM monthly_membership_report_834
WHERE report_date >= ADD_MONTHS ( TRUNC (SYSDATE, 'MONTH')
, -12
AND report_date < ADD_MONTHS ( TRUNC (SYSDATE, 'MONTH')
, 1
SELECT status, description
, MAX (DECODE (month_num, 0, member_count, 0)) AS m0
, MAX (DECODE (month_num, 1, member_count, 1)) AS m1
, MAX (DECODE (month_num, 11, member_count, 0)) AS m11
, MAX (DECODE (month_num, 12, member_count, 0)) AS m12
FROM got_month_num
GROUP BY status, description
ORDER BY status, description
;Output from the sample data you posted:
STATUS DESCRIPTION M0 M1 M11 M12
00 Voluntary Disenrollment 0 1 0
01 Regular 0 1 0 98294
05 HOLD - Medi-Cal Ineligibility 0 1 0
55 Unmet Share of Cost 0 1 0 5488
59 HOLD - HCP Coverage Limits 0 1 0
P4 Pending Enrollment 0 1 0
S0 Retro - Voluntary Disenrollment 0 1 0 75
S1 Retro - Add 0 1 0 7586
S9 Retro - Mandatory Disenrollment 0 1 0 2If you want the column names to indicate the actual months, insrtead of having generic names like m0, then you need dynamic SQL.
In SQL*Plus, that isn't very difficult. Here's one way, using SQL*Plus substiotution variables:
-- Preliminary Query, to set substritution variables
COLUMN m0_col NEW_VALUE m0
COLUMN m1_col NEW_VALUE m1
COLUMN m11_col NEW_VALUE m11
COLUMN m12_col NEW_VALUE m12
SELECT TO_CHAR ( SYSDATE , 'fmMM/YYYY') AS m0_col
, TO_CHAR ( ADD_MONTHS (SYSDATE, -1), 'fmMM/YYYY') AS m1_col
, TO_CHAR ( ADD_MONTHS (SYSDATE, -11), 'fmMM/YYYY') AS m11_col
, TO_CHAR ( ADD_MONTHS (SYSDATE, -12), 'fmMM/YYYY') AS m12_col
FROM dual
-- Main Query
WITH got_month_num AS
SELECT status, description, member_count
, MONTHS_BETWEEN ( TRUNC (SYSDATE, 'MONTH')
, TRUNC (report_date, 'MONTH')
) AS month_num
FROM monthly_membership_report_834
WHERE report_date >= ADD_MONTHS ( TRUNC (SYSDATE, 'MONTH')
, -12
AND report_date < ADD_MONTHS ( TRUNC (SYSDATE, 'MONTH')
, 1
SELECT status, description
, MAX (DECODE (month_num, 0, member_count, 0)) AS "&m0"
, MAX (DECODE (month_num, 1, member_count, 1)) AS "&m1"
, MAX (DECODE (month_num, 11, member_count, 0)) AS "&m11"
, MAX (DECODE (month_num, 12, member_count, 0)) AS "&m12"
FROM got_month_num
GROUP BY status, description
ORDER BY status, description
;Output (when run in July, 2012)
STATUS DESCRIPTION 7/2012 6/2012 8/2011 7/2011
00 Voluntary Disenrollment 0 1 0
01 Regular 0 1 0 98294
05 HOLD - Medi-Cal Ineligibility 0 1 0
55 Unmet Share of Cost 0 1 0 5488
59 HOLD - HCP Coverage Limits 0 1 0
P4 Pending Enrollment 0 1 0
S0 Retro - Voluntary Disenrollment 0 1 0 75
S1 Retro - Add 0 1 0 7586
S9 Retro - Mandatory Disenrollment 0 1 0 2Notice that the main query is exacly what I posted earlier, except that the column aliases reference the siubstitution variables set in the preliminary query.
Edited by: Frank Kulash on Jul 23, 2012 4:39 PM
Added version with dynamic column aliases -
Need Help with Advanced SQL Query
It's advanced for me, at least. I have three tables that I
need to use:
Product (product_id and product_title are the fields)
shipRegion (shipRegion_ID)
product_shipRegion_shipCharge (product_id, shipRegion_ID,
primaryShipCharge, secondaryShipCharge)
What I am trying to do is create a query that will look for
two things:
1. Any product that is listed in the
product_shipRegion_shipCharge table that has a primary or secondary
ship charge of $0.00 or is NULL.
That part is easy:
SELECT DISTINCT p.product_id, p.product_title
FROM product p
INNER JOIN product_shipRegion_shipCharge pss ON p.product_id
= pss.product_id AND (pss.primaryShipCharge IS NULL OR
pss.primaryShipCharge = 0 OR pss.secondaryShipCharge IS NULL OR
pss.secondaryShipCharge = 0)
WHERE p.display = 1
AND p.price > 0
It's this next part that's tricky for me:
2. Get the product_id and product_title (from the product
table) that does NOT have any entry in the
product_shipRegion_shipCharge table.
I'm guessing that I need to include some kind of LEFT JOIN in
the above query to find out what all of the regions are (from the
shipRegion table) and then see what's missing for each product in
the product_shipRegion_shipCharge table, but I have no idea how to
do it.
Anyone?
JoshThis should be what the query would look like using the left
join:
SELECT product_table.product_id, product_table.product_title
FROM product_table
LEFT OUTER JOIN product_shipRegion_shipCharge
ON product_shipRegion_shipCharge.product_id =
product_table.product_id
WHERE product_shipRegion_shipCharge. product_id IS NULL
SQL Server's query optimizer will probably turn the '...IN
(subquery)' or '...exists (subquery)' into this kind of execution
plan on its own - depending on your table structures. -
Newbie - need help with a SQL query for a bar chart
Hi,
I'm a new user on APEX with no real SQL knowledge and I'm trying to build a dashboard with charts into an existing APEX application. Based on another application, I have come up with the following SQL code:
select null link
, CATEGORY label
, count (decode(PROJECT_STATUS,'1',PROJECT_ID))"Active"
, count (decode(PROJECT_STATUS,'2',PROJECT_ID))"Complete"
, count (decode(PROJECT_STATUS,'3',PROJECT_ID))"On Hold"
, count (decode(PROJECT_STATUS,'4',PROJECT_ID))"Pipeline"
, count (decode(PROJECT_STATUS,'5',PROJECT_ID))"Pending Review"
from GRAPO_PROHEADTRK
where (PROJECT_STATUS='1' or PROJECT_STATUS='2' or PROJECT_STATUS='3' or PROJECT_STATUS='4' or PROJECT_STATUS='5' or PROJECT_STATUS='6')
group by CATEGORY
Order by COUNT(PROJECT_ID) DESC
The code from the other app was:
select null link
, FUNCTIONAL_AREA label
, count (decode(PROJECT_STATUS,'Active',PROJECT_ID))"Active"
, count (decode(PROJECT_STATUS,'Complete',PROJECT_ID))"Complete"
, count (decode(PROJECT_STATUS,'On Hold',PROJECT_ID)) "On Hold"
, count (decode(PROJECT_STATUS,'Recurring',PROJECT_ID))"Recurring"
, count (decode(PROJECT_STATUS,'Pipeline',PROJECT_ID))"Pipeline"
, count (decode(PROJECT_STATUS,'Not Approved',PROJECT_ID))"Not Approved"
from PM_V2
where LOB='S2S' and (FUNCTIONAL_AREA='Accounts Payable' or FUNCTIONAL_AREA='Expense' or FUNCTIONAL_AREA='Procurement' or FUNCTIONAL_AREA='Fixed Assets')
group by FUNCTIONAL_AREA
Order by COUNT(PROJECT_ID) DESC
I'm getting a "Failed to parse SQL query!" error when I try to run validation.
Is this enough info for some assistance? Any help would really be appreciated.
Thanks,
RachelHello,
This is more of an SQL question, rather than specifically APEX-related. It's notable that you say: I'm a new user on APEX with no real SQL knowledgeWhich is fine (we all have to start somewhere, afterall) but it might be worth de-coupling the problem from APEX in the first instance. I'd also strongly recommend either taking a course, reading a book (e.g. http://books.google.co.uk/books?id=r5vbGgz7TFsC&printsec=frontcover&dq=Mastering+Oracle+SQL&hl=en#v=onepage&q=Mastering%20Oracle%20SQL&f=false) or looking for a basic SQL tutorial - it will save you a whole lot of heartache, I promise you. Search the oracle forums for the terms "Basic SQL Tutorial" and you should come up with a bunch of results.
Given that you've copied your query template from another, I would suggest ensuring that the actual query works first of all. Try running it in either:
* SQL Editor
* SQL*Plus
* an IDE like SQL Developer (available free from the OTN: http://www.oracle.com/technetwork/developer-tools/sql-developer/downloads/index.html ) or TOAD or similar.
You may find there are syntax errors associated with the query - it's difficult to tell without looking at your data model.
select null link
, CATEGORY label
, count (decode(PROJECT_STATUS,'1',PROJECT_ID))"Active"
, count (decode(PROJECT_STATUS,'2',PROJECT_ID))"Complete"
, count (decode(PROJECT_STATUS,'3',PROJECT_ID))"On Hold"
, count (decode(PROJECT_STATUS,'4',PROJECT_ID))"Pipeline"
, count (decode(PROJECT_STATUS,'5',PROJECT_ID))"Pending Review"
from GRAPO_PROHEADTRK
where (PROJECT_STATUS='1' or PROJECT_STATUS='2' or PROJECT_STATUS='3' or PROJECT_STATUS='4' or PROJECT_STATUS='5' or PROJECT_STATUS='6')
group by CATEGORYNote that your "order by" clause references a field called "PROJECT_ID", which exists in the old query but you've changed other similar references to "PROJECT_STATUS" - is it possible you've just missed this one? The perils of copy-paste coding I'm afraid... -
Hi team,
Please help me on below query,
I have table like given below
Tran_Id tran_date amount. Actorid
1. 10-apr-15. 100. 1
2. 11-apr-15. 100. 1
3. 11-apr-15. 900. 1
4. 12-apr-15. 100. 1
5. 13-apr-15. 350. 1
6. 14-apr-15. 400. 1
Now please find the query,
I want all the actor ids whos tran amount
>1500 and the date when the tran amount
Has breached
Ex:
Actor-id. Breached-date. Total
1. 13-apr-15. 1900
How can I write a query for above requirement?
Regards,
RajendraYour solution (same as Saubhik's) is incorrect. Look at source data - multiple transactions can occur same day. Therefore, your qury will return wrong results if breached amount is 1000:
with trans as (
select 1 tran_Id, to_date('10-apr-15', 'dd-mon-yy') tran_date, 100 amount, 1 actorid from dual union all
select 2, to_date('11-apr-15', 'dd-mon-yy'), 100, 1 from dual union all
select 3, to_date('11-apr-15', 'dd-mon-yy'), 900, 1 from dual union all
select 4, to_date('12-apr-15', 'dd-mon-yy'), 100, 1 from dual union all
select 5, to_date('13-apr-15', 'dd-mon-yy'), 350, 1 from dual union all
select 6, to_date('14-apr-15', 'dd-mon-yy'), 400, 1 from dual union all
select 7, to_date('12-apr-15', 'dd-mon-yy'), 300, 2 from dual union all
select 8, to_date('13-apr-15', 'dd-mon-yy'), 1200, 2 from dual union all
select 9, to_date('14-apr-15', 'dd-mon-yy'), 300, 2 from dual union all
select 10, to_date('15-apr-15', 'dd-mon-yy'), 300, 2 from dual
trans_running_tot as (
select tran_id, tran_date,
sum(amount) over (partition by actorid order by tran_date) tot_amt, actorid
from trans
trans_ranked as (
select actorid,tran_id, tran_date,
rank() over (partition by actorid order by tot_amt) rk
from trans_running_tot
where tot_amt > 1000
select * from trans_ranked where rk=1
ACTORID TRAN_ID TRAN_DATE RK
1 2 11-APR-15 1 -- here total amount was only 200
1 3 11-APR-15 1
2 8 13-APR-15 1
SQL>
As you can see, 2 rows were returned for actor 1. Why? Default for analytic ORDER BY is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Therefore, all rows with same transation_date will fall into same window:
SQL> with trans as (
2 select 1 tran_Id, to_date('10-apr-15', 'dd-mon-yy') tran_date, 100 amount, 1 actorid from dual union all
3 select 2, to_date('11-apr-15', 'dd-mon-yy'), 100, 1 from dual union all
4 select 3, to_date('11-apr-15', 'dd-mon-yy'), 900, 1 from dual union all
5 select 4, to_date('12-apr-15', 'dd-mon-yy'), 100, 1 from dual union all
6 select 5, to_date('13-apr-15', 'dd-mon-yy'), 350, 1 from dual union all
7 select 6, to_date('14-apr-15', 'dd-mon-yy'), 400, 1 from dual union all
8 select 7, to_date('12-apr-15', 'dd-mon-yy'), 300, 2 from dual union all
9 select 8, to_date('13-apr-15', 'dd-mon-yy'), 1200, 2 from dual union all
10 select 9, to_date('14-apr-15', 'dd-mon-yy'), 300, 2 from dual union all
11 select 10, to_date('15-apr-15', 'dd-mon-yy'), 300, 2 from dual
12 )
13 select tran_id, tran_date,
14 sum(amount) over (partition by actorid order by tran_date) tot_amt, actorid
15 from trans
16 /
TRAN_ID TRAN_DATE TOT_AMT ACTORID
1 10-APR-15 100 1
2 11-APR-15 1100 1
3 11-APR-15 1100 1
4 12-APR-15 1200 1
5 13-APR-15 1550 1
6 14-APR-15 1950 1
7 12-APR-15 300 2
8 13-APR-15 1500 2
9 14-APR-15 1800 2
10 15-APR-15 2100 2
10 rows selected.
SQL>
So correct solution is to ORDER BY transation id. Also, just in case if amount can be 0, we should add tranaction id when ordering by sum:
with trans as (
select 1 tran_Id, to_date('10-apr-15', 'dd-mon-yy') tran_date, 100 amount, 1 actorid from dual union all
select 2, to_date('11-apr-15', 'dd-mon-yy'), 100, 1 from dual union all
select 3, to_date('11-apr-15', 'dd-mon-yy'), 900, 1 from dual union all
select 4, to_date('12-apr-15', 'dd-mon-yy'), 100, 1 from dual union all
select 5, to_date('13-apr-15', 'dd-mon-yy'), 350, 1 from dual union all
select 6, to_date('14-apr-15', 'dd-mon-yy'), 400, 1 from dual union all
select 7, to_date('12-apr-15', 'dd-mon-yy'), 300, 2 from dual union all
select 8, to_date('13-apr-15', 'dd-mon-yy'), 1200, 2 from dual union all
select 9, to_date('14-apr-15', 'dd-mon-yy'), 300, 2 from dual union all
select 10, to_date('15-apr-15', 'dd-mon-yy'), 300, 2 from dual
trans_running_tot as (
select tran_id, tran_date,
sum(amount) over (partition by actorid order by tran_id) tot_amt, actorid
from trans
trans_ranked as (
select actorid,tran_id, tran_date,
rank() over (partition by actorid order by tot_amt,tran_id) rk
from trans_running_tot
where tot_amt > 1000
select * from trans_ranked where rk=1
ACTORID TRAN_ID TRAN_DATE RK
1 3 11-APR-15 1
2 8 13-APR-15 1
SQL>
SY. -
Hi Friends,
I am trying to load Employees and their Assignments using APIs.
I have various columns in my staging table like Last Name, First Name, etc., but I need help in writing query in the cursor especially for columns Emp Number and Supervisor Number.
I have data as below
Emp_Number Supervisor_Number
GE0002 GE0064
GE0064 EG0009
EG0009 EG0001
100009 EG0001
EG0001 TU0001
Cursor I write will process the data in the same order as above, but here the problem is...
When it processes first row, it checks for supervisor GE0064 which do not exist and so it errors out.
Similarly for second row, it checks for supervisor EG0009 which again do not exist and so it errors out.
So in order to prevent this, the cursor should process the rows as below
Emp_Number Supervisor_Number
EG0001 TU0001
EG0009 EG0001
GE0064 EG0009
GE0002 GE0064
100009 EG0001
By this way, Supervisor should be defined first as an employee and then it can be used as a supervisor for other employees
is there a way that I can get the output as above(second set of data), when the table has records randomly as above(first set of data)
Appreciate your help!
Thanks,
SrikanthSrikanth wrote:
... but the number of records returned by above query are lot more than number of records in the table.
Why did the number go up?
It's something only you can find out
Maybe some Emp have several Supervisor(s) like
with
t as
(select 'GE0002' Emp,'GE0064' Supervisor from dual union all
select 'GE0064','EG0009' from dual union all
select 'EG0009','EG0001' from dual union all
select 'GE0064','100009' from dual union all
select '100009','EG0001' from dual union all
select 'EG0001','TU0001' from dual
select Emp,Supervisor,lpad('_',3 * (level - 1),'_')||Emp indent
from (select Emp,Supervisor
from t
union all
select supervisor,null
from t tt
where not exists(select null
from t
where emp = tt.supervisor
start with Supervisor is null
connect by prior Emp = Supervisor
EMP
SUPERVISOR
INDENT
TU0001
TU0001
EG0001
TU0001
___EG0001
100009
EG0001
______100009
GE0064
100009
_________GE0064
GE0002
GE0064
____________GE0002
EG0009
EG0001
______EG0009
GE0064
EG0009
_________GE0064
GE0002
GE0064
____________GE0002
Regards
Etbin -
Need helping making a SQL query faster
Below is my query, it takes about 75 seconds to run right now. The outer joins are for cases that do not have records in that table. I am almost completely self taught, so I have some gaps in my knowledge of SQL.
Thanks in advance for your help.
SELECT DISTINCT B1.B1_ALT_ID CASENUMBER,
B1.B1_PER_GROUP
||'/'
||B1.B1_PER_TYPE
||'/'
||B1.B1_PER_SUB_TYPE
||'/'
||B1.B1_PER_CATEGORY APPTYPE,
BPD.B1_COMPLETE_BY RECBY,
BPD.HOUSE_COUNT HOUSECOUNT,
BPD.REC_DATE,
CASE
WHEN B3C.B1_BUS_NAME IS NOT NULL
AND B3C.B1_PRINT_FLAG = 'Y' THEN B3C.B1_BUS_NAME
ELSE B3O.B1_OWNER_FULL_NAME
END CONTRACTOR,
B3O.B1_OWNER_FULL_NAME OWNERNAME,
B3O.B1_MAIL_ADDRESS1,
B3O.B1_MAIL_ADDRESS2,
B3O.B1_MAIL_ADDRESS3,
B3O.B1_MAIL_CITY,
B3O.B1_MAIL_STATE,
B3O.B1_MAIL_ZIP,
FN_GET_PRI_ADDRESS_PARTIAL('RENO',B1.B1_PER_ID1,B1.B1_PER_ID2,B1.B1_PER_ID3) JOBSITE,
B3P.B1_PARCEL_NBR PARCELNUMBER,
B3P.B1_LOT LOT,
BV.G3_VALUE_TTL JOBVALUE,
BW.B1_WORK_DESC WORKDESC,
CASE
WHEN B3CON.B1_BUSINESS_NAME IS NOT NULL
AND B3CON.B1_CONTACT_TYPE = 'Occupant/Tenant' THEN B3CON.B1_BUSINESS_NAME
WHEN B3CON.B1_BUSINESS_NAME IS NULL
AND B3CON.B1_CONTACT_TYPE = 'Occupant/Tenant' THEN B3CON.B1_FNAME
||' '
||B3CON.B1_LNAME
ELSE ''
END TENANT,
(SELECT DISTINCT BAV.ATTRIBUTE_VALUE
FROM BAPPSPECTABLE_VALUE BAV
WHERE B1.B1_PER_ID1 = BAV.B1_PER_ID1
AND B1.B1_PER_ID2 = BAV.B1_PER_ID2
AND B1.B1_PER_ID3 = BAV.B1_PER_ID3
AND BAV.COLUMN_NAME = 'Type of Construction') TOC,
(SELECT DISTINCT BAV.ATTRIBUTE_VALUE
FROM BAPPSPECTABLE_VALUE BAV
WHERE B1.B1_PER_ID1 = BAV.B1_PER_ID1
AND B1.B1_PER_ID2 = BAV.B1_PER_ID2
AND B1.B1_PER_ID3 = BAV.B1_PER_ID3
AND BAV.COLUMN_NAME = 'Occupancy Group') OCCGROUP,
(SELECT DISTINCT BAV.ATTRIBUTE_VALUE
FROM BAPPSPECTABLE_VALUE BAV
WHERE B1.B1_PER_ID1 = BAV.B1_PER_ID1
AND B1.B1_PER_ID2 = BAV.B1_PER_ID2
AND B1.B1_PER_ID3 = BAV.B1_PER_ID3
AND BAV.COLUMN_NAME = 'Occupancy Use') OCCUSE,
(SELECT DISTINCT BC.B1_CHECKLIST_COMMENT
FROM BCHCKBOX BC
WHERE B1.B1_PER_ID1 = BC.B1_PER_ID1
AND B1.B1_PER_ID2 = BC.B1_PER_ID2
AND B1.B1_PER_ID3 = BC.B1_PER_ID3
AND BC.B1_CHECKLIST_COMMENT IS NOT NULL
AND BC.B1_CHECKBOX_DESC = 'Fire Alarm System') FIREALARM,
(SELECT DISTINCT BC.B1_CHECKLIST_COMMENT
FROM BCHCKBOX BC
WHERE B1.B1_PER_ID1 = BC.B1_PER_ID1
AND B1.B1_PER_ID2 = BC.B1_PER_ID2
AND B1.B1_PER_ID3 = BC.B1_PER_ID3
AND BC.B1_CHECKLIST_COMMENT IS NOT NULL
AND BC.B1_CHECKBOX_DESC = 'Fire Sprinkler System') FIRESPRINKLER
FROM B1PERMIT B1
INNER JOIN BPERMIT_DETAIL BPD
ON B1.B1_PER_ID1 = BPD.B1_PER_ID1
AND B1.B1_PER_ID2 = BPD.B1_PER_ID2
AND B1.B1_PER_ID3 = BPD.B1_PER_ID3
LEFT OUTER JOIN B3CONTRA B3C
ON B1.B1_PER_ID1 = B3C.B1_PER_ID1
AND B1.B1_PER_ID2 = B3C.B1_PER_ID2
AND B1.B1_PER_ID3 = B3C.B1_PER_ID3
AND B3C.B1_PRINT_FLAG = 'Y'
LEFT OUTER JOIN B3OWNERS B3O
ON B1.B1_PER_ID1 = B3O.B1_PER_ID1
AND B1.B1_PER_ID2 = B3O.B1_PER_ID2
AND B1.B1_PER_ID3 = B3O.B1_PER_ID3
and b3o.b1_primary_owner = 'Y'
LEFT OUTER JOIN B3PARCEL B3P
ON B1.B1_PER_ID1 = B3P.B1_PER_ID1
AND B1.B1_PER_ID2 = B3P.B1_PER_ID2
AND B1.B1_PER_ID3 = B3P.B1_PER_ID3
INNER JOIN BVALUATN BV
ON B1.B1_PER_ID1 = BV.B1_PER_ID1
AND B1.B1_PER_ID2 = BV.B1_PER_ID2
AND B1.B1_PER_ID3 = BV.B1_PER_ID3
INNER JOIN BWORKDES BW
ON B1.B1_PER_ID1 = BW.B1_PER_ID1
AND B1.B1_PER_ID2 = BW.B1_PER_ID2
AND B1.B1_PER_ID3 = BW.B1_PER_ID3
LEFT OUTER JOIN B3CONTACT B3CON
ON B1.B1_PER_ID1 = B3CON.B1_PER_ID1
AND B1.B1_PER_ID2 = B3CON.B1_PER_ID2
AND B1.B1_PER_ID3 = B3CON.B1_PER_ID3
AND B3CON.B1_CONTACT_TYPE = 'Occupant/Tenant'
LEFT OUTER JOIN BAPPSPECTABLE_VALUE BAV
ON B1.B1_PER_ID1 = BAV.B1_PER_ID1
AND B1.B1_PER_ID2 = BAV.B1_PER_ID2
AND B1.B1_PER_ID3 = BAV.B1_PER_ID3
AND BAV.COLUMN_NAME IN ('Type of Construction',
'Occupancy Group',
'Occupancy Use')
LEFT OUTER JOIN BCHCKBOX BC
ON B1.B1_PER_ID1 = BC.B1_PER_ID1
AND B1.B1_PER_ID2 = BC.B1_PER_ID2
AND B1.B1_PER_ID3 = BC.B1_PER_ID3
AND BC.B1_CHECKBOX_DESC IN ('Fire Alarm System',
'Fire Sprinkler System')
WHERE B1.B1_ALT_ID = UPPER('{?CaseNumber}')
and rownum = 1Hi James
Please provide me the following information.
1)What is the version of crystal reports you are using?
2)Whcih database & connection type are you using?
3)Does this query take 75 mins to run in Crystal reports designer?
4)How much time does it take at your database end?
5)How many records are fetched by this query?
6)Looking at the query it shows that it is using case statements and few joins for restricting the data.
7)Please use the same query in a command object in Crystal reports designer and check the result.
Thanks
Pradeep Hulke -
Need help in the following query
Hi ,
I have the following query to pull out data for a particular timeperiod( campaign)
select
da.acct_nr,smas.mrkt_id,smas.fld_sls_cmpgn_perd_id,smas.acct_key,smas.upln_acct_key,smas.awrd_sls_amt,smas.fld_net_sls_amt
,smas.stf_ind,da1.acct_nr,
smas.fld_sls_cmpgn_perd_id,smas.acct_key,smas.upln_acct_key,smas.awrd_sls_amt,smas.fld_net_sls_amt
,smas.stf_ind,da1.acct_nr,prev.*
From cdw.sum_mrkt_acct_sls smas
join codi.dim_acct da
on (smas.mrkt_id = da.mrkt_id and smas.acct_key = da.acct_key)
join codi.dim_acct da1
on (smas.mrkt_id = da1.mrkt_id and smas.UPLN_acct_key = da1.ACCT_KEY)
where smas.acct_key in
select dwnln_acct_key from codi.dim_ldrshp_genlgy
where mrkt_id = 66 and fld_sls_cmpgn_perd_id = 20100304
and root_upln_acct_key = (select acct_key from codi.dim_acct where acct_nr = '0032622' and mrkt_id = 66)
and
smas.mrkt_id = 66
and smas.fld_sls_cmpgn_perd_id = 20100304
and smas.sls_org_key <> -100
order by 1
So this query will pull out data for campaign 20100304
No i want to modify the query to pull out data for 3 such campaigns.
for a smas.acct_key
so data will
smas.acct_key all the data for 1st campaign , all the data for 2nd campaign. all the data for 3rd campaign
Could you please help in modifying this query.
ThanksHow about this ??
select da.acct_nr,
smas.mrkt_id,
smas.fld_sls_cmpgn_perd_id,
smas.acct_key,
smas.upln_acct_key,
smas.awrd_sls_amt,
smas.fld_net_sls_amt,
smas.stf_ind,
da1.acct_nr,
smas.fld_sls_cmpgn_perd_id,
smas.acct_key,
smas.upln_acct_key,
smas.awrd_sls_amt,
smas.fld_net_sls_amt,
smas.stf_ind,
da1.acct_nr,
prev.*
From cdw.sum_mrkt_acct_sls smas
join codi.dim_acct da on (smas.mrkt_id = da.mrkt_id and
smas.acct_key = da.acct_key)
join codi.dim_acct da1 on (smas.mrkt_id = da1.mrkt_id and
smas.UPLN_acct_key = da1.ACCT_KEY)
where smas.acct_key in (select dwnln_acct_key
from codi.dim_ldrshp_genlgy
where mrkt_id = 66
and fld_sls_cmpgn_perd_id in( 20100304,20100305,20100306)
and root_upln_acct_key =
(select acct_key
from codi.dim_acct
where acct_nr = '0032622'
and mrkt_id = 66))
and smas.mrkt_id = 66
and smas.fld_sls_cmpgn_perd_id in( 20100304,20100305,20100306)
and smas.sls_org_key - 100
order by 1 -
Need help in this sql query to use Case Statement
hi All,
I have the below query -
SELECT DISTINCT OFFC.PROV_ID
,OFFC.WK_DAY
,CASE
WHEN OFFC.WK_DAY ='MONDAY' THEN 1
WHEN OFFC.WK_DAY ='TUESDAY' THEN 2
WHEN OFFC.WK_DAY ='WEDNESDAY' THEN 3
WHEN OFFC.WK_DAY ='THURSDAY' THEN 4
WHEN OFFC.WK_DAY ='FRIDAY' THEN 5
WHEN OFFC.WK_DAY ='SATURDAY' THEN 6
WHEN OFFC.WK_DAY ='SUNDAY' THEN 7
END AS DOW
,OFFC.OFFC_OPENG_TIME
,OFFC.OFFC_CLSNG_TIME
FROM GGDD.PROV_OFFC_HR OFFC
WHERE OFFC.PROV_ID='0000600'
WITH UR;
this query is bringing results in 6 differnt rows with opening and closing time for each day separately. I want to generate the data in one row with each day having opening and closing time, so for 7 days, total 14 columns with opening and closing time. But i am not able to do that using case statement.
can somebody help me in achieving that.
thanks,
iamhereHi,
Welcome to the forum!
That's called a Pivot .
Instead of having 1CASE expression, have 14, one for the opening and one for the closing time each day, and do GROUP BY to combine them onto one row.
SELECT OFFC.PROV_ID
, MIN (CASE WHEN OFFC.WK_DAY ='MONDAY' THEN OFFC.OFFC_OPENG_TIME END) AS mon_opn
, MIN (CASE WHEN OFFC.WK_DAY ='MONDAY' THEN OFFC.OFFC_CLSNG_TIME END) AS mon_cls
, MIN (CASE WHEN OFFC.WK_DAY ='TUESDAY' THEN OFFC.OFFC_OPENG_TIME END) AS tue_opn
, MIN (CASE WHEN OFFC.WK_DAY ='TUESDAY' THEN OFFC.OFFC_CLSNG_TIME END) AS tue_cls
FROM GGDD.PROV_OFFC_HR OFFC
WHERE OFFC.PROV_ID = '0000600'
GROUP BY offc.prov_id
;This assumes there is (at most) only one row in the table for each distinct prov_id and weekday. If not, what do you want to do? Post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
The staement above works in Oracle 8.1 and up, but there's a better way (SELECT ... PIVOT) available in Oracle 11. What version are you using? (It's always a good idea to include this when you post a question.)
Edited by: Frank Kulash on Jan 6, 2011 8:22 PM -
the following query returns me the correct no of rows:
select col1 from tab1 where
col1 like '%'||chr(32)||'%';
but i need to use my query in the following form and it doesn't return any row:
select col1 from tab1 where
col1 IN ('%'||chr(32)||'%');
what am I doing worng?
thanks in advance.Or in 10g (just recycling another example):
WITH t AS (SELECT 'OPTI1457' || CHR(32) col1
FROM dual
UNION
SELECT 'OPT123' || CHR(9)
FROM dual
UNION
SELECT 'OPTIM12345'
FROM dual
SELECT t.*
FROM t
WHERE REGEXP_LIKE(t.col1, CHR(32) || '|' || CHR(9))
; C. -
Employee_table
Emp_no Emp_name Location
100 Ram Mumbai
200 Shyam Delhi
300 Jadu Bangalore
400 Madhu Hyderabad
500 Sidhu Kolkata
Employee_Comm
Emp_no Comm_flag
100 COMM
300 NO COMM
500 COMM
Note - No entry for employee 200 and 400, meand commision independent
SQL - when input flag would be COMM the display employee records 100,500,200,400
when input flag would be NO COMM the display employee records 300,200,400
when input flag would be NULL the display employee records 100,200,300,400,500Ohh,
Thank Karthick! I have changed the SQL to meet the need
WITH EMPLOYEE_TABLE
AS
SELECT 100 EMP_NO,'RAM' EMP_NAME,'MUMBAI' LOCATION FROM DUAL UNION ALL
SELECT 200,'SHYAM','DELHI' FROM DUAL UNION ALL
SELECT 300,'JADU','BANGALORE' FROM DUAL UNION ALL
SELECT 400,'MADHU','HYDERABAD' FROM DUAL UNION ALL
SELECT 500,'SIDHU','KOLKATA' FROM DUAL
), EMPLOYEE_COMM
AS
SELECT 100 EMP_NO,'COMM' COMM_FLAG FROM DUAL UNION ALL
SELECT 300,'NO COMM' FROM DUAL UNION ALL
SELECT 500,'COMM' FROM DUAL
SELECT E1.EMP_NO, EMP_NAME, LOCATION, COMM_FLAG
FROM EMPLOYEE_TABLE E1, EMPLOYEE_COMM E2
WHERE E1.EMP_NO = E2.EMP_NO (+)
AND (COMM_FLAG = :COMM_FLAG OR COMM_FLAG IS NULL);
Note: Derived from Karthick's SQL
*009* -
Need help on the below query or Pl-SQL
Hello Every one,
Please let me know if some one can help me out with the below scenario either in pl-sql or sql.
Source Data:
0000253800 0.25 0845A 0900A
0000253800 1 0900A 1000A
0000253800 1 1300P 1400P
0000253800 1 1500P 1600P
0000253800 1 1600P 1700P
Output needed:
0000253800 1.25 0845A 1000A
0000253800 1 1300P 1400P
0000253800 2 1500P 1700P
Thanks in Advance....
Edited by: user12564103 on Dec 11, 2011 5:54 PMHi,
Welcome to the forum!
Depending on your data and your requirements:
WITH got_times AS
SELECT column_1, column_2, column_3, column_4
, TO_DATE ( substr (column_3, 1, 4)
, 'HH24MI'
) AS time_3
, TO_DATE ( SUBSTR (column_4, 1, 4)
, 'HH24MI'
) AS time_4
FROM table_x
, got_grp_id AS
SELECT column_1, column_2, column_3, column_4
, time_3, time_4
, time_4 - SUM (time_4 - time_3) OVER ( PARTITION BY column_1
ORDER BY time_3
) AS grp_id
FROM got_times
SELECT column_1
, SUM (column_2) AS sum_2
, MIN (column_3) KEEP (DENSE_RANK FIRST ORDER BY time_3)
AS min_3
, MAX (column_4) KEEP (DENSE_RANK LAST ORDER BY time_4)
AS max_4
FROM got_grp_id
GROUP BY column_1
, grp_id
ORDER BY column_1
, grp_id
;Whenever you have a problem, please post CREATE TABLE and INSERT statements for your sample data, as well as the results you want from that data. Explain, with specific examples, how you get the results you want from that data.
Always say which version of Oracle you're using. The query above will work in Oracle 9.1 (and higher).
Since this is your first thread, I'll do this for you:
CREATE TABLE table_x
( column_1 NUMBER
, column_2 NUMBER
, column_3 VARCHAR2 (5)
, column_4 VARCHAR2 (5)
INSERT INTO table_x (column_1, column_2, column_3, column_4) VALUES (253800, .25, '0845A', '0900A');
INSERT INTO table_x (column_1, column_2, column_3, column_4) VALUES (253800, 1, '0900A', '1000A');
INSERT INTO table_x (column_1, column_2, column_3, column_4) VALUES (253800, 1, '1300P', '1400P');
INSERT INTO table_x (column_1, column_2, column_3, column_4) VALUES (253800, 1, '1500P', '1600P');
INSERT INTO table_x (column_1, column_2, column_3, column_4) VALUES (253800, 1, '1600P', '1700P');Column_1 identifes a day.
Column_2 is an amount that I need to total.
Column_3 and column_4 are starting and ending times. We can assume that they are all valid times (in 'HH24MI' format, plus a redundant 'A' or 'P') on the same day, column_3 is always less than column_4, and that the range of two rows for the same value of column_1 never overlap. Column_4 of one row may be equal to column_3 of another rows with the same column_1, but it will never be greater.
Each row of the output represent a contiguous group of rows (each ending exactly when the next one begins) with the same column_1, with the common column_1, the total of column_2, and the range of the group.
For example, the first two rows for a single group, because they have the same value for column_1, and one ends exactly when the other begins (at 9:00 AM). This group represents day 253800, from 8:45 AM to 10:00 AM. The totla of column_2 fro this group is .25 + 1 = 1.25.
The next row (from 1:00 PM to 2:00 PM) is a group all by itself, because there is a gap one either side of it separating it from its nearest neighbor on the same day."
Of course, I'm guessing at lots of things.
Edited by: Frank Kulash on Dec 11, 2011 9:44 PM
Changed TO_DATE calls.
Edited by: Frank Kulash on Dec 11, 2011 9:52 PM
Added sample question. -
Hi,
I have a requirement where I need to show 'F&D' string. I can explain this by below query.
select 'F&D' from dual
but when I run this query, toad prompts me to enter value for &D variable :( But my requirement is to show 'F&D' string.
Can anyone please tell me what modification needs to be done to above query to get string 'F&D' as output.
I know work around for this as below.
select 'F'||chr(38)||'D' from dual
I want permenant solution for this. Can anyone please help me?
Thanks
Shantanusee if below make difference
SQL> select 'F&D' from dual ;
Enter value for d: &D
old 1: select 'F&D' from dual
new 1: select 'F&D' from dual
'F&
F&D
SQL> select 'F'||'&'||'D' from dual ;
'F'
F&D -
Need help creating large SQL query
I am trying collect the sum of records that have a cancelled event prior to the Arrive event. The solution needs to be within the Sum function. Any ideas would be helpful
The structure of the table is as follows
1. Record_Num(primary): varchar
2. Event_Type: varchar
3. Time: timestamp
Each record number can contain multiple events, hence the issue with specifiying the time for an event of 'Arrive' and an event of 'Canceled'
This is part of a much bigger query, here's what that looks like:
Select
Sum(Case when di.calltype = 'Missed' then 1 else null end) as "Canceled Prior To Liftoff",
Sum(Case
when dt.eventtype = 'Canceled' Then 1
when
(select to_char(dt.datetimestamp, 'HH:MI:SS')
from progpennstar.dispatchtime dt
where Lower(dt.eventtype) in 'aborted')
< any (select to_char(dt.datetimestamp, 'HH:MI:SS')
from progpennstar.dispatchtime dt
where dt.eventtype in 'Arrive Locale')
Then 1
Else null end) as "Canceled Enroute to Call"
From progpennstar.dispatch di, progpennstar.dispatchtime dt
where di.dateoftransport >= to_date('07/01/2008', 'MM/DD/YYYY') and
di.dateoftransport <= to_date('09/30/2008', 'MM/DD/YYYY') and
di.dispatch_id = dt.dispatch_idto the op: I think SomeoneElse already got the solution, I just want to add a few minor points. You can replace the ANY construct (which is rarely used) with a simple comparison. In general ANY constructs are more difficult to understand. Check if the correct logic is applied.
SELECT SUM(CASE
WHEN di.calltype = 'Missed' THEN 1
ELSE NULL
END) AS "Canceled Prior To Liftoff"
,SUM(CASE
WHEN dt.eventtype = 'Canceled' THEN 1
WHEN dt.eventtype = 'Aborted'
AND exists (SELECT null
FROM dispatchtime dt2
WHERE dt2.eventtype IN 'Arrive Locale'
AND dt2.dispatch_id = di.dispatch_id
AND dt.datetimestamp < dt2.datetimestamp /* this is the new comparison */
THEN 1
ELSE NULL
END) AS "Canceled Enroute to Call"
FROM dispatch di,
dispatchtime dt
WHERE di.dateoftransport >= to_date('07/01/2008', 'MM/DD/YYYY')
AND di.dateoftransport < to_date('09/30/2008', 'MM/DD/YYYY') /* Carefull! I changed <= to <. Do you want to include the full last day or only midnight? */
AND di.dispatch_id = dt.dispatch_id;I also changed the alias of the remaining subquery so that different aliases are used.
Edited by: Sven W. on Dec 11, 2008 1:01 PM
Maybe you are looking for
-
MacBook Pro (2010) Trackpad 2 finger scroll gesture
I upgraded to OS Lion and I cannot get the scroll direction: natural two finger scroll to work. I have tried playing around with the trackpad in system preferences to no avail. Any ideas from anyone? I saw something about Terminal in another threa
-
Sub-query problem on Oracle 10g
The following query works on Oracle 10.2.0.1.0 on windows,but doesn't work on Oracle 10.2.0.2.0 on Linux. Error report: SQL Error: ORA-00904: "T"."AUDIT_USECS": invalid identifier 00904. 00000 - "%s: invalid identifier" It works after i remove the su
-
I have a document javascript that used to work. It prints the current date upon first opening the PDF into a field called Today. Thereafter, the date does not change no matter how many more times the PDF is opened. var f = this.getField("Today"); if
-
Would the mac box set work with Mac os 10.4.11
I have a macbook that has mac os x 10.4.11 I am thinking about getting the mac box set to update its software. but I was wondering if it would work on my computer?
-
I have, previously, had to reinstall everything on my PC (due to a windows issue) and have recently upgraded my computer h/ware, which also meant I had to re-install windows and all my other applications. When I install Elements 9, it gives me a mess