Several count() statements from same table?
Hello all,
I have to count number of bills (several million entries) connected to contracts (aprox. 75000 entries). When I try
select contract.number,count(bill.id),count(a.id)
from contract,bill, bill a
where bill.contractid = contract.id
and bill.type = 0
and a.contractid = contract.id
and a.type = 1
the result of count(a.id) is always the same as count(bill.id). Well knowing that this is wrong, I've tried to change the count() into own select-statments but that doesn't help and the performance is more than poor.
Does anybody have an idea to get the correct count-results with a suitable performance?
Thanks Carsten
Hi, Carsten,
Instead of joing separate copies of the bill table for each type (for which you might need outer joins), just join one copy, on the broadest possible conditions.
You can use CASE expressions to count which rows joined on different criteria, like this:
SELECT contract.contract_number
, COUNT (CASE WHEN bill.type = 0 THEN bill.id END) AS type_0_cnt
, COUNT (CASE WHEN bill.type = 1 THEN bill.id END) AS type_1_cnt
FROM contract
, bill
WHERE contract.id = bill.contractid
AND bill.type IN (0, 1)
GROUP BY contract.contract_number
;Output:
CONTRACT_NUMBER TYPE_0_CNT TYPE_1_CNT
contract 1 4 2Do you really want 1 in the last column? If so, explain how you get that value.
NUMBER is a reserver word in Oracle, so it's not a good column name.
Edited by: Frank Kulash on Nov 30, 2009 9:38 AM
Edited by: Frank Kulash on Nov 30, 2009 9:52 AM
Changed "THEN 1" to 'THEN bill.id", after Bhushan
Similar Messages
-
How to count records from 2 tables and show in RDLC Report
hi all,
its being a one day searching for the solution but No Luck.
I have two SQL tables tblstudetail and tblfeereceiptdetail.
i just want to count records from both tables and show in RDLC report.
I tried SQl Query Like This:
select a.session, a.course,
Count(CASE a.ADstatus WHEN 'OK' THEN 1 ELSE 0 END ) AS Admission,
Count(CASE s .I_receiptstatus WHEN 'OK' THEN 1 ELSE 0 END) AS Feeprint
from
tblstudetail a
FULL join
tblfeereceiptdetail s on s.studentID = a.studentID
where a.session = '2015' AND s.Fsession = '2015' AND a.adcat = 'Regular'
GROUP BY a.session,a.course
ORDER by a.course
The result Show the Same Value in Both columns
Session Course Admission FeeDetail
2015 B.A. I 275 275
2015 B.A. II 307 307
2015 B.A. III 255 255
2015 B.Sc. I 110 110
2015 B.Sc. II 105 105
2015 B.Sc. III 64 64
Actully I want to Count How many ADMISSION have been Taken(FROM tblstudetail) and How many FEE RECEIPT have been Print (From tblfeereceiptdetail).
please guide me for this as soon as possible.
thanks in advance...I am counting 'OK' in both the table columns I.e 'ADstatus' in tblstudetail and 'feereceiptstatus' in tblfeereceiptdetail
please suggest me -
Display Several Sales Orders from Same Business Customer TOGETHER in MD04
Hello
In MD04 is it possible to display several sales orders from same business customer together?
For instance one business customer order bikes for two times.
The Order-No are 12912(50st by 07/08/09) and 12913(100st by 10/08/09).
Normally after runnning of MRP(MD02), the planned orders are displayed seperately in MD04.
I want to display two orders together.
The reason is if some trouble happens in Production(for Ord-no 12912) and it cannot be deliveried on schedule, however Production (for Ord-no 12913) is going without trouble, the final-products for order 12913 should move for 12912.
If you have some good solutions, please share it.
Thank you for your advice.
Regards,Hi Riyolshibashi,
Lets look at the two main scenarios, MTS and MTO.
In MTS, the general display filters are available in MD04. Standard SAP filters do not filter on 'customer'. I suspect this is because the concept would normally be illogical in a MTS environment. Planned orders do not exist to serve a customer, but to serve 'stock'. But let us not think about SAP and their logic.
SAP allows you to create customized filters, in IMG>Production>MRP>Evaluation>Filter>Define Display filter. Within this definition, under the "Addnl Selections' tab, you can tick 'Define addl selections in the transaction'. When you use this filter in MD04 display, then, one of the options available to you is to enter a specific Customer number, and then the display will only show the requirements from this customer. Since you have excluded all other customer's orders, all orders of a single customer will be displayed consecutively. Next to each other.
In the case of MTO, the 'filter-by-customer' concept makes more sense (although, not for the reason you have stated. You would have to find some manual method to convert a production order from supporting one sales order item, to supporting another sales order item. Possible but difficult). Anyway, the above mentioned customized filter would also work in an MTO environment.
Regards,
DB49 -
Combine 2 Queries (from SAME table) into a SINGLE query
I have this two queries (from SAME table), and want to combine into one SINGLE query, how?
How can we use CASE WHEN THEN for such situation?
Query1:
SELECT t_inner.*,
Floor(t_inner.ProductiveTime/ 3600) || 'hr:' || LPAD(Floor(Mod(t_inner.ProductiveTime,3600) / 60),2,0) || 'min:' AS Productive_Time,
Floor(t_inner.OperatorDownTime/ 3600) || 'hr:' || LPAD(Floor(Mod(t_inner.OperatorDownTime,3600) / 60),2,0) || 'min:' AS OperatorDown_Time
FROM
(SELECT SYSTEMTYPE,
sum(TIME_TEST + TIME_STEP) AS ProductiveTime,
sum(TIME_IDLE) AS OperatorDownTime
FROM PFODS.PPL_TESTSYSTEMS_UTILISATION
WHERE (SYSTEMTYPE = '0005-072')
AND (TS_START >= to_date('13/01/2014', 'DD/MM/YYYY'))
AND TS_End <= to_date('17/01/2014', 'DD/MM/YYYY') + 1 + (1/1440) +(59/86400)
AND MONO != '9999999999'
GROUP BY SYSTEMTYPE ) t_inner
Query 2:
SELECT t_inner.*,
Floor(t_inner.MachineDownTime/ 3600) || 'hr ' || LPAD(Floor(Mod(t_inner.MachineDownTime,3600) / 60),2,0) || 'min' AS MachineDown_Time
FROM
(SELECT SYSTEMTYPE,
sum(TIME_IDLE) AS MachineDownTime
FROM PFODS.PPL_TESTSYSTEMS_UTILISATION
WHERE (SYSTEMTYPE = '0005-072')
AND (TS_START >= to_date('13/01/2014', 'DD/MM/YYYY'))
AND TS_End <= to_date('17/01/2014', 'DD/MM/YYYY') + 1 + (1/1440) +(59/86400)
AND MONO = '9999999999'
GROUP BY SYSTEMTYPE) t_inner
see http://postimg.org/image/koq87iyyz/ and
http://postimg.org/image/fv3zxa38nwith the first query,
SELECT t_inner.*,
Floor(t_inner.ProductiveTime/ 3600) || 'hr:' || LPAD(Floor(Mod(t_inner.ProductiveTime,3600) / 60),2,0) || 'min' AS Productive_Time
FROM
(SELECT SYSTEMTYPE,
--sum(TIME_TEST) AS TIME_TEST,
--sum(TIME_SYSTEM) AS TIME_SYSTEM,
--sum(TIME_STEP) AS TIME_STEP,
--sum(TIME_IDLE) AS TIME_IDLE,
sum(TIME_TEST + TIME_STEP) AS ProductiveTime
FROM PFODS.PPL_TESTSYSTEMS_UTILISATION
WHERE (SYSTEMTYPE = '0005-072')
AND (TS_START >= to_date('13/01/2014', 'DD/MM/YYYY'))
AND TS_End <= to_date('17/01/2014', 'DD/MM/YYYY') + 1 + (1/1440) +(59/86400)
AND MONO != '9999999999'
GROUP BY SYSTEMTYPE) t_inner
it gives output as from
http://postimg.org/image/koq87iyyz/
with the second query,
SELECT t_inner.*,
Floor(t_inner.MachineDownTime/ 3600) || 'hr ' || LPAD(Floor(Mod(t_inner.MachineDownTime,3600) / 60),2,0) || 'min' AS MachineDown_Time
FROM
(SELECT SYSTEMTYPE,
sum(TIME_IDLE) AS MachineDownTime
FROM PFODS.PPL_TESTSYSTEMS_UTILISATION
WHERE (SYSTEMTYPE = '0005-072')
AND (TS_START >= to_date('13/01/2014', 'DD/MM/YYYY'))
AND TS_End <= to_date('17/01/2014', 'DD/MM/YYYY') + 1 + (1/1440) +(59/86400)
AND MONO = '9999999999'
GROUP BY SYSTEMTYPE) t_inner
it gives output as from
http://postimg.org/image/fv3zxa38n/
I want to come those 2 queries into a single query, such that it gives both outputs as above. Let me know if you need any other information. thanks. -
Report with multiple COUNT columns with counts from same table
I am new to discoverer so I am a bit lost.
I am working to create a report to show usage data from the eBusiness Knowledge Base. I have the query written in SQL using subqueries that is in the format:
Solution Number | Soultion Title | Solution Views | Positive Feedback | Negative Feedback
12345 ___________ Title ________ 345 ____________ 98 _______________ 34
The 'Views', 'Positive' and 'Negative' entries are stored in the same table so i am doing a count where setid=setid and usedtype=VS, then counting where usedtype=PF and usedtype=NF
In discoverer I can get the solution number, title and ONE of the totals but I can't seem to figure out how to get a COUNT for three different things from the same table in columns on the same row.
When I go to edit sheet -> select items once I select the COUNT option for the UsedType column in the table CS_KB_SET_USED_HISTS I can't select it again. I also have found now way to add a column based on an entered query.
If anyone could help it would be much appreciated.
Thanks
Edited by: Toolman21 on Dec 2, 2010 2:17 PM
added ______ to correct spacing.Hi,
You can separate the column by using a case or decode.
for example create 2 calculations:
case
when usedtype='PF'
then <you original column> --- the one contain them both
else 0
end
case
when usedtype='NF'
then <you original column> --- the one contain them both
else 0
end
after that you can create the count aggregation over those.
Tamir -
SELECT STATEMENT FROM THIS TABLES
HI FRIENDS,
PLE HELP ME REGARDING THIS.
data from 3 tables
TABLES: CUST_CONT,CONT_CAT_FOLD,CONT_FOLD_***
cust_cont_pk (IS COMMON FIELD IN TABLE I AND 3)
cont_cat_fold_pk( IS COMMON FIELD IN TABLE 2 AND 3)
HOW CAN I WRITE SELECT STATEMENT
Thanking u
suneel.Hi
Join the two tables
CUST_CONT and CONT_FOLD_*** with cust_cont_pk field and
CONT_CAT_FOLD and CONT_FOLD_*** with cont_cat_fold_pk field
see the doc
Joins are used to fetch data fast from Database tables:
Tables are joined with the proper key fields to fetch the data properly.
If there are no proper key fields between tables don't use Joins;
Important thing is that don't USE JOINS FOR CLUSTER tableslike BSEG and KONV.
Only use for Transparenmt tables.
You can also use joins for the database VIews to fetch the data.
JOINS
... FROM tabref1 [INNER] JOIN tabref2 ON cond
Effect
The data is to be selected from transparent database tables and/or views determined by tabref1 and tabref2. tabref1 and tabref2 each have the same form as in variant 1 or are themselves Join expressions. The keyword INNER does not have to be specified. The database tables or views determined by tabref1 and tabref2 must be recognized by the ABAP Dictionary.
In a relational data structure, it is quite normal for data that belongs together to be split up across several tables to help the process of standardization (see relational databases). To regroup this information into a database query, you can link tables using the join command. This formulates conditions for the columns in the tables involved. The inner join contains all combinations of lines from the database table determined by tabref1 with lines from the table determined by tabref2, whose values together meet the logical condition (join condition) specified using ON>cond.
Inner join between table 1 and table 2, where column D in both tables in the join condition is set the same:
Table 1 Table 2
A
B
C
D
D
E
F
G
H
a1
b1
c1
1
1
e1
f1
g1
h1
a2
b2
c2
1
3
e2
f2
g2
h2
a3
b3
c3
2
4
e3
f3
g3
h3
a4
b4
c4
3
|--|||--|
Inner Join
A
B
C
D
D
E
F
G
H
a1
b1
c1
1
1
e1
f1
g1
h1
a2
b2
c2
1
1
e1
f1
g1
h1
a4
b4
c4
3
3
e2
f2
g2
h2
|--||||||||--|
Example
Output a list of all flights from Frankfurt to New York between September 10th and 20th, 2001 that are not sold out:
DATA: DATE LIKE SFLIGHT-FLDATE,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID.
SELECT FCARRID FCONNID F~FLDATE
INTO (CARRID, CONNID, DATE)
FROM SFLIGHT AS F INNER JOIN SPFLI AS P
ON FCARRID = PCARRID AND
FCONNID = PCONNID
WHERE P~CITYFROM = 'FRANKFURT'
AND P~CITYTO = 'NEW YORK'
AND F~FLDATE BETWEEN '20010910' AND '20010920'
AND FSEATSOCC < FSEATSMAX.
WRITE: / DATE, CARRID, CONNID.
ENDSELECT.
If there are columns with the same name in both tables, you must distinguish between them by prefixing the field descriptor with the table name or a table alias.
Note
In order to determine the result of a SELECT command where the FROM clause contains a join, the database system first creates a temporary table containing the lines that meet the ON condition. The WHERE condition is then applied to the temporary table. It does not matter in an inner join whether the condition is in the ON or WHEREclause. The following example returns the same solution as the previous one.
Example
Output of a list of all flights from Frankfurt to New York between September 10th and 20th, 2001 that are not sold out:
DATA: DATE LIKE SFLIGHT-FLDATE,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID.
SELECT FCARRID FCONNID F~FLDATE
INTO (CARRID, CONNID, DATE)
FROM SFLIGHT AS F INNER JOIN SPFLI AS P
ON FCARRID = PCARRID
WHERE FCONNID = PCONNID
AND P~CITYFROM = 'FRANKFURT'
AND P~CITYTO = 'NEW YORK'
AND F~FLDATE BETWEEN '20010910' AND '20010920'
AND FSEATSOCC < FSEATSMAX.
WRITE: / DATE, CARRID, CONNID.
ENDSELECT.
Note
Since not all of the database systems supported by SAP use the standard syntax for ON conditions, the syntax has been restricted. It only allows those joins that produce the same results on all of the supported database systems:
Only a table or view may appear to the right of the JOIN operator, not another join expression.
Only AND is possible in the ON condition as a logical operator.
Each comparison in the ON condition must contain a field from the right-hand table.
If an outer join occurs in the FROM clause, all the ON conditions must contain at least one "real" JOIN condition (a condition that contains a field from tabref1 amd a field from tabref2.
Note
In some cases, '*' may be specified in the SELECT clause, and an internal table or work area is entered into the INTO clause (instead of a list of fields). If so, the fields are written to the target area from left to right in the order in which the tables appear in the FROM clause, according to the structure of each table work area. There can then be gaps between table work areas if you use an Alignment Request. For this reason, you should define the target work area with reference to the types of the database tables, not simply by counting the total number of fields. For an example, see below:
Variant 3
... FROM tabref1 LEFT [OUTER] JOIN tabref2 ON cond
Effect
Selects the data from the transparent database tables and/or views specified in tabref1 and tabref2. tabref1 und tabref2 both have either the same form as in variant 1 or are themselves join expressions. The keyword OUTER can be omitted. The database tables or views specified in tabref1 and tabref2 must be recognized by the ABAP-Dictionary.
In order to determine the result of a SELECT command where the FROM clause contains a left outer join, the database system creates a temporary table containing the lines that meet the ON condition. The remaining fields from the left-hand table (tabref1) are then added to this table, and their corresponding fields from the right-hand table are filled with ZERO values. The system then applies the WHERE condition to the table.
Left outer join between table 1 and table 2 where column D in both tables set the join condition:
Table 1 Table 2
A
B
C
D
D
E
F
G
H
a1
b1
c1
1
1
e1
f1
g1
h1
a2
b2
c2
1
3
e2
f2
g2
h2
a3
b3
c3
2
4
e3
f3
g3
h3
a4
b4
c4
3
|--|||--|
Left Outer Join
A
B
C
D
D
E
F
G
H
a1
b1
c1
1
1
e1
f1
g1
h1
a2
b2
c2
1
1
e1
f1
g1
h1
a3
b3
c3
2
NULL
NULL
NULL
NULL
NULL
a4
b4
c4
3
3
e2
f2
g2
h2
|--||||||||--|
Example
Output a list of all custimers with their bookings for October 15th, 2001:
DATA: CUSTOMER TYPE SCUSTOM,
BOOKING TYPE SBOOK.
SELECT SCUSTOMNAME SCUSTOMPOSTCODE SCUSTOM~CITY
SBOOKFLDATE SBOOKCARRID SBOOKCONNID SBOOKBOOKID
INTO (CUSTOMER-NAME, CUSTOMER-POSTCODE, CUSTOMER-CITY,
BOOKING-FLDATE, BOOKING-CARRID, BOOKING-CONNID,
BOOKING-BOOKID)
FROM SCUSTOM LEFT OUTER JOIN SBOOK
ON SCUSTOMID = SBOOKCUSTOMID AND
SBOOK~FLDATE = '20011015'
ORDER BY SCUSTOMNAME SBOOKFLDATE.
WRITE: / CUSTOMER-NAME, CUSTOMER-POSTCODE, CUSTOMER-CITY,
BOOKING-FLDATE, BOOKING-CARRID, BOOKING-CONNID,
BOOKING-BOOKID.
ENDSELECT.
If there are columns with the same name in both tables, you must distinguish between them by prefixing the field descriptor with the table name or using an alias.
Note
For the resulting set of a SELECT command with a left outer join in the FROM clause, it is generally of crucial importance whether a logical condition is in the ON or WHERE condition. Since not all of the database systems supported by SAP themselves support the standard syntax and semantics of the left outer join, the syntax has been restricted to those cases that return the same solution in all database systems:
Only a table or view may come after the JOIN operator, not another join statement.
The only logical operator allowed in the ON condition is AND.
Each comparison in the ON condition must contain a field from the right-hand table.
Comparisons in the WHERE condition must not contain a field from the right-hand table.
The ON condition must contain at least one "real" JOIN condition (a condition in which a field from tabref1 as well as from tabref2 occurs).
Note
In some cases, '*' may be specivied as the field list in the SELECT clause, and an internal table or work area is entered in the INTO clause (instead of a list of fields). If so, the fields are written to the target area from left to right in the order in which the tables appear in the llen in der FROM clause, according to the structure of each table work area. There can be gaps between the table work areas if you use an Alignment Request. For this reason, you should define the target work area with reference to the types of the database tables, as in the following example (not simply by counting the total number of fields).
Example
Example of a JOIN with more than two tables: Select all flights from Frankfurt to New York between September 10th and 20th, 2001 where there are available places, and display the name of the airline.
DATA: BEGIN OF WA,
FLIGHT TYPE SFLIGHT,
PFLI TYPE SPFLI,
CARR TYPE SCARR,
END OF WA.
SELECT * INTO WA
FROM ( SFLIGHT AS F INNER JOIN SPFLI AS P
ON FCARRID = PCARRID AND
FCONNID = PCONNID )
INNER JOIN SCARR AS C
ON FCARRID = CCARRID
WHERE P~CITYFROM = 'FRANKFURT'
AND P~CITYTO = 'NEW YORK'
AND F~FLDATE BETWEEN '20010910' AND '20010920'
AND FSEATSOCC < FSEATSMAX.
WRITE: / WA-CARR-CARRNAME, WA-FLIGHT-FLDATE, WA-FLIGHT-CARRID,
WA-FLIGHT-CONNID.
ENDSELECT.
reward points if useful
regards
Anji -
Creating View based on Current and previous data from same table
I am trying to create a view based on a table called “my_companies”. The table has following columns: “company_id”, “start_date”, “product_type_code”, “partner_id”
The main purpose of this view is to obtain previous company_id’s associated with a particular “partner_id“and “product_type_code”. So basically, I’m looking to create an extra column, say “prev_company_id” in the view along with all the other columns. The “previous” data can be obtained depending on the “start_date”. I’m not able to write a query successfully to do this.
Can anyone help? Thanks in advance.Hi,
Whenver you have a question, it helps to post:
(1) The version of Oracle (and any other relevant software) you're using
(2) A little sample data (just enough to show what the problem is) from all the relevant tables
(3) The results you want from that data
(4) Your best attempt so far
Executable SQL statements (like "CREATE TABLE AS ..." or "INSERT ..." statements) are best for (2).
Formatted tabular output is okay for (3). Type before and after the tabular text, to preserve spacing. The sample results should include any special cases that you foresee, such as one company having many previous companies, or vice-versa.
It sounds like might need a +self-join+, where you have two copies of the same table, as if the current companies were in one table and the previous companies were in another, even though they are all actually in the same table. -
Parent/child records from same table
I want to create a query that is a union such that the 2nd resultset is based on the 1st resultset. I have a table that has parent/child records in the same table.
Table: EVENTS
EVENT_ID
PARENT_EVENT_ID
CREATED_DATE
(other columns)
if PARENT_EVENT_ID is null then it is a parent record, else it is a child record. I want to select all parent records then union them with all the associated child records...something like this:
select * from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null -- All parents
union
select * from EVENTS where PARENT_EVENT_ID in (select EVENT_ID from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null) -- include any children of parents selected from above
This works but it's kind of ugly, I want to avoid using the sub-select in the 2nd because it is a repeat of the 1st statement, is there a way to alias the first statement and just refer to it in the 2nd query?Hi,
kev374 wrote:
Thanks, one question...
I did a test and it seems the child rows have to also satisfy the parent row's where clause, take this example:
EVENT_ID|PARENT_EVENT_ID|CREATED_DATE
2438 | (null) | April 9 2013
2439 | 2438 | April 11 2013
2440 | 2438 | April 11 2013
select * from EVENTS where CREATED_DATE < sysdate - 9
start with EVENT_ID = 2438
connect by PARENT_EVENT_ID = prior EVENT_IDSo you've changed the condition about only wanting roots and their children, and now you want descendants at all levels.
This pulls in record #2438 (per the sysdate - 9 condition) but 2439 and 2440 are not connected. Is there a way to supress the where clause evaluation for the child records? I just want to pull ALL child records associated with the parent and only want to do the date check on the parent.Since the roots (the only rows you want to exclude) have LEVEL=1, you can get the results you requested like this:
WHERE created_date < SYSDATE - 9
OR LEVEL > 1However, since you're not ruling out the grandchildren and great-grandchildren any more, why wouldn't you just say:
SELECT *
FROM events
WHERE created_date < SYSDATE - 9
OR parent_event_id IS NOT NULL;?
CONNECT BY is slow. Don't use it if you don't need it.
If you x-reference my original query:
select * from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null -- All parents
union
select * from EVENTS where PARENT_EVENT_ID in (select EVENT_ID from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null) -- include any children of parents selected from above
The 2nd select does not apply the created_date < sysdate - 90 on the children but rather pulls in all related children :)Sorry; my mistake. That's what happens when you don't post sample data, and desired results; people can't test their solutions and find mistakes like that. -
Hello,
I have a master table with several Forign Keys all of them to the same table. In order to display a column from the lookup table I'm using a select like:
select m.code,
m.lcode1, f1.label,
m.lcode2, f2.label,
m.lcode6, f6.label
from MTABLE m,
FTABLE f1,
FTABLE f2,
FTABLE f6
Where m.lcode1 = f1.code and
m.lcode2 = f1.code and
m.lcode6 = f6.codeIs this the correct and optimal way of doing it?
Thanks for the help,
Arie.Arie,
It sounds like perhaps the best way might be to create your forms/reports based upon a view instead of the underlying tables (especially if you are running 10gR1 or 10gR2).
I say this because:
1. With 10g (maybe even back in 9i?), you can create INSTEAD_OF triggers, so instead of manipulating (insert, update or delete) the view, the trigger has the code to perform the action to the underlying base tables instead.
2. This makes development easier, since all your joins are pre-defined in one place instead of re-creating them in numerous places.
3. Grants can be issued to the views, instead of the various tables, with just select privs to the lookup tables.
4. Change your join conditions to use the JOIN syntax, LEFT OUTER JOIN, etc., whatever is appropriate for your query. It's much easier to (eventually) figure out and maintain when you have multiple join conditions (especially against multiple tables), when any or all of them could null.
For details on the JOIN syntax or the INSTEAD_OF triggers, try the on-line Oracle documentation (I'm at home today and don't have all my reference material handy).
I'm using the above with great success (so far). I'm swamped with different work projects and different bosses and different priorities, so anything that makes my life easier is worth a couple hours of research, even if I have to do it from home in my off-time (if I can fit it into my schedule). I've been working on an HTMLDB (AppEx) application for over a year now, but I've probably only spent about 40-60 hours actually working on it, so I'm always forgetting most of what I've previously learned and done.
Bill Ferguson -
Count rows from multiple tables using SQL only
Hi, I know this has probably been answered before, but I couldn't find the answer anywhere. Please help.
I'd like count(*) [rows] for all tables in database using SQL only - no PL/SQL
The result should be something like:
Table RowCount
DBA_TABLES 1000
DBA_USERS 50
etc.
Thanks!offcource write this script:
create or replace procedure count_tables (ip_schema VARCHAR2)
is
lv_owner VARCHAR2(100);
lv_table_name VARCHAR2(100);
lv_sql_statement VARCHAR2(2000);
lv_count_table NUMBER;
CURSOR c1 IS
SELECT owner, table_name
FROM all_tables
WHERE owner = ip_schema
ORDER BY table_name;
begin
dbms_output.put_line ('+--------------------------------------------------------------------+');
dbms_output.put_line ('¦ | | ¦');
dbms_output.put_line ('¦ Schema Name | Table Name | Number of Rows ¦');
dbms_output.put_line ('¦ | | ¦');
dbms_output.put_line ('¦------------------------------------------------------------------¦');
OPEN c1;
LOOP
FETCH c1 INTO lv_owner , lv_table_name;
EXIT WHEN c1%NOTFOUND;
lv_sql_statement := 'SELECT count(*) FROM ' || lv_owner || '.' || lv_table_name;
EXECUTE IMMEDIATE lv_sql_statement INTO lv_count_table;
IF lv_count_table > 0 THEN
dbms_output.put_line ('| '||rpad(lv_owner, 14, ' ')||'| '|| rpad(lv_table_name, 32, ' ')||'| '|| rpad(lv_count_table, 16, ' ')||' |');
-- dbms_output.put_line ('|---------------|---------------------------------|------------------|');
END IF;
END LOOP;
CLOSE c1;
dbms_output.put_line ('+--------------------------------------------------------------------+');
exception
WHEN OTHERS THEN
dbms_output.put_line ('owner: '||lv_owner||' - table: '||lv_table_name||' - '||sqlerrm);
end count_tables;
set serveroutput on size 1000000
exec count_tables
drop procedure count_tables; -
With SSAS Tabular using Excel:
If I place a single measure MyMeasure:=SUM([ColumnNameOnFactTable])
it happens very quickly.
I have 3 other dimensions from 3 other dimension tables on Excel with this "MyMeasure" as the value.
YearMonth in the columns and say Department ID, Account ID, and Call Center (just all made up for this example).
Now, when I place a second measure from that same table as "MyMeasure" call it SecondMeasure:SUM([AnotherColumnNameOnFactTable]) the OLAP query in Excel spins, and sometimes even throws the out of memory error.
The server has 24 GB of RAM, and the model is only a few hundred megs.
I assume something must be off here?
Either I've done something foolish with the model or I'm missing something?
EDIT:
It SEEMS to work better if I place all y measures on the Excel grid first, then go and add my "dimensions", adding the measures after the dimensions appears to incur a rather steep penalty?
Number of rows:
Largest table (account ID lookup has 180,000)
Fact table has 7,000
The others are 1,000 or less...Hi,
Thank you for your question.
I am trying to involve someone more familiar with this topic for a further look at this issue. Sometime delay might be expected from the job transferring. Your patience is greatly appreciated.
Thank you for your understanding and support.
Regards,
Charlie Liao
TechNet Community Support -
Create ViewCriteria comparing two columns from same table
Does anyone know how I can create a ViewCriteria where clause that compares two columns from the same table?
For example if I had two integer columns (MINSAL and MAXSAL) and wanted to see if they are equal. I would normally do the following SQL below.
SELECT * FROM EMPL
WHERE MINSAL = MAXSALIt works, but it is not ideal.
Setup a Transient column that performs a groovy evaluation of MINSAL=MAXSAL and then my ViewCriteria evaluates the column to true and I set Query Execution Mode to Both. -
Insert or Modify statement from internal table to database table
Hi All,
I have three tables wakh, wrf and wakp. I have an internal table with 5 columns col1, col2, col3, col4 and col5.
The value in Col1 is my article no and the articleno. is in the table wakh. The value in col2 is my ccode and it is in the table wrf. The rest three columns col3, col4 and col5 are unit, qty and price and they are in the wakp table. Now when my articleno is equal to ccode I need to update the col3, col4 and col5 values in the wakp. wakp has around 20 columns.
Can anyone of you guys please give me the code for this issue. Your help is highly appreciated and thanks for all for your time.
Cheers,
ChengHi Rob,
let me explain you the whole process what i am trying to do. I have a screen where there are 3 fields. In my first field I have a promoiton no. As soon as the user enters the promotion no. its description will be populated in my second field. If the promotion is not maintained then it will throw an error. In my third field User will upload an excel sheet which has 5 columns articleno, colorcode, salesunit, qty, mdprice. Here articleno is coming from wakh and colorcode is in wrf_charval table and the rest three fields are coming from wakp table. So for the article no. which is in col1. and for its corresponding colorcode which is in col3 i need to update col3, col4, col5 values.
With my below code I am able to upload the excel into internal table and display it. So instead of displaying I need to update in the database. Can you please let me know how I need to attach the function module within my code and update accordingly.
REPORT ZTest.
tables : wakh, wakt.
Parameter: PromoID type wakh-aktnr, PromoDec type wakt-aktkt, p_file LIKE rlgrap-filename
DEFAULT 'c:\test.xls' OBLIGATORY. " File Name
*FileName type string.
*file_nm type localfile.
TYPES: BEGIN OF t_datatab,
col1(25) TYPE c,
col2(30) TYPE c,
col3(30) TYPE c,
col4(30) TYPE c,
col5(30) TYPE c,
END OF t_datatab.
DATA: it_datatab TYPE STANDARD TABLE OF t_datatab INITIAL SIZE 0,
wa_datatab TYPE t_datatab.
Data : p_table type t_datatab occurs 0 with header line.
DATA : gd_scol TYPE i VALUE '1',
gd_srow TYPE i VALUE '2',
gd_ecol TYPE i VALUE '5',
gd_erow TYPE i VALUE '65536'.
DATA: it_tab TYPE filetable,
gd_subrc TYPE i.
field-symbols : <fs>.
AT selection-screen on PromoID.
select single * from wakh where aktnr = PromoID.
if sy-subrc EQ 0.
select aktkt from wakt into PromoDec where aktnr eq PromoID.
endselect.
else.
message A000(ZI) with 'Promotion ID is not Maintained.'.
endif.
*Title : Excel Uploading
*Selection screen definition
*SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
*PARAMETERS: p_file LIKE rlgrap-filename
DEFAULT 'c:\test.xls' OBLIGATORY. " File Name
*SELECTION-SCREEN END OF BLOCK b1.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
REFRESH: it_tab.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = 'Select File'
default_filename = '*.xls'
multiselection = ' '
CHANGING
file_table = it_tab
rc = gd_subrc.
LOOP AT it_tab INTO p_file.
so_fpath-sign = 'I'.
so_fpath-option = 'EQ'.
append so_fpath.
ENDLOOP.
START-OF-SELECTION.
PERFORM upload_excel_file TABLES it_datatab
USING p_file
gd_scol
gd_srow
gd_ecol
gd_erow.
END-OF-SELECTION.
END-OF-SELECTION.
LOOP AT it_datatab INTO wa_datatab.
WRITE:/ wa_datatab-col1,
wa_datatab-col2,
wa_datatab-col3,
wa_datatab-col4,
wa_datatab-col5.
ENDLOOP.
*& Form UPLOAD_EXCEL_FILE
upload excel spreadsheet into internal table
-->P_TABLE Table to return excel data into
-->P_FILE file name and path
-->P_SCOL start column
-->P_SROW start row
-->P_ECOL end column
-->P_EROW end row
FORM upload_excel_file TABLES p_table
USING p_file
p_scol
p_srow
p_ecol
p_erow.
DATA : lt_intern TYPE kcde_cells OCCURS 0 WITH HEADER LINE.
Has the following format:
Row number | Colum Number | Value
i.e. 1 1 Name1
2 1 Joe
DATA : ld_index TYPE i.
Note: Alternative function module - 'ALSM_EXCEL_TO_INTERNAL_TABLE'
CALL FUNCTION 'KCD_EXCEL_OLE_TO_INT_CONVERT'
EXPORTING
filename = p_file
i_begin_col = p_scol
i_begin_row = p_srow
i_end_col = p_ecol
i_end_row = p_erow
TABLES
intern = LT_INTERN
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
FORMAT COLOR COL_BACKGROUND INTENSIFIED.
WRITE:/ 'Error Uploading file'.
EXIT.
ENDIF.
IF lt_intern[] IS INITIAL.
FORMAT COLOR COL_BACKGROUND INTENSIFIED.
WRITE:/ 'No Data Uploaded'.
EXIT.
ELSE.
SORT lt_intern BY row col.
LOOP AT lt_intern.
MOVE lt_intern-col TO ld_index.
assign component ld_index of structure
p_table to <fs>.
move : lt_intern-value to <fs>.
MOVE lt_intern-value TO p_table.
AT END OF row.
APPEND p_table.
CLEAR p_table.
ENDAT.
ENDLOOP.
ENDIF.
ENDFORM.
Thanks for your valuable time.
Cheng -
Issue with WRITE statement from Internal Table
Hi All,
I have written the below code in a infoset query, the syntax check is ok & when i run the query in debugging mode, i find the internal table being filled with the values & the write statement outputing the values as well.
But the problem is when i execute the query the output does not show all the lines, instead just displays the last record . What could be the mistake?
Is it because i have defined Y_EBELN, Y_EBELP & Y_OPENQTY as nodes in the Extras Tab of the infoset & have chosen this for display in my query? How to display the output from my internal table in my query output?
Hope my problem is clear, await clarification.
Vivek
Code
types:
Begin of itab,
wl_ebeln type eket-ebeln,
wl_ebelp type eket-ebelp,
wl_openqty type eket-menge,
End of itab.
Data: il_po type table of itab with header line.
*Display open PO for materials
SELECT EKETEBELN EKETEBELP EKETMENGE EKETWEMNG EKET~EINDT
INTO (Y_EBELN, Y_EBELP, WL_MENGE, WL_WEMNG, Y_EINDT)
FROM EKET
INNER JOIN EKPO
ON EKETEBELN = EKPOEBELN
AND EKETEBELP = EKPOEBELP
INNER JOIN MARD
ON EKPOMATNR = MARDMATNR
AND EKPOWERKS = MARDWERKS
AND EKPOLGORT = MARDLGORT
WHERE EKPO~MATNR = MARD-MATNR
AND EKPO~WERKS = MARD-WERKS
AND EKPO~LGORT = MARD-LGORT
AND EKPO~LOEKZ = SPACE
AND EKPO~ELIKZ = SPACE.
*Display only still open qty per schedule line
Y_OPENQTY = WL_MENGE - WL_WEMNG.
il_po-wl_ebeln = Y_EBELN.
il_po-wl_ebelp = Y_EBELP.
il_po-wl_openqty = Y_OPENQTY.
append il_po.
ENDSELECT.
ENDIF.
Loop at il_po.
write:/ il_po-wl_ebeln, il_po-wl_ebelp, il_po-wl_openqty.
endloop.Hi Vivek,
Try it like this.
Data: Begin of itab,
lifnr type lfa1-lifnr,
name1 type lfa1-name1,
land1 type lfa1-land1,
End of itab,
it_lfa1 like table of itab with header line,
lifnr like lfa1-lifnr,
name1 like lfa1-name1,
land1 like lfa1-land1.
select lifnr name1 land1 into (lifnr, name1, land1)
from lfa1.
it_lfa1-lifnr = lifnr.
it_lfa1-name1 = name1.
it_lfa1-land1 = land1.
append it_lfa1.
endselect.
loop at it_lfa1.
write:/ it_lfa1-lifnr, it_lfa1-name1, it_lfa1-land1.
endloop.
Note:
In place of the above select you insert your join select statement. It is working for me.
You have declared your internal table wrongly. In the internal table instead of TYPE use LIKE, it will work.
Hope this will work for you.
Thanks & Regards
Haritha. -
Two collect statement from internal table
Dear Experts,
I want populate values to two internal tables from a internal table.
can i use two collect statement for different internal table from a single internal table.
advise please.
Thanks in advance.
R.Rajendranhi there....
well u can very well use this thing.....
use the two colect statements inside the loop which u will use to read the records of the input table.
hope it helps.
do reward if it does.
Maybe you are looking for
-
Restrict Purchase Order to generate custom output message on condition.
Hi Experts, We have custom message / output types ZEMA(E-Mail) & ZNEU(Standard Mail) configured for Purchase Orders. We have a requirement from client to restrict the generation of output message ZEMA, if the amount of the PO is greater than some lim
-
Initial view 2-up spreads with single cover page
I hope I have this wrong and i'm missing something obvious but elusive: It seems like there is no way in the newest version of Acrobat to modify a generated multi-page PDF via preferences>page display so that on initial view it will show as spreads a
-
Handling Ranges in Script Logic
Hi Experts Is it possible to put logic to check a range of values in Script Logic. For example, if I need to check if the value in Quantity Account is between 200 and 800, then only some Quantity Discount is calculated against this. What command shou
-
Hello, I am having NI CAN PCI-CAN/2 card. Also i am having NI CAN 1.5 and MAX 2.2.Since my application is running in Lab view 5.1, i am using this version. After installing the drivers I mentioned above, I couldn't able to see CAN device in MAX. Also
-
Updating bash version in solaris
Hello guys, Currently my bash version is 3.0.(solaris 10 release 09/10). I want to upgrade it to bash 4.2. Could anybody suggest me the procedure ? -Thank You IA-