I've a problem with the code in Microsoft Access
I would like to copy data from columns in an existing table into a new table then append details to the bottom of a new table for the next portfolio
The column headings aren't needed. I essentially want to copy all the columns
CODE
SELECT Date, PCode, Security, Units, BarVal, BarPrice, MktPrice
INTO "2010 extract - Feb"
FROM BarePortHistory
WHERE BarePortHistory.Pcode ="SGXEE1"
ORDER BY BarePortHistory.Date
INSERT INTO 2010 extract - Feb (Date, PCode, Security, Units, BarVal, BarPrice, MktPrice)
SELECT Date, PCode, Security, Units, BarVal, BarPrice, MktPrice
FROM BarePortHistory
WHERE BarePortHistory.Pcode ="RTXDG1"
ORDER BY BarePortHistory.Date
INSERT INTO 2010 extract - Feb(Date, PCode, Security, Units, BarVal, BarPrice, MktPrice)
SELECT Date, PCode, Security, Units, BarVal, BarPrice, MktPrice
FROM BarePortHistory
WHERE BarePortHistory.Pcode ="SPXUY1"
ORDER BY BarePortHistory.Date
Is it my use of quotation marks?
A table name composed of a few words (which is not recommended!)
should be enclosed in square brackets: [2010 extract - Feb].
you select all data from one table, then use Or to include data matching either condition.
SELECT Date, PCode, Security, Units, BarVal, BarPrice, MktPrice
INTO [2010 extract - Feb]
FROM BarePortHistory
WHERE BarePortHistory.Pcode ="SGXEE1" OR BarePortHistory.Pcode ="RTXDG1" OR BarePortHistory.Pcode ="SPXUY1"
ORDER BY BarePortHistory.Date
Insert into new_table_Name(field1,field2....)
select field1,field2 ... from old_table_name
where condition
There should be no ORDER BY while inserting data to table ..Try this
As marlan said, try changing
INTO "2010 extract - Feb"
To
INTO [2010 extract - Feb]
And your use of quotation marks in your WHERE clauses should be using single quotes.
Like so:
WHERE BarePortHistory.Pcode ='SGXEE1'
Related
I am trying to append two tables together, they don't have quite the same columns but contain data for the same clients. the table called "outcome" contains survey results from clients collected in 1 month and the table "checkpoint" contains survey results from clients collected six months after. I tried to append those two tables and ensured that there is the same number of columns by introducing NULL columns so that the number of columns match in both tables here is my query:
tbl_out AS (
SELECT
PRG_NAME,
CASEREFERENCE,
STARTDATE,
STATUS,
ENDDATE,
LASTWRITTEN,
CLOSURE_REASON,
--these columns were made to match the tbl_check table--
TO_CHAR(NULL) AS Reviewer,
TO_DATE(NULL) AS Month_Schedule_Date,
TO_CHAR(NULL) AS Month_REASON,
TO_DATE(NULL) AS Month_Start_Date,
TO_DATE(NULL) AS Month_End_Date,
TO_CHAR(NULL) AS MONTH_Resubmit_MILESTONE,
TO_CHAR(NULL) AS MONTH_MILESTONE_ACHIEVED,
TO_DATE(NULL) AS MONTH_APPROVED_DATE,
--at 1 month--
OUTCOME_DATE,
Outcome_Reference_ID,
Outcome_EMP_SITUATION,
Outcome_Work_Job_Business,
Outcome_Employment_Type,
Outcome_NUM_JOBS,
Outcome_NAICS,
Outcome_NAICS_Desc,
Outcome_NOC,
Outcome_NOC_Desc,
Outcome_JOB_Nature,
TO_NUMBER(Outcome_WORK_HOURS),
TO_NUMBER(Outcome_WAGE),
Outcome_Change_Employment,
TO_NUMBER(Outcome_NUM_EMP_Change),
Outcome_LAST_UNEMP_DATE,
Outcome_Attend_School,
Outcome_STUDENT_STATUS,
Outcome_STUDENT_Type,
Outcome_EMP_CATEGORIES,
Outcome_Got_Service,
Outcome_Right_Service,
Outcome_Seek_Help_Again,
Outcome_Recommend_Program,
Outcome_Didnot_Seek_Employment
FROM outcome
),
tbl_check AS (
SELECT
PRG_NAME,
CASEREFERENCE,
STARTDATE,
STATUS,
ENDDATE,
LASTWRITTEN,
CLOSURE_REASON,
--info from tbl_out--
TO_DATE(NULL) AS OUTCOME_DATE,
--at 6 months--
TO_CHAR(Reviewer),
TO_DATE(Month_Schedule_Date),
TO_CHAR(Month_REASON),
TO_DATE(Month_Start_Date),
TO_DATE(Month_End_Date),
month_Review_Reference_ID,
month_outcome AS MONTH_EMP_SITUATION,
Month_Work_Job_Business,
Month_Outcome_Employment_Type,
month_NUM_JOBS,
month_NAICS,
Month_NAICS_Desc,
MONTH_NOC,
Month_NOC_Desc,
Month_JOB_Nature,
TO_NUMBER(MONTH_WORK_HOURS),
TO_NUMBER(MONTH_WAGE),
Month_Change_Employment,
TO_NUMBER(Month_NUM_EMP_Change),
MONTH_LAST_UNEMP_DATE,
Month_Attend_School,
Month_STUDENT_STATUS,
Month_STUDENT_Type,
Month_EMP_CATEGORIES,
Month_Got_Service,
Month_Right_Service,
Month_Seek_Help_Again,
Month_Recommend_Program,
Month_Didnot_Seek_Employment,
TO_CHAR(MONTH_Resubmit_MILESTONE),
TO_CHAR(MONTH_MILESTONE_ACHIEVED),
TO_DATE(MONTH_APPROVED_DATE)
FROM checkpoint
)
SELECT * FROM tbl_out
UNION
SELECT * FROM tbl_check
however, I still get this error:
I was wondering if anyone could please tell me how I can fix my query so that the query runs properly? Thank you
The columns in your two CTEs are in different orders. For example, in the first CTE Reviewer is the 8th column, but in the second CTE it's the 9th column. That's causing different datatypes to be in matching positions, not just what looks like non-matching data.
When you do:
SELECT * FROM ...
the projection has the columns in the order they are defined in the CTE; it doesn't automatically reorder them based on name, say; there's no requirement for the names to be the same (and they aren't the same for a lot of your columns).
Rearrange the columns in one or both CTEs to they align properly. Or list the columns in each select list instead of using *, but in this case that's probably not helpful. In general avoid *, but it is sometimes a valid and sensible choice.
This is nothing to do with the nulls, other than you've maybe put those in the wrong place.
Incidentally really, and somewhat personally, rather than doing things like:
TO_CHAR(NULL) AS Reviewer,
TO_DATE(NULL) AS Month_Schedule_Date,
I would usually cast to the right data type:
CAST(NULL AS VARCHAR2(30)) AS Reviewer,
CAST(NULL AS DATE) AS Month_Schedule_Date,
etc., matching the target data type - including string length and number scale/precision for clarity. It's somewhat a matter of taste; but there are four versions of to_char(), which all return varchar2, but it still feels a bit ambiguous.
pl SQL code segment
SELECT Xmlserialize(DOCUMENT
XMLELEMENT("intrastat",
XMLAGG(
Xmlforest(ENVELOPE_ID AS "envID",
XMLFOREST(DATE_ AS "date",TIME_ AS "Time")AS "Date
time", PARTY_ID AS "pid",PARTY_NAME AS "pname",
XMLFOREST(Xmlelement("RC",REGION_CODE) AS RC,Xmlelement("TCPCODE",MODE_OF_TRANSPORT_CODE) AS TCPCODE) AS "item")
)))
FROM INTRASTAT_XML_TEMPLATE_LINE_TMP
part of actual output that make the trouble
<item><RC><RC>as</RC></RC><TCPCODE><TCPCODE>22</TCPCODE></TCPCODE></item>
what i want to get
<item><RC>ads</RC><TCPCODE>22</TCPCODE></item>
Your current plsql code segment :
XMLFOREST(Xmlelement("RC",REGION_CODE) AS RC,Xmlelement("TCPCODE",MODE_OF_TRANSPORT_CODE) AS TCPCODE) AS "item")
And as we have used aliases in here, we are getting multiple tags - One for alias, and one for the first parameter of XMLELEMENT function.
Now, since you just want an element - item, with two tags - RC (holding data of REGION_CODE field) and TCPCODE (holding data of MODE_OF_TRANSPORT_CODE field),
in my opinion, this should suffice your requirement:
Xmlelement("item", XMLFOREST(REGION_CODE "RC", MODE_OF_TRANSPORT_CODE "TCPCODE")
~Kuntal
with data(rc, tcpcode) as (select 'ads', 22 from dual)
select xmlelement("item", xmlforest(rc, tcpcode)) from data;
XMLELEMENT("ITEM",XMLFOREST(RC,TCPCODE))
--------------------------------------------------------------------------------
<item><RC>ads</RC><TCPCODE>22</TCPCODE></item>
For Sample purpose lets create a table with below schema and fill some sample values
CREATE TABLE games(ID INT ,Name VARCHAR(20));
INSERT INTO games(ID,Name) VALUES (2008,'Beijing');
INSERT INTO games(ID,Name) VALUES (2012,'London');
INSERT INTO games(ID,Name) VALUES (2012,12);
INSERT INTO games(ID,Name) VALUES (2012,654);
Output:
ID NAME
2008 Beijing
2012 London
2012 12
2012 654
In the above table we have both number and string data in the name column lets write a query that uses the REGX to filter only the numerical rows
SELECT TO_NUMBER(Name)as Trimmed FROM games where REGEXP_LIKE(Name, '(?<=\s|^)\d+(?=\s|$)', '')
Output:
TRIMMED
12
654
Now here is the problem if write a where clause of getting values greater than 12 from the above result it throws invalid number.
Select * from (SELECT TO_NUMBER(Name)as Trimmed FROM games where REGEXP_LIKE(Name, '(?<=\s|^)\d+(?=\s|$)', '')) T1 where T1.Trimmed >12 ;
I found this is how the oracle query planning works but is there any other way i can achieve this
This will work:
Select * from
(SELECT Name as Trimmed
FROM games where REGEXP_LIKE(Name, '(?<=\s|^)\d+(?=\s|$)', '')) T1
where to_number(T1.Trimmed) >12 ;
Unfortunately you need a subquery. It can't be done with one where.
This can be done in a single query:
with
inputs as (
select 2008 as id, 'Beijing' as name from dual union all
select 2012 , 'London' from dual union all
select 2012 , '12' from dual union all
select 2012 , '654' from dual
)
select id, name
from inputs
where translate(name, 'a0123456789', 'a') is null
and to_number(regexp_replace(name, '[^[:digit:]]', '')) > 12
;
ID NAME
---------- -------
2012 654
1 row selected.
regexp_replace removes all the characters except digits, so the test can be done regardless of what the name is. If there are no digits in the name, the result is NULL, which can be converted to number (it is still null).
The translate solution for testing for "all-digits" is more efficient than using regexp_like. The odd-looking 'a' in translate is needed due to an oddity in the translate function itself (see the documentation). This test is not needed if all the names are either "all letters" or "all digits" (if "all letters", the second test with regexp_replace would suffice); but the first test is needed if names like "Sydney 2000" are possible in the name column.
In Oracle (PROD), we will be creating views on table(s) and the users will be querying the views to fetch data for each reporting period (a single month, eg: between '01-DEC-2015' and '31-DEC-2015'). We created a view as
CREATE OR REPLACE VIEW VW_TABLE1 AS SELECT ACCNT_NBR, BIZ_DATE, MAX(COL1) COL1, MAX(COL2) COL2 FROM TABLE1_D WHERE BIZ_DATE IN (SELECT BIZ_DATE FROM TABLE2_M GROUP BY BIZ_DATE) GROUP BY ACCNT_NBR, BIZ_DATE;
The issue here is TABLE1_D (daily table, has data from Dec2015 to Feb2016) has records with multiple dates for a month say for Dec2015, it has records with 01-DEC-2015, 02-DEC-2015,....,29-DEC-2015,30-DEC-2015 (may not be continuous, but loaded on business date) with each day having close to 2,500,000 of records.
TABLE2_M is a monthly table and has a single date for a month (eg for Dec2015 say 30-DEC-2015) with around 4000 records for each date.
When we query the view as
SELECT * FROM VW_TABLE1 WHERE BIZ_DATE BETWEEN '01-DEC-2015' AND '31-DEC-2015'
it returns the aggregated data in table TABLE1_D for 30-DEC-2015 as expected. I thought the Grouping on BIZ_DATE in TABLE1_D is unnecessary as only one BIZ_DATE will be the output from the INNER query.
Checked by removing the BIZ_DATE in the final GROUP BY assuming that there will be data for a single day from the inner query.
Hence took 2 rows for the dates 30-dec-2015 and 30-jan-2016 from both tables and created them in SIT for testing and created view as
CREATE VIEW VW_TABLE1 AS SELECT ACCNT_NBR, MAX(BIZ_DATE) BIZ_DATE, MAX(COL1) COL1, MAX(COL2) COL2 FROM TABLE1_D WHERE BIZ_DATE IN (SELECT BIZ_DATE FROM TABLE2_M GROUP BY BIZ_DATE) GROUP BY ACCNT_NBR;
The select with between for each month (or = exact month date) gives correct data in SIT; i.e., when used BETWEEN for a single month, it produces the respective months data.
SELECT * FROM VW_TABLE1 WHERE BIZ_DATE BETWEEN '01-DEC-2015' AND '31-DEC-2015';
SELECT * FROM VW_TABLE1 WHERE BIZ_DATE = '30-DEC-2015';
With this, I modified the view DDL in PROD (to be same as SIT). But surprisingly the same select (2nd one with ='30-DEC-2015' ; 1st one was taking too long due to volume of data, hence aborted)
returned no data; as I hope that the inner query is sending out dates all 30-DEC-2015 to 30-JAN-2016 and thereby the MAX(BIZ_DATE) is being derived to be from 30-jan-2016. (Table2_M doesn't have FEB2016 data)
I verified whether there was any version differences of Oracle in SIT and PROD and found it to be same from v$version (11.2.0.4.0). Can you please explain this behavior as the same query on same view DDL in different environments returning different results with same data ...
Using Oracle, PSQL, I am trying to figure out the earliest invoice date for each supplier. That would be simple enough, but I am also trying to figure out the max distribution line on the earliest invoice so I can determine what segment of the business the invoice belongs to. Segment is determined by SEGMENT_NUMBER in the example below. I know a sub query or multiple sub queries are needed here with a group by clause but I am at a loss. The syntax below is not even close, but I wanted to provided something for feedback.
SELECT
SUPPLIER_ID,
INVOICE_NUMBER,
SEGMENT_NUMBER,
MIN(INVOICE_DATE) as EARLIEST_INV_DATE,
MAX(DISTRIBUTION_AMOUNT) as MAX_DIST_LINE
FROM INVOICE_DIST
Use Analytical function like RANK().
SELECT SUPPLIER_ID,
INVOICE_NUMBER,
SEGMENT_NUMBER,
INVOICE_DATE,DISTRIBUTION_AMOUNT
(SELECT SUPPLIER_ID,
INVOICE_NUMBER,
SEGMENT_NUMBER,
INVOICE_DATE,DISTRIBUTION_AMOUNT,
RANK() OVER(PARTITION BY SUPPLIER_ID ORDER BY INVOICE_DATE,DISTRIBUTION_AMOUNT DESC) POSITION FROM INVOICE_DIST) TBL WHERE POSITION=1;