August 21, 2017 at 12:05 pm
Hello,
I have this script which is an extract of my database. I don't design it, so the structure will not change.
I have two tables with ids and ids_done like repairs in progress and done.
CREATE TABLE ids
(
requestID varchar(50),
orderID varchar(50)
);
CREATE TABLE ids_done
(
requestID varchar(50),
orderID varchar(50)
);
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200013','
INSERT INTO ids(requestID, orderID) VALUES ('ABC1706200013-1','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014-1','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014-2','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014-3','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014-4','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014-5','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014-6','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014-7','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014-8','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014-9','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014-A','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014-B','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200014-C','
INSERT INTO ids(requestID, orderID) VALUES ('ABC1706200014-D','
INSERT INTO ids(requestID, orderID) VALUES ('ABC1706200015','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200016','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200016-1','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200016-2','
INSERT INTO ids(requestID, orderID) VALUES ('ABC1706200016-3','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200017','
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200017-2','
INSERT INTO ids(requestID, orderID) VALUES ('ABC1706200017-1','
INSERT INTO ids(requestID, orderID) VALUES ('ABC1706200017-3','
SELECT
MAX(SUBSTRING(ol.orderID, LEN(ol.orderID) - 3, 1)) as Maxi,
COALESCE(ol.requestID,cl.reque
,COALESCE(ol.orderID, cl.orderID) as orderID
FROM ids ol
LEFT join ids_done cl ON cl.orderID LIKE SUBSTRING(ol.orderID, 1, 13 ) + '%'
GROUP BY COALESCE(ol.requestID,cl.reque
,COALESCE(ol.orderID, cl.orderID)
ORDER BY COALESCE(ol.orderID, cl.orderID)
I want to have max order_id for both tables like this :
The increment have these rules :
The first repair is without any '-'.
If the client cancel it, a repair with -1 is created and the first one is transfer to the done table
And so one for next.
The size is limited to 15 letters, that why there is the alphabet after.
Is it possible to help me and obtain the max for all repairs ?
Regards
August 21, 2017 at 2:04 pm
I think it's the GROUP BY with a COALESCE that's mixing it up, maybe if you figure out the base of the requestID for grouping?SELECT MAX(requestID), MAX(orderID)
FROM
(SELECT CASE WHEN CHARINDEX('-', requestID) = 0 THEN requestID ELSE LEFT(requestID, LEN(requestID) - 2) END AS baserequestID, requestID, orderID
FROM ids
UNION ALL
SELECT CASE WHEN CHARINDEX('-', requestID) = 0 THEN requestID ELSE LEFT(requestID, LEN(requestID) - 2) END AS baserequestID, requestID, orderID
FROM ids_done) u
GROUP BY baserequestID
August 22, 2017 at 12:10 pm
Hello,
The result of your query is good.
But as I want the last -x with the '@@1' part, I set the select like this : SELECT MAX(requestID), CONCAT(MAX(orderID),'@@1')
But if you add these lines :
INSERT INTO ids(requestID, orderID) VALUES ('ABC1706200018’,’ABC1706200018@@1’);
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200018’,’ABC1706200018@@2’);
The @@2 line will not appear at the end.
So how can I have it ?
August 22, 2017 at 1:57 pm
I'm confused...the query Chris provided does work--it brings back the '...@@2' value after inserting the 2 '%18%' values.
????
What are you expecting from the query that you are not currently getting?
Alan H
MCSE - Data Management and Analytics
Senior SQL Server DBA
Best way to ask a question: http://www.sqlservercentral.com/articles/Best+Practices/61537/
August 22, 2017 at 2:02 pm
I ran those INSERT statements as you have them in my sandbox database, and reran my query, and I get:
requestID orderID
ABC1706200013-1 ABC1706200013@@1ABC1706200013@@1
ABC1706200014-D ABC1706200014@@1ABC1706200014@@1
ABC1706200015 ABC1706200015@@1ABC1706200015@@1
ABC1706200016-3 ABC1706200016@@1ABC1706200016@@1
ABC1706200017-3 ABC1706200017@@1ABC1706200017@@1
ABC1706200018 ABC1706200018@@2ABC1706200018@@2
August 22, 2017 at 2:15 pm
Hello,
I want something like :
There are last ids, last -x ids and last '@@x' ids.
The @@2 is when someone change the order a little bit, the system create and the '@@2' and close the '@@1'.
To add a sample with a -x :
INSERT INTO ids(requestID, orderID) VALUES ('ABC1706200018-1’,’ABC1706200018-1@@2’);
INSERT INTO ids_done(requestID, orderID) VALUES ('ABC1706200018-1’,’ABC1706200018-1@@1’);
The result should be :
I'm not the owner of the database and can't change the way the tool operate and use it.
So I use the database and can't do any changes.
Regards
August 22, 2017 at 2:43 pm
ah, sorry, I see my mistake, the @ comes after - SELECT requestID, orderID
FROM
(SELECT baserequestID, requestID, orderID, ROW_NUMBER() OVER (PARTITION BY baserequestID ORDER BY requestID DESC, orderID DESC) AS recentness
FROM
(SELECT CASE WHEN CHARINDEX('-', requestID) = 0 THEN requestID ELSE LEFT(requestID, LEN(requestID) - 2) END AS baserequestID, requestID, orderID
FROM ids
UNION ALL
SELECT CASE WHEN CHARINDEX('-', requestID) = 0 THEN requestID ELSE LEFT(requestID, LEN(requestID) - 2) END AS baserequestID, requestID, orderID
FROM ids_done) u
) rn
WHERE rn.recentness = 1
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply