Sqlserver
 sql >> Baza danych >  >> RDS >> Sqlserver

SQL łączenie wielu kolumn w jedną kolumnę

To strzał w ciemno, ponieważ tak naprawdę nie znamy Twoich danych, ale jeśli podoba Ci się istniejące zapytanie, możliwe jest użycie go dwa razy, ale z klauzulą ​​WHERE:

SELECT
-- Common stuff?
ISNULL(Sales.ActivityId, Other.ActivityId), ISNULL(Sales.ScheduledStart, Other.ScheduledStart), ISNULL(Sales.OwnerIdName, Other.OwnerIdName), ISNULL(Sales.AccountIdName, Other.AccountIdName), ISNULL(Sales.new_ContactPersonName, Other.new_ContactPersonName), ISNULL(Sales.[Subject], Other.[Subject]), ISNULL(Sales.new_ColderNotes, Other.new_ColderNotes)
-- Sales stuff?
, Sales.AccountId, Sales.ContactId, Sales.SystemUserId, Sales.OptionalOwner, Sales.OptionalContact, Sales.OptionalAccount, Sales.PartyId, Sales.ParticipationTypeMask, Sales.RequiredContact, Sales.RequiredAccount, Sales.RequiredOwner, Sales.new_BusinessUnit
-- Other Stuff?
, Other.AccountId, Other.ContactId, Other.SystemUserId, Other.OptionalOwner, Other.OptionalContact, Other.OptionalAccount, Other.PartyId, Other.ParticipationTypeMask, Other.RequiredContact, Other.RequiredAccount, Other.RequiredOwner, Other.new_BusinessUnit
FROM
(
    SELECT DISTINCT 
        Appointment.ActivityId, Appointment.ScheduledStart, Appointment.OwnerIdName, Contact.AccountIdName, Appointment.new_ContactPersonName, 
            Appointment.Subject, Appointment.new_ColderNotes, Account.AccountId, Contact_1.ContactId, SystemUser.SystemUserId, SystemUser.FullName AS OptionalOwner, 
            Contact_1.FullName AS OptionalContact, Account.Name AS OptionalAccount, ActivityParty.PartyId, ActivityParty.ParticipationTypeMask, 
            Contact_1.FullName AS RequiredContact, Account.Name AS RequiredAccount, SystemUser.FullName AS RequiredOwner, Account.new_BusinessUnit
    FROM
        Contact AS Contact_1 RIGHT OUTER JOIN
            Account RIGHT OUTER JOIN
            SystemUser RIGHT OUTER JOIN
            Appointment INNER JOIN
            ActivityParty ON Appointment.ActivityId = ActivityParty.ActivityId ON SystemUser.SystemUserId = ActivityParty.PartyId ON Account.AccountId = ActivityParty.PartyId ON
            Contact_1.ContactId = ActivityParty.PartyId LEFT OUTER JOIN
            Contact ON Appointment.new_ContactPerson = Contact.ContactId
    -- Limit this part to Sales?
    WHERE ParticipationTypeMask BETWEEN 1 AND 3
) as Sales
FULL OUTER JOIN
(
    SELECT DISTINCT 
        Appointment.ActivityId, Appointment.ScheduledStart, Appointment.OwnerIdName, Contact.AccountIdName, Appointment.new_ContactPersonName, 
            Appointment.Subject, Appointment.new_ColderNotes, Account.AccountId, Contact_1.ContactId, SystemUser.SystemUserId, SystemUser.FullName AS OptionalOwner, 
            Contact_1.FullName AS OptionalContact, Account.Name AS OptionalAccount, ActivityParty.PartyId, ActivityParty.ParticipationTypeMask, 
            Contact_1.FullName AS RequiredContact, Account.Name AS RequiredAccount, SystemUser.FullName AS RequiredOwner, Account.new_BusinessUnit
    FROM
        Contact AS Contact_1 RIGHT OUTER JOIN
            Account RIGHT OUTER JOIN
            SystemUser RIGHT OUTER JOIN
            Appointment INNER JOIN
            ActivityParty ON Appointment.ActivityId = ActivityParty.ActivityId ON SystemUser.SystemUserId = ActivityParty.PartyId ON Account.AccountId = ActivityParty.PartyId ON
            Contact_1.ContactId = ActivityParty.PartyId LEFT OUTER JOIN
            Contact ON Appointment.new_ContactPerson = Contact.ContactId
    -- Limit this part to Other?
    WHERE ParticipationTypeMask BETWEEN 4 AND 6
) AS Other ON Sales.ActivityId = Other.ActivityId -- More cols for join?


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Jak wykonać procedurę składowaną w zapytaniu wybierającym

  2. Który wiersz ma najwyższą wartość?

  3. Zainstalowany SQL Server 2008, choć wersja bieżąca to nadal 2005

  4. Nie można otworzyć bazy danych, ponieważ jest to wersja 782. Ten serwer obsługuje wersję 706 i wcześniejsze. Ścieżka zmiany wersji nie jest obsługiwana

  5. Nie wybrano zestawu kopii zapasowych do przywrócenia SQL Server 2012