Counting points a on linestring - sql

I am trying to counts the number of points on a line for each row in the following table
CREATE TABLE outils.prod(
pk INTEGER PRIMARY KEY,
cable VARCHAR (25),
PA VARCHAR (10),
Art VARCHAR(7),
FT Numeric,
BT Numeric
);
INSERT INTO outils.prod (pk)
SELECT id_ftth
FROM outils.cable
WHERE type_cable = '2' ;
SELECT ADDGEOMETRYCOLUMN('outils','prod','geom',2154,'MultiLineString',2);
I have tried to update my line table but i have trouble getting an answer for each row.
UPDATE outils.prod SET FT=(SELECT COUNT( ST_INTERSECTION(outils.prod.geom,outils.ft.geom))
FROM outils.prod , outils.ft)
With the above code i managed to get the total number of intersection for every line but i would like to have the count by line in my line table.
Thank you ,
Hugo

You would have to write a sub-query to do the count per line.
Also you don't need to compute the intersection (the geom), but just to check if they intersect, which is much faster.
UPDATE outils.prod
SET FT= sub.cnt
FROM (
SELECT pk, count(*) as cnt
FROM outils.ft
JOIN outils.prod ON ST_INTERSECTS(prod.geom, ft.geom)
)
WHERE prod.pk = sub.pk;

Related

How to find rows with max value without aggregate functions SQLPLUS Oracle11g

I'm trying to find rows with max credit on my table ,
CREATE TABLE Course(
CourseNr INTEGER,
CourseTitel VARCHAR(60),
CourseTyp VARCHAR(10),
CourselenghtDECIMAL,
Credit DECIMAL,
PRIMARY KEY (CourseNr)
);
and there is more than one courses with max value. I dont want to use any default functions for that, any ideas?
Presumably, you want the rows with the maximum credit. A common method is to find any rows that have no larger credit:
select c.*
from course c
where c.credit >= all (select c2.credit from course c2);
Get the rows with Credit for which there don't exist any rows with greater Credit:
SELECT
c.*
FROM Course c
WHERE
NOT EXISTS (
SELECT 1 FROM Course WHERE Credit > c.Credit
)

Reading 2 rows together from SQL Server table

I've got a table which has the following data:
Record Type MID CvvId Date Amount
------------- ---------- ------------- ------------- ---------
2E 8715613516 2014-25-03 27.12
3E asd5156154485 M
2E 8751651650 2014-25-03 27.13
3E asd5165434485 S
No I have to read the values of this table and insert it another table, having columns as:
MID, CvvId, Date, Amount.
Now for every record of Record Type "2E", the row just after that would be always of record type "3E"
For columns MID, CvvId, Date, Amount I have to use the record type "2E". For CvvId, I have to use the record type "3E". After every 2E record, I would get a 3E record. I have to write a stored procedure in SQL Server to insert this data into another table. How can this be achieved?
I changed your table by adding a transactionId column which is unique for both 2E and 3E records as follows
create table tableE(
TId int,
rtype varchar(20),
MID varchar(20),
CvvId varchar(20),
date date,
amount decimal(18,2))
Then following SELECT statement will help you combine them into a single row
select *
from tableE t1
inner join tableE t2 on t1.tid = t2.tid
where t1.rtype = '2E' and t2.rtype = '3E'
Here is a process for doing this using bulk insert. As suggested in another answer, you want to change the definition of the underlying table to have an identity column:
create table atable (
atableId int not null identity(1, 1) primary key,
rtype varchar(20),
MID varchar(20),
CvvId varchar(20),
date date,
amount decimal(18,2)
);
Then, define a view on this table that excludes the identity:
create view v_atable as
select rtype, mid, cvvid, date, amount
from atable;
Now, use bulk insert into the view. This will generate an auto-incrementing identity value for each row. You then have an ordering and can identify the next row.
I got it to work. As recommended above, added a new auto-increment (not necessarily P.k) field as Tid. and wrote the following query: -
SELECT p.MID, LEAD(p.CvvId) OVER (ORDER BY p.Tid) NextCvv,
LEAD(p.RecordType) OVER (ORDER BY p.Tid) NextRecordType
FROM table p
after filtering through the record set, on some business conditions, it worked. Thanks all for help!.

Unique constraint on Distinct select in Oracle database

I have a data processor that would create a table from a select query.
<_config:table definition="CREATE TABLE TEMP_TABLE (PRODUCT_ID NUMBER NOT NULL, STORE NUMBER NOT NULL, USD NUMBER(20, 5),
CAD NUMBER(20, 5), Description varchar(5), ITEM_ID VARCHAR(256), PRIMARY KEY (ITEM_ID))" name="TEMP_TABLE"/>
and the select query is
<_config:query sql="SELECT DISTINCT ce.PRODUCT_ID, ce.STORE, op.USD ,op.CAD, o.Description, ce.ITEM_ID
FROM PRICE op, PRODUCT ce, STORE ex, OFFER o, SALE t
where op.ITEM_ID = ce.ITEM_ID and ce.STORE = ex.STORE
and ce.PRODUCT_ID = o.PRODUCT_ID and o.SALE_ID IN (2345,1234,3456) and t.MEMBER = ce.MEMBER"/>
When I run that processor, I get an unique constraint error, though I have a distinct in my select statement.
I tried with CREATE TABLE AS (SELECT .....) its creating fine.
Is it possible to get that error? I'm doing a batch execute so not able to find the individual record.
The select distinct applies to the entire row, not to each column individually. So, two rows could have the same value of item_id but be different in the other columns.
The ultimate fix might be to have a group by item_id in the query, instead of select distinct. That would require other changes to the logic. Another possibility would be to use row_number() in a subquery and select the first row.

SQL Insert rows into table that must have 2 distinct columns but also one non distinct column

I have two tables, a promotion table and a prize table.
CREATE TABLE PRIZE(
PRIZEID INT NOT NULL PRIMARY KEY,
COST DOUBLE NOT NULL,
PRIZENAME VARCHAR(100) NOT NULL,
)
CREATE TABLE PROMOTION (
PROMOTIONID INTEGER NOT NULL,
LEVEL INTEGER NOT NULL,
AMOUNT NOT NULL,
COST DOUBLE NOT NULL DEFAULT 0,
PRIZENAME VARCHAR(100) NOT NULL DEFAULT ' ',
PRIZEID INTEGER
)
The prize table is currently empty and I want to copy records from the promotion table into the prize table. in doing this I only want to select those records that have unique values in two columns, the prizename and the cost. I will be dropping those columns from promotion afterwards.
Right now I have the following sql statement
INSERT INTO prize(PRIZEID, COST, PRIZENAME)
SELECT r.PRIZEID, r.COST, r.PRIZENAME
FROM PROMOTION r;
but this will insert all records into the prize table. I realize I can use the distinct keyword in the select to select unique instances of cost and prizename but in my case the prizeid will always be unique and since the distinct keyword applies to all columns in the select clause it won't help.
Thanks in advance!
You'll have to just replace the PRIZEID value with new ones. Because it sounds like you currently have duplicates on your PROMOTION table
First add all the distinct PRIZENAMEs and COSTs to your new PRIZE table:
INSERT INTO prize(PRIZEID, COST, PRIZENAME)
SELECT DISTINCT (SELECT MAX(PRIZEID)+1 FROM PRIZE), r.COST, r.PRIZENAME
FROM PROMOTION r;
Then update your PROMOTIONs table with the new PRIZEID
UPDATE PROMOTION R
SET R.PRIZEID =
(SELECT P.PRIZEID
FROM PRIZE
WHERE P.PRIZENAME=R.PRIZENAME
AND P.COST=R.COST);
Then, I think from there you can safely delete the columns from your PROMOTIONs table
This will get the unique prizes based on prizename and cost and get the lowest prizeid for the group.
INSERT INTO prize(PRIZEID, COST, PRIZENAME)
SELECT MIN(r.PRIZEID), r.COST, r.PRIZENAME
FROM PROMOTION r
GROUP BY r.COST, r.PRIZENAME
;

Remove Duplicate Records While Retaining One Record

I have a table of phone records as such:
ID int (Primary Key)
company varchar
dbaname varchar
coaddress varchar
cocity varchar
costate varchar
cozip varchar
phonenum varchar
What I want to accomplish is to remove all the duplicates phone numbers (phonenum field) but retain one occurence.
When doing a duplicate check, I see there are over 41000 duplicate phone numbers in the table (total of about 141000).
How would I go about doing this based on the phone number?
Assuming you want to keep only the latest record:
DELETE yourTable
FROM yourTable T
LEFT JOIN
( SELECT MAX(ID) [ID]
FROM yourTable
GROUP BY Phonenum
) MaxT
ON MaxT.ID = T.ID
WHERE MaxT.ID IS NULL
I'd definitely archive what you are deleting into another table though as there is no guarantee you are removing the correct record without checking manually or adding further criteria to the Delete statement.