Change column name based on reference table - sql

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

Related

SQL - Returning fields based on where clause then joining same table to return max value?

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/

Link records to others

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

How to store output using different unique key (tried CROSS APPLY STRING_SPLIT)?

I'm trying to understand how I can convert what is currently stored in my sql tbl. The LBid is the unique ID and the productString column contains all of the product IDs that exist within each LBid. I need to write a statement to store each individual product ID (stored within productString) as the unique ID with all the associated LBids in a CSV format.
Current output
LBid title noProducts productString
51631 Slide2 NULL NULL
51636 Slide3 1 49518
51638 Slide4 1 49512
51641 Slide5 2 49512,49518
51643 Slide6 4 49512,46163,49518,46157
51645 Slide7 3 49874,47339,46165
51647 Slide8 5 49874,48807,49934,46766,47339
51649 Slide9 7 46165,48807,49874,47339,46766,47648,47948
Desired output
Product ID LBid
49518 51636,51641,51643
49512 51638,51641,51643
etc...
I've tried using the following code which helps me to split out the productString column, but I don't know how to save the output from this SELECT statemnet.
SELECT value, LBid, title, noProducts, productString
FROM dbo.LB
CROSS APPLY STRING_SPLIT(productString, ',')
GROUP BY value, LBid, title, noProducts, productString
ORDER BY value;
Output from above statement (this is a sample, may not directly reflect above code)
value id title productString
45891 52316 Slide337 45891,47205,47205,47209,47209,47443,47447,49246,47131,48744,48746
45909 52708 Slide533 47260,47248,49204,50783,48817,47270
45911 52708 Slide533 47260,47248
45917 51893 Slide129 45917,47910,46073,46077,50119,48813,45921,46729,46708,46706
45921 51893 Slide129 45917,47910,46073,46077,50119,48813,45921,46729,46708,46706
45923 51843 Slide104 50299,50132,50130,49548,49496,49577,45923,50060,46706
45923 51845 Slide105 45923,49577,50132,48923,46706,49747
Any help would be greatly appreciated.
Thanks
SQL Azure / SMSS
I need to write a statement to store each individual product ID (stored within productString) as the unique ID with all the associated LBids in a CSV format.
You need to aggregate:
SELECT lb.LBid, STRING_AGG(s.value, ',')
FROM dbo.LB CROSS APPLY
STRING_SPLIT(lb.productString, ',') s
GROUP BY lb.LBid
ORDER BY lb.LBid;

Simple SQL query with join same table and get value from 2 row in one field

I have some data
I need result of query like this:
With this query :
select
early.sensor,
early.event,
late.value - early.value as value
from data_table as early
inner join data_table as late on
early.sensor=late.sensor and early.event=late.event
where late.event_date > early.event_date;
I get this result:
Assuming that the input table is called "data_table" and the fourth column in it is called event_date, then something like this should do it:
select
early.sensor,
early.event,
late.value - early.value as value
from
data_table as early
inner join data_table as late on
early.sensor=late.sensor
and early.event=late.event
where
late.event_date > early.event_date;
But if there are more than two values (as #Thorston Kettner asks), then you will need some more complex logic.

SQL Count of columns result for all existing Dates in the table

I have a table DTTransaction and DTHotelTransaction with TransactionID is the primary key in former and foreign key in later.
Columns in DTTransaction :machinename(varchar),BookedOn(datetime),TransactionID(int)
Columns in DTHotelTransaction :HCOMCID(int) ,TransactionID
Requirement :
For every BookedOn value in DTTransaction , I want the machine name and count of machinename for these (415428,415429,415430,415431,415432) HCOMCID.
I am trying to do :
select DTTransaction.machinename,count(DTTransaction.machinename) from DTTransaction
join DTHotelReservation on DTTransaction.TransactionID =
DTHotelReservation.TransactionID
and DTHotelReservation.HCOMCID in (
415428,
415429,
415430,
415431,
415432)
where convert(varchar(10),BookedOn,101)='04/10/2013'
group by DTTransaction.machinename
But it will give only for a particular date.I want to get the count for all existing BookedOn values.
Thanks in advance.
If you want the sum of all dates, just remove the where clause:
select DTTransaction.machinename, count(DTTransaction.machinename)
from DTTransaction join
DTHotelReservation
on DTTransaction.TransactionID = DTHotelReservation.TransactionID and
DTHotelReservation.HCOMCID in (415428, 415429, 415430, 415431, 415432)
group by DTTransaction.machinename
If you want the results by date, then include that in your group by. For instance,
select DTTransaction.machinename, convert(varchar(10),BookedOn,101), count(DTTransaction.machinename)
from DTTransaction join
DTHotelReservation
on DTTransaction.TransactionID = DTHotelReservation.TransactionID and
DTHotelReservation.HCOMCID in (415428, 415429, 415430, 415431, 415432)
group by DTTransaction.machinename, convert(varchar(10),BookedOn,101)
order by 1, MAX(BookedOn)
I included an order by clause, so the results will be in order by date within each machine name.