Please help build a sql query
Hello
Please help build a sql query
My Table Test2015 has given below
Header_id Line_id Ordered_item
723887290 199925 MAIN1
199925 723887291 MAIN2
199926 723887292 SH-POS-NO-BR POS-INS
199926 723887293 MAIN2
199927 723887294 IC-ENV-NON-BR-ENV-PXY
199927 723887295 MAIN1
199927 723887297 MAIN2
199927 723887298 PRCSS SH-FAIRY-ELEC DISTR.
199927 723887299 SH-FAIRY-SUM PRO-DE-CONS-HOUSE
I am trying to query my Test2015 table to obtain the records with ordered_item containing 'MAIN1' and 'MAIN2' only. I tried to write a query as below
SELECT * FROM test2015 WHERE ORDERED_ITEM in ('MAIN1', 'MAIN2');
But it gives me all the data with the MAIN2 records found but MAIN1 is absent, I want to retrieve only records to both 'MAIN1' and 'MAIN2' present for Header_id.
While the result below shows me header_id - 199926 and 199929 that he should assume back. I want to fetch documents only with 'MAIN1' and 'MAIN2' both present.
Header_id Line_id Ordered_item
723887290 199925 MAIN1
199925 723887291 MAIN2
199926 723887293 MAIN2
199927 723887295 MAIN1
199927 723887297 MAIN2
199929 723887299 MAIN1
Please suggest.
Thank you and best regards,
Prasad.
Hello
Try like this...
SELECT * FROM test2015 WHERE ORDERED_ITEM in ('MAIN1") and in header_id (select test2015 WHERE ORDERED_ITEM in ('MAIN2') header_id)
Tags: Database
Similar Questions
-
Help me with SQL Query to retrieve data from a view
Hello Guru,
I need help in my sql query.
I use SQL TeraData.
I want an Oracle result in the following form-
Open tickets
Open months failure / Repair Service s/o improvement request Total general
2009-01-2 4 4 5 15
2009-02 1 0 2 3 6
2009-03 4 1 2 2 9
Grand Total 7 5 8 10 30
I wrote the query as where - TIME_PERIOD, RQST_TYPE_DM and DEMAND_SUMMARY_FCT are the points of view and I extract the data from the views only.
Select NVL (CA. TIME_PERIOD. PERIOD_CD, 'Total') THAT year.
COUNT (CASE WHEN CA. RQST_TYPE_DM. RQSTTYP_DESC Like '% of Break' THEN 1 END) as BreakFix
COUNT (CASE WHEN CA. RQST_TYPE_DM. RQSTTYP_DESC as 'N/a', 1 END) by n/a
COUNT (CASE WHEN CA. RQST_TYPE_DM. RQSTTYP_DESC as 'Improvement' THEN 1 END) accessories
COUNT (CASE WHEN CA. RQST_TYPE_DM. RQSTTYP_DESC Like '% Service' THEN 1 END) as ServiceRequests
COUNT (CA. RQST_TYPE_DM. RQSTTYP_DESC) AS grand_total
FROM CA. TIME_PERIOD, CA. RQST_TYPE_DM, CA. DEMAND_SUMMARY_FCT
WHERE (CA. DEMAND_SUMMARY_FCT. RQSTTYP_ID = CA. RQST_TYPE_DM. RQSTTYP_ID)
AND (CASE
WHEN CA. DEMAND_SUMMARY_FCT. MONTH_ID = CA. TIME_PERIOD. PERIOD_ID, 1
WHEN {fn concat ({fn concat (SUBSTR (CA. TIME_PERIOD. {(PERIOD_CD, 3, 4),'-')}, SUBSTR (CA. TIME_PERIOD. PERIOD_CD, 7, 2))} BETWEEN ' 2009-01' AND ' 2009-03' THEN 1
WHEN CA. DEMAND_SUMMARY_FCT. RQSTTYP_ID = '1' then 1
END) = 1
GROUP BY ROLLUP (CA. TIME_PERIOD. PERIOD_CD)
After executing the query, I get the following error:
3076: syntax Error: Data Type 'Time' does not match a defined Type name.
:( Kindly help me with this and let me know where I'm wrong... Please.Messages indicates something wrong with your data... It would seem that the data does not match your format mask.
Thus, the data or the format mask.
-
Hello
I have a data in table (raj_table) with columns (char11) raj_id, raj_number (varchar2 (15)), raj_format (NUMBER), Primary_ID (identity with the values of the primary key column)
Primary_ID raj_id Raj_number Raj_format
1 raj rajvend 1
2 raj rajvend 1
3 raj rajvendor1 2
4 raj rajvendor1 2
5 raj rajvendor1 2
6 raj rajvendor2 3
I used under SQL to get query output as below, but has not achieved the required result:
Select client_id vendor_number, vendor_format, primary_id, row_number() on sl_no (client_id partition, primary_id, vendor_format order of client_id primary_id, vendor_format, vendor_number, vendor_number)
from raj_table by sl_no asc
SL_NO raj_id raj_number raj_format primary_id
1 1 raj rajvendor 1
1 2 raj rajvendor 1
2 3 raj rajvendor1 2
2 4 raj rajvendor1 2
2 5 raj rajvendor1 2
3 6 raj rajvendor2 3
I need help with a SQL query to get the result as above without using the group by clause. I want to bring together the combination of separate line of the three columns (raj_id, raj_number, raj_format) and add a unique serial number for each online game (SL_NO column below). So, above there are 3 unique set of (raj_id, raj_number, raj_format) I can get in a group by clause, but I can not add prmiary_id, SL_NO values if I group by clause. I used the analytical functions like row_number() but no luck. Need solution for this.
with t as)
Select 'raj' raj_id, 'rajvend' raj_number, 1 raj_format, 1 primary_id Union double all the
Select option 2, 'raj', 'rajvend', 1 double Union all
Select 3, 'raj', 'rajvendor1', 2 double Union all
Select 4, 'raj', 'rajvendor1', 2 double Union all
Select 5, 'raj', 'rajvendor1', 2 double Union all
Select 6, 'raj', 'rajvendor2', 3 double
)
Select dense_rank() over (order of raj_id, raj_number, raj_format) sl_no,
t.*
t
order by primary_id
/
PRIMARY_ID RAJ RAJ_NUMBER RAJ_FORMAT SL_NO
---------- ---------- --- ---------- ----------
1 1 raj rajvend 1
1 2 raj rajvend 1
2 3 raj rajvendor1 2
2 4 raj rajvendor1 2
2 5 raj rajvendor1 2
3 6 raj rajvendor2 36 selected lines.
SQL >
SY.
-
Help with making SQL query references to column aliases in the Case statement
I need help with a sql query that I'm trying. I can go about it the wrong way, but I would be grateful if I could get any suggestions on possible solutions. This is my query:
SELECT DISTINCT spriden_pidm, spriden_id id, spriden_last_name | ',' | spriden_first_name name,
CASE
WHEN rcresar_comm_code_01 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_01
WHEN rcresar_comm_code_02 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_02
WHEN rcresar_comm_code_03 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_03
WHEN rcresar_comm_code_04 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_04
WHEN rcresar_comm_code_05 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_05
WHEN rcresar_comm_code_06 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_06
WHEN rcresar_comm_code_07 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_07
WHEN rcresar_comm_code_08 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_08
WHEN rcresar_comm_code_09 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_09
WHEN rcresar_comm_code_10 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_10
END acg_elig_comm_code
CASE
WHEN acg_elig_comm_code = ' 268' THEN 'rigorous HS course. "
WHEN acg_elig_comm_code = '269' THEN ' 2 or several AP or IB"
WHEN acg_elig_comm_code = '270' THEN 'NOC as possible ".
END comm_code_description
OF spriden, rcresar, rcrapp1
WHERE (rcresar_comm_code_01 IN ('268 ', '269', ' 270')
OR rcresar_comm_code_02 ('268 ', '269', ' 270')
OR rcresar_comm_code_03 ('268 ', '269', ' 270')
OR rcresar_comm_code_04 ('268 ', '269', ' 270')
OR rcresar_comm_code_05 ('268 ', '269', ' 270')
OR rcresar_comm_code_06 ('268 ', '269', ' 270')
OR rcresar_comm_code_07 ('268 ', '269', ' 270')
OR rcresar_comm_code_08 ('268 ', '269', ' 270')
OR rcresar_comm_code_09 ('268 ', '269', ' 270')
OR rcresar_comm_code_10 ('268 ', '269', ' 270'))
Rcresar_aidy_code = & aidy_code
AND rcrapp1_aidy_code = rcresar_aidy_code
AND rcrapp1_curr_rec_ind = 'Y '.
AND rcrapp1_seq_no = rcresar_seq_no
AND spriden_pidm = rcresar_pidm
AND rcrapp1_pidm = rcresar_pidm
AND spriden_change_ind IS NULL
ORDER BY name
The second case statement is where I don't know exactly what it takes to get what I want.
Output should be like:
spriden_pidm name ID acg_elig_comm_code comm_code_description
«0000000000', ' 1111111111 ","John Doe","268", «rigorous HS race"»
If I take the second case statement it works great except that I do not have my comm_code description column. My question is how can I use my first statement value box to determine this column? I think that I need a case statement as I have, but I don't know how to reference the value of acg_elig_comm_code. Any help would be greatly appreciated. Thank you.
Published by: blackhole82 on January 20, 2009 09:20Hello
You cannot use the alias column in the query, even where it is set (except in the ORDER BY clause).
You can set the alias in a subquery and then use it in a great query, like this:WITH sub_q AS ( SELECT DISTINCT spriden_pidm,spriden_id id, spriden_last_name||', '||spriden_first_name name, CASE WHEN rcresar_comm_code_01 IN ('268','269','270') THEN rcresar_comm_code_01 WHEN rcresar_comm_code_02 IN ('268','269','270') THEN rcresar_comm_code_02 WHEN rcresar_comm_code_03 IN ('268','269','270') THEN rcresar_comm_code_03 WHEN rcresar_comm_code_04 IN ('268','269','270') THEN rcresar_comm_code_04 WHEN rcresar_comm_code_05 IN ('268','269','270') THEN rcresar_comm_code_05 WHEN rcresar_comm_code_06 IN ('268','269','270') THEN rcresar_comm_code_06 WHEN rcresar_comm_code_07 IN ('268','269','270') THEN rcresar_comm_code_07 WHEN rcresar_comm_code_08 IN ('268','269','270') THEN rcresar_comm_code_08 WHEN rcresar_comm_code_09 IN ('268','269','270') THEN rcresar_comm_code_09 WHEN rcresar_comm_code_10 IN ('268','269','270') THEN rcresar_comm_code_10 END acg_elig_comm_code -- Originally posted with , here (error) FROM spriden, rcresar, rcrapp1 WHERE (rcresar_comm_code_01 IN ('268','269','270') OR rcresar_comm_code_02 IN ('268','269','270') OR rcresar_comm_code_03 IN ('268','269','270') OR rcresar_comm_code_04 IN ('268','269','270') OR rcresar_comm_code_05 IN ('268','269','270') OR rcresar_comm_code_06 IN ('268','269','270') OR rcresar_comm_code_07 IN ('268','269','270') OR rcresar_comm_code_08 IN ('268','269','270') OR rcresar_comm_code_09 IN ('268','269','270') OR rcresar_comm_code_10 IN ('268','269','270')) AND rcresar_aidy_code = &aidy_code AND rcrapp1_aidy_code = rcresar_aidy_code AND rcrapp1_curr_rec_ind = 'Y' AND rcrapp1_seq_no = rcresar_seq_no AND spriden_pidm = rcresar_pidm AND rcrapp1_pidm = rcresar_pidm AND spriden_change_ind IS NULL ) SELECT sub_q.*, CASE WHEN acg_elig_comm_code = '268' THEN 'Rigorous HS course' WHEN acg_elig_comm_code = '269' THEN '2 or more AP or IB' WHEN acg_elig_comm_code = '270' THEN 'ACG possible' END comm_code_description FROM sub_q ORDER BY name
Furthermore, you might think to rearrange your table, so that you do not have 10 columns (rcresar_comm_code_01, rcresar_comm_code_02,...) that essentially do the same thing. The usual way to handle this kind of one-to-many relationship is to have all rcresar_comm_codes in a separate table, one per line, with a pointer to the table where you have them now.
Published by: Frank Kulash, January 20, 2009 11:35
Syntax error has been corrected -
Hello
Could you please help me with this requirement.
I have the table in this format. I need to get into this format.
The attribute and its value are inserted in the first table. I need to get it saved in the second table...
Please notify.
Please help me how to solve this problem...!Table 1. Id name1 value1 name2 value2 name3 value3 name4 value4 1 attr1 attr1text attr2 attr2text attr3 attr3text attr4 attr4text 2 attr2 attr2tect attr3 attr3text .. .. .. .. 3 attr4 attr4text attr3 attr3text .. .. .. .. 4 attr5 attr5text .. .. .. .. .. .. 5 attr1 attr1text .. .. .. .. .. .. Table 2 id name1 value1 name2 value2 name3 value3 name4 value4 1 attr1 attr1text attr2 attr2text attr3 attr3text attr4 attr4text 2 attr2 attr2tect attr3 attr3text .. .. .. .. 3 attr4 attr4text attr3 attr3text .. .. .. .. 4 attr5 attr5text .. .. .. .. .. .. 5 attr1 attr1text .. .. .. .. .. ..
Concerning
AR
Edited by: 871270 December 10, 2012 22:13871270 wrote:
Sorry blushadow,Please find my desired... output above to insert into another table. Please advise me on that...!
Concerning
ARWell, in response to your question, it would be something like this...
SQL> ed Wrote file afiedt.buf 1 with table1 as (select 1 as id, 'attr1' as name1, 'attr1text' as value1 2 , 'attr2' as name2, 'attr2text' as value2 3 , 'attr3' as name3, 'attr3text' as value3 4 , 'attr4' as name4, 'attr4text' as value4 5 from dual union all 6 select 2, 'attr2','attr2tect','attr3','attr3text',null,null,null,null from dual union all 7 select 3, 'attr4','attr4text','attr3','attr3text',null,null,null,null from dual union all 8 select 4, 'attr5','attr5text',null,null,null,null,null,null from dual union all 9 select 5, 'attr1','attr1text',null,null,null,null,null,null from dual) 10 -- 11 -- end of example data, use below query against your own table 12 -- 13 select id, decode('attr1',name1,value1,name2,value2,name3,value3,name4,value4) as attr1 14 ,decode('attr2',name1,value1,name2,value2,name3,value3,name4,value4) as attr2 15 ,decode('attr3',name1,value1,name2,value2,name3,value3,name4,value4) as attr3 16 ,decode('attr4',name1,value1,name2,value2,name3,value3,name4,value4) as attr4 17 ,decode('attr5',name1,value1,name2,value2,name3,value3,name4,value4) as attr5 18* from table1 SQL> / ID ATTR1 ATTR2 ATTR3 ATTR4 ATTR5 ---------- --------- --------- --------- --------- --------- 1 attr1text attr2text attr3text attr4text 2 attr2tect attr3text 3 attr3text attr4text 4 attr5text 5 attr1text
And this selection, you can insert data using INSERT... SELECT statement.
-
Hello
It's my first table REPORT
report as)
Select 'vendor_1' vendor, to_date('1/1/2012','DD/MM/YYYY') supply_date, "customer_1" as a customer, "item_1" like item1, 110 as cost of double union all
Select 'vendor_1' vendor, to_date('1/3/2012','MM/DD/YYYY') supply_date, "customer_1" as a customer, "item_1" like item1, 120 as cost of double union all
Select 'vendor_1' vendor, to_date('1/3/2012','MM/DD/YYYY') supply_date, "customer_1" as a customer, "item_1" like item1, 130 as cost of double union all
Select 'vendor_1' vendor, to_date('1/4/2012','MM/DD/YYYY') supply_date, "customer_1" as a customer, "item_1" like item1, 140 as cost of double union all
Select 'vendor_1' vendor, to_date('1/10/2012','MM/DD/YYYY') supply_date, "customer_1" as a customer, "item_1" element, 200as cost of double union all
Select 'vendor_1' vendor, to_date('1/23/2012','MM/DD/YYYY') supply_date, "customer_1" as a customer, "item_1" as point 0 as the cost of dual union all
Select 'vendor_1' vendor, to_date('1/6/2012','MM/DD/YYYY') supply_date, "customer_2" as a customer, "item_1" like item1, 160 as cost of double union all
Select 'vendor_1' vendor, to_date('1/7/2012','MM/DD/YYYY') supply_date, "customer_2" as a customer, "item_1" like item1, 170 as cost of double union all
Select 'vendor_1' vendor, to_date('1/8/2012','MM/DD/YYYY') supply_date, "customer_2" as a customer, "item_1" like item1, 180 as cost of double union all
Select 'vendor_1' vendor, to_date('1/9/2012','MM/DD/YYYY') supply_date, "customer_2" as a customer, "item_1" like item1, 190 as cost of double union all
Select 'vendor_1' vendor, to_date('1/20/2012','MM/DD/YYYY') supply_date, "customer_2" as a customer, "item_1" like item1, 300 as cost of double)
IT'S MY SECOND TABLE: TEMP_WEEK
WITH temp_week (s)
SELECT January 1, 2012 to 07/01/2012 ' AS the week of all the double union
SELECT August 1, 2012 to 14/01/2012 ' AS the week of all the double union
January 15, 2012 to 21/01/2012 ' AS the week of all the double union
SELECT January 22, 2012 to 28/01/2012 ' AS the week of all the double union
(SELECT 29 January 2012 to 31/01/2012 ' WEEK of double)
To find weekly sales that I wrote query below:
SELECT week, supplier, customer, SUM (cost)
(SELECT week, supplier, customer, TOTAL costs (cost)
FROM (SELECT BOX
WHEN TO_NUMBER (TO_CHAR (TRUNC (supply_date), 'DD')) BETWEEN 1 AND 7 MAY
January 1, 2012 to 07/01/2012 '
WHEN TO_NUMBER (TO_CHAR (TRUNC (supply_date), 'DD')) BETWEEN 8 AND 14 MAY
14/01/2012, 1 August 2012'
WHEN TO_NUMBER (TO_CHAR (TRUNC (supply_date), 'DD')) BETWEEN 15 AND 21 THEN
January 15, 2012 to 21/01/2012 '
WHEN TO_NUMBER (TO_CHAR (TRUNC (supply_date), 'DD')) BETWEEN 22 AND 28 and THEN
January 22, 2012 to 28/01/2012 '
WHEN TO_NUMBER (TO_CHAR (TRUNC (supply_date), 'DD')) BETWEEN 29 AND 31 MAY
29 January 2012 to 31/01/2012 '
END
week, supplier, customer, cost
REPORT)
GROUP BY week, supplier, customer
UNION ALL
SELECT tw.week, vendor, customer or 0
(SELECT week, supplier, customer, TOTAL costs (cost)
FROM (SELECT BOX
WHEN TO_NUMBER (TO_CHAR (TRUNC (supply_date), 'DD')) BETWEEN 1 AND 7 MAY
January 1, 2012 to 07/01/2012 '
WHEN TO_NUMBER (TO_CHAR (TRUNC (supply_date), 'DD')) BETWEEN 8 AND 14 MAY
14/01/2012, 1 August 2012'
WHEN TO_NUMBER (TO_CHAR (TRUNC (supply_date), 'DD')) BETWEEN 15 AND 21 THEN
January 15, 2012 to 21/01/2012 '
WHEN TO_NUMBER (TO_CHAR (TRUNC (supply_date), 'DD')) BETWEEN 22 AND 28 and THEN
January 22, 2012 to 28/01/2012 '
WHEN TO_NUMBER (TO_CHAR (TRUNC (supply_date), 'DD')) BETWEEN 29 AND 31 MAY
29 January 2012 to 31/01/2012 '
END
week, supplier, customer, cost
REPORT)
GROUP BY week, supplier, customer), temp_week tw)
GROUP BY week, supplier, customer
ORDER BY supplier, customer, week;
with above query I get below output:
with output voltage)
Select January 1, 2012 to 07/01/2012 ' week, 'vendor_1' as a provider, "customer_1" as a customer, 500 as cost of double union all
Select August 1, 2012, to 14/01/2012 ' week, 'vendor_1' as a provider, "customer_1" as a customer, 200 as cost of double union all
Select January 15, 2012 to 21/01/2012 ' week, 'vendor_1' as a provider, "customer_1" as a customer, 0 as the cost of dual union all
Select January 22, 2012 to 28/01/2012 ' week, 'vendor_1' as a provider, "customer_1" as a customer, 0 as the cost of dual union all
Select the 29 January 2012 to 31/01/2012 ' week, 'vendor_1' as a provider, "customer_1" as a customer, 0 as the cost of dual union all
Select January 1, 2012 to 07/01/2012 ' week, 'vendor_1' as a provider, "customer_2" as a customer, 330 as costs of double union all
Select August 1, 2012, to 14/01/2012 ' week, 'vendor_1' as a provider, "customer_2" as a customer, 370 as cost of double union all
Select January 15, 2012 to 21/01/2012 ' week, 'vendor_1' as a provider, "customer_2" as a customer, 300 as cost of double union all
Select January 22, 2012 to 28/01/2012 ' week, 'vendor_1' as a provider, "customer_2" as a customer, 0 as the cost of dual union all
Select the 29 January 2012 to 31/01/2012 ' week, 'vendor_1' as a provider, "customer_2" as a customer, double cost of 0)
Clearly, I used cross join. But I want to get the same result with another way. I don't want to use cross join and Union
Please help me in this.What you desire can be accomplished using the outer join partitioned.
The documentation shows an example of [url http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_10002.htm#i2177515] this way to fill the gaps in the data.Here's how you can use it in your case:
SQL> with report as ( 2 select 'vendor_1' as vendor,to_date('1/1/2012','DD/MM/YYYY') supply_date, 'customer_1'as customer,'item_1' as item1, 110 as cost from dual union all 3 select 'vendor_1' as vendor,to_date('1/3/2012','MM/DD/YYYY') supply_date,'customer_1'as customer, 'item_1' as item1,120 as c ost from dual union all 4 select 'vendor_1' as vendor,to_date('1/3/2012','MM/DD/YYYY') supply_date, 'customer_1'as customer, 'item_1' as item1,130 as cost from dual union all 5 select 'vendor_1' as vendor,to_date('1/4/2012','MM/DD/YYYY') supply_date,'customer_1'as customer, 'item_1' as item1,140 as c ost from dual union all 6 select 'vendor_1' as vendor,to_date('1/10/2012','MM/DD/YYYY') supply_date, 'customer_1'as customer, 'item_1' as item ,200as cost from dual union all 7 select 'vendor_1' as vendor,to_date('1/23/2012','MM/DD/YYYY') supply_date, 'customer_1'as customer, 'item_1' as item,0 as co st from dual union all 8 select 'vendor_1' as vendor,to_date('1/6/2012','MM/DD/YYYY') supply_date,'customer_2'as customer, 'item_1' as item1,160 as c ost from dual union all 9 select 'vendor_1' as vendor,to_date('1/7/2012','MM/DD/YYYY') supply_date,'customer_2'as customer, 'item_1' as item1,170 as c ost from dual union all 10 select 'vendor_1' as vendor,to_date('1/8/2012','MM/DD/YYYY') supply_date,'customer_2'as customer, 'item_1' as item1,180 as c ost from dual union all 11 select 'vendor_1' as vendor,to_date('1/9/2012','MM/DD/YYYY') supply_date,'customer_2'as customer,'item_1' as item1,190 as co st from dual union all 12 select 'vendor_1' as vendor,to_date('1/20/2012','MM/DD/YYYY') supply_date,'customer_2'as customer,'item_1' as item1,300 as c ost from dual 13 ), weeks as ( 14 select to_date('1/1/2012','DD/MM/YYYY') + (level-1)*7 week_start 15 , to_date('1/1/2012','DD/MM/YYYY') + (level-1)*7 + 6 week_end 16 , level week_pseudo_id 17 from dual 18 connect by level <= 5 /* substitute desired number of weeks */ 19 ) 20 -- 21 -- end-of-test-data 22 -- 23 select to_char(max(weeks.week_start),'DD/MM/YYYY') 24 || ' to ' || 25 to_char(max(weeks.week_end),'DD/MM/YYYY') week 26 , vendor 27 , customer 28 , nvl(sum(cost),0) cost 29 from report 30 partition by (vendor, customer) 31 right outer join weeks 32 on weeks.week_start <= report.supply_date 33 and weeks.week_end >= report.supply_date 34 group by 35 vendor 36 , customer 37 , week_pseudo_id 38 order by 39 vendor 40 , customer 41 , week_pseudo_id 42 / WEEK VENDOR CUSTOMER COST ------------------------ -------- ---------- ---------- 01/01/2012 to 07/01/2012 vendor_1 customer_1 500 08/01/2012 to 14/01/2012 vendor_1 customer_1 200 15/01/2012 to 21/01/2012 vendor_1 customer_1 0 22/01/2012 to 28/01/2012 vendor_1 customer_1 0 29/01/2012 to 04/02/2012 vendor_1 customer_1 0 01/01/2012 to 07/01/2012 vendor_1 customer_2 330 08/01/2012 to 14/01/2012 vendor_1 customer_2 370 15/01/2012 to 21/01/2012 vendor_1 customer_2 300 22/01/2012 to 28/01/2012 vendor_1 customer_2 0 29/01/2012 to 04/02/2012 vendor_1 customer_2 0 10 rows selected.
I have redesigned weeks of date columns for the join which will probably perform better and can possibly use indexes.
I assume that you have no time portion in your supply_date data? If you do, then week_end will need to be modified. -
Need help with PL/SQL query complex
I need help with a query that need access to data from 3 tables. That's what I did
I created 3 tables
CREATE TABLE post_table
(
post_id varchar (20),
datepost DATE,
KEY (post_id) elementary SCHOOL
) ;
CREATE TABLE topic
(
TOPIC_ID varchar (20),
name varchar (20),
PRIMARY KEY (topic_id)
);
CREATE TABLE blogpost_table
(
TOPIC_ID varchar (20),
post_id varchar (20),
PRIMARY KEY (topic_id, post_id);
FOREIGN KEY (topic_id) REFERENCES topic (topic_id) ON DELETE CASCADE,
FOREIGN KEY (post_id) REFERENCES post_table (post_id) ON DELETE CASCADE
);
Now, I inserted a few values in these tables as
INSERT INTO post_table VALUES ('p1', to_date ('2009-09-14 18:00 "," MM/DD/YYYY mi:ss'));))
INSERT INTO post_table VALUES ('p2', to_date ('2009-07-18 18:00 "," MM/DD/YYYY mi:ss'));))
INSERT INTO post_table VALUES ('p3', to_date ('2009-07-11 18:00 "," MM/DD/YYYY mi:ss'));))
INSERT INTO post_table VALUES ('p4', to_date ('2009-03-11 18:00 "," MM/DD/YYYY mi:ss'));))
INSERT INTO post_table VALUES ('p5', to_date ('2009-07-13 18:00 "," MM/DD/YYYY mi:ss'));))
INSERT INTO post_table VALUES ('p6', to_date ('2009-06-12 18:00 "," MM/DD/YYYY mi:ss'));))
INSERT INTO post_table VALUES ('p7', to_date ('2009-07-11 18:00 "," MM/DD/YYYY mi:ss'));))
INSERT INTO VALUES subject ("t1", "baseball");
INSERT INTO category VALUES ('t2', 'football');
INSERT INTO blogpost_table VALUES ("t1", "p1");
INSERT INTO blogpost_table VALUES ('t1', 'p3');
INSERT INTO blogpost_table VALUES ("t1", "p4");
INSERT INTO blogpost_table VALUES ('t1', 'p5');
INSERT INTO blogpost_table VALUES ('t2', 'p2');
INSERT INTO blogpost_table VALUES ('t2', 'p6');
INSERT INTO blogpost_table VALUES ("t2", "p7");
I'm launching SQL queries on the table in this topic.
I want to write a SQL query that returns me the name of a topic (s) and the number of blog_post (s) associated with the topic in descending order of the number of blog posts created in July.
Can someone please help me to write this query?
Thank you
Published by: user11994430 on October 9, 2009 07:24Thanks for the test of the configuration!
SQL>SELECT t.NAME, COUNT(*) 2 FROM topic t, blogpost_table b, post_table p 3 WHERE b.topic_id = t.topic_id 4 AND p.post_id = b.post_id 5 AND p.datepost >= DATE '2009-07-01' 6 AND p.datepost < DATE '2009-08-01' 7 GROUP BY t.NAME 8 ORDER BY COUNT(*) desc; NAME COUNT(*) -------------------- ---------- baseball 2 soccer 2
HTH, Urs
-
How to build a SQL query to print ranges of week of a month
Hi gurus!
I have a requirement to create a query to display the times of week of a month.
The entries will be Year and month.
The result should be something like this:
If the year = 2009 and month = June
The SQL query should return the following:
June 1, 2009-June 6, 2009
June 7, 2009 to June 13, 2009
June 14, 2009-June 20, 2009
June 21, 2009-June 27, 2009
June 27, 2009 to June 30, 2009
I'm confused how to proceed. Please help me. Any help would be appreciated grately.
Thank you
AhmedHello
This might help (the exit is not exactly the same):
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0 Connected as hr SQL> SQL> SELECT TRUNC(to_date('01-Jun-2009'), 2 'D') + 7 * (LEVEL - 1) AS START_DATE, 3 TRUNC(to_date('01-Jun-2009'), 4 'D') + 7 * (LEVEL - 1) + 6 AS END_DATE 5 FROM dual 6 CONNECT BY LEVEL <= 4; START_DATE END_DATE ----------- ----------- 31/05/2009 06/06/2009 07/06/2009 13/06/2009 14/06/2009 20/06/2009 21/06/2009 27/06/2009 SQL>
Kind regards
-
Hi all
The application team uses under request and asked for recommendation for listening to this query.
The concern was
(1) how to avoid the query select sup as a query of subselects 3 is here which may degrade the performance of the query.
(2) in any case to avoid using the JOBBERDETAILS 5 times in NVL function.
SELECT "0". SM. SITE_ID AS "SD_SITE_NUMBER."
SA. ADDR_L1 AS "SD_SITE_ADDRESS."
SA. CITY_NAME AS "SD_SITE_CITY."
SA. STATE_AC AS "SD_SITE_STATE."
SUBSTR (SA. ZIP_CD, 0, 5) AS "SD_SITE_ZIP."
DECODE (LENGTH (TRIM (SA. ZIP_CD)), 5, '0000', SUBSTR (SA. ZIP_CD, 6, 4)) AS "SD_SITE_ZIP4."
SM. SITE_NAME AS "SD_NAME."
NVL ((SELECT SM. AM_SITE DOJ_NAME. B JOBBER_BASE WHERE B.SITE_ID = SM. (SITE_ID),' ') AS "SD_NAME2."
NVL (AM_SITE. JOBBERDETAILS (SA. (SITE_ID, 'OWNER_ADDRESS'),' ') AS "SD_MAILING_ADDRESS."
NVL (AM_SITE. JOBBERDETAILS (SA. (SITE_ID, 'OWNER_CITY'),' ') AS "SD_MAILING_CITY."
NVL (AM_SITE. JOBBERDETAILS (SA. (SITE_ID, 'OWNER_STATE'),' ') AS "SD_MAILING_STATE."
NVL (AM_SITE. JOBBERDETAILS (SA. (SITE_ID, 'OWNER_ZIP_CODE'),' ') AS "SD_MAILING_ZIP_CODE."
NVL (AM_SITE. JOBBERDETAILS (SA. (SITE_ID, 'OWNER_PHONE'), "0000000000") AS "SD_PHONE_NUMBER."
NVL ((SELECT ST. PHONE FROM AM_SITE. WHERE THE ST. SITE_TELEPHONE ST. SITE_ID = SM. SITE_ID AND SUPERIOR (ST. PHONE) = 'P'), "0000000000") AS "SITE_PHONE_NUMBER."
NVL ((SELECT B.J_SITE_ID FROM AM_SITE. B JOBBER_BASE WHERE B.SITE_ID = SM. SITE_ID AND AM_SITE. GET_COT (SM. (SITE_ID) = 'J'), "") AS "SD_JOBBER_NUMBER."
"1" AS "BANK_LEVEL."
"E' AS"AGENT. "
SM. DEFERRED_IND AS "DEFFERED_SETTLEMENT_INDICATOR."
SM. EXCESSIVE_CHAGK_IND AS "EXCESSIVE_CHARGEBACK_INDICATOR."
NVL (AM_SITE. GET_COT (SM. (SITE_ID),' ') AS "CHANNEL_OF_TRADE."
DECODE (AM_SITE. GET_COT (SM. ("" "" "SITE_ID),'d", '02', 'J', '11', ' C ', '23', "R", '21', 'N ' 21', ' Z ',' 21', 'NA') AS 'OUTLET_CODE '.
OF AM_SITE. SM, AM_SITE SITE_MSTR. SITE_ADDR SA
WHERE SM. SITE_ID = SA. SITE_ID AND SUPERIOR (SA. ADDR_TYPE) = 'P' AND SM. MOP_CD IS NOT NULL
Can someone please help me in setting this query.
Thanks in advanceFollower of the Oracle says:
Hi allThe application team uses under request and asked for recommendation for listening to this query.
The concern was(1) how to avoid the query select sup as a query of subselects 3 is here which may degrade the performance of the query.
Use outer joins instead of scalar subqueries. See for example the recent discussion of Tom Kyte: http://asktom.oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:1594885400346999596
(2) in any case to avoid using the JOBBERDETAILS 5 times in NVL function.
You need to know what the function does, and replace it with a simple corresponding SQL expression. Most of the time these functions search the recursive SQL which can be very expensive. Although the functional separation is better in terms of encapsulation/maintainability ordinary SQL approach gives better performance if you have a result of great value.
Have you checked that above two points are key issues of this query? By work without the function calls and scalar subqueries, you can check if it performs much better. If this isn't the case, you will need to investigate on what is still the main culprit.
Kind regards
RandolfOracle related blog stuff:
http://Oracle-Randolf.blogspot.com/SQLTools ++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676 /.
http://sourceforge.NET/projects/SQLT-pp/ -
Hi all
Here is the sql query,
Select papf.employee_number,
-papf.full_name, ppa.effective_date, pp.payroll_name,
PET.element_name,
PIV. Name input_value,
prrv.result_value
-, ppa.payroll_action_id, ppa.time_period_id
Of
Apps.pay_payroll_actions App,
pay_assignment_actions AAP,
pay_payrolls_f pp,
pay_run_results prr,
prrv pay_run_result_values,
pay_input_values_f piv,
animal pay_element_types_f,
Apps.per_all_assignments_f ADP,
Apps.per_all_people_f women's wear
-where ppa.payroll_action_id =: payroll_action_id - give your payroll_action_id
where ppa.payroll_id =: payroll_id
and ppa.payroll_action_id =: payroll_action_id
- and paa.assignment_action_id =: assignment_action_id
and ppa.payroll_action_id = paa.payroll_action_id
and ppa.payroll_id = pp.payroll_id
and paa.assignment_action_id = prr.assignment_action_id
and prr.run_result_id = prrv.run_result_id
and prrv.input_value_id = piv.input_value_id
and piv.element_type_id = pet.element_type_id
and paaf.assignment_id = paa.assignment_id
and paaf.person_id = papf.person_id
and trunc (sysdate) between pp.effective_start_date and pp.effective_end_date
and trunc (sysdate) between pet.effective_start_date and pet.effective_end_date
and trunc (sysdate) between piv.effective_start_date and piv.effective_end_date
and trunc (sysdate) between paaf.effective_start_date and paaf.effective_end_date
and trunc (sysdate) between papf.effective_start_date and papf.effective_end_date
- and papf.employee_number = '1'
- and ppa.effective_date = July 22, 2014"
and pet.element_name in ('Local Mission allowance', "Compensation of Mission International")
order by 1.3
The result is:
Employee_number Element_Name Input_Value Result_value
1 compensation of Mission international day amount 1000
1 compensation of international Mission Distance days 4
1 value to pay compensation of 1200 International Mission
1 International Mission allowance Start Date 01/01/2014
1 compensation of Mission international day amount 800
1 compensation of international Mission Distance days 10
1 International Mission allowance pay value 2000
1 International Mission allowance Start Date 01/02/2014
1 compensation of Mission local day amount 500
1 compensation of local Mission Distance days 10
1 Mission allowance paid local value 1000
1 compensation of local Mission Start Date 01/11/2014
Desired output:
Employee_number Element_Name Day_Amount Distance_Days Pay_Value Start_Date
1 compensation of international Mission 1000, 1200 4 2014/01/01
1
International Mission allowance 800 10 2000 01/02/2014 1 Mission local compensation 500 10 1000 01/11/2014 Please suggest.
INSERT statement:
TOGETHER TO DEFINE
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', 'Day amount', '1000');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', 'Day amount', '1000');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', 'number of orders', '196');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', 'number of orders', '195');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', ' Distance days, 4 ");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', ' Distance days, 1 ");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
("1', 'International Mission allowance', 'employee Category", "scale of employment medical cities");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
("1', 'International Mission allowance', 'employee Category", "scale of employment medical cities");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', 'Class level', 'G3');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', 'Class level', 'G3');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', 'class of employment, ' ');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', 'class of employment, ' ');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', 'Country of Mission', ' 3003 - Kuwait ");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', 'Country of Mission', ' 2004 - Canada ");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', ' Mission days, 4' ");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', ' Mission days, 3' ");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'compensation of Mission international","End of Mission Date"' 2014/07/10 00:00:00 ');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'compensation of Mission international","End of Mission Date"' 2014/07/19 00:00:00 ');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'compensation of Mission international", 'Mission Start Date', ' 2014/07/07 00:00:00 ');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'compensation of Mission international", 'Mission Start Date', ' 2014/07/17 00:00:00 ');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', 'value of pay', '3000');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('1', 'International Mission allowance', 'value of pay', '4000');
00:00 ');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70', 'Local Mission allowance', 'number of orders', '45');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70', 'Local Mission allowance', 'number of orders', "456789");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70 ', 'local Mission allowance', ' away days '0' ");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70 ', 'local Mission allowance', ' Distance days, 1 ");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70 ', 'local Mission allowance', 'Provided food', 'Y');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70', 'compensation of local Mission","Accommodation provided", 'Y');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70', 'compensation of local Mission","Accommodation provided", 'Y');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70 ', "local Mission allowance", "Mission City", "AL MEDINA");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70 ', "local Mission allowance", "Mission City", "RIYADH");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70 ', 'local Mission allowance', ' Mission days, 4' ");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70 ', 'local Mission allowance', ' Mission days, 5' ");
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70', 'Local Mission allowance', 'End of Mission Date' ' 2014/06/16 00:00:00 ');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70', 'Local Mission allowance', 'End of Mission Date' ' 2014-06-14 00:00:00 ');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70', 'Local Mission allowance', 'Mission Start Date', ' 2014/06/13 00:00:00 ');
Insert into APPS. PER_ALL_PEOPLE_F
(EMPLOYEE_NUMBER, NOM_ELEMENT, INPUT_VALUE, RESULT_VALUE)
Values
('70', 'Local Mission allowance', 'Mission Start Date', ' 2014/06/10 00:00:00 ');
Thank you very much in advance.
Kind regards
Afzal.
So then... something like this:
SELECT employee_number
element_name
MAX (decode (input_value, 'Day amount', result_value)) AS day_amount
MAX (decode (input_value, 'Days of Distance', result_value)) AS Distance_Days
, MAX (decode (input_value, 'Value of pay', result_value)) AS Pay_Value
MAX (decode (input_value, 'Start Date', result_value)) AS Start_Date
from (SELECT papf.employee_number
pet.element_name
piv.NAME input_value
prrv.result_value
prrv.run_result_id
OF apps.pay_payroll_actions App
pay_assignment_actions PAA
pay_payrolls_f pp
pay_run_results prr
pay_run_result_values prrv
pay_input_values_f piv
pay_element_types_f pet
apps.per_all_assignments_f ADP
apps.per_all_people_f women's wear
-where ppa.payroll_action_id =: payroll_action_id - give your payroll_action_id
WHERE ppa.payroll_id =: payroll_id
AND ppa.payroll_action_id =: payroll_action_id
- and paa.assignment_action_id =: assignment_action_id
AND ppa.payroll_action_id = paa.payroll_action_id
AND ppa.payroll_id = pp.payroll_id
AND paa.assignment_action_id = prr.assignment_action_id
AND prr.run_result_id = prrv.run_result_id
AND prrv.input_value_id = piv.input_value_id
AND piv.element_type_id = pet.element_type_id
AND paaf.assignment_id = paa.assignment_id
AND paaf.person_id = papf.person_id
AND trunc (sysdate) BETWEEN pp.effective_start_date AND pp.effective_end_date
AND trunc (sysdate) BETWEEN pet.effective_start_date AND pet.effective_end_date
AND trunc (sysdate) BETWEEN piv.effective_start_date AND piv.effective_end_date
AND trunc (sysdate) BETWEEN paaf.effective_start_date AND paaf.effective_end_date
AND trunc (sysdate) BETWEEN papf.effective_start_date AND papf.effective_end_date
AND pet.element_name IN ('local Mission allowance', 'International Mission'))
GROUP BY employee_number
element_name
run_result_id
;
You should get your desired result.
Roger
-
Hi all
Required clarification of Oracle sql
Example table:
EmpNo empname mgr_id hire_date deptno salary Job
7788 SCOTT ANALYST 7566 3000 APRIL 19, 87 20
7902 7566 3000 3 DECEMBER ANALYST FORD 81 20
7934 MILLER COMMITTED JANUARY 7782 1300 23 82 10
7900 7698 DECEMBER 3, CLERK JAMES 81 950 30
7369 SMITH COMMITTED 7902 17 DECEMBER 20 800 80
7876 ADAMS 7788 1100 23 MAY CLERK 87 20
Need of ' single / one ' sql for this statement of the obligation:
There will be 2 dropdown boxes (1 - work list, 2nd - empno) in the form in which the next set of results is expected
(1) when the user selects the value of the 1st drop down box (employment) as "ANALYST", leaving the second dropdown not selected, the expected result is 2 (No. lines for this work)
(2) when the user selects the value of the 1st drop-down list (job) as "ANALYST" and the value in the 2nd drop-down box like 7902, the expected result is 1 (no lines for this job and empno)
SQLs I tried on my side (given below) gave the expected result and do please help me fix this
Select count (1) in the scott.emp where job = 'ANALYST' and (empno =: empno or empno is null);
Please help for this requirement. Any help is deeply appreciated.
Thank you
Zakari1007144 wrote:
SQLs I tried on my side (given below) gave the expected result and do please help me fix this
Select count (1) in the scott.emp where job = 'ANALYST' and (empno =: empno or empno is null);
You check "IS NULL" on EMPNO clolumn. You should check on the value passed: EMPNO. It should be
select count(1) from scott.emp where job='ANALYST' and ( empno = :empno or :empno is null ) ;
Published by: JAC on May 21, 2013 19:32
Hope that the: value of EMPLOYMENT is required... -
Please help on this sql analysis
Hi currently I have 2 tables, for example, your comments and feedback_history
your comments:
ID, note, result
1, test, resolved
2, test2, resolved
feedback_history
ID, feedback_id, note, result
5.1 test pending
6.1 test pending
7.2, test2, outstanding
8.2, test2, outstanding
so whenever an update is made, your comments will be updated with new comments and feedback_history will be updated.
This means that feedback will be current feedback and feedback_history will contain historical data.
I need to join the 2 tables so that I'll end up with
ID, feedback_id, note, result
5.1 test pending
6.1 test pending
9.1, test, resolved * feedback tbl *.
7.2, test2, outstanding
8.2, test2, outstanding
10.2, test2, resolved * feedback tbl *.
I have something along the line of
Select efh.id as efh_id,
EFH.outcome_id,
EFH.update_date,
EFH.feedback_id
of e_feedback_history efh
Union
Select (select max (id) as e_feedback_history efh_id),
EF.outcome_id,
EF.update_date,
EF.ID as feedback_id
of e_feedback ef
but it seems not very effective and his evil as id is not increment...
Please help thanks :D
Published by: user8840929 on 11/12/2009 18:18
Published by: user8840929 on 11/12/2009 18:22Here's what I could think of:
SELECT row_number() OVER (ORDER BY f.id NULLS LAST, f.feedback_id) + (min(f.id) OVER (ORDER BY f.id) - 1) id, f.feedback_id, f.remark, f.outcome FROM (SELECT fbh.id, fbh.feedback_id, fbh.remark, fbh.outcome FROM feedback_history fbh UNION ALL SELECT null id, fb.id, fb.remark, fb.outcome FROM feedback fb) f ORDER BY feedback_id, id
and run the example
SQL> SELECT row_number() OVER (ORDER BY f.id NULLS LAST, f.feedback_id) + 2 (min(f.id) OVER (ORDER BY f.id) - 1) id, 3 f.feedback_id, 4 f.remark, 5 f.outcome 6 FROM (SELECT fbh.id, 7 fbh.feedback_id, 8 fbh.remark, 9 fbh.outcome 10 FROM feedback_history fbh 11 UNION ALL 12 SELECT null id, 13 fb.id, 14 fb.remark, 15 fb.outcome 16 FROM feedback fb) f 17 ORDER BY feedback_id, id 18 / ID FEEDBACK_ID REMARK OUTCOME ---------- ----------- ------ ---------- 5 1 test unresolved 6 1 test unresolved 9 1 test resolved 7 2 test2 unresolved 8 2 test2 unresolved 10 2 test2 unresolved 6 rows selected SQL>
-
Need help to build the SQL query
Hi all
I struggle to create the select statement.
This is my expected outcome:
Insert into tmp (aa, bb, cc, dd)
values('CUST1','10100001',2,'10-Jan-2013');
Insert into tmp (aa, bb, cc, dd)
values('CUST1','10100002',4,'11-Jan-2013');
Insert into tmp (aa, bb, cc, dd)
values('CUST2','10200001',2,'19-Jan-2013');
Insert into tmp (aa, bb, cc, dd)
values('CUST2','10200002',6,'28-Jan-2013');
Insert into tmp (aa, bb, cc, dd)
values('CUST2','10200003',1,'28-Mar-2013');
My result is:
AA COUNT (1) MAX (DD) by cc dd AA AA
CUST1 2 10100002 4 11 January 2013
CUST2 3 10200003 1 28 MARCH 2013
Can someone help with the select statement?
Thank you and best regards,
Iwan
Just change it to the column:
SELECT
AA
COUNT (aa)
, MAX (bb) keep (last dense_rank command by JJ)
, MAX (cc) keep (last dense_rank command by JJ)
MAX (dd)
OF THE TMP
GROUP BY aa
ORDER BY aa
Note that by default sort order is ascending with the last NULL values.
If in case your line-date-max will contain null value in the bb or cc columns those will be returned.
Post edited by: Correction of chris227
Post edited by: chris227 added note
-
I have a table and passes as below:
START DATE TOTAL NUMBER OF DAYS
11/10/2011 15:00 1
15/10/2011-05:00 1
2011-12-22 10:00 1
22/12/2011 11:00 2
30/12/2011-10:00 1
01/01/2012 01:00 1
01/01/2012 10:00 1
01/01/2012 16:00 2
02/01/2012 14:00 1
03/01/2012-15:00 1
04/01/2012-15:00 2
04/01/2012 18:00 1
I need to ask where the values in the table above
must return values as below:
I tried many ways, but not able to find the solution.
Kindly help me in this regard.
START DATE TOTAL NUMBER OF DAYS
11/10/2011 15:00 1
15/10/2011-05:00 1
22/12/2011 11:00 2
30/12/2011-10:00 1
01/01/2012 16:00 2
02/01/2012 14:00 1
03/01/2012-15:00 1
04/01/2012-15:00 2
the ultimate goal is to summarize the TOTAL number of DAYS column all the numbers in the second table... All jobs must be treated in the same query. I can't use the coding of java to process what my requirement should be included in the query that already exists.
Published by: 915175 on February 16, 2012 23:02select trunc(start_date), max(start_date), max(total_days) from tablename group by trunc(start_date);
Please check below is on my local site with you Test data:
SQL> SQL> with tablename as 2 ( 3 select to_date('10/11/2011 15:00' , 'mm/dd/yyyy hh24:mi') start_date, 1 total_days from dual union all 4 select to_date('10/15/2011 5:00' , 'mm/dd/yyyy hh24:mi') start_date, 1 total_days from dual union all 5 select to_date('12/22/2011 10:00' , 'mm/dd/yyyy hh24:mi') start_date, 1 total_days from dual union all 6 select to_date('12/22/2011 11:00' , 'mm/dd/yyyy hh24:mi') start_date, 2 total_days from dual union all 7 select to_date('12/30/2011 10:00' , 'mm/dd/yyyy hh24:mi') start_date, 1 total_days from dual union all 8 select to_date('1/1/2012 1:00' , 'mm/dd/yyyy hh24:mi') start_date, 1 total_days from dual union all 9 select to_date('1/1/2012 10:00' , 'mm/dd/yyyy hh24:mi') start_date, 1 total_days from dual union all 10 select to_date('1/1/2012 16:00' , 'mm/dd/yyyy hh24:mi') start_date, 2 total_days from dual union all 11 select to_date('1/2/2012 14:00' , 'mm/dd/yyyy hh24:mi') start_date, 1 total_days from dual union all 12 select to_date('1/3/2012 15:00' , 'mm/dd/yyyy hh24:mi') start_date, 1 total_days from dual union all 13 select to_date('1/4/2012 15:00' , 'mm/dd/yyyy hh24:mi') start_date, 2 total_days from dual union all 14 select to_date('1/4/2012 18:00' , 'mm/dd/yyyy hh24:mi') start_date, 1 total_days from dual 15 ) 16 select TO_CHAR(max(start_date),'MM/DD/YYYY HH24:MI') start_date , max(total_days) total_days from tablename 17 group by trunc(start_date); START_DATE TOTAL_DAYS ---------------- ---------- 10/15/2011 05:00 1 01/03/2012 15:00 1 12/30/2011 10:00 1 01/01/2012 16:00 2 10/11/2011 15:00 1 12/22/2011 11:00 2 01/04/2012 18:00 2 01/02/2012 14:00 1 8 rows selected. SQL>
Kind regards
Lifexisxnotxsoxbeautiful...Update: added test done to my database...
Edited by: lifexisxnotxsoxbeautiful Vithalani, on February 16, 2012 23:18
-
NAME VALUE DATE
A1 1 10 - SEP - 2013
A1 2 10 - SEP - 2013
3 9 - SEP - 2013 A1
4 8 - SEP - 2013 A1
A1 5 8 - SEP - 2013
20 11 - SEP - 2013 A1
A1 7 10 - SEP - 2013
A1 8 7 - SEP - 2013
A1 9 7 - SEP - 2013
A1 10 6 - SEP - 2013
A1 11 5 - SEP - 2013
A1 12 5 - SEP - 2013
13-12 - SEP - 2013 A1
A2 2 10 - SEP - 2013
A2 15 15 - SEP - 2013
A2 3 15 - SEP - 2013
17-20 - SEP - 2013 A2
18-10 - SEP - 2013 A2
A2 9 11 - SEP - 2013
20 20 - SEP - 2013 A2
21 19 - SEP - 2013 A2
10-19 - SEP - 2013 A2
6 5 - SEP - 2013 A2
A2 11 5 - SEP - 2013
O/P
NAME VALUE DATE
13-12 - SEP - 2013 A1
20 11 - SEP - 2013 A1
A1 1 10 - SEP - 2013
A1 2 10 - SEP - 2013
A1 7 10 - SEP - 2013
3 9 - SEP - 2013 A1
4 8 - SEP - 2013 A1
A1 5 8 - SEP - 2013
17-20 - SEP - 2013 A2
20 20 - SEP - 2013 A2
21 19 - SEP - 2013 A2
10-19 - SEP - 2013 A2
A2 15 15 - SEP - 2013
A2 3 15 - SEP - 2013
A2 9 11 - SEP - 2013
A2 2 10 - SEP - 2013
18-10 - SEP - 2013 A2
I need to get reviews of the latest 5 dates by category.
Please suggest how this can be achieved.
I had used the query to get the latest 5 recordings below.
SELECT * FROM
(
SELECT name,
value,
date,
ROW_NUMBER() over (PARTITION BY name ORDER BY date DESC) AS RANK
TABLE
)
WHERE < = 5
Thanks in advance
WITH t AS
('A1' SELECT name, 1 val, 10-SEP-2013' dt FROM DUAL)
UNION ALL
SELECT 'A1', 2, 10-SEP-2013' FROM DUAL
UNION ALL
SELECT 'A1', 3, ' 9-SEP-2013' FROM DUAL
UNION ALL
SELECT 'A1', 4 ' 8-SEP-2013' FROM DUAL
UNION ALL
SELECT 'A1', 5 ' 8-SEP-2013' FROM DUAL
UNION ALL
SELECT 'A1', 20, 11-SEP-2013' FROM DUAL
UNION ALL
SELECT 'A1', 7, 10-SEP-2013' FROM DUAL
UNION ALL
SELECT 'A1', 8, ' 7-SEP-2013' FROM DUAL
UNION ALL
SELECT 'A1', 9 ' 7-SEP-2013' FROM DUAL
UNION ALL
SELECT 'A1', 10 ' 6-SEP-2013' FROM DUAL
UNION ALL
SELECT 'A1', 11 ' 5-SEP-2013' FROM DUAL
UNION ALL
SELECT 'A1', 12, ' 5-SEP-2013' FROM DUAL
UNION ALL
SELECT 'A1', 13, 12-SEP-2013' FROM DUAL
UNION ALL
SELECT "A2", 2, 10-SEP-2013' FROM DUAL
UNION ALL
SELECT "A2", 15, 15-SEP-2013' FROM DUAL
UNION ALL
SELECT "A2", 3, 15-SEP-2013' FROM DUAL
UNION ALL
SELECT "A2", 17, 20-SEP-2013' FROM DUAL
UNION ALL
SELECT "A2", 18, 10-SEP-2013' FROM DUAL
UNION ALL
SELECT "A2", 9, 11-SEP-2013' FROM DUAL
UNION ALL
SELECT "A2", 20, 20-SEP-2013' FROM DUAL
UNION ALL
SELECT "A2", 21, 19-SEP-2013' FROM DUAL
UNION ALL
SELECT "A2", 10, 19-SEP-2013' FROM DUAL
UNION ALL
SELECT "A2", 6 ' 5-SEP-2013' FROM DUAL
UNION ALL
SELECT "A2", 11, '' 5-SEP-2013 DOUBLE).
TT AS (SELECT name, val, TO_DATE (dt, 'DD-MON-YYYY') t dt)
SELECT name, val, dt
FROM (SELECT name,
Val,
DT,
DENSE_RANK () OVER (PARTITION BY name ORDER BY DESC dt) rnk
TT)
WHERE rnk<= 5;="">=>
NAME VAL DT A1 13 12/09/2013 A1 20 11/09/2013 A1 1 10/09/2013 A1 7 10/09/2013 A1 2 10/09/2013 A1 3 09/09/2013 A1 4 08/09/2013 A1 5 08/09/2013 A2 17 20/09/2013 A2 20 20/09/2013 A2 21 19/09/2013 A2 10 19/09/2013 A2 15 15/09/2013 A2 3 15/09/2013 A2 9 11/09/2013 A2 2 10/09/2013 A2 18 10/09/2013 See you soon,.
Manik.
Maybe you are looking for
-
Download Firefox by choosing random location
In Firefox, I have my preferences set to ask me where to save downloaded files. However, every time I download a file, Firefox randomly selects a folder I saved the file before, instead of the last used location. For example, if I need to download PD
-
Update the Bios HP 6710 b CNU7371RXQ
Hello! I'm looking for the bios update for my 6710 b but cannot be found in the drivers section this tool for Windows 7 Ultimate. Also the search in the support site does not find my model specifications when you enter the serial number? Sorry for my
-
why I can't connect samsung galaxy s 3 to my wireless home connection
Nice day
-
My box often takes a long time to receive e-mails, Registrant receiving 1 of 5. So when they finally come thrus, I could have 3 exact same emails from the same sender and other 2 separate emails from different senders. Often, I get a message that s
-
Is the secondary node of data direct mandatory or optional?
Can live given VM for UCCE 11.x settle as a head node (i.e., stand-alone node, no HA) or is a secondary node required for deployment? Thanx, Neil