I want to join two tables, and insert the new calculate data on the first one, look at the example:
Table1:
Measure Value Date
Units 1.00 1
Dollar 25.00 1
Units 1.00 2
Dollar 25.00 2
Table2:
Date Rate
1 1.05
2 1.09
I would like to include on the Table 1, this lines that means (Dollar: Value * Rate on the same Date)
Measure Value Date
LocalValue 26,25 1
LocalValue 27,25 2
I'm try to do like this, but I continue have a problem:
JOIN (Table2)
LOAD
'LocalValue' as [Measure],
[Value]*[Rate] AS [Value]
RESIDENT Table1
WHERE [Measure] = 'Dollar'
but I'm geting this error message:
Error Field not found -
What I'm doing wrong?
Example here:
Table1:
Load * inline
[
Measure,Value,Date
Units,1,1
Dollar,25,1
Units,1,2
Dollar,25,2
];
Table2:
Load * inline
[
Date,Rate
1,2
2,3
];
Table1:
JOIN (Table2)
LOAD
'LocalValue' as [Measure],
[Value]*[Rate] AS [Value]
RESIDENT Table1
WHERE [Measure] = 'Dollar'
At the point when you try "join (Table2) " fields Rate and Value do not exists in the same table from which you are trying to load (Table1).
You need to have fields Value and Rate in one table before join the LocalValue calculation.
Your script need to look like this:
Table1:
Load * inline
[
Measure,Value,Date
Units,1,1
Dollar,25,1
Units,1,2
Dollar,25,2
];
join
Table2:
Load * inline [
Date,Rate
1,2
2,3
];
JOIN (Table1)
//Table1:
LOAD
'LocalValue' as [Measure],
[Value]*[Rate] AS [Value]
RESIDENT Table1
WHERE [Measure] = 'Dollar'
And the result table will be:
Measure Value Date Rate
Dollar 25 1 2
Units 1 1 2
Dollar 25 2 3
Units 1 2 3
LocalValue 50 - -
LocalValue 75 - -
Stefan
Related
I have a database in SPSS structured like the following table:
ID
Gender
Age
Var1
Var...
1
0
7
3
...
2
1
8
4
...
3
1
9
5
...
4
1
9
2
...
I want to select only the first n (e.g.: 150) cases, where Gender = 1 and Age = 9, so in the table above the 3. and 4. case. How can I do it? Thanks!
compute filter_ = $sysmis.
compute counter_ = 0.
if $casenum=1 and (Gender = 1 and Age = 9) counter_ =1 .
do if $casenum <> 1.
if ~(Gender = 1 and Age = 9) counter_ = lag(counter).
if (Gender = 1 and Age = 9) counter_ = lag(counter) +1.
end if.
compute filter_ = (Gender = 1 and Age = 9 and counter<= 150).
execute.
I am not sure if this is the most efficient way, but it gets the job done. We use the counter_ variable to assign an order number for each record which satisfies the condition ("counting" records with meet the criteria, from the top of the file downwards). Then create a filter of the first 150 such records.
The below will select the first 150 cases where gender=1 AND age=9 (assuming 150 cases meet that criteria).
N 150.
SELECT IF (Gender=1 AND Age=9).
EXE .
Flipping the order of N and SELECT IF () would yield the same result. You can read more about N in the IBM documentation
I am really sorry for this. I am new oracle and I have created following block which is resulting output and then error.
First cursor is generating output and then error and so second cursor is not generating output.
Please help for error free output .
thanks and regards
Error:
END;
Error report:
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at line 36
06502. 00000 - "PL/SQL: numeric or value error%s"
*Cause:
*Action:
Block:
DECLARE
c_dbuser SYS_REFCURSOR;
c_dbuser1 SYS_REFCURSOR;
temp_dbuser FIT_SCHEMA.fxf_inspt_insp_main%ROWTYPE;
year_date varchar2(20):='2014';
typeOfGraph varchar2(50):='InspectionByInspection';
division varchar2(20):='Division';
subDiv varchar2(20):='ALL';
emp varchar2(20):='';
TYPE t_name IS RECORD( --Error is at this line :(
inspectiondate varchar2(20),
totalcount number(38),
inspectionDesc varchar2(20)
);
r_name t_name; -- name record
TYPE t_monthwise is record (
month_number varchar2(20),
inspcount number(38),
inspectionDesc varchar2(20)
);
t_month t_monthwise;
BEGIN
fit_schema.My_manager(typeOfGraph,year_date,division,subDiv,emp,c_dbuser,c_dbuser1);
LOOP
FETCH c_dbuser INTO r_name ;
EXIT WHEN c_dbuser%NOTFOUND;
dbms_output.put_line( r_name.inspectiondate ||' '||r_name.totalcount||' '||r_name.inspectionDesc);
END LOOP;
LOOP
FETCH c_dbuser1 INTO t_month ;
EXIT WHEN c_dbuser1%NOTFOUND;
dbms_output.put_line( t_month.month_number ||' '||t_month.inspcount||' '||t_month.inspectionDesc);
END LOOP;
CLOSE c_dbuser;
CLOSE c_dbuser1;
END;
Output :
13-DEC-2014 1 3#CPLD Only
13-DEC-2014 4 0#Class Only
14-DEC-2014 1 0#Class Only
15-DEC-2014 2 0#Class Only
16-DEC-2014 1 0#Class Only
17-DEC-2014 1 7#Negative Class
17-DEC-2014 9 0#Class Only
19-DEC-2014 15 0#Class Only
22-DEC-2014 1 11#65% Rule
23-DEC-2014 1 8#XLGH & Class
30-DEC-2014 1 0#Class Only
31-DEC-2014 1 10#Mixed Articles
31-DEC-2014 3 0#Class Only
02-JAN-2015 2 0#Class Only
05-JAN-2015 2 0#Class Only
07-JAN-2015 2 9#XLGH Only
07-JAN-2015 3 1#Class & Reweigh
07-JAN-2015 1 10#Mixed Articles
07-JAN-2015 4 0#Class Only
07-JAN-2015 2 11#65% Rule
08-JAN-2015 5 0#Class Only
08-JAN-2015 1 9#XLGH Only
09-JAN-2015 1 3#CPLD Only
09-JAN-2015 1 11#65% Rule
09-JAN-2015 4 0#Class Only
09-JAN-2015 1 1#Class & Reweigh
12-JAN-2015 1 5#CCD Only
12-JAN-2015 1 3#CPLD Only
19-JAN-2015 1 11#65% Rule
20-JAN-2015 1 0#Class Only
21-JAN-2015 4 0#Class Only
23-JAN-2015 1 10#Mixed Articles
26-JAN-2015 1 7#Negative Class
26-JAN-2015 2 0#Class Only
27-JAN-2015 1 3#CPLD Only
27-JAN-2015 3 0#Class Only
27-JAN-2015 1 6#CCD & Class
28-JAN-2015 6 0#Class Only
29-JAN-2015 3 0#Class Only
29-JAN-2015 1 5#CCD Only
30-JAN-2015 1 3#CPLD Only
30-JAN-2015 1 1#Class & Reweigh
01-FEB-2015 3 0#Class Only
02-FEB-2015 1 4#CPLD & Class
02-FEB-2015 1 8#XLGH & Class
04-FEB-2015 1 0#Class Only
Queries from Procedure :-
SELECT TO_CHAR(MAINTABLE.INSP_CREATED_TMSTP,'dd-MON-yyyy') inspectiondate, --varchar2
COUNT(MAINTABLE.insp_id) AS totalinspection, --count is number
MAINTABLE.insp_type_id ||'#'||ISNPTYPETABLE.INSP_TYPE_DESC AS INSPECTIONDESCRIPTION-- varchar2
FROM FIT_SCHEMA.fxf_inspt_insp_main MAINTABLE
JOIN FIT_SCHEMA.fxf_inspt_emp_detail EMPTABLE
ON(MAINTABLE.inspector_emp_nbr=EMPTABLE.inspector_emp_nbr)
JOIN FIT_SCHEMA.fxf_inspt_drop_down DROPDOWNTABLE
ON (emptable.division_id = DROPDOWNTABLE.drop_down_id)
JOIN FIT_SCHEMA.fxf_inspt_insp_type ISNPTYPETABLE
ON(MAINTABLE.insp_type_id =isnptypetable.insp_type_id)
WHERE dropdowntable.is_active_flg = 1
AND MAINTABLE.STATUS_ID = 8
AND MAINTABLE.insp_type_id IN (0,1,3,4,5,6,7,8,9,10,11,12)
AND TRUNC(MAINTABLE.insp_created_tmstp) between TRUNC(EMPTABLE.EFFECTIVE_FROM_TMSTP)
And Nvl(Trunc(Emptable.Effective_To_Tmstp),Sysdate) AND UPPER(dropdowntable.drop_down_grp) =UPPER('division') AND TRUNC(MAINTABLE.insp_created_tmstp) between '01-JUN-14' AND '31-MAY-15' GROUP BY TO_CHAR(MAINTABLE.INSP_CREATED_TMSTP,'dd-MON-yyyy')
, TRUNC(MAINTABLE.INSP_CREATED_TMSTP), MAINTABLE.insp_type_id ||'#'|| ISNPTYPETABLE.INSP_TYPE_DESC ORDER BY TRUNC(MAINTABLE.INSP_CREATED_TMSTP)
SELECT TO_CHAR(MAINTABLE.insp_created_tmstp,'MM') AS MONTHNUMBER,
COUNT(MAINTABLE.INSP_ID) AS INSPCOUNT ,
MAINTABLE.insp_type_id ||'#'
||ISNPTYPETABLE.INSP_TYPE_DESC AS INSPECTIONDESCRIPTION
FROM FIT_SCHEMA.fxf_inspt_insp_main MAINTABLE
JOIN FIT_SCHEMA.fxf_inspt_emp_detail EMPTABLE
ON(MAINTABLE.inspector_emp_nbr=EMPTABLE.inspector_emp_nbr)
JOIN FIT_SCHEMA.fxf_inspt_drop_down DROPDOWNTABLE
ON (emptable.division_id = DROPDOWNTABLE.drop_down_id)
JOIN FIT_SCHEMA.fxf_inspt_insp_type ISNPTYPETABLE
ON(MAINTABLE.insp_type_id =isnptypetable.insp_type_id)
WHERE dropdowntable.is_active_flg = 1
AND MAINTABLE.insp_type_id IN (0,1,3,4,5,6,7,8,9,10,11,12)
AND dropdowntable.is_active_flg = 1
AND MAINTABLE.STATUS_ID = 8
AND TRUNC(MAINTABLE.insp_created_tmstp) between TRUNC(EMPTABLE.EFFECTIVE_FROM_TMSTP)
And Nvl(Trunc(Emptable.Effective_To_Tmstp),Sysdate) AND UPPER(dropdowntable.drop_down_grp) =UPPER('division')
AND TRUNC(MAINTABLE.insp_created_tmstp) between '01-JUN-14'
AND '31-MAY-15' GROUP BY TO_CHAR(MAINTABLE.insp_created_tmstp,'MM'), MAINTABLE.insp_type_id ||'#'||ISNPTYPETABLE.INSP_TYPE_DESC ORDER BY TO_CHAR(MAINTABLE.insp_created_tmstp,'MM')
Line 36 is:
FETCH c_dbuser INTO r_name ;
So one of the values being returned in your c_dbuser ref cursor is too big. Your date format mask will only generate string 11 characters long so inspectiondate is (more than) adequate, and the count seems unlikely to exceed number(38) so totalcount also seems fine. Which suggests that inspectionDesc is too small for some of the values in the data.
You're generating that from:
MAINTABLE.insp_type_id ||'#'||ISNPTYPETABLE.INSP_TYPE_DESC
If the total length of that concatenated string exceeds 20 characters then you'll get an error on fetch.
If INSP_TYPE_DESC is defined as varchar2(20) then you aren't allowing any room for the rest; you need one extra character for the fixed #, and at least two for the insp_type_id - more if the highest ID can be three digits or more. If it isn't constrained then you'll need to decide the highest number you expect to ever see and allow for that. So if you expect up to three digits for an ID, define inspectionDesc as varchar2(24), etc.
I have to join two tables using left outer join method in Hive. Please find my table details and required output below,
Apologize for big explanation. I gave all the information for more clarity in my doubts.
Table1 columns:
date1, srcid, claimno1
Date1 srcid claimno1
31-01-2015 ABC 432
31-01-2015 ABC 451
31-01-2015 ABC 435
01-02-2015 ABC 451
Table2 columns:
date2,claimno2,amount
Date2 claimno1 amount
31-01-2015 432 150.51
01-02-2015 451 250.61
31-02-2015 451 320.72
01-02-2015 432 381.65
Required Output:
Date1 srcid claimno1 amount
31-01-2015 ABC 432 381.65
31-01-2015 ABC 451 250.61
31-01-2015 ABC 432 381.65
01-02-2015 ABC 451 NULL
Condition:
1) from table1 need to select the record with date as 31-01-2015
2) from table2 need to select the record with date as 01-02-2015
Assumption:
1) claimno1 from table1 is not unique and occur many time for same date1
2) claimno2 from table2 is unique for particular date2
Doubts:
1) Do I need to give all the selected columns in GROUP BY ? (else getting SemanticException error)
2) What is the difference when i used the same condition in WHERE clause and ON clause?
I have used below queries but couldn't able to get required output,
SELECT
a.date1, a.srcid, a.claimno1, b.amount
FROM
table1 a
LEFT OUTER JOIN
tableb ON (a.claimno1 = b.claimno2)
WHERE
a.date1 = '31-01-2015'
AND b.date2 = '01-02-2015'
GROUP BY a.date1 , a.srcid;
FAILED: SemanticException [Error 10002]: Invalid column reference 'claimno1'
SELECT
a.date1, a.srcid, a.claimno1, b.amount
FROM
table1 a
LEFT OUTER JOIN
tableb ON (a.claimno1 = b.claimno2)
WHERE
a.date1 = '31-01-2015'
AND b.date2 = '01-02-2015'
GROUP BY a.date1 , a.srcid , a.claimno1;
FAILED: SemanticException [Error 10002]: Invalid column reference 'amount'
SELECT
a.date1, a.srcid, a.claimno1, b.amount
FROM
table1 a
LEFT OUTER JOIN
tableb ON (a.claimno1 = b.claimno2)
WHERE
a.date1 = '31-01-2015'
AND b.date2 = '01-02-2015'
GROUP BY a.date1 , a.srcid , a.claimno1 , b.amount;
Result: No records selected
SELECT
a.date1, a.srcid, a.claimno1, b.amount
FROM
table1 a
LEFT OUTER JOIN
tableb ON (a.claimno1 = b.claimno2
AND a.date1 = '31-01-2015'
AND b.date2 = '01-02-2015')
GROUP BY a.date1 , a.srcid , a.claimno1 , b.amount;
Result: records selected but amount values are NULL
Apologize for big explanation. I gave all the information for more clarity in my doubts.
Thanks in advance.
I'm struggling to figure out the issue with my SQL table using compute sum.
All that is displayed where the sum of the column should be is a blank box!
Code Below:
TTITLE CENTER ==================== SKIP 1-
CENTER 'U T O O L' skip 1-
CENTER ==================== SKIP 1 -
LEFT 'Tool Report 1.03' SKIP 1 -
LEFT ============ SKIP 2-
RIGHT 'Page:' -
FORMAT 999 SQL.PNO SKIP 2
set pagesize 50
column MEMBERNAME HEADING 'Member Name' format a20
compute sum of TOTAL on Rental_ID
Break on RENTAL_ID
select Member.Member_ID, SUBSTR(Member.FName,0,10) || SUBSTR(' ',0,10) ||
SUBSTR(Member.SName,0,15) as MEMBERNAME,
Rental.Rental_ID,
Tool.Name,
Rental_Line.Qty,
Rental_Line.Price,
TO_Char(Rental_Line.Qty*Rental_Line.Price,'L9,999.99') TOTAL
from Rental_Line
INNER JOIN Rental
on Rental.Rental_ID = Rental_Line.Rental_ID
INNER JOIN Member
on Rental.Member_ID = Member.Member_ID
INNER JOIN Tool_Instance
on Rental_Line.Tool_Instance_ID = Tool_Instance.Tool_Instance_ID
INNER JOIN Tool
on Tool_Instance.Tool_ID = Tool.Tool_ID
where Rental.Rental_ID = '&Rental_ID';
may be this help you, as I understood you need SUM(Rental_Line.Qty) OVER (PARTITION BY Rental.Rental_ID)
select Member.Member_ID,
SUBSTR(Member.FName, 0, 10) || SUBSTR(' ', 0, 10) ||
SUBSTR(Member.SName, 0, 15) as MEMBERNAME,
Rental.Rental_ID,
Tool.Name,
Rental_Line.Qty,
Rental_Line.Price,
TO_Char(Rental_Line.Qty * Rental_Line.Price, 'L9,999.99') TOTAL,
SUM(Rental_Line.Qty) OVER (PARTITION BY Rental.Rental_ID) TOTAL_QTY,
SUM(Rental_Line.Qty * Rental_Line.Price) OVER (PARTITION BY Rental.Rental_ID) TOTAL_SUM
from Rental_Line
INNER JOIN Rental on Rental.Rental_ID = Rental_Line.Rental_ID
INNER JOIN Member on Rental.Member_ID = Member.Member_ID
INNER JOIN Tool_Instance on Rental_Line.Tool_Instance_ID =
Tool_Instance.Tool_Instance_ID
INNER JOIN Tool on Tool_Instance.Tool_ID = Tool.Tool_ID
where Rental.Rental_ID = '&Rental_ID';
I am trying to return results based on the number of cases if greater than 0 but when i try to execute the stored procedure i get an error stating: Operand type clash: uniqueidentifier is is incompatible with tinyint. And that is for my nested select statement in where clause.
SELECT
O.OfficeId,
O.OfficeName AS Name,
AT.Description AS CaseActivity,
SUM(A.Duration) AS [CaseMinutes],
CAST(SUM(A.Duration) AS FLOAT) / 60 AS [CaseHours],
COUNT(A.ActivityId) AS Activities,
COUNT(DISTINCT A.CaseId) AS Cases,
MIN(CAST(A.Duration AS FLOAT) / 60) AS [Case Min Time],
MAX(CAST(A.Duration AS FLOAT) / 60) AS [Case Max Time],
SUM(CAST(A.Duration AS FLOAT) / 60) / COUNT(A.ActivityId) AS [Case Avg Time],
SUM(CAST(A.Duration AS FLOAT) / 60) AS [Case TotalHours]
FROM Activity A
INNER JOIN ActivityType AT ON A.ActivityTypeId = AT.ActivityTypeId
INNER JOIN ActivityEntry AE ON A.ActivityEntryId = AE.ActivityEntryId
INNER JOIN [Case] C ON A.CaseId = C.CaseId
INNER JOIN [Office] O ON AE.OfficeId = O.OfficeId
INNER JOIN [User] U ON C.CreatedByUserId = U.UserId
WHERE A.CaseId in(select A.CaseId from Activity where A.CaseId > 1 AND .dbo.DateOnly(AE.ActivityDate) BETWEEN #BeginDate AND #EndDate)
GROUP BY
O.OfficeId,
O.OfficeName,
AT.Description
**Desired GOAL from stored procedure**
I want to return results from this stored procedure where the case count is greater than 0. Currently this stored procedure returns all activities with cases 0 or greater. I am only interested in getting the activities where the cases are greater than 0. In my where clause i am trying to insert another select statement that will filter the results to cases > 0.
The error message is telling you.
One of the comparisons is comparing things from two columns with different types.
Look at the table structures and see which columns you're comparing has uniqueidentifier and which have ints and then look at the sql and see which ones you're comparing.