Как получить желаемый результат из нескольких таблиц следующим образом - PullRequest
0 голосов
/ 29 сентября 2018

Вот три таблицы Таблица: Типы транспортных средств

VehTypID    Name 
1   Car
2   Public Bus
3   Ambulance
4   Trucks
5   Auto

Таблица: Услуги

VehTypID    Name 
1   Towing Alert 
1   Over Speed Alert
3   Location 
4   Odo Meter
4   Speed Limit
4   Fuel Limit
4   SOS
4   Emergency Control

Таблица: NotificationAndReports

VehTypID    Name 
1   Popup
1   Email 
1   SMS
1   WhatsApp
2   Digital Status
4   Email Reports
4   Daily Summary Reports
4   Live Tracking

и ожидаемый результат ожидаетсяРезультаты

Vehicle Types   Services             NotificationAndReports
Car             Towing Alert         Popup
null            Over Speed Alert     Email 
null            null                 SMS
null            null                 WhatsApp
Public Bus      null                 Digital Status
Ambulance       Location             null
Trucks          Odo Meter            Email Reports
null            Speed Limit          Daily Summary Reports
null            Fuel Limit           Live Tracking
null            SOS                  null
null            Emergency Control    null
Auto            null                 null

Я использую левое соединение, чтобы получить данные, но вместо того, чтобы дать приведенный выше результат, он возвращает несколько значений.

Ответы [ 3 ]

0 голосов
/ 29 сентября 2018

Попробуйте: -

CREATE TABLE #VehicleTypes(
VehTypID    BIGINT,
Name NVARCHAR(max))
INSERT INTO #VehicleTypes
SELECT * FROM (
SELECT 1  as bbbb ,'Car'                    as aaaa union all
SELECT 2  as bbbb,'Public Bus'              as aaaa union all
select 3  as bbbb, 'Ambulance'              as aaaa union all
select 4  as bbbb, 'Trucks'             as aaaa union all
select 5  as bbbb, 'Auto'                   as aaaa ) AS a


CREATE TABLE #Services 
(
VehTypID    BIGINT,
Name NVARCHAR(max))

INSERT INTO #Services
SELECT * FROM (
select 1 as bbbb,  'Towing Alert'                       as aaaa    union all 
select 1 as bbbb,  'Over Speed Alert'                   as aaaa    union all
select 3 as bbbb,  'Location'                           as aaaa    union all
select 4 as bbbb,  'Odo Meter'                          as aaaa    union all
select 4 as bbbb,  'Speed Limit'                        as aaaa    union all
select 4 as bbbb,  'Fuel Limit'                     as aaaa    union all
select 4 as bbbb,  'SOS'                                as aaaa    union all
select 4 as bbbb,  'Emergency CONTROL'                  as aaaa    ) AS a

CREATE TABLE #NotificationAndReports
(
VehTypID    BIGINT,
Name NVARCHAR(max))
INSERT INTO #NotificationAndReports
SELECT * FROM (
select 1 as bbbb ,  'Popup'                         as aaaa     union all
select 1 as bbbb ,  'Email'                         as aaaa     union all
select 1 as bbbb ,  'SMS'                           as aaaa     union all
select 1 as bbbb ,  'WhatsApp'                      as aaaa     union all
select 2 as bbbb ,  'Digital Status'                as aaaa     union all
select 4 as bbbb ,  'Email Reports'                 as aaaa     union all
select 4 as bbbb ,  'Daily Summary Reports'         as aaaa     union all
select 4 as bbbb ,  'Live Tracking'                 as aaaa     ) AS a






SELECT  ROW_NUMBER()OVER (ORDER BY ( SELECT 1 )) AS rno,  ROW_NUMBER()OVER (PARTITION BY #VehicleTypes.VehTypID ORDER BY ( SELECT 1 )) AS PrntID,
ROW_NUMBER()OVER (PARTITION BY #Services.Name ORDER BY ( SELECT 1 ))  AS ChildRn,
#VehicleTypes.Name AS  VehicleTypes,#Services.Name AS Services,#NotificationAndReports.Name AS NotificationAndReports
INTO #temp
FROM #VehicleTypes
  RIGHT JOIN #Services
        ON #Services.VehTypID = #VehicleTypes.VehTypID
  LEFT JOIN #NotificationAndReports
        ON #NotificationAndReports.VehTypID = #VehicleTypes.VehTypID 
        ORDER BY #VehicleTypes.VehTypID


SELECT CASE WHEN PrntID = 1 THEN VehicleTypes ELSE NULL END AS VehicleTypes,CASE WHEN ChildRn = 1 THEN Services ELSE NULL END AS Services,NotificationAndReports
 FROM #temp ORDER BY rno


DROP TABLE #NotificationAndReports,#Services,#VehicleTypes,#temp`
0 голосов
/ 29 сентября 2018
/* Works In Microsoft SQL Server */
CREATE TABLE #VehicleTypes
        (
            VehTypID BIGINT ,
            Name NVARCHAR(MAX)
        );
    INSERT INTO #VehicleTypes
                SELECT  *
                FROM    (
                            SELECT  1 AS bbbb ,
                                    'Car' AS aaaa
                            UNION ALL
                            SELECT  2 AS bbbb ,
                                    'Public Bus' AS aaaa
                            UNION ALL
                            SELECT  3 AS bbbb ,
                                    'Ambulance' AS aaaa
                            UNION ALL
                            SELECT  4 AS bbbb ,
                                    'Trucks' AS aaaa
                            UNION ALL
                            SELECT  5 AS bbbb ,
                                    'Auto' AS aaaa
                        ) AS a;


    CREATE TABLE #Services
        (
            VehTypID BIGINT ,
            Name NVARCHAR(MAX)
        );

    INSERT INTO #Services
                SELECT  *
                FROM    (
                            SELECT  1 AS bbbb ,
                                    'Towing Alert' AS aaaa
                            UNION ALL
                            SELECT  1 AS bbbb ,
                                    'Over Speed Alert' AS aaaa
                            UNION ALL
                            SELECT  3 AS bbbb ,
                                    'Location' AS aaaa
                            UNION ALL
                            SELECT  4 AS bbbb ,
                                    'Odo Meter' AS aaaa
                            UNION ALL
                            SELECT  4 AS bbbb ,
                                    'Speed Limit' AS aaaa
                            UNION ALL
                            SELECT  4 AS bbbb ,
                                    'Fuel Limit' AS aaaa
                            UNION ALL
                            SELECT  4 AS bbbb ,
                                    'SOS' AS aaaa
                            UNION ALL
                            SELECT  4 AS bbbb ,
                                    'Emergency CONTROL' AS aaaa
                        ) AS a;

    CREATE TABLE #NotificationAndReports
        (
            VehTypID BIGINT ,
            Name NVARCHAR(MAX)
        );
    INSERT INTO #NotificationAndReports
                SELECT  *
                FROM    (
                            SELECT  1 AS bbbb ,
                                    'Popup' AS aaaa
                            UNION ALL
                            SELECT  1 AS bbbb ,
                                    'Email' AS aaaa
                            UNION ALL
                            SELECT  1 AS bbbb ,
                                    'SMS' AS aaaa
                            UNION ALL
                            SELECT  1 AS bbbb ,
                                    'WhatsApp' AS aaaa
                            UNION ALL
                            SELECT  2 AS bbbb ,
                                    'Digital Status' AS aaaa
                            UNION ALL
                            SELECT  4 AS bbbb ,
                                    'Email Reports' AS aaaa
                            UNION ALL
                            SELECT  4 AS bbbb ,
                                    'Daily Summary Reports' AS aaaa
                            UNION ALL
                            SELECT  4 AS bbbb ,
                                    'Live Tracking' AS aaaa
                        ) AS a;




    /*Way 1 */
    SELECT  CASE WHEN LAG(#VehicleTypes.Name, 1, 0) OVER ( ORDER BY #VehicleTypes.VehTypID ) = '0'
                      OR LAG(#VehicleTypes.Name, 1, 0) OVER ( ORDER BY #VehicleTypes.VehTypID ) <> #VehicleTypes.Name THEN
                     #VehicleTypes.Name
                 ELSE NULL
            END AS VehicleName ,
            CASE WHEN LAG(#Services.Name, 1, 0) OVER ( ORDER BY #VehicleTypes.VehTypID ) = '0'
                      OR LAG(#Services.Name, 1, 0) OVER ( ORDER BY #VehicleTypes.VehTypID ) <> #Services.Name THEN
                     #Services.Name
                 ELSE NULL
            END AS ServicesName ,
            #VehicleTypes.VehTypID ,
            #Services.VehTypID ,
            #NotificationAndReports.VehTypID ,
            #NotificationAndReports.Name
    FROM    #VehicleTypes
            RIGHT JOIN #Services ON #Services.VehTypID = #VehicleTypes.VehTypID
            LEFT JOIN #NotificationAndReports ON #NotificationAndReports.VehTypID = #VehicleTypes.VehTypID;

    /*Way 2 */
    SELECT  CASE WHEN ROW_NUMBER() OVER ( PARTITION BY #VehicleTypes.Name
                                          ORDER BY #VehicleTypes.VehTypID
                                        ) = 1 THEN #VehicleTypes.Name
                 ELSE NULL
            END AS VehicleName ,
            CASE WHEN ROW_NUMBER() OVER ( PARTITION BY #Services.Name
                                          ORDER BY #VehicleTypes.VehTypID
                                        ) = 1 THEN #Services.Name
                 ELSE NULL
            END AS ServicesName ,
            #VehicleTypes.VehTypID ,
            #Services.VehTypID ,
            #NotificationAndReports.VehTypID ,
            #NotificationAndReports.Name
    FROM    #VehicleTypes
            RIGHT JOIN #Services ON #Services.VehTypID = #VehicleTypes.VehTypID
            LEFT JOIN #NotificationAndReports ON #NotificationAndReports.VehTypID = #VehicleTypes.VehTypID;



    DROP TABLE #VehicleTypes ,
               #NotificationAndReports ,
               #Services;
0 голосов
/ 29 сентября 2018

Вы не можете избавиться от нескольких значений в ваших результатах.Из-за реляционных баз данных, которые используют «отношения».Представьте, что вы избавляетесь от дубликатов, но, например, ожидаемая строка

"null, null, SMS"

не имеет ссылки на типы или службы.Вероятно, вы можете проверить предыдущую строку, но в реляционных базах данных каждая строка должна иметь всю информацию, потому что строки равны и порядок строк не должен влиять на результат (в терминах реляционной алгебры).

Ноесли вы используете базу данных, которая поддерживает массивы (например, Postgresql), вы можете сгладить ваш запрос с помощью массивов.С таким SQL-кодом

select t.Name, 
(select array(select s.Name from Services as s where s.VehTypID = t.VehTypID)) as "Services",
(select array(select r.Name from Reports as r where r.VehTypID = t.VehTypID)) as "Reports"
from Types as t

Вы получите такой результат

| name       | Services                                               | Reports                                           |
| ---------- | ------------------------------------------------------ | ------------------------------------------------- |
| Car        | Towing Alert,Over Speed Alert                          | Popup,Email,SMS,WhatsApp                          |
| Public Bus |                                                        | Digital Status                                    |
| Ambulance  | Location                                               |                                                   |
| Trucks     | Odo Meter,Speed Limit,Fuel Limit,SOS,Emergency Control | Email Reports,Daily Summary Reports,Live Tracking |
| Auto       |                                                        |                                                   |

См. www.db-fiddle.com

...