SQL transform two records into two columns - sql

I don´t know if the title is correct but basicaly I need this(result of query):
r_e_s_id person_id
89074 161704
89074 161703
89095 161708
89095 161707
68651 129884
68651 129883
81512 161074
81512 161073
To be inserted/updated(in case that there will be r_e_s_id values in table) into table COMM_PROP which has cols - r_e_s_id, perid1, perid2...like this:
r_e_s_id perid1 perid2
89074 161704 161703
89095 161708 161707
68651 129884 129883
81512 161074 161073
How can I do this? I´m using Oracle 11g.
Thank you for any help!!!

Considering only 2 values i.e. 1 max and 1 min per r_e_s_id, Try this:-
SELECT r_e_s_id, MAX(person_id) AS perid1, MIN(person_id) AS perid2
FROM YOUR_TABLE
GROUP BY r_e_s_id;

Related

How to fetch specified data based on two columns in PostgreSQL

I am new to writing queries in SQL, and struggling to get the datas from table.
I have a table called "address" in DB. Here, I have more columns and specifically from two column I need to get the datas filtered out. Both are integers
frequency_type_id
date_of_invoice_id
I need to get rows only where frequency_type_id = 1 and date_of_invoice_id=2, and frequency_type_id = 2 and date_of_invoice_id=3, frequency_type_id = 3 and date_of_invoice_id=1.
I am trying with the following query, but its not filtering properly,
SELECT address_id, company_name,
invoice_batch_billing, frequency_type_id, date_of_invoice_id
FROM pls.address where invoice_style='Consolidated'
and frequency_type_id in
(SELECT frequency_type_id from pls.address where frequency_type_id=1 and date_of_invoice_id=1);
Hope someone assist.
Try below query:
SELECT address_id, company_name,
invoice_batch_billing, frequency_type_id, date_of_invoice_id
FROM pls.address where invoice_style='Consolidated'
and ((frequency_type_id=1 and date_of_invoice_id=2) or (frequency_type_id=2 and date_of_invoice_id=2) or (frequency_type_id=3 and date_of_invoice_id=2))
Try this:
SELECT
address_id,
company_name,
invoice_batch_billing,
frequency_type_id,
date_of_invoice_id
FROM
pls.address
WHERE
frequency_type_id IN (1,2,3)
AND
date_of_invoice_id=2
If I have understand your question correctly, this will suffice your requirement.

compare records within same table

I have Student_Table with following structure.
Student_Note Table
student_id seq_num note
11212 1 firstnote
11212 2 secondNote
11212 3 thirdNote
21232 1 secondstudentnote1
21232 2 secondstudentnote2
so on
I want to get latest note (which has largest seq_num) for a particilar student.
I have tried following query
select tn.note from Student_Note tn JOIN Student_Note tn1
ON (tn.student_id =tn1.student_id AND tn.seq_num < tn1.seq_num)
where tn.student_id=11212
it is giving more than one row. How to achieve aforementioned scenario ?
I forgot mention that I am using above query as subquery . As per sybase TOP clause will not work in subquery.
P.S. I am using sybase.
OK - changed as apparently cannot use TOP.........
select tn.note from Student_Note tn
where tn.student_id=11212
and tn.seq_num = (SELECT MAX(seq_num) from Student_Note WHERE Student_Note.Student_Id = tn.Student_Id)
Please try this
select substring(max(str(seq_num,10,'0') + note),11,len(note))
from Student_Note
where student_id=11212

How to sum column of result in sql server

I'm facing a problem, I want to sum the column of my results, this my code:
SELECT
[study_service]
,DATEDIFF(minute,[study_date_arrivee],[study_date_patient_prepared]) as [Time-ARRIVED-PREPARED]
,DATEDIFF(minute,[study_date_arrivee],[study_date_start]) as [Time-ARRIVED-START]
,DATEDIFF(minute,[study_date_start],[study_date_dossier_pret]) as [Time-START-DOSSPRET]
,DATEDIFF(minute,[study_date_dossier_pret],[study_date_remise_resultat]) as [Time-DOSSPRET-DOSSREMIS]
FROM
[VisionTST].[dbo].[PremierTrimestre]
I want the final result like this:
Total of `[Time-ARRIVED-PREPARED]`,`[Time-ARRIVED-Start]`,...
SELECT
[study_service]
,sum(DATEDIFF(minute,[study_date_arrivee],[study_date_patient_prepared])) as [Time-ARRIVED-PREPARED]
,sum(DATEDIFF(minute,[study_date_arrivee],[study_date_start])) as [Time-ARRIVED-START]
,sum(DATEDIFF(minute,[study_date_start],[study_date_dossier_pret])) as [Time-START-DOSSPRET]
,sum(DATEDIFF(minute,[study_date_dossier_pret],[study_date_remise_resultat])) as [Time-DOSSPRET- DOSSREMIS]
FROM [VisionTST].[dbo].[PremierTrimestre]

How can I group by a table by the date?

I have a table that contains:
Name / date
test1 24-11-2014
test2 24-11-2014
teste3 23-11-2014
Im trying to make a query that count the number of names per date, But Its not working fine, it is returning 3 instead of 2.
My query:
SELECT DATE(data),COUNT(data) as num FROM users
WHERE name = '$user' GROUP BY DATE(data)
How can I fix it?
THanks! Sorry for my english..
You don't need the DATE() function you can simply do this, also you are counting names, not the dates
SELECT date,COUNT(name) as num FROM users
WHERE name = '$user' GROUP BY date
Also you are writing 'data' instead of 'date', what is the column name?

How to Avoid Duplicate ID's In Access SQL

I have a problem that I hope you can help me.
I have the next query on Access SQL.
SELECT
ID_PLAN_ACCION, ID_SEGUIMIENTO,
Max(FECHA_SEGUIMIENTO) AS MAX_FECHA
FROM
SEGUIMIENTOS
WHERE
(((ID_PLAN_ACCION) = [CODPA]))
GROUP BY
ID_PLAN_ACCION, ID_SEGUIMIENTO;
And it returns this information:
ID_PLAN_ACCION ID_SEGUIMIENTO MAX_FECHA
-----------------------------------------------
A1-01 1 16/01/2014
A1-01 2 30/01/2014
But I really need that it throws off this:
ID_PLAN_ACCION ID_SEGUIMIENTO MAX_FECHA
----------------------------------------------
A1-01 2 30/01/2014
As you can see I only need the record that has the most recently date, not all the records
The GROUP BY doesn't work.
Please can you tell me what can I do? I am new on all this.
Thank you so much!!
PD: Sorry for my english, I'm learning
This will produce the results you want:
SELECT ID_PLAN_ACCION, max(ID_SEGUIMIENTO) as ID_SEGUIMIENTO, Max(FECHA_SEGUIMIENTO) AS MAX_FECHA
FROM SEGUIMIENTOS
WHERE ID_PLAN_ACCION = [CODPA]
GROUP BY ID_PLAN_ACCION;
I removed id_sequiimiento from the group by and added an aggregation function to get the max value. If the ids increase along with the date, this will work.
Another way to approach this query, though, is to use top and order by:
SELECT top 1 ID_PLAN_ACCION, ID_SEGUIMIENTO, FECHA_SEGUIMIENTO
FROM SEGUIMIENTOS
WHERE ID_PLAN_ACCION = [CODPA]
ORDER BY FECHA_SEGUIMIENTO desc;
This works because you are only returning one row.
EDIT:
If you have more codes, that you are looking at, you need a more complicated query. Here is an approach using where/not exists:
SELECT ID_PLAN_ACCION, ID_SEGUIMIENTO, FECHA_SEGUIMIENTO
FROM SEGUIMIENTOS s
WHERE not exists (select 1
from SEGUIMIENTOS s2
where s.ID_PLAN_ACCION = s2.ID_PLAN_ACCION and
s2.FECHA_SEGUIMIENTO > s.FECHA_SEGUIMIENTO
)
ORDER BY FECHA_SEGUIMIENTO desc;
You can read this as: "Get me all rows from SEGUIMIENTOS where there is no other row with the same ID_PLAN_ACCION that has a larger date". Is is another way of saying that the original row has the maximum date.