Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. problem in union

problem in union

Scheduled Pinned Locked Moved Database
databasehelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mhd sbt
    wrote on last edited by
    #1

    hi to all how to union two below query thanks in advance

    SELECT
    dailyAbsences.*,'غیبت' as [RowType], '0' as [RowTypeCode]
    INTO
    #fullPersonnelDetails2
    FROM
    #personnelWorkingPeriodRange personnelWorkingPeriodRange
    INNER JOIN dbo.tkp_vwDailyAbsences As dailyAbsences
    ON dailyAbsences.PersonnelBaseID = personnelWorkingPeriodRange.PersonnelBaseID
    And dailyAbsences.StartDate >= personnelWorkingPeriodRange.StartDate
    And dailyAbsences.EndDate <= DateAdd(day, 1, personnelWorkingPeriodRange.EndDate)
    LEFT JOIN tkp_CalculationQueue As calculationQueue
    ON dailyAbsences.PersonnelBaseID = calculationQueue.PersonnelBaseID
    And calculationQueue.StartDate <= personnelWorkingPeriodRange.EndDate
    ORDER BY
    dailyAbsences.PersonnelBaseID,
    dailyAbsences.StartDate
    UNION
    SELECT
    'اضافه کاری' as [RowType], '1' as [RowTypeCode]
    FROM
    dbo.tkp_DailyStatistics dailystatistics
    WHERE
    dailystatistics.ActualExtraWork > dailystatistics.ExtraWork AND
    dailystatistics.PersonnelBaseID = @PersonnelBaseID AND
    dailystatistics.WorkingPeriodID = @WorkingPeriodID AND
    dailystatistics.YearWorkingPeriodID = @WorkingPeriodYear

    IF EXISTS ( SELECT * FROM tempdb..SYSOBJECTS WHERE Name = '#PersonnelWorkingPeriodRange' and xType = 'u')
    DROP TABLE #PersonnelWorkingPeriodRange

    M 1 Reply Last reply
    0
    • M mhd sbt

      hi to all how to union two below query thanks in advance

      SELECT
      dailyAbsences.*,'غیبت' as [RowType], '0' as [RowTypeCode]
      INTO
      #fullPersonnelDetails2
      FROM
      #personnelWorkingPeriodRange personnelWorkingPeriodRange
      INNER JOIN dbo.tkp_vwDailyAbsences As dailyAbsences
      ON dailyAbsences.PersonnelBaseID = personnelWorkingPeriodRange.PersonnelBaseID
      And dailyAbsences.StartDate >= personnelWorkingPeriodRange.StartDate
      And dailyAbsences.EndDate <= DateAdd(day, 1, personnelWorkingPeriodRange.EndDate)
      LEFT JOIN tkp_CalculationQueue As calculationQueue
      ON dailyAbsences.PersonnelBaseID = calculationQueue.PersonnelBaseID
      And calculationQueue.StartDate <= personnelWorkingPeriodRange.EndDate
      ORDER BY
      dailyAbsences.PersonnelBaseID,
      dailyAbsences.StartDate
      UNION
      SELECT
      'اضافه کاری' as [RowType], '1' as [RowTypeCode]
      FROM
      dbo.tkp_DailyStatistics dailystatistics
      WHERE
      dailystatistics.ActualExtraWork > dailystatistics.ExtraWork AND
      dailystatistics.PersonnelBaseID = @PersonnelBaseID AND
      dailystatistics.WorkingPeriodID = @WorkingPeriodID AND
      dailystatistics.YearWorkingPeriodID = @WorkingPeriodYear

      IF EXISTS ( SELECT * FROM tempdb..SYSOBJECTS WHERE Name = '#PersonnelWorkingPeriodRange' and xType = 'u')
      DROP TABLE #PersonnelWorkingPeriodRange

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Your 2 select statements do not have the same number of columns - union requires the 2 queries to match exactly. dailyabscence.* is not in your 2nd query.

      Never underestimate the power of human stupidity RAH

      C 1 Reply Last reply
      0
      • M Mycroft Holmes

        Your 2 select statements do not have the same number of columns - union requires the 2 queries to match exactly. dailyabscence.* is not in your 2nd query.

        Never underestimate the power of human stupidity RAH

        C Offline
        C Offline
        Corporal Agarn
        wrote on last edited by
        #3

        Also using * is not advised. Technically okay but it is better to spell out the columns.

        M 1 Reply Last reply
        0
        • C Corporal Agarn

          Also using * is not advised. Technically okay but it is better to spell out the columns.

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          I think using .* is the least of his problem if he does not understand the structuring of a union query. He obviously has not even glanced at the BOL doco for union so expecting him to understand a more subtle issue of using * is probably asking a little too much.

          Never underestimate the power of human stupidity RAH

          C 1 Reply Last reply
          0
          • M Mycroft Holmes

            I think using .* is the least of his problem if he does not understand the structuring of a union query. He obviously has not even glanced at the BOL doco for union so expecting him to understand a more subtle issue of using * is probably asking a little too much.

            Never underestimate the power of human stupidity RAH

            C Offline
            C Offline
            Corporal Agarn
            wrote on last edited by
            #5

            I agree. I was just pointing out one of many things to look at.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups