I have a table with commissions for example: https://i.stack.imgur.com/WWgTe.png
And second table with relations between them: https://i.stack.imgur.com/f3472.png
How to link all commissions which commission_number ends with "/1" to other with the same number?
Example:
222/2021/1 (parent) link to 222/2021/2, 222/2021/3, 222/2021/4... (child).
Can I link all commissions to his parents with one procedure?
You could construct the parent number by replacing the suffix with "1" and aggregate. The following gives a list of all related commission numbers:
select regexp_replace(commission_number, '[0-9]+$', '1') as parent_commission_number,
array_agg(commission_number)
from t
group by parent_commission_number;
You can do it like that
insert into commission_to_commission(child_commission_id,parent_commission_id)
select c_child.commission_id,c_parent.commission_id
from commission c_child, commission c_parent
where c_parent.commission_number = left(c_child.commission_number,9) || '1'
and right(c_child.commission_number,2) <> '/1';
See example here Fiddle
Related
I have a table named Ticket Numbers, which (for this example) contain the columns:
Ticket_Number
Assigned_Group
Assigned_Group_Sequence_No
Reported_Date
Each ticket number could contain 4 rows, depending on how many times the ticket changed assigned groups. Some of these rows could contain an assigned group of "Desktop Support," but some may not. Here is an example:
Example of raw data
What I am trying to accomplish is to get the an output that contains any ticket numbers that contain 'Desktop Support', but also the assigned group of the max sequence number. Here is what I am trying to accomplish with SQL:
Queried Data
I'm trying to use SQL with the following query but have no clue what I'm doing wrong:
select ih.incident_number,ih.assigned_group, incident_history2.maxseq, incident_history2.assigned_group
from incident_history_public as ih
left join
(
select max(assigned_group_seq_no) maxseq, incident_number, assigned_group
from incident_history_public
group by incident_number, assigned_group
) incident_history2
on ih.incident_number = incident_history2.incident_number
and ih.assigned_group_seq_no = incident_history2.maxseq
where ih.ASSIGNED_GROUP LIKE '%DS%'
Does anyone know what I am doing wrong?
You might want to create a proper alias for incident_history. e.g.
from incident_history as incident_history1
and
on incident_history1.ticket_number = incident_history2.ticket_number
and incident_history1.assigned_group_seq_no = incident_history2.maxseq
In my humble opinion a first error could be that I don't see any column named "incident_history2.assigned_group".
I would try to use common table expression, to get only ticket number that contains "Desktop_support":
WITH desktop as (
SELECT distinct Ticket_Number
FROM incident_history
WHERE Assigned_Group = "Desktop Support"
),
Than an Inner Join of the result with your inner table to get ticket number and maxSeq, so in a second moment you can get also the "MAXGroup":
WITH tmp AS (
SELECT i2.Ticket_Number, i2.maxseq
FROM desktop D inner join
(SELECT Ticket_number, max(assigned_group_seq_no) as maxseq
FROM incident_history
GROUP BY ticket_number) as i2
ON D.Ticket_Number = i2.Ticket_Number
)
SELECT i.Ticket_Number, i.Assigned_Group as MAX_Group, T.maxseq, i.Reported_Date
FROM tmp T inner join incident_history i
ON T.Ticket_Number = i.Ticket_Number and i.assigned_group_seq_no = T.maxseq
I think there are several different method to resolve this question, but I really hope it's helpful for you!
For more information about Common Table Expression: https://www.essentialsql.com/introduction-common-table-expressions-ctes/
I'm trying to learn SQL (through a company guide).
I have a list of invoices in a table and a tax code associated with one.
All of the invoices have one tax code, except number 1000001 which has two (and therefore two records in dbo.InvoiceTaxBreakDown.
The exercise wants me to create the variables TaxCode1 and TaxCode2. All except one record should have 'NULL' in TaxCode2.
This is my code:
SELECT
InvoiceNo,
TaxCode1 =
(
SELECT
TOP 1 T.TaxCode
FROM
dbo.InvoiceTaxBreakDown as T
WHERE
I.InvoiceGUID = T.InvoiceGUID
ORDER BY
T.TaxCode DESC
)
,
TaxCode2 =
(
SELECT
TOP 1 T.TaxCode
FROM
dbo.InvoiceTaxBreakDown as T
WHERE
I.InvoiceGUID = T.InvoiceGUID
ORDER BY
T.TaxCode ASC
)
FROM
dbo.InvoiceTaxBreakDown as I
I'm not sure if I even need to reference two data sources..
Only the first record is correct! Please help.
[Output][1]
My company database has column names like this: [VHCONO],[VHFACI],[VHPRNO],[VHMFNO],[VHITNO]. There is a reference table that identifies these colums.
I would like to write a query and have the field names changed based on this reference table. The ref table is named CFIFFD
Here is my query now:
SELECT VHCONO, VHFACI, VHPRNO, VHMFNO, VHITNO, VHVANO
FROM MWOHED
I think you want such an unpivoting
select VHFLDI, VHFTXT
from
( Select max(VHCONO) As VHCONO,
max(VHFACI) As VHFACI,
max(VHPRNO) As VHPRNO,
max(VHMFNO) As VHMFNO,
max(VHITNO) As VHITNO,
max(VHVANO) As VHVANO
From MWOHED ) p
unpivot
(VHFTXT for VHFLDI in
([VHCONO], [VHFACI], [VHPRNO], [VHMFNO], [VHITNO], [VHVANO])
) unpvt;
VHFLDI VHFTXT
------ ----------------
VHCONO Company
VHFACI Facility
VHPRNO Product
VHMFNO Man.Order Number
VHITNO item number
VHVANO product variant
Rextester Demo
I am trying to write a query based on 2 components. I have two fields called 'Plan_code' and another field called 'PolicyNum'. I have multiple records of 'PolicyNum', but they will have unique 'Plan_code' associated to them. What I want to find is if one of the 'PolicyNum' duplicates have 'Plan_Code' of 1, to pull all the PolicyNum duplicates. When I write the below query
SELECT PolicyNum
,Plan_Code
FROM [dbo].[IMS_IFCRP01_AA]
WHERE Plan_Code = '1'
I will only get that one record where a PolicyNum will have Plan_Code = 1. I need all the duplicates of the PolicyNum's for this analysis. Any help would be appreciated. Thanks.
One method uses exists or in:
SELECT ia.*
FROM [dbo].[IMS_IFCRP01_AA] ia
WHERE ia.PolicyNum IN (SELECT ia2.PolicyNum
FROM [dbo].[IMS_IFCRP01_AA] ia2
WHERE ia2.Plan_Code = '1'
);
If you need dublicate PolicyNum.
SELECT
PolicyNum
FROM [dbo].[IMS_IFCRP01_AA]
WHERE
Plan_Code = '1'
GROUP BY
PolicyNum
HAVING COUNT(*) > 1
Need help on a query using sql server 2005
I am having two tables
code
chargecode
chargeid
orgid
entry
chargeid
itemNo
rate
I need to list all the chargeids in entry table if it contains multiple entries having different chargeids
which got listed in code table having the same charge code.
data :
code
100,1,100
100,2,100
100,3,100
101,11,100
101,12,100
entry
1,x1,1
1,x2,2
2,x3,2
11,x4,1
11,x5,1
using the above data , it query should list chargeids 1 and 2 and not 11.
I got the way to know how many rows in entry satisfies the criteria, but m failing to get the chargeids
select count (distinct chargeId)
from entry where chargeid in (select chargeid from code where chargecode = (SELECT A.chargecode
from code as A join code as B
ON A.chargecode = B.chargeCode and A.chargetype = B.chargetype and A.orgId = B.orgId AND A.CHARGEID = b.CHARGEid
group by A.chargecode,A.orgid
having count(A.chargecode) > 1)
)
First off: I apologise for my completely inaccurate original answer.
The solution to your problem is a self-join. Self-joins are used when you want to select more than one row from the same table. In our case we want to select two charge IDs that have the same charge code:
SELECT DISTINCT c1.chargeid, c2.chargeid FROM code c1
JOIN code c2 ON c1.chargeid != c2.chargeid AND c1.chargecode = c2.chargecode
JOIN entry e1 ON e1.chargeid = c1.chargeid
JOIN entry e2 ON e2.chargeid = c2.chargeid
WHERE c1.chargeid < c2.chargeid
Explanation of this:
First we pick any two charge IDs from 'code'. The DISTINCT avoids duplicates. We make sure they're two different IDs and that they map to the same chargecode.
Then we join on 'entry' (twice) to make sure they both appear in the entry table.
This approach gives (for your example) the pairs (1,2) and (2,1). So we also insist on an ordering; this cuts to result set down to just (1,2), as you described.