Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 4729

Merging SQL queries

$
0
0
Hi, 

I posted here a few weeks ago, and received some great help. It ultimately ended up that I had to figure some stuff out on my own, b/c I was using an ODBC connection to use microsoft query against an HP3000, and that was resulting in some querying limitations. But thanks to all for the help up to that point.

I've since convinced the client that we need to start using SQL Server, so I've setup mirrored select tables from the HP database. So I'm using microsoft query against a SQL server database, and have made alot of progress.

However, I'm struggling with merging the 2 queries below. I want to create one SQL statement that produces the final results, so I don't have to go through a final step of a vlookup in excel. I'm really struggling with how to merge these. Do I do a nested query? Another join?

From a business perspective, what I'm trying to do (from an overall description) is output the customer, order, and # of pallets per order. The first query pulls back appropriate customer and order info, and the 2nd query returns # of pallets per order.

Query 1:

SELECT

OUTSIDE.OUTSIDE_SALES,

INSIDE.INSIDE_SALES,

CUST.CUST_NAME,

SOE.CUSTOMER_NO,

SOE.SALE_ORDER_NO,

SOE.INVOICED_DATE,

PICKUP.PICKUP,

(SOE.SALES_AMT/100) AS SALES,

(SOE.COST_GOODS_SOLD/100) AS COGS,

(SOE.SALES_AMT/100) - (SOE.COST_GOODS_SOLD/100) AS MARGIN

FROM

SOE_HEADER SOE,

CUST_NAME CUST,

INSIDE_SALES INSIDE,

OUTSIDE_SALES OUTSIDE,

PICKUP PICKUP

WHERE

SOE.CUSTOMER_NO = CUST.CUSTOMER_NO

AND SOE.INSIDE_ROUTE = INSIDE.INSIDE_ROUTE

AND SOE.SALESMAN_NO = OUTSIDE.SALESMAN_NO

AND SOE.TYPE_OF_FRGHT = PICKUP.TYPE_OF_FRGHT

AND SOE.TYPE_OF_ORDER = '00'

AND SOE.ORDERS_STATUS = '06'

AND SOE.DATE_OF_ORDER > 20080101

AND SOE.TYPE_OF_FRGHT IN ('06','08','10','13','14')

AND SOE.CUSTOMER_NO <> '1000027000'

AND SOE.CUSTOMER_NO <> '1000039000'

AND SOE.CUSTOMER_NO <> '1014869000'

AND SOE.CUSTOMER_NO <> '1014869001'

AND SOE.CUSTOMER_NO <> '1014869002'

AND SOE.CUSTOMER_NO <> '1014869003'

AND SOE.CUSTOMER_NO <> '1014889000'

AND SOE.CUSTOMER_NO <> '1014890000'

AND SOE.CUSTOMER_NO <> '4400000000'

AND SOE.CUSTOMER_NO <> '4499998000'

AND SOE.CUSTOMER_NO <> '4500012000'

AND SOE.CUSTOMER_NO <> '4500018000'

AND SOE.CUSTOMER_NO <> '6900002000'

AND CUST.CUST_NAME NOT LIKE '%PETCO%'

ORDER BY

OUTSIDE.OUTSIDE_SALES,

CUST.CUST_NAME,

SOE.INVOICED_DATE,

SOE.TYPE_OF_FRGHT


Query 2:

SELECT

SALES.SALE_ORDER_NO,

COUNT(TRACKING.PALLET_ID)

FROM

PALLET_SALES_ORD SALES INNER JOIN PALLET_TRACKING TRACKING

ON SALES.PALLET_ID = TRACKING.PALLET_ID

WHERE

TRACKING.STATUS_WORD = '04'

OR TRACKING.STATUS_WORD = '02'

GROUP BY

SALES.SALE_ORDER_NO

ORDER BY

SALES.SALE_ORDER_NO




Viewing all articles
Browse latest Browse all 4729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>