Is there an alternative to MySQL Field() function in VFP? - visual-foxpro

I have a multi fields table with a Countries column. I like the results from a query to be ordered by a particular country first and the rest alphabetically. In MySQL I would do something like:
Select * from myTable Order By Field(Countries,'Italy'),Countries
In Visual-FoxPro I have tried indexing the Cursor created by this query:
Select * from myTable Order By Countries
Index on Countries<>'Italy' TAG test
This would display all results for 'Italy' first, but leave the rest in an unpredictable order.
How to achieve this in Visual-FoxPro?

In VFP you can do it with something like this:
SELECT * FROM myTable WHERE Countries='Italy' ;
UNION ALL ;
SELECT * FROM (SELECT * FROM myTable WHERE Countries<>'Italy' ORDER BY Countries) as secsel

It does order by "if countries is not Italy first then Italy", Countries. Right?
In VFP you can use IIF(). ie:
Select *, iif(Countries == 'Italy', 1, 0) as CItaly ;
from myTable :
Order By CItaly,Countries
Note: If you want to do this via an index then you can use a composite index like:
index on iif(Countries = 'Italy', '1', '0') + Countries tag myCountry

Related

Oracle select rows from a query which are not exist in another query

Let me explain the question.
I have two tables, which have 3 columns with same data tpyes. The 3 columns create a key/ID if you like, but the name of the columns are different in the tables.
Now I am creating queries with these 3 columns for both tables. I've managed to independently get these results
For example:
SELECT ID, FirstColumn, sum(SecondColumn)
FROM (SELECT ABC||DEF||GHI AS ID, FirstTable.*
FROM FirstTable
WHERE ThirdColumn = *1st condition*)
GROUP BY ID, FirstColumn
;
SELECT ID, SomeColumn, sum(AnotherColumn)
FROM (SELECT JKM||OPQ||RST AS ID, SecondTable.*
FROM SecondTable
WHERE AlsoSomeColumn = *2nd condition*)
GROUP BY ID, SomeColumn
;
So I make a very similar queries for two different tables. I know the results have a certain number of same rows with the ID attribute, the one I've just created in the queries. I need to check which rows in the result are not in the other query's result and vice versa.
Do I have to make temporary tables or views from the queries? Maybe join the two tables in a specific way and only run one query on them?
As a beginner I don't have any experience how to use results as an input for the next query. I'm interested what is the cleanest, most elegant way to do this.
No, you most probably don't need any "temporary" tables. WITH factoring clause would help.
Here's an example:
with
first_query as
(select id, first_column, ...
from (select ABC||DEF||GHI as id, ...)
),
second_query as
(select id, some_column, ...
from (select JKM||OPQ||RST as id, ...)
)
select id from first_query
minus
select id from second_query;
For another result you'd just switch the tables, e.g.
with ... <the same as above>
select id from second_query
minus
select id from first_query

Oracle Select via loop in Stored Procedure

I'm trying to select data from a table based on a array of filters calculated from another query. I have tried to describe what I'm trying to accomplish
in pseudo code below.
SELECT Equipment, MIN(TIME) as FractionStart, MAX(TIME) as FractionEnd
INTO FRACTIONS
FROM DATA
WHERE ID = 1
GROUP BY (Equipment)
/* Pseudo code */
FOR EACH ROW IN FRACTIONS
INSERT INTO MYTABLE (SELECT * FROM EVENTTABLE WHERE EVTTIME BETWEEN ROW.FractionStart AND ROW.FractionEnd AND EVTAREA = ROW.Equipment);
FOR NEXT;
RETURN MYTABLE;
I have been looking at cursors, but I haven't figured out how I can add rows to them in a loop. Am I looking at the right functions? or is there a better way to solve this?
Can you try this?
INSERT INTO MYTABLE
(
SELECT * FROM EVENTTABLE te,
(
SELECT Equipment, MIN(TIME) as FractionStart, MAX(TIME) as FractionEnd
FROM DATA
WHERE ID = 1
GROUP BY (Equipment)
) td
WHERE te.EVTTIME BETWEEN td.FractionStart AND td.FractionEnd AND te.EVTAREA = td.Equipment
);

ORA-000932 When joining an xmltype to a varchar field

a little backstory, i have a comma delimited column in a database, that i have tried to convert into a many to many junction table, so instead of having
ID REPORT
1 5,6,7
i would see
ID REPORT
1 5
1 6
1 7
the query below does just that, and seems to work correctly,
with t as (select id,hqcat from report)
SELECT id, EXTRACT(column_value,'/e/text()') hqcat from t x,
TABLE(XMLSEQUENCE(EXTRACT(XMLTYPE('<ROW><e>'||REPLACE(hqcat,',','</e><e>')||'</e></ROW>'),'//e')))
however when i try to join it to my lookup table by hqcat, i get a ORA-00932 error about inconstant data types, please help me, how do i change my original query to make it work with other tables and joins?
here is the join causing the error:
select *
from BIN03 a11
join ( with t as (select id,hqcat from report)
SELECT id, EXTRACT(column_value,'/e/text()') hqcat from t x,
TABLE(XMLSEQUENCE(EXTRACT(XMLTYPE('<ROW><e>'||REPLACE(hqcat,',','</e><e>')||'</e></ROW>'),'//e')))) a12
on (a11.CODE = a12.HQCAT);
Thank you!
i've also tried to create my new "many to many" view as so:
WITH TAB AS
( (select id ID, hqcat STR from report))
SELECT ID as ID,
REGEXP_SUBSTR (STR, '[^,]+', 1, LEVEL) HQCAT FROM TAB
CONNECT BY LEVEL <= (regexp_count(str,',') + 1);
but this just reduces query speeds by 1000's.....
The (deprecated) extract function returns an XMLType, not a varchar2. You can cast it to the type you want, either in the inline view:
cast(EXTRACT(column_value,'/e/text()') as varchar2(3))
or when comparing the value:
a11.CODE = cast(a12.HQCAT as varchar2(3))
I've guessed the variable size you need, obviously, you might need it to be different. You could also use the XMLType getStringVal() function as shown below.
It seems little odd to mix an old-style cross join with a comma in the inline view's from clause, with ANSI joins in the rest, and you can get odd (or hard to debug, anyway) results doing that. You could also use another CTE for clarity:
with t as (select id, hqcat from report),
a12 as (
select id, extract(column_value,'/e/text()').getstringval() hqcat
from t
cross join table(xmlsequence(extract(xmltype('<ROW><e>'||
replace(hqcat,',','</e><e>')||'</e></ROW>'),'//e')))
)
select *
from a12
join bin03 a11 on a11.code = a12.hqcat;

Need to select column from subquery into main query

I have a query like below - table names etc. changed for keeping the actual data private
SELECT inv.*,TRUNC(sysdate)
FROM Invoice inv
WHERE (inv.carrier,inv.pro,inv.ndate) IN
(
SELECT carrier,pro,n_dt FROM Order where TRUNC(Order.cr_dt) = TRUNC(sysdate)
)
I am selecting records from Invoice based on Order. i.e. all records from Invoice which are common with order records for today, based on those 3 columns...
Now I want to select Order_Num from Order in my select query as well.. so that I can use the whole thing to insert it into totally seperate table, let's say orderedInvoices.
insert into orderedInvoices(seq_no,..same columns as Inv...,Cr_dt)
(
SELECT **Order.Order_Num**, inv.*,TRUNC(sysdate)
FROM Invoice inv
WHERE (inv.carrier,inv.pro,inv.ndate) IN
(
SELECT carrier,pro,n_dt FROM Order where TRUNC(Order.cr_dt) = TRUNC(sysdate)
)
)
?? - how to do I select that Order_Num in main query for each records of that sub query?
p.s. I understand that trunc(cr_dt) will not use index on cr_dt (if a index is there..) but I couldn't select records unless I omit the time part of it..:(
If the table ORDER1 is unique on CARRIER, PRO and N_DT you can use a JOIN instead of IN to restrict your records, it'll also enable you to select whatever data you want from either table:
select order.order_num, inv.*, trunc(sysdate)
from Invoice inv
join order ord
on inv.carrier = ord.carrier
and inv.pro = ord.pro
and inv.ndate = ord.n_dt
where trunc(order.cr_dt) = trunc(sysdate)
If it's not unique then you have to use DISTINCT to deduplicate your record set.
Though using TRUNC() on CR_DT will not use an index on that column you can use a functional index on this if you do need an index.
create index i_order_trunc_cr_dt on order (trunc(cr_dt));
1. This is a really bad name for a table as it's a keyword, consider using ORDERS instead.

how to get alphabetically next and prev records wiht minimal fetched records?

I have a page that is displaying a company name and its details from a table A.
Now say i have a company displayed,and its name is 'Company_one' now i want to have alphabetically sorted next company and previous company and their details etc.
The data in my table is not sorted.Its stored as it gets the data.
So now what kind of query should i write that it gives only one previous and one next alphabetically sorted record??
Plz help!!
There's no nice way to do that in a single query. Just do two queries.
To get the previous one:
SELECT * FROM companies
WHERE name < variable_with_current_name
ORDER BY name DESC
LIMIT 1
To get the next one along:
SELECT * FROM companies
WHERE name > variable_with_current_name
ORDER BY name ASC
LIMIT 1
You need to use the sort clause to sort your table. The prototype for sort is:
sort by fieldname
Example Query:
select * from your_table sort by company asc
If you want to limit records, use limit clause:
select * from your_table sort by company asc limit 0, 1
Based on Dominic's answer, you can achieve the same result using a single query by combining them with WHERE and OR.
For example:
SELECT * FROM `companies`
WHERE (
`name` = IFNULL(
(SELECT `name` FROM `companies`
WHERE `name` < 'variable_with_current_name'
ORDER BY `name` DESC
LIMIT 1)
, 0)
OR
`name` = IFNULL(
(SELECT `name` FROM `companies`
WHERE `name` > 'variable_with_current_name'
ORDER BY `name` ASC
LIMIT 1)
, 0)
)
Hope that helps.

Resources