We help IT Professionals succeed at work.
Get Started

How to UNION SQL query

160 Views
Last Modified: 2015-08-18
Hello there,

I have these 2 sql query and I want to UNION them to gether and get as one result.Can somebody please help me

cheers
Zolf

SELECT 
    a.code,
    a.productName,
    a. BranchCode,
    a.Branch,
    SUM(a.AvailQty) as AvailableQty
    FROM 
 (
SELECT
RIGHT('000' + CAST(kol.code AS VARCHAR), 3) + RIGHT('00'+ CONVERT(VARCHAR,dbo.Mohin.code),3) + '' + RIGHT('0000' + CAST(tafsil.code AS VARCHAR), 4) AS code,
    dbo.Tafsil.description as productName,
     dbo.Branch.code as BranchCode,
    dbo.Branch.name as Branch,
    SUM(dbo.Batch.actualQuantity -  dbo.Batch.reservedQuantity) as AvailQty
FROM
    dbo.Batch
INNER JOIN
    dbo.Tafsil
ON
    (
        dbo.Batch.productId = dbo.Tafsil.id)
INNER JOIN
    dbo.Warehouse
ON
    (
        dbo.Batch.warehouseId = dbo.Warehouse.id)
INNER JOIN
    dbo.Branch
ON
    (
        dbo.Warehouse.branchId = dbo.Branch.id) 
        INNER JOIN
    dbo.WarehouseSubType1
ON
    (
        dbo.Warehouse.warehouseSubType1Id = dbo.WarehouseSubType1.id)
        INNER JOIN
    dbo.Mohin
ON
    (
        dbo.Tafsil.mohinId = dbo.Mohin.id)
INNER JOIN
    dbo.Kol
ON
    (
        dbo.Mohin.kolId = dbo.Kol.id)
GROUP BY
    dbo.Batch.expiryDate,
    dbo.Tafsil.description,
    dbo.Branch.name,
    dbo.WarehouseSubType1.warehouseSubTypeId,
    kol.code,
    Mohin.code,
    Tafsil.code,
     dbo.Branch.code
        HAVING 
        SUM(dbo.Batch.actualQuantity -  dbo.Batch.reservedQuantity) > 0
        AND  dbo.WarehouseSubType1.warehouseSubTypeId IN (1, 4)
      AND dbo.Batch.expiryDate < dateadd(month,3,getdate())
      )  a
      GROUP BY
      a.code,
       a.productName,
       a.Branch,
       a.BranchCode

Open in new window


SELECT
     RIGHT('000' + CAST(kol.code AS VARCHAR), 3) + RIGHT('00'+ CONVERT(VARCHAR,dbo.Mohin.code),3) + '' + RIGHT('0000' + CAST(tafsil.code AS VARCHAR), 4) AS code,
    dbo.Tafsil.description AS ProductName,
    dbo.Branch.code,
     dbo.Branch.name        AS Branch,
    SUM(dbo.WarehouseProductQuantity.actualQuantity - dbo.WarehouseProductQuantity.reservedQuantity
    ) AS AvailQty
FROM
    dbo.WarehouseSubType
INNER JOIN
    dbo.WarehouseType
ON
    (
        dbo.WarehouseSubType.warehouseTypeId = dbo.WarehouseType.id)
INNER JOIN
    dbo.WarehouseSubType1
ON
    (
        dbo.WarehouseSubType.id = dbo.WarehouseSubType1.warehouseSubTypeId)
INNER JOIN
    dbo.Warehouse
ON
    (
        dbo.WarehouseSubType1.id = dbo.Warehouse.warehouseSubType1Id)
INNER JOIN
    dbo.Branch
ON
    (
        dbo.Warehouse.branchId = dbo.Branch.id)
INNER JOIN
    dbo.WarehouseProductQuantity
ON
    (
        dbo.Warehouse.id = dbo.WarehouseProductQuantity.warehouseId)
INNER JOIN
    dbo.Tafsil
ON
    (
        dbo.WarehouseProductQuantity.tafsilId = dbo.Tafsil.id)
INNER JOIN
    dbo.TafsilLink
ON
    (
        dbo.Tafsil.id = dbo.TafsilLink.sourceId)
INNER JOIN
    dbo.FinishProduct
ON
    (
        dbo.TafsilLink.targetId = dbo.FinishProduct.tafsilId)
INNER JOIN
    dbo.Supplier
ON
    (
        dbo.FinishProduct.supplierId = dbo.Supplier.tafsilId)
INNER JOIN
    dbo.Mohin
ON
    (
        dbo.Tafsil.mohinId = dbo.Mohin.id)
INNER JOIN
    dbo.Kol
ON
    (
        dbo.Mohin.kolId = dbo.Kol.id)
WHERE
    dbo.WarehouseSubType1.warehouseSubTypeId IN (1,
                                                 4)
GROUP BY
    dbo.Branch.name,
    dbo.Tafsil.id,
    dbo.Tafsil.description ,
     dbo.Branch.code,
     kol.code,
    Mohin.code,
    Tafsil.code

Open in new window

Comment
Watch Question
Associate Principal Engineer
CERTIFIED EXPERT
Top Expert 2014
Commented:
This problem has been solved!
Unlock 3 Answers and 16 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE