ORDER BY a subqueries which has JOIN, LEFT JOIN and UNION












0















My question is: How do I order the subquery by PositionAssetId and then follow by its related PhysicalAssetId based on table TrxAssetPool?



I need LEFT JOIN because not all Position and Physical were linked together. Some of Position/Physical were standalone. A Physical might exist in PhysicalAsset and TrxPhysicalAsset but not exist in TrxAssetPool because it was not linked to any Position; and vice versa. These data also needed to be displayed.



CREATE TABLE `PositionAssets` (
`Id` int(5) unsigned NOT NULL,
`Code` varchar(50) NOT NULL,
`Desc` varchar(200) NOT NULL,
PRIMARY KEY (`Id`)
);
CREATE TABLE `PhysicalAssets` (
`Id` int(5) unsigned NOT NULL,
`Code` varchar(50) NOT NULL,
`Desc` varchar(200) NOT NULL,
PRIMARY KEY (`Id`)
);
CREATE TABLE `TrxPositionAssets` (
`Id` int(5) unsigned NOT NULL,
`MaintTrxId` int(5) unsigned NOT NULL,
`PositionAssetId` int(5) NOT NULL,
PRIMARY KEY (`Id`,`MaintTrxId`)
);
CREATE TABLE `TrxPhysicalAssets` (
`Id` int(5) unsigned NOT NULL,
`MaintTrxId` int(5) unsigned NOT NULL,
`PhysicalAssetId` int(5) NOT NULL,
PRIMARY KEY (`Id`,`MaintTrxId`)
);
CREATE TABLE `TrxAssetPool` (
`Id` int(5) unsigned NOT NULL,
`MaintTrxId` int(5) NOT NULL,
`PositionAssetId` int(5) NOT NULL,
`PhysicalAssetId` int(5) NOT NULL,
PRIMARY KEY (`Id`)
);
INSERT INTO `PositionAssets` (`Id`, `Code`, `Desc`) VALUES
('1', 'PositionC', 'Air conditioner'),
('2', 'PositionB', 'Laptop'),
('3', 'PositionA', 'Mobile Phone')
;
INSERT INTO `PhysicalAssets` (`Id`, `Code`, `Desc`) VALUES
('1', 'PhysicalD', 'Dunlop Car Tyre'),
('2', 'PhysicalA1', 'Samsung'),
('3', 'PhysicalB2', 'Acer'),
('4', 'PhysicalB1', 'Lenovo')
;
INSERT INTO `TrxPositionAssets` (`Id`, `MaintTrxId`, `PositionAssetId`) VALUES
('1', '1', '2'),
('2', '1', '3'),
('3', '1', '1')
;
INSERT INTO `TrxPhysicalAssets` (`Id`, `MaintTrxId`, `PhysicalAssetId`) VALUES
('1', '1', '2'),
('2', '1', '3'),
('3', '1', '1'),
('4', '1', '4')
;
INSERT INTO `TrxAssetPool` (`Id`,`MaintTrxId`,`PositionAssetId`,`PhysicalAssetId`) VALUES
('1', '1', '3', '2'),
('2', '1', '2', '4'),
('3', '1', '2', '3')
;

SELECT DataType, DataCode, DataDesc
FROM (
SELECT 'Position' AS DataType, pos.Code AS DataCode, pos.Desc AS DataDesc
FROM TrxPositionAssets trxpos
JOIN PositionAssets pos ON pos.Id = trxpos.PositionAssetId
LEFT JOIN TrxAssetPool trxpool ON (trxpool.PositionAssetId = trxpos.PositionAssetId and trxpool.MaintTrxId = trxpos.MaintTrxId)
WHERE trxpos.MaintTrxId = 1
UNION
SELECT 'Physical' AS DataType, phy.Code AS DataCode, phy.Desc AS DataDesc
FROM TrxPhysicalAssets trxphy
JOIN PhysicalAssets phy ON phy.Id = trxphy.PhysicalAssetId
LEFT JOIN TrxAssetPool trxpool ON (trxpool.PhysicalAssetId = trxphy.PhysicalAssetId and trxpool.MaintTrxId = trxphy.MaintTrxId)
WHERE trxphy.MaintTrxId = 1
) DataPool


Sample at sqlfiddle.com



Current result:



DataType    DataCode    DataDesc

Position PositionA Mobile Phone
Position PositionB Laptop
Position PositionC Air conditioner
Physical PhysicalA1 Samsung
Physical PhysicalB1 Lenovo
Physical PhysicalB2 Acer
Physical PhysicalD Dunlop Car Tyre


Expected Result:



DataType    DataCode    DataDesc

Position PositionA Mobile Phone
Physical PhysicalA1 Samsung
Position PositionB Laptop
Physical PhysicalB1 Lenovo
Physical PhysicalB2 Acer
Position PositionC Air conditioner
Physical PhysicalD Dunlop Car Tyre


Air conditioner is not related to any Physical. Dunlop Car Tyre is not related to any Position.










share|improve this question

























  • How do you know which position assets match which physical assets?

    – Gordon Linoff
    Nov 23 '18 at 12:08











  • @GordonLinoff it was linked in TrxAssetPool which exists PositionAssetId and PhysicalAssetId field

    – Eja
    Nov 27 '18 at 7:36











  • There is no point in repeating in natural language what we can read in code. Tell us how the output table is a function of the input tables--a row of values (DataId, ...) is in the result if & only if what is in the input? We don't need a link/image, we need DDL & the rest of a Minimal, Complete, and Verifiable example--cut & paste & runnable code including initialization code ((tabular format) with desired result (preferably ordered, so diffable) & clear specification.

    – philipxy
    Nov 27 '18 at 8:28













  • Possible duplicate of Using union and order by clause in mysql

    – philipxy
    Nov 27 '18 at 8:56











  • This is a faq. Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on Minimal, Complete, and Verifiable example.

    – philipxy
    Nov 27 '18 at 8:57
















0















My question is: How do I order the subquery by PositionAssetId and then follow by its related PhysicalAssetId based on table TrxAssetPool?



I need LEFT JOIN because not all Position and Physical were linked together. Some of Position/Physical were standalone. A Physical might exist in PhysicalAsset and TrxPhysicalAsset but not exist in TrxAssetPool because it was not linked to any Position; and vice versa. These data also needed to be displayed.



CREATE TABLE `PositionAssets` (
`Id` int(5) unsigned NOT NULL,
`Code` varchar(50) NOT NULL,
`Desc` varchar(200) NOT NULL,
PRIMARY KEY (`Id`)
);
CREATE TABLE `PhysicalAssets` (
`Id` int(5) unsigned NOT NULL,
`Code` varchar(50) NOT NULL,
`Desc` varchar(200) NOT NULL,
PRIMARY KEY (`Id`)
);
CREATE TABLE `TrxPositionAssets` (
`Id` int(5) unsigned NOT NULL,
`MaintTrxId` int(5) unsigned NOT NULL,
`PositionAssetId` int(5) NOT NULL,
PRIMARY KEY (`Id`,`MaintTrxId`)
);
CREATE TABLE `TrxPhysicalAssets` (
`Id` int(5) unsigned NOT NULL,
`MaintTrxId` int(5) unsigned NOT NULL,
`PhysicalAssetId` int(5) NOT NULL,
PRIMARY KEY (`Id`,`MaintTrxId`)
);
CREATE TABLE `TrxAssetPool` (
`Id` int(5) unsigned NOT NULL,
`MaintTrxId` int(5) NOT NULL,
`PositionAssetId` int(5) NOT NULL,
`PhysicalAssetId` int(5) NOT NULL,
PRIMARY KEY (`Id`)
);
INSERT INTO `PositionAssets` (`Id`, `Code`, `Desc`) VALUES
('1', 'PositionC', 'Air conditioner'),
('2', 'PositionB', 'Laptop'),
('3', 'PositionA', 'Mobile Phone')
;
INSERT INTO `PhysicalAssets` (`Id`, `Code`, `Desc`) VALUES
('1', 'PhysicalD', 'Dunlop Car Tyre'),
('2', 'PhysicalA1', 'Samsung'),
('3', 'PhysicalB2', 'Acer'),
('4', 'PhysicalB1', 'Lenovo')
;
INSERT INTO `TrxPositionAssets` (`Id`, `MaintTrxId`, `PositionAssetId`) VALUES
('1', '1', '2'),
('2', '1', '3'),
('3', '1', '1')
;
INSERT INTO `TrxPhysicalAssets` (`Id`, `MaintTrxId`, `PhysicalAssetId`) VALUES
('1', '1', '2'),
('2', '1', '3'),
('3', '1', '1'),
('4', '1', '4')
;
INSERT INTO `TrxAssetPool` (`Id`,`MaintTrxId`,`PositionAssetId`,`PhysicalAssetId`) VALUES
('1', '1', '3', '2'),
('2', '1', '2', '4'),
('3', '1', '2', '3')
;

SELECT DataType, DataCode, DataDesc
FROM (
SELECT 'Position' AS DataType, pos.Code AS DataCode, pos.Desc AS DataDesc
FROM TrxPositionAssets trxpos
JOIN PositionAssets pos ON pos.Id = trxpos.PositionAssetId
LEFT JOIN TrxAssetPool trxpool ON (trxpool.PositionAssetId = trxpos.PositionAssetId and trxpool.MaintTrxId = trxpos.MaintTrxId)
WHERE trxpos.MaintTrxId = 1
UNION
SELECT 'Physical' AS DataType, phy.Code AS DataCode, phy.Desc AS DataDesc
FROM TrxPhysicalAssets trxphy
JOIN PhysicalAssets phy ON phy.Id = trxphy.PhysicalAssetId
LEFT JOIN TrxAssetPool trxpool ON (trxpool.PhysicalAssetId = trxphy.PhysicalAssetId and trxpool.MaintTrxId = trxphy.MaintTrxId)
WHERE trxphy.MaintTrxId = 1
) DataPool


Sample at sqlfiddle.com



Current result:



DataType    DataCode    DataDesc

Position PositionA Mobile Phone
Position PositionB Laptop
Position PositionC Air conditioner
Physical PhysicalA1 Samsung
Physical PhysicalB1 Lenovo
Physical PhysicalB2 Acer
Physical PhysicalD Dunlop Car Tyre


Expected Result:



DataType    DataCode    DataDesc

Position PositionA Mobile Phone
Physical PhysicalA1 Samsung
Position PositionB Laptop
Physical PhysicalB1 Lenovo
Physical PhysicalB2 Acer
Position PositionC Air conditioner
Physical PhysicalD Dunlop Car Tyre


Air conditioner is not related to any Physical. Dunlop Car Tyre is not related to any Position.










share|improve this question

























  • How do you know which position assets match which physical assets?

    – Gordon Linoff
    Nov 23 '18 at 12:08











  • @GordonLinoff it was linked in TrxAssetPool which exists PositionAssetId and PhysicalAssetId field

    – Eja
    Nov 27 '18 at 7:36











  • There is no point in repeating in natural language what we can read in code. Tell us how the output table is a function of the input tables--a row of values (DataId, ...) is in the result if & only if what is in the input? We don't need a link/image, we need DDL & the rest of a Minimal, Complete, and Verifiable example--cut & paste & runnable code including initialization code ((tabular format) with desired result (preferably ordered, so diffable) & clear specification.

    – philipxy
    Nov 27 '18 at 8:28













  • Possible duplicate of Using union and order by clause in mysql

    – philipxy
    Nov 27 '18 at 8:56











  • This is a faq. Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on Minimal, Complete, and Verifiable example.

    – philipxy
    Nov 27 '18 at 8:57














0












0








0








My question is: How do I order the subquery by PositionAssetId and then follow by its related PhysicalAssetId based on table TrxAssetPool?



I need LEFT JOIN because not all Position and Physical were linked together. Some of Position/Physical were standalone. A Physical might exist in PhysicalAsset and TrxPhysicalAsset but not exist in TrxAssetPool because it was not linked to any Position; and vice versa. These data also needed to be displayed.



CREATE TABLE `PositionAssets` (
`Id` int(5) unsigned NOT NULL,
`Code` varchar(50) NOT NULL,
`Desc` varchar(200) NOT NULL,
PRIMARY KEY (`Id`)
);
CREATE TABLE `PhysicalAssets` (
`Id` int(5) unsigned NOT NULL,
`Code` varchar(50) NOT NULL,
`Desc` varchar(200) NOT NULL,
PRIMARY KEY (`Id`)
);
CREATE TABLE `TrxPositionAssets` (
`Id` int(5) unsigned NOT NULL,
`MaintTrxId` int(5) unsigned NOT NULL,
`PositionAssetId` int(5) NOT NULL,
PRIMARY KEY (`Id`,`MaintTrxId`)
);
CREATE TABLE `TrxPhysicalAssets` (
`Id` int(5) unsigned NOT NULL,
`MaintTrxId` int(5) unsigned NOT NULL,
`PhysicalAssetId` int(5) NOT NULL,
PRIMARY KEY (`Id`,`MaintTrxId`)
);
CREATE TABLE `TrxAssetPool` (
`Id` int(5) unsigned NOT NULL,
`MaintTrxId` int(5) NOT NULL,
`PositionAssetId` int(5) NOT NULL,
`PhysicalAssetId` int(5) NOT NULL,
PRIMARY KEY (`Id`)
);
INSERT INTO `PositionAssets` (`Id`, `Code`, `Desc`) VALUES
('1', 'PositionC', 'Air conditioner'),
('2', 'PositionB', 'Laptop'),
('3', 'PositionA', 'Mobile Phone')
;
INSERT INTO `PhysicalAssets` (`Id`, `Code`, `Desc`) VALUES
('1', 'PhysicalD', 'Dunlop Car Tyre'),
('2', 'PhysicalA1', 'Samsung'),
('3', 'PhysicalB2', 'Acer'),
('4', 'PhysicalB1', 'Lenovo')
;
INSERT INTO `TrxPositionAssets` (`Id`, `MaintTrxId`, `PositionAssetId`) VALUES
('1', '1', '2'),
('2', '1', '3'),
('3', '1', '1')
;
INSERT INTO `TrxPhysicalAssets` (`Id`, `MaintTrxId`, `PhysicalAssetId`) VALUES
('1', '1', '2'),
('2', '1', '3'),
('3', '1', '1'),
('4', '1', '4')
;
INSERT INTO `TrxAssetPool` (`Id`,`MaintTrxId`,`PositionAssetId`,`PhysicalAssetId`) VALUES
('1', '1', '3', '2'),
('2', '1', '2', '4'),
('3', '1', '2', '3')
;

SELECT DataType, DataCode, DataDesc
FROM (
SELECT 'Position' AS DataType, pos.Code AS DataCode, pos.Desc AS DataDesc
FROM TrxPositionAssets trxpos
JOIN PositionAssets pos ON pos.Id = trxpos.PositionAssetId
LEFT JOIN TrxAssetPool trxpool ON (trxpool.PositionAssetId = trxpos.PositionAssetId and trxpool.MaintTrxId = trxpos.MaintTrxId)
WHERE trxpos.MaintTrxId = 1
UNION
SELECT 'Physical' AS DataType, phy.Code AS DataCode, phy.Desc AS DataDesc
FROM TrxPhysicalAssets trxphy
JOIN PhysicalAssets phy ON phy.Id = trxphy.PhysicalAssetId
LEFT JOIN TrxAssetPool trxpool ON (trxpool.PhysicalAssetId = trxphy.PhysicalAssetId and trxpool.MaintTrxId = trxphy.MaintTrxId)
WHERE trxphy.MaintTrxId = 1
) DataPool


Sample at sqlfiddle.com



Current result:



DataType    DataCode    DataDesc

Position PositionA Mobile Phone
Position PositionB Laptop
Position PositionC Air conditioner
Physical PhysicalA1 Samsung
Physical PhysicalB1 Lenovo
Physical PhysicalB2 Acer
Physical PhysicalD Dunlop Car Tyre


Expected Result:



DataType    DataCode    DataDesc

Position PositionA Mobile Phone
Physical PhysicalA1 Samsung
Position PositionB Laptop
Physical PhysicalB1 Lenovo
Physical PhysicalB2 Acer
Position PositionC Air conditioner
Physical PhysicalD Dunlop Car Tyre


Air conditioner is not related to any Physical. Dunlop Car Tyre is not related to any Position.










share|improve this question
















My question is: How do I order the subquery by PositionAssetId and then follow by its related PhysicalAssetId based on table TrxAssetPool?



I need LEFT JOIN because not all Position and Physical were linked together. Some of Position/Physical were standalone. A Physical might exist in PhysicalAsset and TrxPhysicalAsset but not exist in TrxAssetPool because it was not linked to any Position; and vice versa. These data also needed to be displayed.



CREATE TABLE `PositionAssets` (
`Id` int(5) unsigned NOT NULL,
`Code` varchar(50) NOT NULL,
`Desc` varchar(200) NOT NULL,
PRIMARY KEY (`Id`)
);
CREATE TABLE `PhysicalAssets` (
`Id` int(5) unsigned NOT NULL,
`Code` varchar(50) NOT NULL,
`Desc` varchar(200) NOT NULL,
PRIMARY KEY (`Id`)
);
CREATE TABLE `TrxPositionAssets` (
`Id` int(5) unsigned NOT NULL,
`MaintTrxId` int(5) unsigned NOT NULL,
`PositionAssetId` int(5) NOT NULL,
PRIMARY KEY (`Id`,`MaintTrxId`)
);
CREATE TABLE `TrxPhysicalAssets` (
`Id` int(5) unsigned NOT NULL,
`MaintTrxId` int(5) unsigned NOT NULL,
`PhysicalAssetId` int(5) NOT NULL,
PRIMARY KEY (`Id`,`MaintTrxId`)
);
CREATE TABLE `TrxAssetPool` (
`Id` int(5) unsigned NOT NULL,
`MaintTrxId` int(5) NOT NULL,
`PositionAssetId` int(5) NOT NULL,
`PhysicalAssetId` int(5) NOT NULL,
PRIMARY KEY (`Id`)
);
INSERT INTO `PositionAssets` (`Id`, `Code`, `Desc`) VALUES
('1', 'PositionC', 'Air conditioner'),
('2', 'PositionB', 'Laptop'),
('3', 'PositionA', 'Mobile Phone')
;
INSERT INTO `PhysicalAssets` (`Id`, `Code`, `Desc`) VALUES
('1', 'PhysicalD', 'Dunlop Car Tyre'),
('2', 'PhysicalA1', 'Samsung'),
('3', 'PhysicalB2', 'Acer'),
('4', 'PhysicalB1', 'Lenovo')
;
INSERT INTO `TrxPositionAssets` (`Id`, `MaintTrxId`, `PositionAssetId`) VALUES
('1', '1', '2'),
('2', '1', '3'),
('3', '1', '1')
;
INSERT INTO `TrxPhysicalAssets` (`Id`, `MaintTrxId`, `PhysicalAssetId`) VALUES
('1', '1', '2'),
('2', '1', '3'),
('3', '1', '1'),
('4', '1', '4')
;
INSERT INTO `TrxAssetPool` (`Id`,`MaintTrxId`,`PositionAssetId`,`PhysicalAssetId`) VALUES
('1', '1', '3', '2'),
('2', '1', '2', '4'),
('3', '1', '2', '3')
;

SELECT DataType, DataCode, DataDesc
FROM (
SELECT 'Position' AS DataType, pos.Code AS DataCode, pos.Desc AS DataDesc
FROM TrxPositionAssets trxpos
JOIN PositionAssets pos ON pos.Id = trxpos.PositionAssetId
LEFT JOIN TrxAssetPool trxpool ON (trxpool.PositionAssetId = trxpos.PositionAssetId and trxpool.MaintTrxId = trxpos.MaintTrxId)
WHERE trxpos.MaintTrxId = 1
UNION
SELECT 'Physical' AS DataType, phy.Code AS DataCode, phy.Desc AS DataDesc
FROM TrxPhysicalAssets trxphy
JOIN PhysicalAssets phy ON phy.Id = trxphy.PhysicalAssetId
LEFT JOIN TrxAssetPool trxpool ON (trxpool.PhysicalAssetId = trxphy.PhysicalAssetId and trxpool.MaintTrxId = trxphy.MaintTrxId)
WHERE trxphy.MaintTrxId = 1
) DataPool


Sample at sqlfiddle.com



Current result:



DataType    DataCode    DataDesc

Position PositionA Mobile Phone
Position PositionB Laptop
Position PositionC Air conditioner
Physical PhysicalA1 Samsung
Physical PhysicalB1 Lenovo
Physical PhysicalB2 Acer
Physical PhysicalD Dunlop Car Tyre


Expected Result:



DataType    DataCode    DataDesc

Position PositionA Mobile Phone
Physical PhysicalA1 Samsung
Position PositionB Laptop
Physical PhysicalB1 Lenovo
Physical PhysicalB2 Acer
Position PositionC Air conditioner
Physical PhysicalD Dunlop Car Tyre


Air conditioner is not related to any Physical. Dunlop Car Tyre is not related to any Position.







sql join subquery left-join union






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 4:35







Eja

















asked Nov 23 '18 at 10:47









EjaEja

45




45













  • How do you know which position assets match which physical assets?

    – Gordon Linoff
    Nov 23 '18 at 12:08











  • @GordonLinoff it was linked in TrxAssetPool which exists PositionAssetId and PhysicalAssetId field

    – Eja
    Nov 27 '18 at 7:36











  • There is no point in repeating in natural language what we can read in code. Tell us how the output table is a function of the input tables--a row of values (DataId, ...) is in the result if & only if what is in the input? We don't need a link/image, we need DDL & the rest of a Minimal, Complete, and Verifiable example--cut & paste & runnable code including initialization code ((tabular format) with desired result (preferably ordered, so diffable) & clear specification.

    – philipxy
    Nov 27 '18 at 8:28













  • Possible duplicate of Using union and order by clause in mysql

    – philipxy
    Nov 27 '18 at 8:56











  • This is a faq. Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on Minimal, Complete, and Verifiable example.

    – philipxy
    Nov 27 '18 at 8:57



















  • How do you know which position assets match which physical assets?

    – Gordon Linoff
    Nov 23 '18 at 12:08











  • @GordonLinoff it was linked in TrxAssetPool which exists PositionAssetId and PhysicalAssetId field

    – Eja
    Nov 27 '18 at 7:36











  • There is no point in repeating in natural language what we can read in code. Tell us how the output table is a function of the input tables--a row of values (DataId, ...) is in the result if & only if what is in the input? We don't need a link/image, we need DDL & the rest of a Minimal, Complete, and Verifiable example--cut & paste & runnable code including initialization code ((tabular format) with desired result (preferably ordered, so diffable) & clear specification.

    – philipxy
    Nov 27 '18 at 8:28













  • Possible duplicate of Using union and order by clause in mysql

    – philipxy
    Nov 27 '18 at 8:56











  • This is a faq. Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on Minimal, Complete, and Verifiable example.

    – philipxy
    Nov 27 '18 at 8:57

















How do you know which position assets match which physical assets?

– Gordon Linoff
Nov 23 '18 at 12:08





How do you know which position assets match which physical assets?

– Gordon Linoff
Nov 23 '18 at 12:08













@GordonLinoff it was linked in TrxAssetPool which exists PositionAssetId and PhysicalAssetId field

– Eja
Nov 27 '18 at 7:36





@GordonLinoff it was linked in TrxAssetPool which exists PositionAssetId and PhysicalAssetId field

– Eja
Nov 27 '18 at 7:36













There is no point in repeating in natural language what we can read in code. Tell us how the output table is a function of the input tables--a row of values (DataId, ...) is in the result if & only if what is in the input? We don't need a link/image, we need DDL & the rest of a Minimal, Complete, and Verifiable example--cut & paste & runnable code including initialization code ((tabular format) with desired result (preferably ordered, so diffable) & clear specification.

– philipxy
Nov 27 '18 at 8:28







There is no point in repeating in natural language what we can read in code. Tell us how the output table is a function of the input tables--a row of values (DataId, ...) is in the result if & only if what is in the input? We don't need a link/image, we need DDL & the rest of a Minimal, Complete, and Verifiable example--cut & paste & runnable code including initialization code ((tabular format) with desired result (preferably ordered, so diffable) & clear specification.

– philipxy
Nov 27 '18 at 8:28















Possible duplicate of Using union and order by clause in mysql

– philipxy
Nov 27 '18 at 8:56





Possible duplicate of Using union and order by clause in mysql

– philipxy
Nov 27 '18 at 8:56













This is a faq. Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on Minimal, Complete, and Verifiable example.

– philipxy
Nov 27 '18 at 8:57





This is a faq. Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on Minimal, Complete, and Verifiable example.

– philipxy
Nov 27 '18 at 8:57












3 Answers
3






active

oldest

votes


















1














In the end of query put,



ORDER BY DATA.DataId ASC;





share|improve this answer
























  • Thanks..but cant do that because the Id will never sequence-ly sorted. (^.^")

    – Eja
    Nov 27 '18 at 7:45



















1














You need to select the information you want in the subquery. Also, the LEFT JOINs are not necessary, because they are undone by the WHERE and you probably want UNION ALL:



SELECT Data.[DataId], Data.[TrxnDataId], Data.[Type]
FROM ((SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Position' AS Type,
tap.PositionAssetId, 1 as ord
FROM {TrxPositionAssets} tpa JOIN
{PositionAssets} pa
ON pa.[Id] = tpa.[PositionAssetId] JOIN
TrxAssetPool} tap
ON tap.[PositionAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId])
WHERE tpa.[TrxId] = @TrxId
) UNION ALL
(SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Physical' AS Type,
tap.PositionAssetId, 2 as ord
FROM {TrxPhysicalAssets} tpa JOIN
{PhysicalAssets} pa
ON pa.[Id] = tpa.[PhysicalAssetId] JOIN
{TrxAssetPool} tap
ON tap.[PhysicalAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId]
WHERE tpa.[TrxId] = @TrxId
)
) data
ORDER BY PositionAssetId, ord, dataId;





share|improve this answer
























  • I think I forgot to mention why I need LEFT JOIN, it is because not all Position and Physical was linked together. Some of Position/Physical was stand alone. Physical might exists in PhysicalAsset and TrxPhysicalAsset but not exists on TrxAssetPool because it was not linked to any Position and vice versa. These data also needed to be displayed

    – Eja
    Nov 27 '18 at 7:33











  • By the way, thank you for your efforts to help me. (^^,)

    – Eja
    Nov 27 '18 at 8:15



















0














To solve your problem you have to simplify it and solve it step by step, it will be easier to find a solution.



E.g. Simply join two table



SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;





share|improve this answer
























  • Err..thanks..but i guess this is not as simple as your example. (^.^")

    – Eja
    Nov 27 '18 at 7:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445214%2forder-by-a-subqueries-which-has-join-left-join-and-union%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














In the end of query put,



ORDER BY DATA.DataId ASC;





share|improve this answer
























  • Thanks..but cant do that because the Id will never sequence-ly sorted. (^.^")

    – Eja
    Nov 27 '18 at 7:45
















1














In the end of query put,



ORDER BY DATA.DataId ASC;





share|improve this answer
























  • Thanks..but cant do that because the Id will never sequence-ly sorted. (^.^")

    – Eja
    Nov 27 '18 at 7:45














1












1








1







In the end of query put,



ORDER BY DATA.DataId ASC;





share|improve this answer













In the end of query put,



ORDER BY DATA.DataId ASC;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 10:59









Kumar Gaurav SharmaKumar Gaurav Sharma

69112




69112













  • Thanks..but cant do that because the Id will never sequence-ly sorted. (^.^")

    – Eja
    Nov 27 '18 at 7:45



















  • Thanks..but cant do that because the Id will never sequence-ly sorted. (^.^")

    – Eja
    Nov 27 '18 at 7:45

















Thanks..but cant do that because the Id will never sequence-ly sorted. (^.^")

– Eja
Nov 27 '18 at 7:45





Thanks..but cant do that because the Id will never sequence-ly sorted. (^.^")

– Eja
Nov 27 '18 at 7:45













1














You need to select the information you want in the subquery. Also, the LEFT JOINs are not necessary, because they are undone by the WHERE and you probably want UNION ALL:



SELECT Data.[DataId], Data.[TrxnDataId], Data.[Type]
FROM ((SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Position' AS Type,
tap.PositionAssetId, 1 as ord
FROM {TrxPositionAssets} tpa JOIN
{PositionAssets} pa
ON pa.[Id] = tpa.[PositionAssetId] JOIN
TrxAssetPool} tap
ON tap.[PositionAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId])
WHERE tpa.[TrxId] = @TrxId
) UNION ALL
(SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Physical' AS Type,
tap.PositionAssetId, 2 as ord
FROM {TrxPhysicalAssets} tpa JOIN
{PhysicalAssets} pa
ON pa.[Id] = tpa.[PhysicalAssetId] JOIN
{TrxAssetPool} tap
ON tap.[PhysicalAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId]
WHERE tpa.[TrxId] = @TrxId
)
) data
ORDER BY PositionAssetId, ord, dataId;





share|improve this answer
























  • I think I forgot to mention why I need LEFT JOIN, it is because not all Position and Physical was linked together. Some of Position/Physical was stand alone. Physical might exists in PhysicalAsset and TrxPhysicalAsset but not exists on TrxAssetPool because it was not linked to any Position and vice versa. These data also needed to be displayed

    – Eja
    Nov 27 '18 at 7:33











  • By the way, thank you for your efforts to help me. (^^,)

    – Eja
    Nov 27 '18 at 8:15
















1














You need to select the information you want in the subquery. Also, the LEFT JOINs are not necessary, because they are undone by the WHERE and you probably want UNION ALL:



SELECT Data.[DataId], Data.[TrxnDataId], Data.[Type]
FROM ((SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Position' AS Type,
tap.PositionAssetId, 1 as ord
FROM {TrxPositionAssets} tpa JOIN
{PositionAssets} pa
ON pa.[Id] = tpa.[PositionAssetId] JOIN
TrxAssetPool} tap
ON tap.[PositionAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId])
WHERE tpa.[TrxId] = @TrxId
) UNION ALL
(SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Physical' AS Type,
tap.PositionAssetId, 2 as ord
FROM {TrxPhysicalAssets} tpa JOIN
{PhysicalAssets} pa
ON pa.[Id] = tpa.[PhysicalAssetId] JOIN
{TrxAssetPool} tap
ON tap.[PhysicalAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId]
WHERE tpa.[TrxId] = @TrxId
)
) data
ORDER BY PositionAssetId, ord, dataId;





share|improve this answer
























  • I think I forgot to mention why I need LEFT JOIN, it is because not all Position and Physical was linked together. Some of Position/Physical was stand alone. Physical might exists in PhysicalAsset and TrxPhysicalAsset but not exists on TrxAssetPool because it was not linked to any Position and vice versa. These data also needed to be displayed

    – Eja
    Nov 27 '18 at 7:33











  • By the way, thank you for your efforts to help me. (^^,)

    – Eja
    Nov 27 '18 at 8:15














1












1








1







You need to select the information you want in the subquery. Also, the LEFT JOINs are not necessary, because they are undone by the WHERE and you probably want UNION ALL:



SELECT Data.[DataId], Data.[TrxnDataId], Data.[Type]
FROM ((SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Position' AS Type,
tap.PositionAssetId, 1 as ord
FROM {TrxPositionAssets} tpa JOIN
{PositionAssets} pa
ON pa.[Id] = tpa.[PositionAssetId] JOIN
TrxAssetPool} tap
ON tap.[PositionAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId])
WHERE tpa.[TrxId] = @TrxId
) UNION ALL
(SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Physical' AS Type,
tap.PositionAssetId, 2 as ord
FROM {TrxPhysicalAssets} tpa JOIN
{PhysicalAssets} pa
ON pa.[Id] = tpa.[PhysicalAssetId] JOIN
{TrxAssetPool} tap
ON tap.[PhysicalAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId]
WHERE tpa.[TrxId] = @TrxId
)
) data
ORDER BY PositionAssetId, ord, dataId;





share|improve this answer













You need to select the information you want in the subquery. Also, the LEFT JOINs are not necessary, because they are undone by the WHERE and you probably want UNION ALL:



SELECT Data.[DataId], Data.[TrxnDataId], Data.[Type]
FROM ((SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Position' AS Type,
tap.PositionAssetId, 1 as ord
FROM {TrxPositionAssets} tpa JOIN
{PositionAssets} pa
ON pa.[Id] = tpa.[PositionAssetId] JOIN
TrxAssetPool} tap
ON tap.[PositionAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId])
WHERE tpa.[TrxId] = @TrxId
) UNION ALL
(SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Physical' AS Type,
tap.PositionAssetId, 2 as ord
FROM {TrxPhysicalAssets} tpa JOIN
{PhysicalAssets} pa
ON pa.[Id] = tpa.[PhysicalAssetId] JOIN
{TrxAssetPool} tap
ON tap.[PhysicalAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId]
WHERE tpa.[TrxId] = @TrxId
)
) data
ORDER BY PositionAssetId, ord, dataId;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 12:14









Gordon LinoffGordon Linoff

779k35307410




779k35307410













  • I think I forgot to mention why I need LEFT JOIN, it is because not all Position and Physical was linked together. Some of Position/Physical was stand alone. Physical might exists in PhysicalAsset and TrxPhysicalAsset but not exists on TrxAssetPool because it was not linked to any Position and vice versa. These data also needed to be displayed

    – Eja
    Nov 27 '18 at 7:33











  • By the way, thank you for your efforts to help me. (^^,)

    – Eja
    Nov 27 '18 at 8:15



















  • I think I forgot to mention why I need LEFT JOIN, it is because not all Position and Physical was linked together. Some of Position/Physical was stand alone. Physical might exists in PhysicalAsset and TrxPhysicalAsset but not exists on TrxAssetPool because it was not linked to any Position and vice versa. These data also needed to be displayed

    – Eja
    Nov 27 '18 at 7:33











  • By the way, thank you for your efforts to help me. (^^,)

    – Eja
    Nov 27 '18 at 8:15

















I think I forgot to mention why I need LEFT JOIN, it is because not all Position and Physical was linked together. Some of Position/Physical was stand alone. Physical might exists in PhysicalAsset and TrxPhysicalAsset but not exists on TrxAssetPool because it was not linked to any Position and vice versa. These data also needed to be displayed

– Eja
Nov 27 '18 at 7:33





I think I forgot to mention why I need LEFT JOIN, it is because not all Position and Physical was linked together. Some of Position/Physical was stand alone. Physical might exists in PhysicalAsset and TrxPhysicalAsset but not exists on TrxAssetPool because it was not linked to any Position and vice versa. These data also needed to be displayed

– Eja
Nov 27 '18 at 7:33













By the way, thank you for your efforts to help me. (^^,)

– Eja
Nov 27 '18 at 8:15





By the way, thank you for your efforts to help me. (^^,)

– Eja
Nov 27 '18 at 8:15











0














To solve your problem you have to simplify it and solve it step by step, it will be easier to find a solution.



E.g. Simply join two table



SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;





share|improve this answer
























  • Err..thanks..but i guess this is not as simple as your example. (^.^")

    – Eja
    Nov 27 '18 at 7:39
















0














To solve your problem you have to simplify it and solve it step by step, it will be easier to find a solution.



E.g. Simply join two table



SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;





share|improve this answer
























  • Err..thanks..but i guess this is not as simple as your example. (^.^")

    – Eja
    Nov 27 '18 at 7:39














0












0








0







To solve your problem you have to simplify it and solve it step by step, it will be easier to find a solution.



E.g. Simply join two table



SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;





share|improve this answer













To solve your problem you have to simplify it and solve it step by step, it will be easier to find a solution.



E.g. Simply join two table



SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 11:03









Hemant KumarHemant Kumar

404317




404317













  • Err..thanks..but i guess this is not as simple as your example. (^.^")

    – Eja
    Nov 27 '18 at 7:39



















  • Err..thanks..but i guess this is not as simple as your example. (^.^")

    – Eja
    Nov 27 '18 at 7:39

















Err..thanks..but i guess this is not as simple as your example. (^.^")

– Eja
Nov 27 '18 at 7:39





Err..thanks..but i guess this is not as simple as your example. (^.^")

– Eja
Nov 27 '18 at 7:39


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445214%2forder-by-a-subqueries-which-has-join-left-join-and-union%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)