Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL - combine queries without using UNION ALL

Posted on 2013-01-19
7
Medium Priority
?
408 Views
Last Modified: 2013-01-20
How can I combine the queries below without using UNIION ALL?
 
 SELECT ORDERS.orderid,
INVENTORY.partid,
Inventory.description,
ORDERITEMS.qty,
 Inventory.price,
 (OrderItems.qty * Inventory.price) AS 'Total Original Price',
 
 -----If an order contains ten or more units of a given product, give a 5% discount on that line item.

 (OrderItems.qty * Inventory.price *
CASE WHEN OrderItems.qty >=10 THEN 0.05 END) AS 'Total Discount Value' ,

----Get Total_Final_Price_After_Discount

 OrderItems.qty * Inventory.price - (OrderItems.qty * Inventory.price *
CASE WHEN OrderItems.qty >=10 THEN 0.05 END) AS 'Total_Final_Price_After_Discount'

FROM orders, inventory, orderitems
 WHERE ORDERS.ORDERID = ORDERITEMS.ORDERID AND ORDERITEMS.PARTID = INVENTORY.PARTID
 AND ORDERITEMS.qty >=10

 UNION ALL

 SELECT ORDERS.orderid,
INVENTORY.partid,
Inventory.description,
ORDERITEMS.qty,
 Inventory.price,
 (OrderItems.qty * Inventory.price) as 'Total Original Price',
 
----If an order contains five or more units of a given product, give a 2% discount for that line item.

(OrderItems.qty * Inventory.price *
CASE WHEN OrderItems.qty < 10 THEN 0.2 END) as 'Total Discounted Value',

----Get Total_Final_Price_After_Discount

 OrderItems.qty * Inventory.price - (OrderItems.qty * Inventory.price *
CASE WHEN OrderItems.qty < 10 THEN 0.2 END) AS 'Total_Final_Price_After_Discount'
 
 FROM orders, inventory, orderitems
 WHERE ORDERS.ORDERID = ORDERITEMS.ORDERID AND ORDERITEMS.PARTID = INVENTORY.PARTID
 AND ORDERITEMS.qty BETWEEN 5 AND 9
 ORDER BY ORDERS.orderid, INVENTORY.partid
0
Comment
Question by:ocdc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 35

Expert Comment

by:David Todd
ID: 38797701
Hi,

Can you explain why you don't want to use union all?

Regards
  David
0
 

Author Comment

by:ocdc
ID: 38797723
would like to use CASE Statement only.   Also how can I  add STR   function for formatting dollar amounts  with two decimal place?  Thank you.

 CASE WHEN OrderItems.qty >=10 THEN 0.05 END) AS 'Total_Final_Price_After_Discount'
0
 
LVL 35

Expert Comment

by:David Todd
ID: 38797736
Hi,

Does this help?

I reformatted your select so it was readable and added this line to each case statement
else 0.00

SELECT 
	ORDERS.orderid
	, INVENTORY.partid
	, Inventory.description
	, ORDERITEMS.qty
	, Inventory.price
	, (OrderItems.qty * Inventory.price) AS 'Total Original Price'
  
	-----If an order contains ten or more units of a given product, give a 5% discount on that line item.

	, (
		OrderItems.qty 
		* Inventory.price 
		* CASE 
			WHEN OrderItems.qty >=10 THEN 0.05 
			-- added line
			else 0.00
		END
		) AS 'Total Discount Value'

	----Get Total_Final_Price_After_Discount

	, OrderItems.qty 
		* Inventory.price 
		- (
			OrderItems.qty 
			* Inventory.price 
			* CASE 
				WHEN OrdeItems.qty >=10 THEN 0.05 
				-- added line
				else 0.00
			END
		) AS 'Total_Final_Price_After_Discount' 

Open in new window

HTH
  David
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:ocdc
ID: 38797774
I combined it as follows:  I get  5% and 2% discount as output for each item.   That is fine but  I need to format the Dollar values of  the  output columns.  Example: I am getting 10.000 which is correct . I need to format it to 10.00.

 So How can I format   OrderItems.qty
            * Inventory.price
            * CASE
                  WHEN OrderItems.qty >=10 THEN 0.05
                  -- added line
                  else 0.00
            END
            ) AS 'Total Discount Value'



SELECT
      ORDERS.orderid
      , INVENTORY.partid
      , Inventory.description
      , ORDERITEMS.qty
      , Inventory.price AS 'Unit price'
      , (OrderItems.qty * Inventory.price) AS 'Original Cost'
 
      -----If an order contains ten or more units of a given product, give a 5% discount on that line item.

      , (
            OrderItems.qty
            * Inventory.price
            * CASE
                  WHEN OrderItems.qty >=10 THEN 0.05
                  -- added line
                  else 0.00
            END
            ) AS 'Total Discount Value'

      ----Final Cost

      , OrderItems.qty
            * Inventory.price
            - (
                  OrderItems.qty
                  * Inventory.price
                  * CASE
                        WHEN OrderItems.qty >=10 THEN 0.05
                        -- added line
                        else 0.00
                  END
            ) AS 'Final Cost'  
            
            FROM ORDERS, INVENTORY, ORDERITEMS
 WHERE ORDERS.OrderID = ORDERITEMS.OrderID  AND ORDERITEMS.PartID = INVENTORY.PartID
 AND ORDERITEMS.qty >=10
 ORDER BY ORDERS.OrderId, INVENTORY.PartId
 
 UNION ALL
 
 SELECT
      ORDERS.orderid
      , INVENTORY.partid
      , Inventory.description
      , ORDERITEMS.qty
      , Inventory.price AS 'Unit price'
      , (OrderItems.qty * Inventory.price) AS 'Original Cost'
 
      -----If an order contains ten or more units of a given product, give a 5% discount on that line item.

      , (
            OrderItems.qty
            * Inventory.price
            * CASE
                  WHEN OrderItems.qty < 10 THEN 0.2
                  -- added line
                  else 0.00
            END
            ) AS 'Total Discount Value'

      ----Get Total_Final_Price_After_Discount

      , OrderItems.qty
            * Inventory.price
            - (
                  OrderItems.qty
                  * Inventory.price
                  * CASE
                        WHEN OrderItems.qty < 10 THEN 0.2
                        -- added line
                        else 0.00
                  END
            ) AS 'Final Cost'  
            
            FROM ORDERS, INVENTORY, ORDERITEMS
 WHERE ORDERS.OrderID = ORDERITEMS.OrderID  AND ORDERITEMS.PartID = INVENTORY.PartID
  AND ORDERITEMS.qty BETWEEN 5  AND  9
 ORDER BY ORDERS.OrderId, INVENTORY.PartId
0
 
LVL 35

Expert Comment

by:David Todd
ID: 38797841
Hi

With the lines I added to the case, loose the where clause, and loose the union all.

Formatting is something best left to presentation layer - excel, SSRS, web, application, etched.

Hth
  David
0
 

Author Comment

by:ocdc
ID: 38797854
Hi,

you only did -----If an order contains ten or more units of a given product, give a 5% discount

I also need one  for -----If an order contains 5 or more units of a given product, give a 2% discount.  And need to combine both in the CASE statement.  

As far as formatting is concern, I need to format the output columns to 2 decimal.    Thank you
 for your help.
0
 
LVL 35

Accepted Solution

by:
David Todd earned 1500 total points
ID: 38797898
Hi

You should be able to add the 5+ Line into the case - they are evaluated top to bottom so test for 10+ then 5-10 etc.

Regards
  David
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question