Select single query working unexpectedly
Hi all,
I have used below select single * query as shown :
I have data in my table S022 as shown below, it has 2 records for material and aufnr combination :
WERKS ARBPL kapar MATNR AUFNR
w1 ar1 004 mat1 000300156789
w1 ar2 mat1 000300156789
The code used :
select single *
from s022
where werks = itab-ltap-werks "w1
and matnr = w_matnr "mat1
and aufnr = w_aufnr. " 000300156789
runtime select query is picking second record i.e of ar2.
I hope it should have picked ar1 record but its doesnt .
Also sometimes it picks 1st record for different data and for this data it picks the second record.
Kindly help me to explain why its not picking the first record.
Regards.
ujjwal_d15 wrote:
It is as per the data in the table . The records are one below the another in database table.
> So i feel it should have picked the first record.
Hello Ujjwal,
The records shown in the databrowser is a snapshot of the entries in the DB & not how the recs are actually stored!
In RDBMS the sequence of entries is not defined at the DB layer. In SELECT SINGLE the 1st rec to be hit is returned to the result set. In your case this is the 2nd rec.
BR,
Suhas
Similar Messages
-
Problem in SELECT SINGLE Query
Hi,
I am writing a small quesry in PS, Though the entry exist in database table PRPS, The sy-subrc is 4 for this query.Can u help me.
data : wa_src_prps like prps-posid.
start-of-selection.
wa_src_prps = '3-3310-02-01-01-10-20'.
DATA: lv_pbukr LIKE prps-pbukr.
CLEAR: lv_pbukr.
SELECT SINGLE pbukr INTO lv_pbukr
FROM prps
WHERE posid EQ wa_src_prps.
if sy-subrc = 0.
write:/5 wa_src_prps, lv_pbukr.
endif.hi,
DATA : wa_src_prps LIKE prps-posid.
START-OF-SELECTION.
wa_src_prps = '0-1000-0'.
" Conversion Routine for domain PS_POSID
CALL FUNCTION 'CONVERSION_EXIT_ABPSN_INPUT'
EXPORTING
input = wa_src_prps
IMPORTING
output = wa_src_prps.
DATA: lv_pbukr LIKE prps-pbukr.
CLEAR: lv_pbukr.
SELECT SINGLE pbukr INTO lv_pbukr
FROM prps
WHERE posid EQ wa_src_prps.
IF sy-subrc = 0.
WRITE:/5 wa_src_prps, lv_pbukr.
ENDIF. -
MIN in select SINGLE query.
I need to find a single record with lowest DDUP.
If there are 2 records exist, then i need a single record with lowest DDUP.
loop at it_mara.
SELECT SINGLE EKORG MATNR MFRPN ZEINR KUNNRS MIN( DDUP ) DCURR
FROM ZNVT_PRICE
INTO CORRESPONDING FIELDS OF LW_ZNVT_PRICE
WHERE EKORG EQ P_EKORG AND
MATNR EQ IT_MARA-MATNR AND
MFRPN EQ IT_MARA-MFRPN AND
ZEINR EQ IT_MARA-ZEINR AND
KUNNRS EQ IT_MARA-KUNNR AND
DATAB LE SY-DATLO AND
DATBI GE SY-DATLO
GROUP BY EKORG MATNR MFRPN ZEINR KUNNRS DCURR.
endloop.
there is only one record in the table ZNVT_PRICE. (1234.00)
Some times i am getting 1234.
Some times 0.00.
I need to find a single record with lowest DDUP.
If there are 2 records exist, then i need a single record with lowest DDUP.
if I do not use GROUP BY i am getting error.
I have to use SELECT SINGLE.
Guys...please help me to write reliable query.Hi,
Instead of SELECT SINGLE use
SELECT.. UP TO 1 ROWS
ENDSELECT.
It will also be very fast and suit your requirement.
Regards,
Atish -
All selected 'n' consecutive rows in retrieved in a single query
hello,
I have table T with 50,000 rows
create table T
(student_id number,
class_id number,
quiz_id number,
marks number)
some sample rows like
INSERT INTO T VALUES (1,1, 1, 50);
INSERT INTO T VALUES (2,2, 2, 40);
INSERT INTO T VALUES (3,1, 3, 34);
INSERT INTO T VALUES (1,1, 4, 10);
INSERT INTO T VALUES (1,1, 5, 30);
INSERT INTO T VALUES (1,1, 6, 29);
INSERT INTO T VALUES (3,2, 7, 34);
INSERT INTO T VALUES (3,2, 8, 33);
INSERT INTO T VALUES (3,2, 9, 56);
INSERT INTO T VALUES (1,1, 7, 90);
INSERT INTO T VALUES (2,2, 8, 0,);
INSERT INTO T VALUES (1,1, 8, 80);
INSERT INTO T VALUES (2,2, 8, 65);
INSERT INTO T VALUES (1,1, 9, 34);
INSERT INTO T VALUES (2,2, 9, 11);each student belongs to one class_id. each student participates in many quizes. each quiz has its unique id. each student can appear once in a quiz_id
I am doing the below analysis and query:
1. with below query I am finding which student_id had most marks in any 3 successive quizes (see the 3-1 part below) in the query..
SELECT QUIZ_ID,
STUDENT_ID,
SUM (MARKS) OVER (PARTITION BY STUDENT_ID ORDER BY QUIZ_ID1
RANGE BETWEEN CURRENT ROW AND (3-1) FOLLOWING) consecMARKS , MARKS FROM
(SELECT QUIZ_ID,
STUDENT_ID,
MARKS,
ROW_NUMBER() OVER (PARTITION BY STUDENT_ID ORDER BY T.QUIZ_ID) QUIZ_ID1
FROM T
WHERE MARKS IS NOT NULL
ORDER BY 1
ORDER BY 3 DESC
SQL> /
QUIZ_ID STUDENT_ID CONSECMARKS
7 1 170
6 1 166
8 1 129
5 1 106
8 3 89
8 2 76
3 3 68
7 3 67
8 2 65
1 1 60
9 3 56
QUIZ_ID STUDENT_ID CONSECMARKS
9 1 49
2 2 40
4 1 40
9 2 11
15 rows selected.With above query, I can play around and find for any 'n' number of consecutive quizes, like marks in 2 consecutives quizes, 3, 4 and so on but for each 'n' value I've to run a seperate query mentioning (2-1) or (3-1) or (4-1) and so on..
since my table is big and there are about 400 quizes so what I want to find out is for each 'n' consecutive quiz (from 1 to 400) which student had most marks for each consecutie 'n' quiz. Like in 1 (consecutive) quiz which student had the highest marks and then 2 conseuctive quiz who had most marks and then in 3 consecutive quiz who had most marks and so on till 400 consecutive quiz who had most marks... rather than running query for each 'n' value seperately i want a single query that can give me a summary of most marks in each n consecutive quizes...
my sample output is:
Nth consecutive quiz student_id sum(marks)
1 1 90
2 1 170
3 1 246
4
100
200
300
400 ? ? Is this possible to get the above output from one single query? If there are two or more students with equal most marks for any 'n' conseutive quizes then both should come in the summary.
Hope I have been able to put up my question clearly.
regards
RamisSomething like:
SELECT N,
QUIZ_ID,
STUDENT_ID,
SUM(MARKS) OVER (PARTITION BY N,STUDENT_ID ORDER BY QUIZ_ID1 RANGE BETWEEN CURRENT ROW AND (N-1) FOLLOWING) consecMARKS,
MARKS
FROM (SELECT QUIZ_ID,
STUDENT_ID,
MARKS,
ROW_NUMBER() OVER (PARTITION BY STUDENT_ID ORDER BY T.QUIZ_ID) QUIZ_ID1
FROM T
WHERE MARKS IS NOT NULL
SELECT LEVEL N
FROM DUAL
CONNECT BY LEVEL <= (
SELECT COUNT(DISTINCT QUIZ_ID)
FROM T
ORDER BY N,
consecMARKS DESC
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
1 7 1 90 90
1 8 1 80 80
1 8 2 65 65
1 9 3 56 56
1 1 1 50 50
1 2 2 40 40
1 9 1 34 34
1 7 3 34 34
1 3 3 34 34
1 8 3 33 33
1 5 1 30 30
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
1 6 1 29 29
1 9 2 11 11
1 4 1 10 10
1 8 2 0 0
2 7 1 170 90
2 6 1 119 29
2 8 1 114 80
2 8 3 89 33
2 8 2 76 65
2 3 3 68 34
2 7 3 67 34
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
2 8 2 65 0
2 1 1 60 50
2 5 1 59 30
2 9 3 56 56
2 2 2 40 40
2 4 1 40 10
2 9 1 34 34
2 9 2 11 11
3 7 1 204 90
3 6 1 199 29
3 5 1 149 30
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
3 7 3 123 34
3 8 1 114 80
3 2 2 105 40
3 3 3 101 34
3 1 1 90 50
3 8 3 89 33
3 8 2 76 65
3 8 2 76 0
3 4 1 69 10
3 9 3 56 56
3 9 1 34 34
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
3 9 2 11 11
4 6 1 233 29
4 5 1 229 30
4 7 1 204 90
4 4 1 159 10
4 3 3 157 34
4 7 3 123 34
4 1 1 119 50
4 2 2 116 40
4 8 1 114 80
4 8 3 89 33
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
4 8 2 76 0
4 8 2 76 65
4 9 3 56 56
4 9 1 34 34
4 9 2 11 11
5 5 1 263 30
5 4 1 239 10
5 6 1 233 29
5 1 1 209 50
5 7 1 204 90
5 3 3 157 34
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
5 7 3 123 34
5 2 2 116 40
5 8 1 114 80
5 8 3 89 33
5 8 2 76 0
5 8 2 76 65
5 9 3 56 56
5 9 1 34 34
5 9 2 11 11
6 1 1 289 50
6 4 1 273 10
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
6 5 1 263 30
6 6 1 233 29
6 7 1 204 90
6 3 3 157 34
6 7 3 123 34
6 2 2 116 40
6 8 1 114 80
6 8 3 89 33
6 8 2 76 0
6 8 2 76 65
6 9 3 56 56
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
6 9 1 34 34
6 9 2 11 11
7 1 1 323 50
7 4 1 273 10
7 5 1 263 30
7 6 1 233 29
7 7 1 204 90
7 3 3 157 34
7 7 3 123 34
7 2 2 116 40
7 8 1 114 80
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
7 8 3 89 33
7 8 2 76 0
7 8 2 76 65
7 9 3 56 56
7 9 1 34 34
7 9 2 11 11
8 1 1 323 50
8 4 1 273 10
8 5 1 263 30
8 6 1 233 29
8 7 1 204 90
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
8 3 3 157 34
8 7 3 123 34
8 2 2 116 40
8 8 1 114 80
8 8 3 89 33
8 8 2 76 0
8 8 2 76 65
8 9 3 56 56
8 9 1 34 34
8 9 2 11 11
9 1 1 323 50
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
9 4 1 273 10
9 5 1 263 30
9 6 1 233 29
9 7 1 204 90
9 3 3 157 34
9 7 3 123 34
9 2 2 116 40
9 8 1 114 80
9 8 3 89 33
9 8 2 76 0
9 8 2 76 65
N QUIZ_ID STUDENT_ID CONSECMARKS MARKS
9 9 3 56 56
9 9 1 34 34
9 9 2 11 11
135 rows selected.
SQL> SY. -
Selecting data from single table with different condition in single query
Hi everybody...
I have one table with col1, col2, col3, col4, col5... as columns.
I want to select col1, col2, col3 with condition (x=y and a=b and c=d)
I want to select col4, col5 with condition (x=y and a=b and m=n )
in single query...
Thanx for ur helpGiven this data set...
SQL> select * from oddity
2 /
COL1 COL2 COL3 COL4 COL5 A X C M
1 2 3 4 5 B Y M
1 2 3 4 5 A Y C N
1 2 3 4 5 A Y D M
1 2 3 4 5 A Y D N
1 2 3 4 5 B Y D N
1 2 3 4 5 B Y D U
6 rows selected.
SQL>The following query meets the requirements. Of course, the requirements as stated are incomplete. I ahave assumed that we select all five columns if C=D andM=N.
SQL> SELECT decode(c, 'D', col1, '0') AS col1
2 , decode(c, 'D', col2, '0') AS col2
3 , decode(c, 'D', col3, '0') AS col3
4 , decode(m, 'N', col4, '-8') AS col4
5 , decode(m, 'N', col5, '-8') AS col5
6 FROM oddity
7 WHERE a = 'B'
8 AND x = 'Y'
9 /
COL1 COL2 COL3 COL4 COL5
0 0 0 -8 -8
1 2 3 4 5
1 2 3 -8 -8
SQL> Cheers, APC -
PNPCE in ABAP Query(SQ01) Selection is not working completely
Hi All,
I have developed an ABAP query using LDB PNPCE. I am reading data from IT000,IT0001 etc . I test the query in QA and I noticed that the PNCPCE screen doesnt care about the selection in query. Like I have given the date range in PNCPE default screen. But its not filtering that data at all. It pulling up the employees regardless of date, it behaves the same for field action type (P0000-MASSn). The surprising thing is that for company code its working fine. My question is that Why PNPCE standard screen-selection not filtering data in ABAP query.
Did any one of you also experience that?
FYI
I also find an OSS note # 730005 but we are already on 52 Patch !!
OSS note :PNPCE: Incorrect data of infotype 0000 and 0001
Symptom
When you carry out a reprot which is based on logical database PNPCE, errors occur during the processing of infotypes 0000 and 0001.
Other terms
PNPCE, AS PERSON, P0000, P0001, data selection period, person selection period, CHECK_IT_0000_DATA_RESTRICTED, CHECK_IT_0001_DATA_RESTRICTED
Reason and Prerequisites
You start a report which is based on logical database PNPCE. In the report, infotype 0000 or 0001 is processed (that is, it was declared via the INFOTYPES statement). At event 'GET GROUP' or 'GET PERAS', the system either returns too few data records in the infotype tables or it returns data records twice.
The error (missing data records) has only occured since the implementation of Note 687004 or the import of Support Package SAPKE47028.
Solution
Implement the attached corrections. or import the corresponding Support Package.
Thanks,
Saquib KhanHi Saquib,
Recently i am facing the same issue can u pls do reply how you have overcome this issue..The data is not getting picked up at the right time it is lately picked up by the program . Earlier it was PNP and it has been changed to PNPCE also we had implemented OSS Note 687004 and the later's Notes were also implemented but still now we are unable to track down why there is delay in picking up the records this has been happening for the past 8-10 Months(i m not sure far more before). I am finding 2 main issues,
1. we are using GET peras in PNPCE and no other event after that may be this is not fetching all the data because my selection screen does contain PERSONID where i suppose there should be GET PERSON event to define it
2. RP_READ_INFOTYPE macro while using for P0000 ,, the code in the macro is such that it fetches using P0001 also ...I suppose if there are any inconsistency between IT0 and IT1 then this might one to take care ...
kindly let me know how you have approached to resolve this issue. -
Different selection in a single query according to an ID
Hi
I'm looking for a way to perform different selections in a single query according to a specific value:
Here is the first selection:
select g.*,gf.*,gs.*
FROM graphs g
LEFT JOIN graph_frames gf on g.graph_id = gf.graph_id
LEFT JOIN graph_sets gs on gf.frame_id = gs.frame_id
WHERE g.graph_id = :IDHere is the second selection:
SELECT gg.graph_id, gg.graph_name
FROM generic_graphs gg
INNER JOIN generic_graph_frames ggf on gg.graph_id = ggf.graph_id
INNER JOIN generic_graph_sets ggs on ggf.frame_id = ggs.frame_id
WHERE gg.graph_id = :IDNow, the ID cannot be in both the tables and I want to perform that in a single query, UNION cannot be applied since the tables are different.
Any ideas?
Edited by: BluShadow on 14-Sep-2011 09:09
added {noformat}{noformat} tags. Please read {message:id=9360002} and learn to do this yourself.Example of consolidating the columns...
SQL> ed
Wrote file afiedt.buf
1 with t as (select &id as id from dual)
2 select e.empno, e.ename, e.job, e.mgr, d.deptno, d.dname, d.loc
3 from (select * from emp cross join t where empno = t.id) e
4 full outer join
5 (select * from dept cross join t where deptno = t.id) d
6* on (1=1)
SQL> /
Enter value for id: 7521
old 1: with t as (select &id as id from dual)
new 1: with t as (select 7521 as id from dual)
EMPNO ENAME JOB MGR DEPTNO DNAME LOC
7521 WARD SALESMAN 7698
SQL> /
Enter value for id: 10
old 1: with t as (select &id as id from dual)
new 1: with t as (select 10 as id from dual)
EMPNO ENAME JOB MGR DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
SQL>Though, this would be considered poor design because you are trying to query two disperate things, so they should be treated differently. i.e. in my example, I should already know if I'm querying an employee or a department beforehand. -
"SELECT SINGLE" is not working.
Hi all,
here i have one report. And in this i want just one row when my input material number match with my table material number this time if movement type is 131 then program will be terminated otherwise it's working but this coding is not working properly so can you please help me out.
My code is below .
data : matnr1 LIKE mseg-matnr,
bwart1 LIKE mseg-bwart.
loop at mseg.
SELECT SINGLE matnr bwart from mseg into (matnr1,bwart1) where matnr = matnr.
endloop.
if bwart1 = '131'.
MESSAGE 'DATA NOT FOUND FOR THIS SELECTION1' type 'I'.
SUBMIT ZPP_DAYWISE_MY_OWN VIA SELECTION-SCREEN AND RETURN .
LEAVE PROGRAM.
endif.
CLEAR matnr1.
CLEAR bwart1.there should be all key fields in where condition of SELECT SINGLE
here in select which matnr you are passing? selection screen of mseg-matnr?
loop at mseg.
SELECTmatnr bwart from mseg upto 1 rows into (matnr1,bwart1) where matnr = matnr. endselect.
endloop.
if bwart1 = '131'.
MESSAGE 'DATA NOT FOUND FOR THIS SELECTION1' type 'I'.
SUBMIT ZPP_DAYWISE_MY_OWN VIA SELECTION-SCREEN AND RETURN .
LEAVE PROGRAM.
CLEAR matnr1.
CLEAR bwart1.
endif.
Edited by: Sachin Bidkar on Feb 23, 2010 7:12 AM -
Select top row in Single Query?
Hi
Can somebody help me to write a query to get the first row after order by clause using single query alone.
Example:
I can write following query to select first row
select * from (selec * from t order by col1) where rownum = 1;
But here I should not use inline view to get the result. Because my original requirement needs to use this query in select list and it needs to use a column (of a table from the FROM clause) in the where clause of inline query. Because there is restriction that we can not use the column (of a table from the FROM clause) more than one level of inline query.
Please help me.Raghav.786 wrote:
Hi
Can somebody help me to write a query to get the first row after order by clause using single query alone.
Example:
I can write following query to select first row
select * from (selec * from t order by col1) where rownum = 1;
But here I should not use inline view to get the result. Because my original requirement needs to use this query in select list and it needs to use a column (of a table from the FROM clause) in the where clause of inline query. Because there is restriction that we can not use the column (of a table from the FROM clause) more than one level of inline query.
Please help me.
What Oracle version are you?
If you have 12c you can use
select col1,...
from t
order by col1
fetch first 1 row only;
If less than 12c, you have can't do it without a subquery.
What are you actually trying to do? Read Re: 2. How do I ask a question on the forums?
and follow the advice there by giving example create table and insert sample data statements and
explaining clearly what you are trying to do. Then we can help more. -
Hi All
I want to know how exactly the internal flow of select query works. Can someone please explain
ThanksHi,
Thanks for the link.Is there any example for back end process of Select queryBingo.. ;)
See, a process (or) set of programs which are working internally. You can get some of the results about the statistics of query how much cpu time it consuming, fetches etc., check v$sql_monitor. Apart from that if you are looking for some thing else then kindly elaborate your requirement.
- Pavan Kumar N -
Single Query for getting total no of records N getting records from a selected range
Hi,
Got the below query:
SELECT a.*, rowid FROM (SELECT name, postcode FROM Tbl ORDER BY name asc)a WHERE ROWNUM <=30
MINUS
SELECT b.*, rowid FROM (SELECT name, postcode FROM Tbl ORDER BY name asc) b WHERE ROWNUM <= 10
Though I got the results right, I also want to know the total no of records from "SELECT name, postcode FROM Tbl ORDER BY name asc". Does anyone knows how to do it in a single query?
Thanks.hi Carol
The following output may help you.
SQL> l
1 select * from emp where (rowid,0) in (select rowid,mod(rownum,10)-rownum from emp)
2 minus
3* select * from emp where (rowid,0) in (select rowid,mod(rownum,6)-rownum from emp)
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
The above query fetches the 6,7,8,9th records only.
Well my suggestion would be not to use ROWNUM directly in where clause since it changes dynamically. it is not a fixed value. for example pls see the result.
SQL> select rownum,empno,ename,sal,job from emp where sal > 3000;
ROWNUM EMPNO ENAME SAL JOB
1 7839 KING 5000 PRESIDENT
SQL> select rownum,empno,ename,sal,job from emp where sal > 1000;
ROWNUM EMPNO ENAME SAL JOB
1 7566 JONES 2975 MANAGER
2 7654 MARTIN 1250 SALESMAN
3 7698 BLAKE 2850 MANAGER
4 7782 CLARK 2450 MANAGER
5 7788 SCOTT 3000 ANALYST
6 7839 KING 5000 PRESIDENT
7 7844 TURNER 1500 SALESMAN
8 7876 ADAMS 1100 CLERK
9 7902 FORD 3000 ANALYST
10 7934 MILLER 1300 CLERK
10 rows selected.
SQL> select * from emp where rownum = 1;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7566 JONES MANAGER 7839 02-APR-81 2975 20
The record of employee KING is getting the rownum differently.
My understanding (out of my little knowledge) is the rownum values are assigned to the records only after the records are read physically and after applying the conditions(without rownum). Then the row numbers (rownum) is assigned to the records. Hence the rownum is not constant to a record since it is a dynamic value.
Well i would like to know the suggestions of the ORACLE EXPERTS here in the discussion forum.
If my finding is correct then OK if not Pls excuse me and pls give the correct solution
Regards
Prakash Eranki
[email protected] -
Update or select in a single query
Hi , I want to select or update from a single query. Can I do that ?
I tried Merge into, but it is for Update or Insert.
Can anyone help me please. I do not want to use stored Proc/function.userPrasad wrote:
I want to fire a update or merge statement inside a select SQL. Is there any way to do that ?Just to clarify:
It is possible to UPDATE based on a SELECT condition within SQL, i.e.,
UPDATE table1 t1
SET col1 = new_val
WHERE col2 IN (
SELECT col2
FROM table2 t2
WHERE t1.col1 = t2.col1
AND col1 = old_val);or to MERGE:
MERGE into table1 t1
USING (SELECT * FROM table2) t2
ON (t1.col1 = t2.col1)
WHEN MATCHED...
WHEN NOT MATCHED...Both of those will use a SELECT, and MERGE can UPDATE, INSERT, both or either one, or none.
Is that what you had in mind? -
Multiple conditin on a single column in a single query
i want to select data from the table using diff conditions on a single column
for eg:
i need emp details like whose having empno as 5-15,19-45,50-89 ..etc this how can i give a condition in a single query
we can use in or between for 2 or 3 conditions but i have plenty off like thisHi,
848525 wrote:
i want to select data from the table using diff conditions on a single column
for eg:
i need emp details like whose having empno as 5-15,19-45,50-89 ..etc this how can i give a condition in a single query
we can use in or between for 2 or 3 conditions but i have plenty off like thisYou can use OR for any number of conditions; it works the same for 4 or more.
You could also put the ranges into a table (or a sub-query, as shown below) and join:
WITH empno_ranges AS
SELECT 3000 AS low_val, 5000 AS high_val FROM dual UNION ALL
SELECT 7000, 7499 FROM dual UNION ALL
SELECT 7500, 7599 FROM dual UNION ALL
SELECT 7800, 7899 FROM dual
SELECT r.*
, COUNT (e.empno) AS emp_cnt
FROM empno_ranges r
LEFT OUTER JOIN scott.emp e ON e.empno BETWEEN r.low_val
AND r.high_val
GROUP BY r.low_val, r.high_val
ORDER BY r.low_val
;Output:
LOW_VAL HIGH_VAL EMP_CNT
3000 5000 0
7000 7499 2
7500 7599 2
7800 7899 3 -
Is it possible to combine 2 different reports in a single Query?
Dear All,
What am I working at?
I produced a Query for Debtors Aged Analysis which mimics the Official SAP B1 8.8 Aging Report.
It ages the outstanding invoices by Posting Date ( RefDate in JDT1)
I also made another Report which ages outstanding amounts by Document Date simply by replacing all RefDate by TaxDate
They give different answers if Posting Date is different from Document Date, e.g a manual invoice dated 5 Jun 2011 is posted on 2 Jul 2011.
What I want to do?
My idea is to produce a SINGLE QUERY which will generate either Aging depending on settings:
(a) A Debtors Aging by Posting Date
(b) A Debtors Aging by Document Date
I declare 2 variables:
(a) @refdt is [%1] and represents Posting Date (JDT1 RefDate)
(b) @taxdt is [%2] and represents Document Date (JDT1 TaxDate)
When the Query is run, the foll dialog appears:
Query - Selection Criteria
Posting Date Equal .......
Document Date Equal .......
OK Cancel
We are expected to fill in only 1 date and leave the other blank, and Query will generate the required report .
What is my problem?
Query runs smoothly, but amounts in the Balance column does not get analyzed in the Age brackets: Current / 1 Mth Ago / etc
I think I know where's the problem
I am assuming (wrongly) that if we don't fill one date field, the query returns NULL for that variable. In fact, it appears to return GetDate().
Help
Can anybody help me put the correct commands so that all my balances are correctly analysed in the respective age buckets?
Thanks
Leon Lai
Here's a simplified SQL:
Tables :
JDT1 T0 = Journal Entry - Rows
OCRD T1 = Business Partner
OCPR T2 = Contact Person
OJDT T3 = Journal Entry - Header
OINV T4 = A/R Invoices - Header
ORIN T5 - A/R Credit Memo - Header
declare @refdt date
declare @taxdt date
set @refdt
/*Select 1 from jdt1 t where t.RefDate*/ = [%1]
set @taxdt
/*Select 1 from jdt1 w where w.TaxDate*/ = [%2]
SELECT
'company1' AS 'Company',
T1.CardCode AS 'BP Code',
T2.Notes2 AS 'BP Name',
T0.RefDate AS 'Pstg Dt',
T0.TaxDate AS 'Doc Dt',
CASE
WHEN T0.TransType = 13 THEN 'IN'
WHEN T0.TransType = 14 THEN 'CN'
WHEN T0.TransType = 30 THEN 'JE'
WHEN T0.TransType = 24 THEN 'RC'
WHEN T0.TransType = 46 THEN 'PS'
ELSE 'Error ! ! !'
END AS 'Doc Type',
T0.Ref1 'Doc. Number',
ISNULL(T0.FCCurrency, ' - ') AS 'Ccy',
(T0.FCDebit - T0.FCCredit) AS 'Orig. F.Ccy',
(T0.BalFcDeb - T0.BalFcCred) AS 'Bal. F. Ccy',
(T0.Debit - T0.Credit) AS 'Orig. Rs',
(T0.BalDueDeb - T0.BalDueCred) AS 'Bal. Rs',
/* ######################## PROBLEM is here ################## */
CASE
WHEN (@refdt is not null) and (@taxdt is null)
THEN ISNULL((SELECT T0.BalDueDeb -T0.BalDueCred
WHERE DateDiff(mm, T0.RefDate, @refdt) = 0 ) ,0)
WHEN (@refdt is null) and (@taxdt is not null)
THEN ISNULL((SELECT T0.BalDueDeb -T0.BalDueCred
WHERE DateDiff(mm, T0.TaxDate, @taxdt) = 0 ) ,0)
END AS 'Current Mth',
CASE
WHEN (@refdt is not null) and (@taxdt is null)
THEN ISNULL((SELECT T0.BalDueDeb -T0.BalDueCred
WHERE DateDiff(mm, T0.RefDate, @refdt) = 1 ) ,0)
WHEN (@refdt is null) and (@taxdt is not null)
THEN ISNULL((SELECT T0.BalDueDeb -T0.BalDueCred
WHERE DateDiff(mm, T0.TaxDate, @taxdt) = 1 ) ,0)
END AS '1 Mth Ago'
/* Similarly for other age buckets */
FROM company1.dbo.JDT1 T0
INNER JOIN company1.dbo.OCRD T1 ON T0.ShortName = T1.CardCode
LEFT OUTER JOIN company1.dbo.OCPR T2 ON T1.CardCode = T2.Cardcode
LEFT OUTER JOIN company1.dbo.OJDT T3 ON T0.TransID = T3.TransID
LEFT OUTER JOIN company1.dbo.OINV T4 ON T3.TransID = T4.TransID
LEFT OUTER JOIN company1.dbo.ORIN T5 ON T3.TransID = T5.TransID
WHERE
T1.CardType = 'C' and Balance != 0
and (T0.BalDueDeb - T0.BalDueCred) != 0HI
I generate the next aged analysis
CREATE PROCEDURE [dbo].[Aged_Analysis] (@end datetime,@Client VarChar (20)) AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets
-- interfering with SELECT statements.
SET NOCOUNT ON;
Declare @SAPUNION Table (SN VarChar(50), TransId Int, ReconSum Decimal(19,2), DebHab VarChar(1),
Linea Int)
insert into @SAPUNION
SELECT X0.ShortName 'SN', X0.TransId 'TransId', SUM(X0.ReconSum)'ReconSum', X0.IsCredit 'DebHab', X0.TransRowId 'Linea'
FROM ITR1 X0
INNER JOIN OITR X1 ON X1.ReconNum = X0.ReconNum
WHERE X1.ReconDate <= @end AND X1.CancelAbs = ''
GROUP BY X0.ShortName, X0.TransId, X0.IsCredit, X0.TransRowId
SELECT T0.CardCode, T0.CardName,T0.Address,T0.CreditLine,
T1.TransId , T4.BaseRef , T1.Ref2 , T1.RefDate, T1.DueDate,
CASE
WHEN T3.DebHab = 'D' THEN T1.Debit-T1.Credit-T3.ReconSum
WHEN T3.DebHab = 'C' THEN T1.Debit-T1.Credit+T3.ReconSum
ELSE (T1.Debit-T1.Credit)
END 'Balance',
CASE
when DateDiff(Day,t1.RefDate,GetDate()) <= 30 and T3.DebHab = 'D' then T1.Debit-T1.Credit-T3.ReconSum
when DateDiff(Day,t1.RefDate,GetDate()) <= 30 and T3.DebHab = 'C' then T1.Debit-T1.Credit+T3.ReconSum
when DateDiff(Day,t1.RefDate,GetDate()) <= 30 then (T1.Debit-T1.Credit) end '0-30 dias',
CASE
when DateDiff(Day,t1.refdate,GetDate()) between 31 and 45 and T3.DebHab = 'D' then T1.Debit-T1.Credit-T3.ReconSum
when DateDiff(Day,t1.refdate,GetDate()) between 31 and 45 and T3.DebHab = 'C' then T1.Debit-T1.Credit+T3.ReconSum
when DateDiff(Day,t1.refdate,GetDate()) between 31 and 45 then (T1.Debit-T1.Credit) end '31-45 dias',
CASE
when DateDiff(Day,t1.refdate,GetDate()) between 46 and 60 and T3.DebHab = 'D' then T1.Debit-T1.Credit-T3.ReconSum
when DateDiff(Day,t1.refdate,GetDate()) between 46 and 60 and T3.DebHab = 'C' then T1.Debit-T1.Credit+T3.ReconSum
when DateDiff(Day,t1.refdate,GetDate()) between 46 and 60 then (T1.Debit-T1.Credit) end '46-60 dias',
CASE
when DateDiff(Day,t1.refdate,GetDate()) between 61 and 75 and T3.DebHab = 'D' then T1.Debit-T1.Credit-T3.ReconSum
when DateDiff(Day,t1.refdate,GetDate()) between 61 and 75 and T3.DebHab = 'C' then T1.Debit-T1.Credit+T3.ReconSum
when DateDiff(Day,t1.refdate,GetDate()) between 61 and 75 then (T1.Debit-T1.Credit) end '61-75 dias',
CASE
when DateDiff(Day,t1.refdate,GetDate()) BETWEEN 76 AND 89 and T3.DebHab = 'D' then T1.Debit-T1.Credit-T3.ReconSum
when DateDiff(Day,t1.refdate,GetDate()) BETWEEN 76 AND 89 and T3.DebHab = 'C' then T1.Debit-T1.Credit+T3.ReconSum
when DateDiff(Day,t1.refdate,GetDate()) BETWEEN 76 AND 89 then (T1.Debit-T1.Credit) end '76-89 dias',
CASE
when DateDiff(Day,t1.refdate,GetDate()) >90 and T3.DebHab = 'D' then T1.Debit-T1.Credit-T3.ReconSum
when DateDiff(Day,t1.refdate,GetDate()) >90 and T3.DebHab = 'C' then T1.Debit-T1.Credit+T3.ReconSum
when DateDiff(Day,t1.refdate,GetDate()) >90 then (T1.Debit-T1.Credit) end '90-Mas',
CASE T1.TransType
WHEN '13' THEN (SELECT Y.Comments FROM OINV Y WHERE Y.TransId = T1.TransId)
WHEN '14' THEN (SELECT Y.Comments FROM ORIN Y WHERE Y.TransId = T1.TransId)
WHEN '24' THEN (SELECT Y.Comments FROM ORCT Y WHERE Y.TransId = T1.TransId)
ELSE T1.LineMemo
END 'Comments'
FROM OCRD T0
INNER JOIN JDT1 T1 ON T1.ShortName = T0.CardCode
INNER JOIN OACT T2 ON T2.AcctCode = T1.Account
INNER JOIN OJDT T4 ON T4.TransId = T1.TransId
LEFT JOIN OINV T5 ON T5.TransId = T4.TransId and t5.ObjType = t4.TransType
LEFT JOIN @SAPUNION T3 ON T3.TransId = T1.TransId AND T3.SN = T1.ShortName AND T3.Linea = T1.Line_ID
WHERE T0.CardType = 'C' /*FOR CLIENTS*/ AND T1.RefDate <= @end AND T2.AcctCode = /*YOUR CLIENT ACCOUNT*/ AND
(CASE
WHEN T3.DebHab = 'D' THEN (T1.Debit-T1.Credit-T3.ReconSum)
WHEN T3.DebHab = 'C' THEN (T1.Debit-T1.Credit+T3.ReconSum)
ELSE (T1.Debit-T1.Credit)
END) != '0'
AND T0.CardCode= @Client
ORDER BY T0.CardCode,T1.TransId,t1.Ref2
END
AND Execute by SAP
DECLARE @VAR INT, @DATE DATETIME, @BP VARCHAR(8)
SET @VAR = (SELECT TOP 1 T.DocEntry FROM [dbo].[OINV] T WHERE T.DocDate <='[%0]' AND T.CardCode='[%1]')
SET @DATE = '[%0]'
SET @BP = '[%1]'
EXECUTE [dbo].[Aged_Analysis]
@end = @DATE,
@Client = @BP
Regards
Floyola
Edited by: Floyola on Jul 22, 2011 10:17 AM -
Tricky SQL query... how to get all data in a single query?
create table employee_definition (def_id number, def_name varchar(50));
insert into employee_definition values (100, 'EMAIL');
insert into employee_definition values (200, 'MOBILE_PHONE');
insert into employee_definition values (300, 'HOME_PHONE');
SQL> select * from employee_definition;
DEF_ID DEF_NAME
100 EMAIL
200 MOBILE_PHONE
300 HOME_PHONE
create table employee_data (def_id number, def_value varchar(20), emp_id number);
insert into employee_data values (100, '[email protected]', 123);
insert into employee_data values (200, '01232222', 123);
insert into employee_data values (300, '5555', 123);
insert into employee_data values (100, '[email protected]', 666);
insert into employee_data values (200, '888', 666);
insert into employee_data values (300, '999', 666);
insert into employee_data values (300, '444', 777);
SQL> select * from employee_data;
DEF_ID DEF_VALUE EMP_ID
100 [email protected] 123
200 01232222 123
300 5555 123
100 [email protected] 666
200 888 666
300 999 666
300 999 777
7 rows selected.I'm supposed to create a SQL that will return me the email, mobile_phone, and home_phone for a set of employees. The result will be something like this:
EMPLOYEE ID | HOME_PHONE | MOBILE_PHONE | EMAIL
123 | 5555 | 01232222 | [email protected]
666 | 999 | 888 | [email protected]
777 | 444 | null | nullThe thing I'm finding difficulty here is that the same column is used to store different values, based on the value in employee_definition table (something like a key/value pair). If I do:
SQL> select emp_id, def_value as email from employee_data, employee_definition
2 where employee_data.def_id = employee_definition.def_id
3 and employee_definition.def_name = 'EMAIL';
EMP_ID EMAIL
123 [email protected]
666 [email protected]'s partially ok.. I'm just getting the definition for 'EMAIL'. But how can I get all the values in a single query, knowing that the column stores different values based on def_name?Oh no, not again.
Entity attribute models always seem like a great idea to people who have been in the profession for five minutes and lack any kind of fundamental knowledge.
It staggers me that someone with 2,345 posts still believes "you need a 'detail table' for [storing multiple telephone numbers]"
"A person can have multiple telephone numbers" is not an excuse to build a tired person_attribute table. Niether is the bizarre proposal by someone with over 4k posts who should know better in an earlier post that EAV models are necessary to support temporal fidelity.
Taken to it's logical conclusion, EAV modelling leads to just two application tables. THINGS and THING_ATTRIBUTES. And when you consider that a THING_ATTRIBUTE is also a THING, why not roll those two tables up into one also? Hmmm, what does THINGS and THING_ATTRIBUTES look like? I know, TABLES and COLUMNS. Who would've guessed? SQL already provides the completely flexible extensible attribute model the advocates of EAV proscribe. But it also has data types, physical data independence, constraints and an efficient query language which EAV does not.
EAV modelling errodes the semantics of the attributes which are bundled into the "attribute" table.
There is no point in storing 12 different phone numbers with implied functional dependency to unconstrained and often repeating notional attributes like "MOBILE", "LANDLINE", "WORK", err, "WORK2", err, "MOBILE2", err, ... when this phone type attribute has no semantic value. When you want to call someone, you invariably want to retrive the prefered_phone_number which may depend on a time of day, or a call context.
These things need to be modelled properly (i.e normalised to BCNF) within the context of the database.
Maybe you are looking for
-
To get all tsql errors in sql server arised on particular date
Hello all, Is there any T-Sql Command in Sql Server to get the list of Errors Occurred , While Writing a Queries. Like in server any thing if we get a error it will be stored in logs right (EG : Exec SP_ReadErrorLog ),Or going through SqlServer-Agen
-
I AM NO LONGER ABLE TO ADD SPECIAL EFFECTS TO MY MOVIES
Hi i am making a video using a green screen. I used to be able to double click my video on the project bin and choose a special effect such as romantic or black and white. Now i can no longer do this. It brings up video stabalization, audio and video
-
Dear all.. In PO I have selected tax code with BED 14 % + VAT 4% as per PO when I received Vendor invoice which is not matching with my PO tax code so that I have changed excise amount from 140/- to 155 /- in MIGO capture excise invoice. And then pos
-
Nvarchar field in SQL not showing up in BVM
Hi All, I have some fields which are dclared as nvarchar in SQL and i have used cast function to get them as strings into CR. Now i want to create a Dynamic parameter in BVM. In the Business Element level,i am having the same issue as the data type i
-
How can I make the initial "Meet Mozilla Firefox" window stop opening?
I update to Firefox 8 today and now every time I go into Firefox I keep getting the "Meet Mozilla Firefox" window. How can I make that window not open again?