Select Query with minimum values
Table name: employess_inout
Column name: employee_code number(data type)
IN_Time date(data type)
Out_time date(data type)
i want to select only in_time coloumn data with min intime as in one date A employee have more then 2 times in_time entry
example
employee_code in_time out_time
1 18-mar-12 08:15:21 18-mar-12 13:02:01
1 18-mar-12 14:07:46 18-mar-12 18:01:32
1 19-mar-12 09:15:11 19-mar-12 12:58:54
1 19-mar-12 14:10:01 19-mar-12 16:21:57
1 19-mar-12 16:53:37 19-mar-12 18:15:33
In above example I only want to select in_time column values which is minimum as 18-mar-12(08:15:21) like wise in date 19-mar-12 minimum value is 09:15:11.
Please write the script.
thanks in advance
Dear Frank Kulash
the Script is
Select ei.emp_code,p.ename, d.department_name, ds.designation_name,
to_char(ei.intime, 'dd') "IN_Date",
to_char (ei.intime,'hh24:mi') "IN_Time" /*here i used "min" but not solved*/
FROM einout ei, personnel p,departments d, designations ds
where ei.emp_code = '470'
and intime between to_date ('01/03/2012 08:10', 'dd/mm/YYYY hh24:mi')
and to_date ('21/03/2012 10:00','dd/mm/YYYY hh24:mi')
and ei.emp_code = p.emp_code
AND p.dept_code = d.dept_code
and p.desig_code = ds.desig_code
group by ei.emp_code,p.ename, d.department_name, ei.intime,ds.designation_name --, ei.outtime
order by ei.intime, ei.emp_code asc;
Out Put
EMP_CODE ENAME DEPARTMENT_NAME DESIGNATION_NAME IN_Date IN_Time
470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 01 09:15
*470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 02 08:58*
*470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 02 14:04*
*470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 02 15:11*
470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 03 09:06
470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 05 17:07
470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 06 09:47
470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 07 09:36
*470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 07 19:39* 470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 08 12:16
470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 09 09:26
*470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 09 14:08*
I want to take out put
like that
EMP_CODE ENAME DEPARTMENT_NAME DESIGNATION_NAME IN_Date IN_Time
470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 01 09:15
470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 02 08:58
470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 03 09:06
470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 05 17:07
470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 06 09:47
470 GHULAM YASSEN INFORMATION TECHNOLOGY OFFICER 07 09:36
I only need min time (value) once of A date.
[Bold rows are no need in output]
please tell how it will be possible
Similar Messages
-
Issue in select query with where clause
Hi guys,
I'm facing an issue while using select query with the where clause. When I'm selecting all the data from the table it returns the correct result. But when I'm using the where clause to get the specific rows from the table it returns no rows. But the data I'm trying to fetch using the where condition exists in the table.
Here is my query which causing the issue,
select * from mytable where myfield = 'myvalue'
But if I use the following query it returns the result correctly.
select * from mytable
Also the myfield value 'myvalue' exists in the table.
I have tried by running this query in both SQL Developer and SQL Plus. I have tried this query in mssql as well. It works perfectly and returns correct result sets for both the queries I have mentioned above. I'm unable to predict the issue as I'm new to ORACLE. Please help.
Thanks,
Ram.Hi Ram,
I experienced an issue similar to this with a varchar2 field. Some of our records had a hidden newline character at the end of them, which was making queries like the one below fail:
select * from employees
where email = '[email protected]'The best way I found to detect this was to use
select 'XX'||email||'XX' from employeesTo make sure that there were no newlines. But that is just a guess. If you could provide some example table data and the outputs of your selects, it would be helpful.
Jeff -
How could I replace hard coded value in my sql query with constant value?
Hi all,
Could anyone help me how to replace hardcoded value in my sql query with constant value that might be pre defined .
PROCEDURE class_by_day_get_bin_data
in_report_parameter_id IN NUMBER,
in_site_id IN NUMBER,
in_start_date_time IN TIMESTAMP,
in_end_date_time IN TIMESTAMP,
in_report_level_min IN NUMBER,
in_report_level_max IN NUMBER
IS
bin_period_length NUMBER(6,0);
BEGIN
SELECT MAX(period_length)
INTO bin_period_length
FROM bin_data
JOIN site_to_data_source_lane_v
ON bin_data.data_source_id = site_to_data_source_lane_v.data_source_id
JOIN bin_types
ON bin_types.bin_type = bin_data.bin_type
WHERE site_to_data_source_lane_v.site_id = in_site_id
AND bin_data.start_date_time >= in_start_date_time - numtodsinterval(1, 'DAY')
AND bin_data.start_date_time < in_end_date_time + numtodsinterval(1, 'DAY')
AND bin_data.bin_type = 2
AND bin_data.period_length <= 60;
--Clear the edr_class_by_day_bin_data temporary table and populate it with the data for the requested
--report.
DELETE FROM edr_class_by_day_bin_data;
SELECT site_to_data_source_lane_v.site_id,
site_to_data_source_lane_v.site_lane_id,
site_to_data_source_lane_v.site_direction_id,
site_to_data_source_lane_v.site_direction_name,
bin_data_set.start_date_time,
bin_data_set.end_date_time,
bin_data_value.bin_id,
bin_data_value.bin_value
FROM bin_data
JOIN bin_data_set
ON bin_data.bin_serial = bin_data_set.bin_serial
JOIN bin_data_value
ON bin_data_set.bin_data_set_serial = bin_data_value.bin_data_set_serial
JOIN site_to_data_source_lane_v
ON bin_data.data_source_id = site_to_data_source_lane_v.data_source_id
AND bin_data_set.lane = site_to_data_source_lane_v.data_source_lane_id
JOIN (
SELECT CAST(report_parameter_value AS NUMBER) lane_id
FROM report_parameters
WHERE report_parameters.report_parameter_id = in_report_parameter_id
AND report_parameters.report_parameter_group = 'LANE'
AND report_parameters.report_parameter_name = 'LANE'
) report_lanes
ON site_to_data_source_lane_v.site_lane_id = report_lanes.lane_id
JOIN (
SELECT CAST(report_parameter_value AS NUMBER) class_id
FROM report_parameters
WHERE report_parameters.report_parameter_id = in_report_parameter_id
AND report_parameters.report_parameter_group = 'CLASS'
AND report_parameters.report_parameter_name = 'CLASS'
) report_classes
ON bin_data_value.bin_id = report_classes.class_id
JOIN edr_rpt_tmp_inclusion_table
ON TRUNC(bin_data_set.start_date_time) = TRUNC(edr_rpt_tmp_inclusion_table.date_time)
WHERE site_to_data_source_lane_v.site_id = in_site_id
AND bin_data.start_date_time >= in_start_date_time - numtodsinterval(1, 'DAY')
AND bin_data.start_date_time < in_end_date_time + numtodsinterval(1, 'DAY')
AND bin_data_set.start_date_time >= in_start_date_time
AND bin_data_set.start_date_time < in_end_date_time
AND bin_data.bin_type = 2
AND bin_data.period_length = bin_period_length;
END class_by_day_get_bin_data;In the above code I'm using the hard coded value 2 for bin type
bin_data.bin_type = 2But I dont want any hard coded number or string in the query.
How could I replace it?
I defined conatant value like below inside my package body where the actual procedure comes.But I'm not sure whether I have to declare it inside package body or inside the procedure.
bin_type CONSTANT NUMBER := 2;But it does't look for this value. So I'm not able to get desired value for the report .
Thanks.
Edited by: user10641405 on May 29, 2009 1:38 PMDeclare the constant inside the procedure.
PROCEDURE class_by_day_get_bin_data(in_report_parameter_id IN NUMBER,
in_site_id IN NUMBER,
in_start_date_time IN TIMESTAMP,
in_end_date_time IN TIMESTAMP,
in_report_level_min IN NUMBER,
in_report_level_max IN NUMBER) IS
bin_period_length NUMBER(6, 0);
v_bin_type CONSTANT NUMBER := 2;
BEGIN
SELECT MAX(period_length)
INTO bin_period_length
FROM bin_data
JOIN site_to_data_source_lane_v ON bin_data.data_source_id =
site_to_data_source_lane_v.data_source_id
JOIN bin_types ON bin_types.bin_type = bin_data.bin_type
WHERE site_to_data_source_lane_v.site_id = in_site_id
AND bin_data.start_date_time >=
in_start_date_time - numtodsinterval(1, 'DAY')
AND bin_data.start_date_time <
in_end_date_time + numtodsinterval(1, 'DAY')
AND bin_data.bin_type = v_bin_type
AND bin_data.period_length <= 60;
--Clear the edr_class_by_day_bin_data temporary table and populate it with the data for the requested
--report.
DELETE FROM edr_class_by_day_bin_data;
INSERT INTO edr_class_by_day_bin_data
(site_id,
site_lane_id,
site_direction_id,
site_direction_name,
bin_start_date_time,
bin_end_date_time,
bin_id,
bin_value)
SELECT site_to_data_source_lane_v.site_id,
site_to_data_source_lane_v.site_lane_id,
site_to_data_source_lane_v.site_direction_id,
site_to_data_source_lane_v.site_direction_name,
bin_data_set.start_date_time,
bin_data_set.end_date_time,
bin_data_value.bin_id,
bin_data_value.bin_value
FROM bin_data
JOIN bin_data_set ON bin_data.bin_serial = bin_data_set.bin_serial
JOIN bin_data_value ON bin_data_set.bin_data_set_serial =
bin_data_value.bin_data_set_serial
JOIN site_to_data_source_lane_v ON bin_data.data_source_id =
site_to_data_source_lane_v.data_source_id
AND bin_data_set.lane =
site_to_data_source_lane_v.data_source_lane_id
JOIN (SELECT CAST(report_parameter_value AS NUMBER) lane_id
FROM report_parameters
WHERE report_parameters.report_parameter_id =
in_report_parameter_id
AND report_parameters.report_parameter_group = 'LANE'
AND report_parameters.report_parameter_name = 'LANE') report_lanes ON site_to_data_source_lane_v.site_lane_id =
report_lanes.lane_id
JOIN (SELECT CAST(report_parameter_value AS NUMBER) class_id
FROM report_parameters
WHERE report_parameters.report_parameter_id =
in_report_parameter_id
AND report_parameters.report_parameter_group = 'CLASS'
AND report_parameters.report_parameter_name = 'CLASS') report_classes ON bin_data_value.bin_id =
report_classes.class_id
JOIN edr_rpt_tmp_inclusion_table ON TRUNC(bin_data_set.start_date_time) =
TRUNC(edr_rpt_tmp_inclusion_table.date_time)
WHERE site_to_data_source_lane_v.site_id = in_site_id
AND bin_data.start_date_time >=
in_start_date_time - numtodsinterval(1, 'DAY')
AND bin_data.start_date_time <
in_end_date_time + numtodsinterval(1, 'DAY')
AND bin_data_set.start_date_time >= in_start_date_time
AND bin_data_set.start_date_time < in_end_date_time
AND bin_data.bin_type = v_bin_type
AND bin_data.period_length = bin_period_length;
END class_by_day_get_bin_data; -
How to force readObejct query with PK value to go to DB?
The default behaviour for read-object queries with a PK value is to use the cache and not go to the database. How can we change this behaviour?
We have cases that a row was deleted by some other process not through toplink, but the corresponding object still in the cache and if you use the readObject query with PK value, the object will be returned as result.
Thanks in advance,There are several mechanisms to disable caching in TopLink. Note that disabling caching will affect your performance.
In 9.0.4 you can use:
- You can configure you cache type to use a WeakIdentityMap to ensure that only referenced objects are cached.
- On descriptor you can call disableCacheHits() and alwaysRefreshCache() in code or click these options on the Caching/Identity tab in the Mapping Workbench.
- Or to explicitly remove a deleted object from the cache uses session.removeFromIdentityMap(), but you must ensure there are no other objects referencing it.
In 10.1.3 you can also use:
- On descriptor you can call setIsIsolated(true) in code, or select isolated in the Caching tab in the Mapping Workbench.
- Alternatively you can set a CacheInvalidationPolicy on your descriptor to ensure objects are not cached for longer than a specified time, or invalidated daily.
- Or to explicitly invalidate a deleted object use, session.getIdentityMapAccessor().invalidateObject() -
How to re-write this big SELECT Query with INNER JOINs?
Hi Experts
I have a performance killer SELECT query with an inner join of 3 tables u2013 VBAP, VBAK and VBEP together, which populates records to an internal table INT_COLL_ORD. Based on these records selected, in another SELECT query, records are fetched from VBUK table to the internal table INT_VBUK.
SELECT A~VBELN A~POSNR A~MATNR A~KWMENG A~KBMENG A~ERDAT A~ERZET A~PSTYV D~AUART E~ETTYP E~EDATU
INTO TABLE INT_TAB_RES
FROM VBAP AS A INNER JOIN VBAK AS D
ON D~VBELN EQ A~VBELN AND D~MANDT EQ A~MANDT
INNER JOIN VBEP AS E
ON E~VBELN EQ A~VBELN AND E~POSNR EQ A~POSNR AND E~MANDT EQ A~MANDT
WHERE A~VBELN IN s_VBELN AND
D~auart in s_auart AND
D~vkorg in s_vkorg AND
D~vbtyp eq 'C' AND
( ( matnr LIKE c_prefix_sp AND zz_msposnr NE 0 AND kbmeng EQ 0 )
OR ( matnr LIKE c_prefix_fp AND kwmeng NE A~kbmeng ) ) AND
A~ABGRU EQ SPACE AND
A~MTVFP IN R_MTVFP AND
A~PRCTR IN R_PRCT AND
E~ETENR EQ '1'.
SORT INT_COLL_ORD BY VBELN POSNR ETTYP.
DELETE ADJACENT DUPLICATES FROM INT_TAB_RES COMPARING VBELN POSNR.
CHECK NOT INT_TAB_RES [] IS INITIAL.
SELECT VBELN UVALL CMGST INTO TABLE INT_VBUK
FROM VBUK FOR ALL ENTRIES IN INT_TAB_RES
WHERE VBELN = INT_TAB_RES-VBELN AND UVALL NE 'A'.
Now, the requirement is:
I want to split this query. Like, first join VBAK and VBUK first. With this selection, go to the inner join of VBAP and VBEP (on key VBELN) to get the results. How can I re-write this Query?
Please help.
Thx n RgdsHi Nagraj
As of your suggestion, I have re-written the query as below:
* Declarations
TYPES: BEGIN OF TYP_COLL_ORD,
VBELN LIKE VBAK-VBELN,
POSNR LIKE VBUP-POSNR,
MATNR LIKE VBAP-MATNR,
KWMENG LIKE VBAP-KWMENG,
KBMENG LIKE VBAP-KBMENG,
ERDAT LIKE VBAK-ERDAT,
ERZET LIKE VBAK-ERZET,
PSTYV LIKE VBAP-PSTYV,
AUART LIKE VBAK-AUART, u201Calready exists in type
ETTYP LIKE VBEP-ETTYP,
EDATU LIKE VBEP-EDATU.
TYPES: END OF TYP_COLL_ORD.
DATA: INT_COLL_ORD TYPE TABLE OF TYP_COLL_ORD WITH HEADER LINE.
TYPES: BEGIN OF TYP_VBUK,
AUART LIKE VBAK-AUART, u201Chave added this field
VBELN LIKE VBUK-VBELN,
UVALL LIKE VBUK-UVALL,
CMGST LIKE VBUK-CMGST.
TYPES: END OF TYP_VBUK.
DATA: INT_VBUK TYPE TABLE OF TYP_VBUK WITH HEADER LINE.
*QUERY#1 u2013 for VBAK & VBUK Join
SELECT A~AUART B~VBELN B~UVALL B~CMGST
INTO TABLE INT_VBUK
FROM VBAK AS A INNER JOIN VBUK AS B
ON A~VBELN EQ B~VBELN
WHERE A~VBELN IN s_VBELN AND
A~auart in s_auart AND
A~vkorg in s_vkorg AND
A~vbtyp eq 'C' AND
B~UVALL NE 'A'.
IF NOT INT_VBUK[] IS INITIAL.
SORT INT_VBUK BY VBELN.
DELETE ADJACENT DUPLICATES FROM INT_VBUK COMPARING VBELN.
*QUERY#2 u2013 for VBAP & VBEP Join
SELECT A~VBELN A~POSNR A~MATNR A~KWMENG A~KBMENG A~ERDAT A~ERZET A~PSTYV B~ETTYP B~EDATU
INTO TABLE INT_COLL_ORD
FROM VBAP AS A INNER JOIN VBEP AS B
ON B~VBELN EQ A~VBELN AND B~POSNR EQ A~POSNR AND B~MANDT EQ A~MANDT
FOR ALL ENTRIES IN INT_VBUK
WHERE A~VBELN = INT_VBUK-VBELN AND
( ( matnr LIKE c_prefix_sp AND zz_msposnr NE 0 AND kbmeng EQ 0 )
OR ( matnr LIKE c_prefix_fp AND kwmeng NE A~kbmeng ) ) AND
A~ABGRU EQ SPACE AND
A~MTVFP IN R_MTVFP AND
A~PRCTR IN R_PRCT AND
B~ETENR EQ '1'.
ENDIF.
SORT INT_COLL_ORD BY VBELN POSNR ETTYP.
DELETE ADJACENT DUPLICATES FROM INT_COLL_ORD
COMPARING VBELN POSNR.
CHECK NOT INT_COLL_ORD[] IS INITIAL.
LOOP AT INT_COLL_ORD.
CLEAR: L_MTART,L_ATPPR,L_ETTYP.
IF L_PREVIOUS_ETTYP NE INT_COLL_ORD-ETTYP OR
L_PREVIOUS_AUART NE INT_COLL_ORD-AUART.
READ TABLE INT_OVRCTL WITH KEY AUART = INT_COLL_ORD-AUART ETTYP = INT_COLL_ORD-ETTYP.
CHECK SY-SUBRC NE 0.
Now, the issue is:
Please note that declaration for INT_COLL_ORD has a field AUART, which is used in further parts of program (see the statement just above)
But, since neither VBAP nor VBEP contains AUART field, it cannot be fetched through the QUERY#2. So this value is not populated into INT_COLL_ORD through SELECT Query.
Since this field is used in later part of program & that the internal table has no value for this field, it dumps!!
How to include this value into the INT_COLL_ORD?
Plz suggest.... -
How to write XSJS Select Query with input parameters
Hello Experts,
I am creating a xsjs file and in that file I am trying to write a Select Query based on a Calculation View
I have tried it the following way:
var query = 'SELECT TOP 100 \"Name\", \"Address\", \"City\", \"Country\" FROM \"_SYS_BIC\".\"Test.HL/AddressView\"'
+ 'WITH PARAMETERS(\'PLACEHOLDER\' = (\'$$P_Name$$\', \' Akhil \'),'
+ '\'PLACEHOLDER\' = (\'$$P_City$$\', \' Lucknow \'))';
But it gives me the "Mixed spaces and tabs error".
How should I write XSJS Select Query with input parameters?
Regards,
Rohit>But it gives me the "Mixed spaces and tabs error".
Mixed spaces and tabs has nothing to do with the syntax of the statement. You used both spaces and the tab in the content - which JSLint doesn't like. Remove the beginning spaces of each line and use only one or the other.
The actual syntax of your statement doesn't look right. The problem is that you are escaping the \ when you don't need to if you are using ' instead of " for your string. You escape with \" in the first line but then escape with \' in the 2nd and 3rd line. That is going to cause serious parsing problems with the command. -
Absolute dynamic select query with dynamic join and where
Has anyone ever tried creating an absolutely dynamic SELECT query with dynamic Join and Where conditions.
I have a requirement of creating such a query in an Utility Class, and i have written the code. But its throwing my sysntax errors.
Please let me know where am I going wrong OR is it really possible to create such a dynamic Query??
SELECT (FIELDS) INTO TABLE IT_TABLES
FROM ( (ME->TABLE1) inner join ( me->table2 )
on ( on_condition ) )
WHERE (me->where_fields).
Ags.It worked for me in a following way:
select * into corresponding fields of table <result_table>
from (join_string)
where (l_where).
Where the contents of join_string were dynamically build using concatenation. So it will be something like
concatenate ME->TABLE1 'as a INNER JOIN' me->table2 'as b ON (' into join_string separated by space.
<...>
add here matching/reference colums, something like
concatenate 'a~' me->TABLE1_JOIN_COL into temp1.
concatenate 'b~' me->TABLE2_JOIN_COL into temp2.
concatenate join_string temp1 '=' temp2 into join_string separated by space.
<...>
concatenate join_string ')' into join_string separated by space.
And then use similar approach for l_where variable. -
Dynamic select query with dynamic where condition
Hi all,
I want to use the dynamic select query with dynamic where condition. For that I used the below code but I am getting dump when using this code.
Please advice, if there is any other way to achieve this requirement.
Thanks,
Sanket Sethi
Code***************
PARAMETERS: p_tabnam TYPE tabname,
p_selfl1 TYPE edpline,
p_value TYPE edpline,
p_where1 TYPE edpline .
DATA: lt_where TYPE TABLE OF edpline,
lt_sel_list TYPE TABLE OF edpline,
l_wa_name TYPE string,
ls_where TYPE edpline,
l_having TYPE string,
dref TYPE REF TO data,
itab_type TYPE REF TO cl_abap_tabledescr,
struct_type TYPE REF TO cl_abap_structdescr,
elem_type TYPE REF TO cl_abap_elemdescr,
comp_tab TYPE cl_abap_structdescr=>component_table,
comp_fld TYPE cl_abap_structdescr=>component.
TYPES: f_count TYPE i.
FIELD-SYMBOLS : <lt_outtab> TYPE ANY TABLE,
* <ls_outtab> TYPE ANY,
<l_fld> TYPE ANY.
struct_type ?= cl_abap_typedescr=>describe_by_name( p_tabnam ).
elem_type ?= cl_abap_elemdescr=>describe_by_name( 'F_COUNT' ).
comp_tab = struct_type->get_components( ).
comp_fld-name = 'F_COUNT'.
comp_fld-type = elem_type.
APPEND comp_fld TO comp_tab.
struct_type = cl_abap_structdescr=>create( comp_tab ).
itab_type = cl_abap_tabledescr=>create( struct_type ).
l_wa_name = 'l_WA'.
CREATE DATA dref TYPE HANDLE itab_type.
ASSIGN dref->* TO <lt_outtab>.
*CREATE DATA dref TYPE HANDLE struct_type.
*ASSIGN dref->* TO <ls_outtab>.
* Creation of the selection fields
APPEND p_selfl1 TO lt_sel_list.
APPEND 'COUNT(*) AS F_COUNT' TO lt_sel_list.
** Creation of the "where" clause
*CONCATENATE p_selfl1 '= '' p_value ''.'
* INTO ls_where
* SEPARATED BY space.
*APPEND ls_where TO lt_where.
* Creation of the "where" clause
APPEND p_where1 TO lt_where.
* Creation of the "having" clause
l_having = 'count(*) >= 1'.
* THE dynamic select
SELECT (lt_sel_list)
FROM (p_tabnam)
INTO CORRESPONDING FIELDS OF TABLE <lt_outtab>.
* WHERE (lt_where).Hi Sanket,
The above given logic of mine works for you, put the code in the If condition and try-
just like below:
IF NOT P_EBELN IS INITIAL.
lt_where = '& = ''&'' '.
REPLACE '&' WITH p_ebeln INTO lt_where.
REPLACE '&' WITH field_value INTO lt_where.
SELECT (lt_sel_list) INTO CORRESPONDING FIELDS OF TABLE <lt_outtab>
FROM (p_tabnam)
WHERE (lt_where).
ENDIF.
thanks\
Mahesh -
BDC select query with addition based on all If conditions
Hi can any one send me the select query with conditions like
If Itab is not initial.
Endif. and if possible with valiadations messages also.
IF CHECK_NUMBER of CHECK_ADVICE of the flat file = PAYR-CHECT. Then update SAP field BSEG-XREF2 .
9. When Flat file check Number = PAYR-CHECT then Insert or Update Flat file RECEIPT_DATE into SAP field BSEG-XREF1.
Please send me immediately.> SELECT rsnum
> rspos
> matnr
> werks
> lgort
> shkzg
> aufnr
> bdmng
> enmng
> FROM resb INTO TABLE it_rsnum FOR ALL ENTRIES IN it_aufnr
> WHERE rsnum EQ it_aufnr-rsnum
> AND xloek NE 'X'
> AND postp NE 'X'
> AND resbbdmng GE resbenmng.
> ENDIF.
>
> Database Table RESB: 40,000,000 Records (40 Million)
> Internal Table it_aufnr: 20,000 Entries
> Entries selected from RESB: 150,000.
>
Hi,
the problem is the big for all entries table.
Your 20.000 records FAE will be split into SEVERAL sql statements depending on the size of the table.
Where do you get the it_auftrn from?
If it's another transparent table try to JOIN this table to your big table.
SELECT rsnum
rspos
matnr
werks
lgort
shkzg
aufnr
bdmng
enmng
FROM resb JOIN tab_auftrn on resbrsnum = tab_auftrnauftrn
AND xloek NE 'X'
AND postp NE 'X'
AND resbbdmng GE resbenmng.
Make sure that your WHERE filter and the JOIN keys on both tables are supported by indexes.
150.000 records about of 40 Mio. can definitly be serviced by an appropriate index:
i.e. Index fields (check your PK or secondary indexes):
rsnum
enmng
aufnr
bye
yk -
I need to add a single field from with_item table . need to write select query with reference to company code , account doc no , fiscal year
Hi Arun ,
Can you explain little bit more ??
what is account doc no?
what are the transactions should be displayed in your output??
-Rajesh N -
So, I work on a legacy CF web site and there are numerous SELECT * FROM USERS_TABLE queries all over the site.
Well, we changed the structure of said table in the database on our Testing and Staging sites, with no issues.
When we pushed up to our production environment and changed the structure of the production DB table, the server kept kicking back "Value can not be converted to requested type."
After doing some searching out there, it looks like CF caches the structure of the table, and you either have to restart CF to clear it, or rename and then name-back the DSN to fix the issue.
http://www.bennadel.com/blog/194-ColdFusion-Query-Error-Value-Can-Not-Be-Converted-To-Requ ested-Type.htm
That said, this doesn't happen in our testing and staging environments - so what would be the difference?
Is there some setting I need to change in the CF Admin to keep this from happening again?Also, if you can use a Stored Procedure to retrieve the data, do so. Standard queries gets all the information, anyway, chokes bandwidth passing it all to the CF server, and forces the CF server to filter, sort, and format the data. SPs tell the db server to get ONLY the data requested, and forces the db server to filter and sort the data, leaving only formatting to the CF server.
That's not true. The only time CF messes with data returned from the DB is if there's a maxrows attribute, and the record set returnded from the DB has more than that number of records... which causes CF to truncate the recordset to the correct size before returning it. The DB might or might not stop sending rows down to CF after CF says "yeah, I've got all I want now".
Other than that, for all intents and purposes all CF does with the SQL is pass it to the DB and wait for an answer. The only thing it does to the returned data is to create a CF record set ("query") with it... this does not involve any filtering and sorting.
Adam -
Select query with secondary index
hi,
i have a report which is giving performance issues on a perticular select query on KONH table.
the select query doesnt use the primary key fields and table already has around 19 million entries.So there was a secondary index created for the fields in the table.
now, KONH is a client specific table, and hence has MANDT as the first field. when the table is not indexed it is sorted according to the order of fields, like first MANDT, then primary key fields and then remaining fields.. (correct me if i am wrong)
but the secondary index created doesnt has MANDT in it..(yea, a mistake! )...
but instead of correccting the secondary index, i am told to change the select query..
so, i used a "client specific" syntax to sort the issue.. but i dont understand whre i should put the "where mandt eq sy-mandt" clause..
should i put it right after all my secondary index fields are over? or what happens to the order of fields which are not present in the list of secondary index?
kindaly help.
thanx.Hi chinmay kulkarni,
its better if you can ask concerned person to add MANDT field in your index as well....
Indexes and MANDT
If a table begins with the mandt field, so should its indexes. If a table begins with mandt and an index doesn't, the optimizer might not use the index.
Remember, if you will, Open SQL's automatic client handling feature. When select * from ztxlfa1 where land1 = 'US' is executed, the actual SQL sent to the database is select * from ztxlfa1 where mandt = sy-mandt and land1 = 'US'. Sy-mandt contains the current logon client. When you select rows from a table using Open SQL, the system automatically adds sy-mandt to the where clause, which causes only those rows pertaining to the current logon client to be found.
When you create an index on a table containing mandt, therefore, you should also include mandt in the index. It should come first in the index, because it will always appear first in the generated SQL.
Index: Technical key of a database table.
Primary index: The primary index contains the key fields of the table and a pointer to the non-key fields of the table. The primary index is created automatically when the table is created in the database.
Secondary index: Additional indexes could be created considering the most frequently accessed dimensions of the table.
Structure of an Index
An index can be used to speed up the selection of data records from a table.
An index can be considered to be a copy of a database table reduced to certain fields. The data is stored in sorted form in this copy. This sorting permits fast access to the records of the table (for example using a binary search). Not all of the fields of the table are contained in the index. The index also contains a pointer from the index entry to the corresponding table entry to permit all the field contents to be read.
When creating indexes, please note that:
An index can only be used up to the last specified field in the selection! The fields which are specified in the WHERE clause for a large number of selections should be in the first position.
Only those fields whose values significantly restrict the amount of data are meaningful in an index.
When you change a data record of a table, you must adjust the index sorting. Tables whose contents are frequently changed therefore should not have too many indexes.
Make sure that the indexes on a table are as disjunctive as possible.
(That is they should contain as few fields in common as possible. If two indexes on a table have a large number of common fields, this could make it more difficult for the optimizer to choose the most selective index.)
For Example...
SELECT KUNNR KUNN2 INTO TABLE T_CUST_TERR
FROM KNVP CLIENT SPECIFIED
WHERE MANDT = SY-MANDT " here MANDT shd be first
AND KUNN2 IN S_TERR
AND PARVW LIKE 'Z%'.
Accessing tables using Indexes
The database optimizer decides which index on the table should be used by the database to access data records.
You must distinguish between the primary index and secondary indexes of a table. The primary index contains the key fields of the table. The primary index is automatically created in the database when the table is activated. If a large table is frequently accessed such that it is not possible to apply primary index sorting, you should create secondary indexes for the table.
The indexes on a table have a three-character index ID. '0' is reserved for the primary index. Customers can create their own indexes on SAP tables; their IDs must begin with Y or Z.
If the index fields have key function, i.e. they already uniquely identify each record of the table, an index can be called a unique index. This ensures that there are no duplicate index fields in the database.
When you define a secondary index in the ABAP Dictionary, you can specify whether it should be created on the database when it is activated. Some indexes only result in a gain in performance for certain database systems. You can therefore specify a list of database systems when you define an index. The index is then only created on the specified database systems when activated
Also pls have a look on below link
http://www.sapfans.com/sapfans/forum/devel/messages/30240.html
Hope it will solve your problem..
Reward points if useful...
Thanks & Regards
ilesh 24x7 -
Hi,
I have one table test_tab with column names empno,empname.The test_tab values is as follows,
empno empname
01 siva
02 ram
03 kamal
04 sathish
i will give the empname values like 'siva','roshan',but in the table roshan name is not there in my table.I need the below mentioned o/p
example qry: select empno,empname from test_tab where empname in('siva','roshan');
my o/p should be like this,
empname empno
siva 01
roshan ''
But the empname 'roshan' is not there in table.I need the empname as 'roshan' and empno is null value in a single select query or subquery or any sql statements.
Please help me the resolve this issue.
Thanks
Sivaramankn_sivaraman wrote:
Hi,
Yes,we will see performance issue later.Now we want put where clause in this query like empno >'03'.Suppose we will pass values like 'kamal,roshan,sathish'
example qry: select empno,empname from test_tab where empname in('kamal','roshan','sathish') and empno>03;
my o/p should be like this,
empname empno
sathish 04
roshan ''
For the current query we will get the all three values.Please help this issue.
Thanks
SivaThat logic doesn't make sense.
You're original requirement was to get all those names you passed in and just show a blank empno if they weren't in the table.
Now you're adding a condition that restricts what is seen in the table, so of course you will still get all 3 values you pass, just that "kamal" is not seen as being in the table so becomes part of the original requirement to show those passed in without their empno.
I can only guess you're wanting to just restrict the overall output, in which case you need to put the restriction outside the join but still cater for null empno's...
SQL> ed
Wrote file afiedt.buf
1 with t as (-- comma seperated input values
2 select 'kamal,roshan,sathish' as names from dual
3 )
4 ,s as (-- split the names into rows
5 select regexp_substr(names, '[^,]+', 1, level) as nm
6 from t
7 connect by regexp_substr(names, '[^,]+', 1, level) is not null
8 )
9 -- now outer join the names to the data
10 select s.nm as empname
11 ,test_tab.empno
12 from s left outer join test_tab on (s.nm = test_tab.empname)
13* where to_number(test_tab.empno) > 3 or test_tab.empno is null
SQL> /
EMPNAME EM
sathish 04
roshan -
Hi All,
I have written a query as belows: but I dont get the output as desired.
This is a with query - I need the query output in a single record so I have pivoted the first query spl. The second set of query stk gives me the stock.
My requirement is whnever there is a not matching mmg_id of 1st query with mmg5_id of 2nd query it should give the second record as follows
LOC_LOC_ID MMG_ID TP_TP_ID 'S10' 'O3' 'O7' 'O8' CN_STK_QTY CN_STK_VAL
1097 1610 20132123 207.5 62.08 7504 25.06 603 93027.5
1097 0024 20132123 -1 -15.99
{code}
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.2.0
Connected as sizsupport
SQL>
SQL> WITH spl AS
2 (
3 SELECT * FROM
4 (SELECT '1097' loc_loc_id, '1610' mmg_id, '20132123' tp_tp_id, 'S10' measure_name,'207.5' val FROM dual UNION ALL
5 SELECT '1097' , '1610' , '20132123' , 'O3' measure_name, '62.08' FROM dual UNION ALL
6 SELECT '1097' , '1610' , '20132123' , 'O7' measure_name, '7504' FROM dual UNION ALL
7 SELECT '1097' , '1610' , '20132123' , 'O8' measure_name, '25.06' FROM dual )
8 pivot ( sum(val) FOR measure_name IN
9 ('S10','O3','O7','O8'))
10 ),
11 stk AS
12 (
13 SELECT '1097' loc_loc_id, '0024' mmg5_id, '20132123' tp_tp_id, '-1' cn_stk_qty, '-15.99' cn_stk_val FROM dual
14 UNION ALL
15 SELECT '1097' loc_loc_id, '1610' mmg5_id, '20132123' tp_tp_id, '603' cn_stk_qty, '93027.5' cn_stk_val FROM dual
16 )
17 SELECT spl.*,stk.cn_stk_qty,stk.cn_stk_val
18 FROM spl RIGHT OUTER JOIN stk
19 ON spl.loc_loc_id = stk.loc_loc_id
20 AND spl.tp_tp_id = stk.tp_tp_id
21 AND spl.mmg_id = stk.mmg5_id
22 ;
LOC_LOC_ID MMG_ID TP_TP_ID 'S10' 'O3' 'O7' 'O8' CN_STK_QTY CN_STK_VAL
1097 1610 20132123 207.5 62.08 7504 25.06 603 93027.5
-1 -15.99
SQL>
{code}Hi,
When there is no match between stk and spl, then all columns from spl will be NULL, but the values from stk will be available. Take the columns you need to display from stk, not from spl.
WITH spl AS
SELECT *
FROM raw_data
PIVOT ( SUM (val)
FOR measure_name IN ( 'S10' AS s10
, 'O3' AS p_03
, 'O7' AS p_07
, 'O8' AS p_08
SELECT stk.loc_loc_id
, stk.mmg5_id
, stk.tp_tp_id
, spl.s10
, spl.p_03
, spl.p_07
, spl.p_08
, stk.cn_stk_qty
, stk.cn_stk_val
FROM stk
LEFT OUTER JOIN spl ON spl.loc_loc_id = stk.loc_loc_id
AND spl.tp_tp_id = stk.tp_tp_id
AND spl.mmg_id = stk.mmg5_id
I gave the pivoted columns standard names, like s10 and p_03. You don't have to do that; you can use "'S10'" and "'P_03'" if you prefer. -
How to select rows with min value on a specific column
I have a query:
select
m.x1,
round (to_date (l.y1, 'mm/dd/yyyy HH:MI:SS AM') - m.x2, 0) as numofdays
from
table1 m,
table2 l
where
l.x3 = m.x3 and
to_date (l.y1, 'mm/dd/yyyy HH:MI:SS AM') >= TO_DATE('01012013','MMDDYYYY') and
and I got this result table:
x1 (ID)
numofdays
001
5
001
10
002
2
003
3
003
1
004
0
005
66
several ID's have multiple values on the second column, I want to have only distinct IDs with smallest "numofdays" like this:
x1 (ID)
numofdays
001
5
002
2
003
1
004
0
005
66
Any ideas?Hi,
The most general and versatile way is a Top-N Query:
WITH got_r_num AS
select
m.x1,
round (to_date (l.y1, 'mm/dd/yyyy HH:MI:SS AM') - m.x2, 0) as numofdays
, ROW_NUMBER () OVER ( PARTITION BY m.x1
ORDER BY to_date (l.y1, 'mm/dd/yyyy HH:MI:SS AM') - m.x2
) AS r_num
from
table1 m,
table2 l
where
l.x3 = m.x3 and
to_date (l.y1, 'mm/dd/yyyy HH:MI:SS AM') >= TO_DATE('01012013','MMDDYYYY') and
SELECT x1, numofdays
FROM got_r_num
WHERE r_num = 1
If you'd care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.
Notice that the sub-query (got_r_num) is exactly what you posted, only with a new column (r_num) added to the SELECT clause.
Maybe you are looking for
-
I have restored my ipod and now the songs in my library wont sync onto it:)).. !! please help!!
-
My iPhone 4 says I have an update but I have to be connected to wifi why ?
-
I will briefly describe what i am doing, but my problem is not with any hardware it is with programming iterative loops in labVIEW. What i am trying to do is get the input for an activex aircraft instrument. The attitude data is coming from an inerti
-
HOW TO INSTALL SAP Business One to Windows Server 2008
Hi everybody, anyone can help me how to install SAPB1 in windows server 2008? last night i try to install SAPB1 to my sever windows 2008 but after installation i cannot find for the shortcut SAP business one in my desktop but usaully in default insta
-
I have an older itouch with 2.2.1 software where can I find apps?
I have an older itouch with 2.2.1 software, where can I find apps for it?