Merge 2 queries.

  • I cannot merge these 2 seperate queries. Can anyone show me how please?

    1.

    set dateformat dmy

    select * from (

    select distinct PERSONKEYIDS.FullName AS 'Sales Manager Name',

    PERSONKEYIDS.PersonKeyId AS 'ID', convert(varchar,max(convert(datetime,Timesheets.[date])),103) as [Last Date Entered]

    from AGENT_TEAM_FACT join PERSONKEYIDS

     ON AGENT_TEAM_FACT.ParentKeyID = PERSONKEYIDS.PersonKeyId

    left outer join Timesheets

     on PERSONKEYIDS.PersonKeyId = Timesheets.SMID

    group by PERSONKEYIDS.FullName, PERSONKEYIDS.PersonKeyId) z

    order by convert(datetime,[Last Date Entered])

    2.

    (select distinct CAMPAIGNS.Site

     from CAMPAIGNS inner join PERSONKEYIDS

     on CAMPAIGNS.PersonKeyId = PERSONKEYIDS.PersonKeyId)

    Ive tried to simply add no.2 as another join and its gives me an incorrect result set.

  • Well, I guess that you've already realized that 'the easy' way to merge (which I assume in this case means to add the sites column to the 1st query results) doesn't work since you've already discovered that you don't get your expected results.

    You need to provide some stuff in order for anyone to help you out on this.

    We need the DDL of the tables involved with info on how they relate to eachother, and a few rows of sample data and a description of your desired output. It would also be much helpful if you provide an explanation to what the purpose is with the particular query. You should provide all the above in a 'script' form, so that it's quick and easy to just copy and paste into QA.

    Without these things, it's impossible to do anything than pure speculation about your problem...

    /Kenneth

  • Also, are you trying to merge as in UNION with both sets of rows or merge as in adding more columns to your result set?

    In addition to Kenneth's ?s.

  • Because the two queries return different columns and you mention an additional join, I am going to infer that you want one query (the first query with the column from the second query added).

    If so, you are probably having a problem related to the "Group By" clause.  You need to make all the joins before the group by and take a look at the tables' schemas to make sure the joins make sense.

    Again, if you could post the DDl for the tables and some sample data, we could be more helpful.

     

     

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply