Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to get rid of using one table by adding code as a subselect to the UNION

I have this working Union

INSERT INTO [Transform].PosSalesUnion 
         (DeviceId, DayClosureId, TransactionId, IndetId, TicketId, CategoryId, ArticleId, CurrencyId, Datetime, IsSuccessful, Quantity, Turnover, TurnoverTarget, SalesTransactionTypeId, ValidFrom, ValidTo, SerialNumber, poolID)


SELECT poolID, DeviceId, DayClosureId, TransactionId, IndetId, TicketId, CategoryId, ArticleId, CurrencyId, Datetime, IsSuccessful, Quantity, Turnover, TurnoverTarget, SalesTransactionTypeId, ValidFrom, ValidTo, SerialNumber 
FROM [Transform].Devices_DeviceGroups_PosSales
  
UNION

SELECT NULL, DeviceId, DayClosureId, TransactionId, IndetId, TicketId, CategoryId, ArticleId, CurrencyId, Datetime, IsSuccessful, Quantity, Turnover, TurnoverTarget, SalesTransactionTypeId, ValidFrom, ValidTo, SerialNumber 
FROM [Transform].PosSalesNotMatch
   

As you can see I read from table [Transform].PosSalesNotMatch. Now I hope to minimize the usage of separate tables and instead add the code that populate this table [Transform].PosSalesNotMatch.

The code that populate this table [Transform].PosSalesNotMatch is this code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

INSERT INTO [Transform].PosSalesNotMatch
   SELECT a.DeviceId, a.DayClosureId, a.TransactionId, a.IndetId, a.TicketId,a.CategoryId,a.ArticleId,a.CurrencyId,a.Datetime,a.IsSuccessful,a.Quantity,a.Turnover,a.TurnoverTarget,a.SalesTransactionTypeId,a.ValidFrom,a.ValidTo,a.SerialNumber
   from [Transform].PosSales a
   left join [Transform].Devices_DeviceGroups b on a.DeviceId = b.DeviceId
   where b.DeviceId is null

Now I have tried to add this code as a subselect to the union but I get syntax error. The final code that I have tried is this

 INSERT INTO [Transform].PosSalesUnion 
         (DeviceId, DayClosureId, TransactionId, IndetId, TicketId, CategoryId, ArticleId, CurrencyId, Datetime, IsSuccessful, Quantity, Turnover, TurnoverTarget, SalesTransactionTypeId, ValidFrom, ValidTo, SerialNumber, poolID)
 

 SELECT poolID, DeviceId, DayClosureId, TransactionId, IndetId, TicketId, CategoryId, ArticleId, CurrencyId, Datetime, IsSuccessful, Quantity, Turnover, TurnoverTarget, SalesTransactionTypeId, ValidFrom, ValidTo, SerialNumber 
 FROM [Transform].Devices_DeviceGroups_PosSales

   UNION

 SELECT NULL, DeviceId, DayClosureId, TransactionId, IndetId, TicketId, CategoryId, ArticleId, CurrencyId, Datetime, IsSuccessful, Quantity, Turnover, TurnoverTarget, SalesTransactionTypeId, ValidFrom, ValidTo, SerialNumber 

   (SELECT a.DeviceId, a.DayClosureId, a.TransactionId, a.IndetId, a.TicketId,a.CategoryId,a.ArticleId,a.CurrencyId,a.Datetime,a.IsSuccessful,a.Quantity,a.Turnover,a.TurnoverTarget,a.SalesTransactionTypeId,a.ValidFrom,a.ValidTo,a.SerialNumber
   from [Transform].PosSales a
 left join [Transform].Devices_DeviceGroups b on a.DeviceId = b.DeviceId
   where b.DeviceId is null)

>Solution :

Looks like the problem is you did not have the FROM keyword. On SQL Server you also have to give an alias to sub-queries.

 INSERT INTO [Transform].PosSalesUnion 
         (DeviceId, DayClosureId, TransactionId, IndetId, TicketId, CategoryId, ArticleId, CurrencyId, Datetime, IsSuccessful, Quantity, Turnover, TurnoverTarget, SalesTransactionTypeId, ValidFrom, ValidTo, SerialNumber, poolID)
 

   SELECT poolID, DeviceId, DayClosureId, TransactionId, IndetId, TicketId, CategoryId, ArticleId, CurrencyId, Datetime, IsSuccessful, Quantity, Turnover, TurnoverTarget, SalesTransactionTypeId, ValidFrom, ValidTo, SerialNumber 
   FROM [Transform].Devices_DeviceGroups_PosSales

     UNION

   SELECT NULL, DeviceId, DayClosureId, TransactionId, IndetId, TicketId, CategoryId, ArticleId, CurrencyId, Datetime, IsSuccessful, Quantity, Turnover, TurnoverTarget, SalesTransactionTypeId, ValidFrom, ValidTo, SerialNumber 
   FROM 
     (SELECT a.DeviceId, a.DayClosureId, a.TransactionId, a.IndetId, a.TicketId,a.CategoryId,a.ArticleId,a.CurrencyId,a.Datetime,a.IsSuccessful,a.Quantity,a.Turnover,a.TurnoverTarget,a.SalesTransactionTypeId,a.ValidFrom,a.ValidTo,a.SerialNumber
      from [Transform].PosSales a
      left join [Transform].Devices_DeviceGroups b on a.DeviceId = b.DeviceId
      where b.DeviceId is null
     ) as X

After looking at it formatted well I can see you don’t need the sub-query

 INSERT INTO [Transform].PosSalesUnion 
         (DeviceId, DayClosureId, TransactionId, IndetId, TicketId, CategoryId, ArticleId, CurrencyId, Datetime, IsSuccessful, Quantity, Turnover, TurnoverTarget, SalesTransactionTypeId, ValidFrom, ValidTo, SerialNumber, poolID)
 

   SELECT poolID, DeviceId, DayClosureId, TransactionId, IndetId, TicketId, CategoryId, ArticleId, CurrencyId, Datetime, IsSuccessful, Quantity, Turnover, TurnoverTarget, SalesTransactionTypeId, ValidFrom, ValidTo, SerialNumber 
   FROM [Transform].Devices_DeviceGroups_PosSales

     UNION

   SELECT NULL, a.DeviceId, a.DayClosureId, a.TransactionId, a.IndetId, a.TicketId,a.CategoryId,a.ArticleId,a.CurrencyId,a.Datetime,a.IsSuccessful,a.Quantity,a.Turnover,a.TurnoverTarget,a.SalesTransactionTypeId,a.ValidFrom,a.ValidTo,a.SerialNumber
   FROM [Transform].PosSales a
   LEFT JOIN [Transform].Devices_DeviceGroups b on a.DeviceId = b.DeviceId
   WHERE b.DeviceId is null
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading