How To on Select Query
I have following table.
Id Amount
1 30
2 20
3 10
4 10
5 30
6 10
7 20
8 10
i need following output
sum
60 sum where id <=3
100 sum where id <=5
140 sum where id <=8
how can i achieve this?
1 with v as (
2 select 1 id, 30 amount from dual union all
3 select 2, 20 from dual union all
4 select 3, 10 from dual union all
5 select 4, 10 from dual union all
6 select 5, 30 from dual union all
7 select 6, 10 from dual union all
8 select 7, 20 from dual union all
9 select 8, 10 from dual
10 )
11 select dsc, sum
12 from (
13 select id
14 , 'Sum where id <= '||id dsc
15 , sum(amount) over (
16 order by id
17 ) sum
18 from v
19 )
20* where id in (3, 5, 8)
SQL> /
DSC SUM
Sum where id <= 3 60
Sum where id <= 5 100
Sum where id <= 8 140
3 rows selected.
Similar Messages
-
How to alter Select Query while altering selectionscreen for same output?
Hi,
i have to change the selection screen fields with some new fields , but i dont know how to change the selection query accordingly, because i need the same output with the same fields but with a different set of fields in the selection screen.
The original code is
TYPES: BEGIN OF ty_output,
mblnr TYPE mseg-mblnr,
mjahr TYPE mseg-mjahr,
zeile TYPE mseg-zeile,
bwart TYPE mseg-bwart,
werks TYPE mseg-werks,
lgort TYPE mseg-lgort,
ebeln TYPE mseg-ebeln,
ebelp TYPE mseg-ebelp,
lfbnr TYPE mseg-lfbnr,
lfpos TYPE mseg-lfpos,
sjahr TYPE mseg-sjahr,
dmbtr TYPE mseg-dmbtr,
menge TYPE mseg-menge,
matnr TYPE mseg-matnr,
mtart TYPE mara-mtart,
matkl TYPE mara-matkl,
bismt TYPE mara-bismt,
meins TYPE mara-meins,
bedat TYPE ekko-bedat,
waers TYPE ekko-waers,
wkurs TYPE ekko-wkurs,
verkf TYPE ekko-verkf,
EFFWR TYPE EKPO-EFFWR,
tax TYPE ekpo-effwr,
maktx TYPE makt-maktx,
budat TYPE mkpf-budat,
usnam TYPE mkpf-usnam,
m_menge TYPE ekpo-menge,
ablad TYPE mseg-ablad,
wempf TYPE mseg-wempf,
END OF ty_output.
TYPES: BEGIN OF ty_mseg_103,
mblnr TYPE mseg-mblnr,
mjahr TYPE mseg-mjahr,
zeile TYPE mseg-zeile,
bwart TYPE mseg-bwart,
werks TYPE mseg-werks,
lgort TYPE mseg-lgort,
ebeln TYPE mseg-ebeln,
ebelp TYPE mseg-ebelp,
lfbnr TYPE mseg-lfbnr,
lfpos TYPE mseg-lfpos,
sjahr TYPE mseg-sjahr,
dmbtr TYPE mseg-dmbtr,
menge TYPE mseg-menge,
matnr TYPE mseg-matnr,
mtart TYPE mara-mtart,
matkl TYPE mara-matkl,
bismt TYPE mara-bismt,
meins TYPE mara-meins,
bedat TYPE ekko-bedat,
waers TYPE ekko-waers,
wkurs TYPE ekko-wkurs,
verkf TYPE ekko-verkf,
EFFWR TYPE EKPO-EFFWR,
tax TYPE ekpo-effwr,
maktx TYPE makt-maktx,
budat TYPE mkpf-budat,
usnam TYPE mkpf-usnam,
m_menge TYPE ekpo-menge,
ablad TYPE mseg-ablad,
wempf TYPE mseg-wempf,
END OF ty_mseg_103.
TYPES: BEGIN OF ty_mseg_105,
mblnr TYPE mseg-mblnr,
mjahr TYPE mseg-mjahr,
zeile TYPE mseg-zeile,
bwart TYPE mseg-bwart,
werks TYPE mseg-werks,
lgort TYPE mseg-lgort,
ebeln TYPE mseg-ebeln,
ebelp TYPE mseg-ebelp,
lfbnr TYPE mseg-lfbnr,
lfpos TYPE mseg-lfpos,
sjahr TYPE mseg-sjahr,
dmbtr TYPE mseg-dmbtr,
menge TYPE mseg-menge,
matnr TYPE mseg-matnr,
mtart TYPE mara-mtart,
matkl TYPE mara-matkl,
bismt TYPE mara-bismt,
meins TYPE mara-meins,
bedat TYPE ekko-bedat,
waers TYPE ekko-waers,
wkurs TYPE ekko-wkurs,
verkf TYPE ekko-verkf,
effwr TYPE ekpo-effwr,
tax TYPE ekpo-effwr,
maktx TYPE makt-maktx,
budat TYPE mkpf-budat,
ablad TYPE mseg-ablad,
wempf TYPE mseg-wempf,
END OF ty_mseg_105.
DATA: Begin with W_ *
DATA : it_fcat TYPE slis_t_fieldcat_alv. " alv field catalog
DATA : listhead TYPE slis_t_listheader WITH HEADER LINE.
DATA: event TYPE slis_t_event WITH HEADER LINE.
INTERNAL TABLES: Begin with IT_ *
DATA : it_output TYPE STANDARD TABLE OF ty_output WITH KEY mblnr mjahr,
it_mseg_103 TYPE STANDARD TABLE OF ty_mseg_103,
it_mseg_105 TYPE STANDARD TABLE OF ty_mseg_103,
it_mseg_106 TYPE STANDARD TABLE OF ty_mseg_103, " LNTDEV1 ADDED
it_mara TYPE STANDARD TABLE OF ty_mara,
it_ekko TYPE STANDARD TABLE OF ty_ekko,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo,
it_makt TYPE STANDARD TABLE OF ty_makt,
it_mkpf TYPE STANDARD TABLE OF ty_mkpf WITH KEY mblnr.
WORKAREAS: Begin with WA_ *
DATA : wa_fcat LIKE LINE OF it_fcat.
DATA : wa_output LIKE LINE OF it_output,
wa_mara LIKE LINE OF it_mara,
wa_ekko LIKE LINE OF it_ekko,
wa_ekpo LIKE LINE OF it_ekpo,
wa_makt LIKE LINE OF it_makt,
wa_mkpf LIKE LINE OF it_mkpf,
wa_mseg_103 LIKE LINE OF it_mseg_103,
wa_mseg_105 LIKE LINE OF it_mseg_105,
wa_mseg_106 LIKE LINE OF it_mseg_106. " LNTDEV1 ADDED
DATA : w_menge LIKE ekpo-menge.
FIELD-SYMBOLS: Begin with FS_ *
PARAMETERS: Begin with PR_ *
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS : pr_werks LIKE mseg-werks OBLIGATORY.
SELECT-OPTIONS : so_budat FOR mkpf-budat.
SELECTION-SCREEN END OF BLOCK b1.
SELECT-OPTIONS: Begin with SO_ *
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME.
SELECT-OPTIONS : so_mtart FOR mara-mtart.
SELECT-OPTIONS : so_matkl FOR mara-matkl.
SELECT-OPTIONS : so_matnr FOR mara-matnr.
SELECT-OPTIONS : so_bismt FOR mara-bismt.
SELECT-OPTIONS : so_ebeln FOR mseg-ebeln.
SELECT-OPTIONS : so_verkf FOR ekko-verkf.
SELECT-OPTIONS : so_bedat FOR ekko-bedat.
SELECT-OPTIONS : so_mblnr FOR mseg-mblnr.
SELECTION-SCREEN END OF BLOCK b2.
*AT SELECTION-SCREEN.
*SO_BUDAT-HIGH = SO_BUDAT-HIGH + 1.
Query for getting all the Document Segment: Material data from MSEG *
Table which having Movement Types 103 and put into it_mseg table, *
Filtering by reference document and material document number *
Filtering by selection criteria (Material Docu No, Purchase order *
and Plant) *
START-OF-SELECTION.
SELECT pmblnr smblnr p~mjahr zeile bwart lfbnr werks lgort ebeln
ebelp lfpos sjahr dmbtr usnam menge sbudat pmatnr q~mtart
pablad pwempf
FROM mseg AS p INNER JOIN mara AS q
ON pmatnr = qmatnr
JOIN mkpf AS s ON smblnr EQ pmblnr
INTO CORRESPONDING FIELDS OF
TABLE it_mseg_103
WHERE bwart EQ 103
AND p~matnr IN so_matnr
AND werks EQ pr_werks
AND ebeln IN so_ebeln
AND q~mtart IN so_mtart
AND p~mblnr IN so_mblnr
AND s~budat IN so_budat.
IF sy-subrc = 0.
ENDIF.
SELECT pmblnr smblnr p~mjahr zeile bwart lfbnr werks lgort ebeln
ebelp lfpos sjahr dmbtr usnam menge sbudat pmatnr q~mtart
pablad pwempf
FROM mseg AS p INNER JOIN mara AS q
ON pmatnr = qmatnr
JOIN mkpf AS s ON smblnr EQ pmblnr
INTO CORRESPONDING FIELDS OF
TABLE it_mseg_105
WHERE bwart EQ 105 OR bwart EQ 104 OR bwart EQ 124 OR bwart EQ 124
AND p~matnr IN so_matnr
AND werks EQ pr_werks
AND ebeln IN so_ebeln
AND q~mtart IN so_mtart
AND p~mblnr IN so_mblnr
AND s~budat IN so_budat.
IF sy-subrc = 0.
ENDIF.
LOOP AT IT_MSEG_103 INTO WA_MSEG_103.
READ TABLE IT_MSEG_105 INTO WA_MSEG_105 WITH KEY LFBNR =
WA_MSEG_103-MBLNR.
IF SY-SUBRC <> 0.
APPEND WA_MSEG_103 TO IT_OUTPUT.
ENDIF.
ENDLOOP.
SELECT pmblnr smblnr p~mjahr zeile bwart lfbnr werks lgort ebeln
ebelp lfpos sjahr dmbtr usnam menge sbudat pmatnr q~mtart
pablad pwempf
FROM mseg AS p INNER JOIN mara AS q
ON pmatnr = qmatnr
JOIN mkpf AS s ON smblnr EQ pmblnr
INTO CORRESPONDING FIELDS OF
TABLE it_mseg_106
WHERE bwart EQ 106
AND p~matnr IN so_matnr
AND werks EQ pr_werks
AND ebeln IN so_ebeln
AND q~mtart IN so_mtart
AND p~mblnr IN so_mblnr
AND s~budat IN so_budat.
LOOP AT it_mseg_103 INTO wa_mseg_103.
READ TABLE it_mseg_106 INTO wa_mseg_106 WITH KEY
lfbnr = wa_mseg_103-mblnr.
IF sy-subrc = 0.
APPEND wa_mseg_106 TO it_output.
clear wa_mseg_106.
ENDIF.
ENDLOOP.
Present selection criteria is
1. Plant
2. Posting Date
3. Material Type
4. Material Group
5. Material No.
6. Old Material No.
7. PO Order No.
8. Old PO No.
9. PO Date
10. Material Document No.
New selection criteria is
1. PO no.
2. PO date
3. PO value (Ex: >=10000 <=50000)
4. Purch org
5. Doc type
6. Vendor
7. Material
8. Account assignment category
9. OrderHi,
i have to change the selection screen fields with some new fields , but i dont know how to change the selection query accordingly, because i need the same output with the same fields but with a different set of fields in the selection screen.
The original code is
TYPES: BEGIN OF ty_output, mblnr TYPE mseg-mblnr, mjahr TYPE mseg-mjahr, zeile TYPE mseg-zeile, bwart TYPE mseg-bwart, werks TYPE mseg-werks, lgort TYPE mseg-lgort, ebeln TYPE mseg-ebeln, ebelp TYPE mseg-ebelp, lfbnr TYPE mseg-lfbnr, lfpos TYPE mseg-lfpos, sjahr TYPE mseg-sjahr, dmbtr TYPE mseg-dmbtr, menge TYPE mseg-menge, matnr TYPE mseg-matnr, mtart TYPE mara-mtart, matkl TYPE mara-matkl, bismt TYPE mara-bismt, meins TYPE mara-meins, bedat TYPE ekko-bedat, waers TYPE ekko-waers, wkurs TYPE ekko-wkurs, verkf TYPE ekko-verkf, * EFFWR TYPE EKPO-EFFWR, tax TYPE ekpo-effwr, maktx TYPE makt-maktx, budat TYPE mkpf-budat, usnam TYPE mkpf-usnam, m_menge TYPE ekpo-menge, ablad TYPE mseg-ablad, wempf TYPE mseg-wempf, END OF ty_output. TYPES: BEGIN OF ty_mseg_103, mblnr TYPE mseg-mblnr, mjahr TYPE mseg-mjahr, zeile TYPE mseg-zeile, bwart TYPE mseg-bwart, werks TYPE mseg-werks, lgort TYPE mseg-lgort, ebeln TYPE mseg-ebeln, ebelp TYPE mseg-ebelp, lfbnr TYPE mseg-lfbnr, lfpos TYPE mseg-lfpos, sjahr TYPE mseg-sjahr, dmbtr TYPE mseg-dmbtr, menge TYPE mseg-menge, matnr TYPE mseg-matnr, mtart TYPE mara-mtart, matkl TYPE mara-matkl, bismt TYPE mara-bismt, meins TYPE mara-meins, bedat TYPE ekko-bedat, waers TYPE ekko-waers, wkurs TYPE ekko-wkurs, verkf TYPE ekko-verkf, * EFFWR TYPE EKPO-EFFWR, tax TYPE ekpo-effwr, maktx TYPE makt-maktx, budat TYPE mkpf-budat, usnam TYPE mkpf-usnam, m_menge TYPE ekpo-menge, ablad TYPE mseg-ablad, wempf TYPE mseg-wempf, END OF ty_mseg_103. TYPES: BEGIN OF ty_mseg_105, mblnr TYPE mseg-mblnr, mjahr TYPE mseg-mjahr, zeile TYPE mseg-zeile, bwart TYPE mseg-bwart, werks TYPE mseg-werks, lgort TYPE mseg-lgort, ebeln TYPE mseg-ebeln, ebelp TYPE mseg-ebelp, lfbnr TYPE mseg-lfbnr, lfpos TYPE mseg-lfpos, sjahr TYPE mseg-sjahr, dmbtr TYPE mseg-dmbtr, menge TYPE mseg-menge, matnr TYPE mseg-matnr, mtart TYPE mara-mtart, matkl TYPE mara-matkl, bismt TYPE mara-bismt, meins TYPE mara-meins, bedat TYPE ekko-bedat, waers TYPE ekko-waers, wkurs TYPE ekko-wkurs, verkf TYPE ekko-verkf, effwr TYPE ekpo-effwr, tax TYPE ekpo-effwr, maktx TYPE makt-maktx, budat TYPE mkpf-budat, ablad TYPE mseg-ablad, wempf TYPE mseg-wempf, END OF ty_mseg_105. *----------------------------------------------------------------------* * DATA: Begin with W_ * *----------------------------------------------------------------------* DATA : it_fcat TYPE slis_t_fieldcat_alv. " alv field catalog DATA : listhead TYPE slis_t_listheader WITH HEADER LINE. DATA: event TYPE slis_t_event WITH HEADER LINE. *----------------------------------------------------------------------* * INTERNAL TABLES: Begin with IT_ * *----------------------------------------------------------------------* DATA : it_output TYPE STANDARD TABLE OF ty_output WITH KEY mblnr mjahr, it_mseg_103 TYPE STANDARD TABLE OF ty_mseg_103, it_mseg_105 TYPE STANDARD TABLE OF ty_mseg_103, it_mseg_106 TYPE STANDARD TABLE OF ty_mseg_103, " LNTDEV1 ADDED it_mara TYPE STANDARD TABLE OF ty_mara, it_ekko TYPE STANDARD TABLE OF ty_ekko, it_ekpo TYPE STANDARD TABLE OF ty_ekpo, it_makt TYPE STANDARD TABLE OF ty_makt, it_mkpf TYPE STANDARD TABLE OF ty_mkpf WITH KEY mblnr. *----------------------------------------------------------------------* * WORKAREAS: Begin with WA_ * *----------------------------------------------------------------------* DATA : wa_fcat LIKE LINE OF it_fcat. DATA : wa_output LIKE LINE OF it_output, wa_mara LIKE LINE OF it_mara, wa_ekko LIKE LINE OF it_ekko, wa_ekpo LIKE LINE OF it_ekpo, wa_makt LIKE LINE OF it_makt, wa_mkpf LIKE LINE OF it_mkpf, wa_mseg_103 LIKE LINE OF it_mseg_103, wa_mseg_105 LIKE LINE OF it_mseg_105, wa_mseg_106 LIKE LINE OF it_mseg_106. " LNTDEV1 ADDED DATA : w_menge LIKE ekpo-menge. *----------------------------------------------------------------------* * FIELD-SYMBOLS: Begin with FS_ * *----------------------------------------------------------------------* *----------------------------------------------------------------------* * PARAMETERS: Begin with PR_ * *----------------------------------------------------------------------* SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME. PARAMETERS : pr_werks LIKE mseg-werks OBLIGATORY. SELECT-OPTIONS : so_budat FOR mkpf-budat. SELECTION-SCREEN END OF BLOCK b1. *----------------------------------------------------------------------* * SELECT-OPTIONS: Begin with SO_ * *----------------------------------------------------------------------* SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME. SELECT-OPTIONS : so_mtart FOR mara-mtart. SELECT-OPTIONS : so_matkl FOR mara-matkl. SELECT-OPTIONS : so_matnr FOR mara-matnr. SELECT-OPTIONS : so_bismt FOR mara-bismt. SELECT-OPTIONS : so_ebeln FOR mseg-ebeln. SELECT-OPTIONS : so_verkf FOR ekko-verkf. SELECT-OPTIONS : so_bedat FOR ekko-bedat. SELECT-OPTIONS : so_mblnr FOR mseg-mblnr. SELECTION-SCREEN END OF BLOCK b2. *AT SELECTION-SCREEN. * *SO_BUDAT-HIGH = SO_BUDAT-HIGH + 1. *---------------------------------------------------------------------* * Query for getting all the Document Segment: Material data from MSEG * * Table which having Movement Types 103 and put into it_mseg table, * * Filtering by reference document and material document number * * Filtering by selection criteria (Material Docu No, Purchase order * * and Plant) * *---------------------------------------------------------------------* *---------------------------------------------------------------------* START-OF-SELECTION. *---------------------------------------------------------------------* SELECT p~mblnr s~mblnr p~mjahr zeile bwart lfbnr werks lgort ebeln ebelp lfpos sjahr dmbtr usnam menge s~budat p~matnr q~mtart p~ablad p~wempf FROM mseg AS p INNER JOIN mara AS q ON p~matnr = q~matnr JOIN mkpf AS s ON s~mblnr EQ p~mblnr INTO CORRESPONDING FIELDS OF TABLE it_mseg_103 WHERE bwart EQ 103 AND p~matnr IN so_matnr AND werks EQ pr_werks AND ebeln IN so_ebeln AND q~mtart IN so_mtart AND p~mblnr IN so_mblnr AND s~budat IN so_budat. ** IF sy-subrc = 0. ENDIF. SELECT p~mblnr s~mblnr p~mjahr zeile bwart lfbnr werks lgort ebeln ebelp lfpos sjahr dmbtr usnam menge s~budat p~matnr q~mtart p~ablad p~wempf FROM mseg AS p INNER JOIN mara AS q ON p~matnr = q~matnr JOIN mkpf AS s ON s~mblnr EQ p~mblnr INTO CORRESPONDING FIELDS OF TABLE it_mseg_105 WHERE bwart EQ 105 OR bwart EQ 104 OR bwart EQ 124 OR bwart EQ 124 AND p~matnr IN so_matnr AND werks EQ pr_werks AND ebeln IN so_ebeln AND q~mtart IN so_mtart AND p~mblnr IN so_mblnr AND s~budat IN so_budat. IF sy-subrc = 0. ENDIF. LOOP AT IT_MSEG_103 INTO WA_MSEG_103. READ TABLE IT_MSEG_105 INTO WA_MSEG_105 WITH KEY LFBNR = WA_MSEG_103-MBLNR. IF SY-SUBRC 0. APPEND WA_MSEG_103 TO IT_OUTPUT. ENDIF. ENDLOOP. SELECT p~mblnr s~mblnr p~mjahr zeile bwart lfbnr werks lgort ebeln ebelp lfpos sjahr dmbtr usnam menge s~budat p~matnr q~mtart p~ablad p~wempf FROM mseg AS p INNER JOIN mara AS q ON p~matnr = q~matnr JOIN mkpf AS s ON s~mblnr EQ p~mblnr INTO CORRESPONDING FIELDS OF TABLE it_mseg_106 WHERE bwart EQ 106 AND p~matnr IN so_matnr AND werks EQ pr_werks AND ebeln IN so_ebeln AND q~mtart IN so_mtart AND p~mblnr IN so_mblnr AND s~budat IN so_budat. LOOP AT it_mseg_103 INTO wa_mseg_103. READ TABLE it_mseg_106 INTO wa_mseg_106 WITH KEY lfbnr = wa_mseg_103-mblnr. IF sy-subrc = 0. APPEND wa_mseg_106 TO it_output. clear wa_mseg_106. ENDIF. ENDLOOP.
Present selection criteria is 1. Plant 2. Posting Date 3. Material Type 4. Material Group 5. Material No. 6. Old Material No. 7. PO Order No. 8. Old PO No. 9. PO Date 10. Material Document No. New selection criteria is 1. PO no. 2. PO date 3. PO value (Ex: >=10000 <=50000) 4. Purch org 5. Doc type 6. Vendor 7. Material 8. Account assignment category 9. Order
I hope the code is readable now.
Thanks in advance. -
How to run select query from one SID to fetch record from another SID?
I have two SID, one is AAA and another is BBB and the connection name is A1 and B1 respectively. I want to run one select query from A1 connection to fetch record B1. Can anyone help me how I can connect from one SID to another SID?
Hi 869357,
You can also use the copy command. (Works with limited types)
try:
help copyRegards,
Turloch -
How to write selection Query for the following requirment.
Hi All,
I am new to ABAP, I need a help ,
I need to select all plants(WERKS) from MARC at Plant/Material level,
then I need to take all sales organozation(VKORG) from T001w,
then I need the company code(BUKRS) from TVKO based on VKORG,
then I need the currency key(WAERS) from T001 based on BUKRS,
Can any one help me in writing selection Query for the same?
Thanks All,
Debrup.Hi,
Its easy for you if you learn SELECT with JOIN to complete your task. So SEARCH the forum with SELECT statement and you will get a lot of examples using which you can write your own.
If you struck up anywhere revert back.
Regards
Karthik D -
How to store select query output in arrays.
i created a varray type variables and now want to assign some data though select query output in pl/sql code as well as in reports 6i.
You're in the wrong forum (this one is for issues with the SQL Developer tool). You were in the right one where you posted first, but don't reuse unrelated threads as you did.
Regards,
K. -
How to LOOP SELECT Query in Oracle?
Hello
I have a query which use a variable (:enddate) and returns a set of data. Lets assume it gives sales totals by account for a given month (specified using the variable :enddate). So the query would look something like :
SELECT ACCOUNT_NO, SUM(SALE_AMT) TOTAL_SALES FROM SCHEMA1.SALES_LINES, :ENDDATE as END_DATE WHERE SALE_DATE BETWEEN addmonths(:ENDDATE,-1) and :ENDDATE
GROUP BY ACCOUNT_NO
Now, currently, I have to run this query each time I need results for a particular date, and thats OK but the problem arises when I need to run this query for an entire year. Then I have to run the query 12 times, passing the date variable each time and re-running the query.
What I would like to do, is to create a loop which covers a time period entered by the user (e.g. user enters 12 months as the total time and 1 month as the period, and the query automatically runs for the previous 12 months, passing :enddate 1 month at a time and then presents the combined results) OR takes a bunch of dates entered in an array, (e.g. :enddate in (JAN-31-2009, Feb-28-2009, MAR-31-2009, APR-30,2009 etc) and runs the query for each of them and presents the results together in one set.
How can I do this using a simple PLSQL loop? I would like to avoid creating a procedure if possible. Sample code will be much appreciated.
BTW, I am using Oracle XE 10g.
ThanksHi,
It will be more efficient to get all the results at once, rather than running 12 separate queries.
You can do that without loops or PL/SQL, something like this:
SELECT account_no
, TRUNC (sale_date, 'MONTH') AS mnth
, SUM (sale_amt) AS total_sales
FROM schema1.sales_lines
WHERE sale_date > ADD_MONTHS (:enddate, -12)
AND sale_date <= :enddate
GROUP BY account_no
, TRUNC (sale_date, 'MONTH')
;Of course, you can do a query like this within PL/SQL if you need to.
If you'd care to post some sample data (CREATE TABLE and INSERT statements) and the results you want from that data, then I could test it.
Simplify as much as possible. For example, instead of 12 sepaarte months, you can post just 2 or 3. Adapting the solution for 12 months should be easy.
In pure SQL, bind varibales can't be DATEs, but you could define the bind variable as a string and use TO_DATE to convert it to a DATE, OR use a substitution variable instead of a bind variable.
The solution above works in all versions Oracle. The way you're using :enddate in the FROM clause, and a function called addmonths (rather than ADD_MONTHS) makes me wonder if you're using Oracle at all. -
How to write select query for all the user tables in database
Can any one tell me how to select the columns from all the user tables in a database
Here I had 3columns as input...
1.phone no
2.memberid
3.sub no.
I have to select call time,record,agn from all the tables in a database...all database tables have the same column names but some may have additional columns..
Eg: select call time, record,agn from ah_t_table where phone no= 6186759765,memberid=j34563298
Query has to execute not only for this table but for all user tables in the database..all tables will start with ah_t
I am trying for this query since 30days...
Help me please....any kind of help is appreciated.....Hi,
user13113704 wrote:
... i need to include the symbol (') for the numbers(values) to get selected..
eg: phone no= '6284056879'To include a single-quote in a string literal, use 2 or them in a row, as shown below.
Starting in Oracle 10, you can also use Q-notation:
http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/sql_elements003.htm#i42617
...and also can you tell me how to execute the output of this script. What front end are you using? If it's SQL*Plus, then you can SPOOL the query to a file, and then execute that file, like this:
-- Suppress SQL*Plus features that interfere with raw output
SET FEEDBACK OFF
SET PAGESIZE 0
-- Run preliminary query to generate main query
SPOOL c:\my_sql_dir\all_ah_t.sql
SELECT 'select call time, record, agn from '
|| owner
|| '.'
|| table_name
|| ' where phone_no = ''6186759765'' and memberid = j34563298'
|| CASE
WHEN ROW_NUMBER () OVER ( ORDER BY owner DESC
, table_name DESC
) = 1
THEN ';'
ELSE ' UNION ALL'
END AS txt
FROM all_tables
WHERE SUBSTR (table_name, 1, 4) = 'AH_T'
ORDER BY owner
, table_name
SPOOL OFF
-- Restore SQL*Plus features that interfere with raw output (if desired)
SET FEEDBACK ON
SET PAGESIZE 50
-- Run main query:
@c:\my_sql_dir\all_ah_t.sql
so that i form a temporary view for this script as a table(or store the result in a temp table) and my problem will be solved..Sorry, I don't understand. What is a "temporary view"? -
How to write select query for this
Hi,
I had a html form and in the for i had drop down box and it needs to select multiple values from the drop down box. When i select multiple values then i have to write the SQL select statement query .
When i try to write the select statement and trying to run i am getting error.
select * from Table
where emo_no = '1,2,3'
this is how i write query please suggest me how to write query for selecting multiple values from the drop down box.
Thanksselect * from Table
where emo_no in ( 1,2,3)
for integer values
select * from Table
where emo_no in ('1','2','3')
for characters
If we talk about large scale applications that may have millions of records, I would suggest this.
declare @t table (v int)
insert into t (v) values (1)
insert into t (v) valves (2)
insert into t (v) values (3)
select *
from table
inner join @t t on table.emo_no = t.v
Using "in" for a where clause is not so bad for filtering on a few values, but if you are filtering a lot of rows and a lot of values (emo_no) the performance degrades quickly for some reasons beyond the scope of this.
This is just one solution, I'll through this out as well, instead of an in memory (@t) table, doing a disk based temp table (#t) and creating an index on the column "v".
create table #t (v int)
insert into #t (v) values (1)
insert into #t (v) valves (2)
insert into #t (v) values (3)
create index ix_t on #t (v)
select *
from table
inner join #t t on table.emo_no = t.v
drop table #t
Pardon any syntax errors and careful using a drop statement.
Sometimes in memory tables work better than disk temp tables, it takes some testing and trial and error depending on your datasets to determine the best solution.
Probably too much info ;-)
Byron Mann
[email protected]
[email protected]
Software Architect
hosting.com | hostmysite.com
http://www.hostmysite.com/?utm_source=bb -
How to get select query for crystal report in c# code
In C#, (Visual Studio 2008 ) I need to use the native Crystal Reports objects to get the complete SQL statement as it appears when you view it in Crystal Reports.
In Crystal Reports 12.0, Go to "Database > Show SQL Query..." ... That is what I need to get via C# code. I can get bits and pieces of parameter info etc but I just want the whole SQL statement.
How to get this?Hi Ganesh,
It's simple to use RAS, include the assemblies and use this to open your report document:
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportAppServer.ClientDoc;
using CrystalDecisions.ReportAppServer.Controllers;
using CrystalDecisions.ReportAppServer.ReportDefModel;
using CrystalDecisions.ReportAppServer.DataSetConversion;
using CrystalDecisions.ReportAppServer.DataDefModel;
CrystalDecisions.CrystalReports.Engine.ReportDocument rpt = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
ISCDReportClientDocument rptClientDoc;
To open the report use:
object rptName = openFileDialog.FileName;
rpt.Load(rptName.ToString());
rptClientDoc = rpt.ReportClientDocument;
Then to get the SQL set the log on info:
rptClientDoc.DatabaseController.LogonEx("10.50.212.77,1433", "xtreme", "sa", "password");
GroupPath gp = new GroupPath();
string tmp = String.Empty;
rptClientDoc.RowsetController.GetSQLStatement(gp, out tmp);
MessageBox.Show(tmp, "Data Source Set and SQL Statement", MessageBoxButtons.OK, MessageBoxIcon.Information);
Thank you
Don -
How to improve 'select query with subselects'?
Oracle Database 11g Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0Hi there,
I'm a C# developer so I really have troubles with understanding PL/SQL. I use a lot of subselects in my queries because I find it easy to understand, however, I need to learn to make use of the power of queries. I made this little library sample to illustrate the problem I have in a smaller scale. I have multiple subselects looking in the same table, running through the same amount of columns to obtain data. This is a waste of performance and resources.
Idea of the query:
Information about a book
- Location of this book
- Number of unique readers of this book
- Number of days it was rent
- Number of reservation of this book
Test data
with
book as (
select 1 as id, 'reference0001' as reference, 'titel0001' as title, 'description0001' as description, 'author0001' as author, 1 as locid from dual union all
select 2 as id, 'reference0002' as reference, 'titel0002' as title, 'description0002' as description, 'author0002' as author, 1 as locid from dual union all
select 3 as id, 'reference0003' as reference, 'titel0003' as title, 'description0003' as description, 'author0003' as author, 1 as locid from dual union all
select 4 as id, 'reference0004' as reference, 'titel0004' as title, 'description0004' as description, 'author0004' as author, 2 as locid from dual union all
select 5 as id, 'reference0005' as reference, 'titel0005' as title, 'description0005' as description, 'author0005' as author, 2 as locid from dual union all
select 6 as id, 'reference0006' as reference, 'titel0006' as title, 'description0006' as description, 'author0006' as author, 2 as locid from dual union all
select 7 as id, 'reference0007' as reference, 'titel0007' as title, 'description0007' as description, 'author0007' as author, 3 as locid from dual union all
select 8 as id, 'reference0008' as reference, 'titel0008' as title, 'description0008' as description, 'author0008' as author, 3 as locid from dual
location as (
select 1 as id, '100.1' as roomnumber from dual union all
select 2 as id, '100.2' as roomnumber from dual union all
select 3 as id, '100.3' as roomnumber from dual
client as (
select 1 as id, 'client001' as name from dual union all
select 2 as id, 'client002' as name from dual union all
select 3 as id, 'client003' as name from dual
book_history as (
select 1 as bookid, 1 as clientid, 10 as days, to_date('06-06-2012') as rentdate from dual union all
select 3 as bookid, 1 as clientid, 5 as days, to_date('06-06-2012') as rentdate from dual union all
select 2 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012') as rentdate from dual union all
select 1 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012') as rentdate from dual union all
select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012') as rentdate from dual union all
select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012') as rentdate from dual union all
select 3 as bookid, 1 as clientid, 3 as days, to_date('09-06-2012') as rentdate from dual
reservation_history as (
select 1 as bookid, 1 as clientid, to_date('04-06-2012') as reservationdate from dual union all
select 3 as bookid, 1 as clientid, to_date('04-06-2012') as reservationdate from dual union all
select 2 as bookid, 2 as clientid, to_date('05-06-2012') as reservationdate from dual union all
select 1 as bookid, 2 as clientid, to_date('05-06-2012') as reservationdate from dual union all
select 2 as bookid, 1 as clientid, to_date('06-06-2012') as reservationdate from dual union all
select 2 as bookid, 1 as clientid, to_date('06-06-2012') as reservationdate from dual union all
select 3 as bookid, 1 as clientid, to_date('07-06-2012') as reservationdate from dual
)Normally my queries look like this
select b.id,
b.reference,
b.title,
b.description,
b.author,
l.roomnumber,
select count(distinct bh.clientid)
from book_history bh
where bh.bookid = b.id
and bh.rentdate >= to_date('01-01-2012')
and bh.rentdate < to_date('01-01-2013')
) as different_readers,
nvl((
select sum(bh.days)
from book_history bh
where bh.bookid = b.id
and bh.rentdate >= to_date('01-01-2012')
and bh.rentdate < to_date('01-01-2013')
),0) as days_of_rent,
select count(1)
from reservation_history rh
where rh.bookid = b.id
and rh.reservationdate >= to_date('01-01-2012')
and rh.reservationdate < to_date('01-01-2013')
) as times_reserved
from book b
join location l
on b.locid = l.id I tried to improve it to this
select b.id,
b.reference,
b.title,
b.description,
b.author,
l.roomnumber,
nvl(t1.clients, 0) as different_readers,
nvl(t1.days, 0) as days_of_rent,
select count(1)
from reservation_history rh
where rh.bookid = b.id
and rh.reservationdate >= to_date('01-01-2012')
and rh.reservationdate < to_date('01-01-2013')
) as times_reserved
from book b
left join location l
on b.locid = l.id
left join (select bh.bookid as bookid,
count(distinct bh.clientid) as clients,
sum(bh.days) as days
from book_history bh
where bh.rentdate >= to_date('01-01-2012')
and bh.rentdate < to_date('01-01-2013')
group by bh.bookid
) t1
on t1.bookid = b.id I am told that I should try to move out the group by from the last left join or try to use partitioning.
Now this is a bit too much for my understanding. When I use partitioning
select distinct
b.id,
b.reference,
b.title,
b.description,
b.author,
l.roomnumber,
nvl(count(distinct bh.clientid) over (partition by bh.bookid), 0) as different_readers,
nvl(sum(bh.days) over (partition by bh.bookid),0) as days_of_rent,
select count(1)
from reservation_history rh
where rh.bookid = b.id
and rh.reservationdate >= to_date('01-01-2012')
and rh.reservationdate < to_date('01-01-2013')
) as times_reserved
from book b
left join location l
on b.locid = l.id
left join book_history bh
on b.id = bh.bookid
and bh.rentdate >= to_date('01-01-2012')
and bh.rentdate < to_date('01-01-2013')I suppressed same result rows by using distinct, however I am told to avoid using distinct as this slows down a lot.
Is there any other solution I can use to achieve the same?
I hope my question is clear enough to be answered! Thanks in advance for your time!
Kind regards,
Metroickha
Edited by: 858378 on 6-feb-2013 2:37Thanks for providing the data in a usable format, that really helps.
858378 wrote:
I am told that I should try to move out the group by from the last left join or try to use partitioning.
Now this is a bit too much for my understanding. When I use partitioning
select b.id,
b.reference,
b.title,
b.description,
b.author,
l.roomnumber,
count(distinct bh.clientid) over (partition by bh.bookid) as different_readers,
nvl(sum(bh.days) over (partition by bh.bookid),0) as days_of_rent
from book b
left join location l
on b.locid = l.id
left join book_history bh
on b.id = bh.bookid
and bh.rentdate >= '01-01-2012'
and bh.rentdate < '01-01-2013'I get multiple rows with the same values (as expected). So what do I need to add to remove the multiple same rows?Use a DISTINCT...
SQL> ed
Wrote file afiedt.buf
1 with book as (
2 select 1 as id, 'reference0001' as reference, 'titel0001' as title, 'description0001' as description, 'author0001' as author, 1 as locid from dual union all
3 select 2 as id, 'reference0002' as reference, 'titel0002' as title, 'description0002' as description, 'author0002' as author, 1 as locid from dual union all
4 select 3 as id, 'reference0003' as reference, 'titel0003' as title, 'description0003' as description, 'author0003' as author, 1 as locid from dual union all
5 select 4 as id, 'reference0004' as reference, 'titel0004' as title, 'description0004' as description, 'author0004' as author, 2 as locid from dual union all
6 select 5 as id, 'reference0005' as reference, 'titel0005' as title, 'description0005' as description, 'author0005' as author, 2 as locid from dual union all
7 select 6 as id, 'reference0006' as reference, 'titel0006' as title, 'description0006' as description, 'author0006' as author, 2 as locid from dual union all
8 select 7 as id, 'reference0007' as reference, 'titel0007' as title, 'description0007' as description, 'author0007' as author, 3 as locid from dual union all
9 select 8 as id, 'reference0008' as reference, 'titel0008' as title, 'description0008' as description, 'author0008' as author, 3 as locid from dual
10 ),
11 location as (
12 select 1 as id, '100.1' as roomnumber from dual union all
13 select 2 as id, '100.2' as roomnumber from dual union all
14 select 3 as id, '100.3' as roomnumber from dual
15 ),
16 client as (
17 select 1 as id, 'client001' as name from dual union all
18 select 2 as id, 'client002' as name from dual union all
19 select 3 as id, 'client003' as name from dual
20 ),
21 book_history as (
22 select 1 as bookid, 1 as clientid, 10 as days, to_date('06-06-2012','DD-MM-YYYY') as rentdate from dual union all
23 select 3 as bookid, 1 as clientid, 5 as days, to_date('06-06-2012','DD-MM-YYYY') as rentdate from dual union all
24 select 2 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012','DD-MM-YYYY') as rentdate from dual union all
25 select 1 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012','DD-MM-YYYY') as rentdate from dual union all
26 select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012','DD-MM-YYYY') as rentdate from dual union all
27 select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012','DD-MM-YYYY') as rentdate from dual union all
28 select 3 as bookid, 1 as clientid, 3 as days, to_date('09-06-2012','DD-MM-YYYY') as rentdate from dual
29 )
30 select distinct
31 b.id
32 ,b.reference
33 ,b.title
34 ,b.description
35 ,b.author
36 ,l.roomnumber
37 ,count(distinct bh.clientid) over (partition by b.id) as different_readers
38 ,nvl(sum(bh.days) over (partition by b.id),0) as days_of_rent
39 from book b
40 join location l on (b.locid = l.id)
41 left outer join book_history bh on ( bh.bookid = b.id
42 and bh.rentdate >= to_date('01-01-2012','DD-MM-YYYY')
43 and bh.rentdate < to_date('01-01-2013','DD-MM-YYYY')
44 )
45* order by 1
SQL> /
ID REFERENCE TITLE DESCRIPTION AUTHOR ROOMN DIFFERENT_READERS DAYS_OF_RENT
1 reference0001 titel0001 description0001 author0001 100.1 2 15
2 reference0002 titel0002 description0002 author0002 100.1 2 15
3 reference0003 titel0003 description0003 author0003 100.1 1 8
4 reference0004 titel0004 description0004 author0004 100.2 0 0
5 reference0005 titel0005 description0005 author0005 100.2 0 0
6 reference0006 titel0006 description0006 author0006 100.2 0 0
7 reference0007 titel0007 description0007 author0007 100.3 0 0
8 reference0008 titel0008 description0008 author0008 100.3 0 0
8 rows selected.
SQL>also ensure you treat dates as dates and specify a format mask when using TO_DATE. -
How to avoid select query in loop
Hi All,
I have to select entries from table iclpay looping it_dfkkop and delete the entries in it_dfkkop_tmp which are not present in iclpay.
For this If use below code its working fine, but there is performance issue
CLEAR: it_iclpay[],it_iclpay.
LOOP AT it_dfkkop_tmp.
SELECT claim documentref paycat status FROM iclpay
APPENDING CORRESPONDING FIELDS OF TABLE it_iclpay
WHERE
active = c_active AND
claim = it_dfkkop_tmp-claim AND
documentref = it_dfkkop_tmp-opbel.
IF sy-subrc NE 0.
DELETE it_dfkkop_tmp.
ENDIF.
ENDLOOP.
If I write the code like below its not working
SELECT claim documentref paycat status FROM iclpay
INTO TABLE it_iclpay
FOR ALL ENTRIES IN it_dfkkop_tmp WHERE
active = c_active AND
claim = it_dfkkop_tmp-claim AND
documentref = it_dfkkop_tmp-opbel.
Can anyone help on this.It did not dump but did not get final(required) output.
I got the solution now.
Thanks a lot for responding.
I have written the below code
SELECT claim
documentref
paycat
status
FROM
iclpay
APPENDING CORRESPONDING FIELDS OF TABLE it_iclpay
FOR ALL ENTRIES IN it_dfkkop_tmp WHERE
active = c_active AND
claim = it_dfkkop_tmp-claim AND
documentref = it_dfkkop_tmp-opbel.
SORT it_dfkkop_tmp BY claim opbel.
LOOP AT it_dfkkop_tmp.
READ TABLE it_iclpay INTO wa_iclpay WITH KEY
claim = it_dfkkop_tmp-claim
documentref = it_dfkkop_tmp-opbel.
IF sy-subrc NE 0.
DELETE it_dfkkop_tmp.
ENDIF.
ENDLOOP. -
Hi All,
I need to modify the SQL for retrieving assets from assets tab and assets in merchandising in 10.1.2. I found the class for SQLQuery builder, but I want to change the SQL.
Could you anyone please how to solve this.
Thanks & Regards,
BalaHi All,
I need to modify the SQL for retrieving assets from assets tab and assets in merchandising in 10.1.2. I found the class for SQLQuery builder, but I want to change the SQL.
Could you anyone please how to solve this.
Thanks & Regards,
Bala -
How to write select query for below requirement
Hi sir,
i have a table x have one column y which containing value like below
Y
a
b
c
d
I want out put like below kindly help me:
Y
a
d
c
bHi ,
Please check:
select y from (
select case when y='a' then 1 else 2 end no, y from table_x
) order by no asc, y desc
with table_x as(
select 'a' y
from dual
union
select 'b' y
from dual
union
select 'c' y
from dual
union
select 'd' y
from dual
select y from (
select case when y='a' then 1 else 2 end no, y from table_x
) order by no asc, y desc
Thank you -
How to get result of select query from oracle in VC
Dear All ,
I have a application in oracle which insert the data in the oracle database table.
Now i want to show all the data that has been inserted into the database table in my VC application but i don't know how to handle select query in VC.
Regards
KrishanHi Goivndu
Thanks for your reply .
I know all those things.
I have created the system & alias for my backend oracle system.
I can also see all the stored procedure that are there in my oracle system.
I just want to know how to write a select query in a stored procedure .
you can insert data , Update data through oracle procedure but i don't think there is any way to get the result of select query in stored procedure .
If you know any way to do that please do let me know .
Regards
Krishan -
Query in Select query operation
Please excuse me for posting this again.
I'm stuck up with this for 3 days. Lenghty post pls do read and help me.
How is the select query going to be changed in the SAP so that the original database behind it understands it. I want to know the operation of IN operation in where clause where in we can use ranges.
I'm asking this because I'm facing a problem using Native SQL.
Query is like this:
EXEC SQl.
OPEN C1 for
SELECT field1 from table@domain where field2 IN :ABAP_VAR.
ENDEXEC.
EXEC SQL.
FETCH C1 NEXT INTO :VAR1 , :VAR2
ENDEXEC.
Oracle usually recognises this statement IN ( '2222' , '3658' , '6895' )
But here this field that is build dynamically like this is not recognising them as seperate fileds but instead it is taking them as a single string and not selecting anything.
But if a single value is passed it is fetching the data into the cursor. And the code is working fine.
Any help in this is highly appreciable."if a in r_range" is is effectively: does the value held in 'a' comply with the values held in range 'r_range'. Ranges can hold NE, patterns etc so this can get quite complex.
You can see that it is very different from the SQL type IN.
If your range hold several values you could unstrip it into a string varaible so that it ended up having a value something like: ( '2222' , '3658' , '6895' ). Not sure if you need the '(' but it would be something like this:
eg,
assuming your range is simple and only holds 'I' and 'EQ' values:
data v_string type string.
loop at r_range.
concatenate v_string '@''' r_range-low '''@' into v_string.
endloop.
REPLACE
ALL OCCURRENCES OF '@'
IN v_string
WITH ' '.
Then you can use:
SELECT field1 from table@domain where field2 IN :v_string.
Maybe you are looking for
-
My iphone 4 will not back up to icloud
how do i get my phone to back up to back up to icloud when in my itunes i have it set to back up to icloud and on my phone its on but ye it still wont back up to icloud?
-
Unexpected power off now mac mini want boot up pass the grey screen
i have a mac mini the power went off when it came back on and i tryd to cut mini back on it only went to gray apple screen them goes out the monitor says not supported i need to get class assignments off of it.
-
Error when trying to activate the NI Fbus Ver 4
I have tryed the online activation, fill out all the info then when i hit continue i get an error, i then tried the phone activation line gave all my info and it said technical error, i then stayed on the line and talked to someone who also tryed it
-
Inspection Lot for Material Group (without Material Code)
Hello, We need to create Inspection Lots (either manually or automatically while Goods Receipt) without particular material reference. Scenario is that Vendors send the materials (as text, through some material group) and we need to inspect these mat
-
Hi all, I m using 10gr2 (10.2.0.4.0) database as standalone and using oracle lite r3 (10.3.0.2) I have an application created with oracle branch office platform. I downloaded a client in a mobile device and started doing data entry. I made an entry i