SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How do you use a sub query in a join?


How do you use a sub query in a join?

Author
Message
dwilliscp
dwilliscp
SSC Eights!
SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)

Group: General Forum Members
Points: 872 Visits: 775
Looking to take the following query:

SELECT sales_order_number
, count(sales_order_number) as Notification_cnt
from ztb_IMP_Notifications
group by sales_order_number

and use it to feed the ztb_IMP_Notifications section of the below query:

SELECT ztb_Carrier_Delivery_Metrics.Delivery
, ztb_Carrier_Delivery_Metrics.Delivery_Item
, ztb_IMP_Notifications.Sales_Order_Number
, ztb_Carrier_Delivery_Metrics.REF_DOC
-- insert Notification_cnt here
FROM ztb_Carrier_Delivery_Metrics INNER JOIN
ztb_IMP_Notifications ON ztb_Carrier_Delivery_Metrics.REF_DOC = ztb_IMP_Notifications.Sales_Order_Number
where ztb_carrier_Delivery_Metrics.Ref_Doc > ''
Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29569 Visits: 39986
it's going to look somethign like this: i wasn't sure ont eh join criteria: i was assuming the table ztb_IMP_Notifications has the same sales_order_number

SELECT
ztb_Carrier_Delivery_Metrics.Delivery,
ztb_Carrier_Delivery_Metrics.Delivery_Item,
ztb_IMP_Notifications.Sales_Order_Number,
ztb_Carrier_Delivery_Metrics.REF_DOC,
-- insert Notification_cnt here
MyAlias.Notification_cnt
FROM ztb_Carrier_Delivery_Metrics
INNER JOIN ztb_IMP_Notifications
ON ztb_Carrier_Delivery_Metrics.REF_DOC = ztb_IMP_Notifications.Sales_Order_Number
LEFT OUTER JOIN (SELECT
sales_order_number,
COUNT(sales_order_number) AS Notification_cnt
FROM ztb_IMP_Notifications
GROUP BY sales_order_number
) MyAlias
ON ztb_IMP_Notifications.sales_order_number = MyAlias.sales_order_number
WHERE ztb_carrier_Delivery_Metrics.Ref_Doc > ''



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17474 Visits: 19122
You could also consider using CTEs.


WITH Notifications AS (
SELECT sales_order_number
, count(sales_order_number) as Notification_cnt
FROM ztb_IMP_Notifications
GROUP BY sales_order_number)
SELECT ztb_Carrier_Delivery_Metrics.Delivery
, ztb_Carrier_Delivery_Metrics.Delivery_Item
, n.Sales_Order_Number
, ztb_Carrier_Delivery_Metrics.REF_DOC
, n.Notification_cnt
FROM ztb_Carrier_Delivery_Metrics cdm
INNER JOIN Notifications n ON cdm.REF_DOC = n.Sales_Order_Number
WHERE cdm.Ref_Doc > ''




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
dwilliscp
dwilliscp
SSC Eights!
SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)

Group: General Forum Members
Points: 872 Visits: 775
Thanks for your help... I just could not figure out how to word the statement ... looking at some samples I found on the internet.

Thanks for the help from both of you.
dwilliscp
dwilliscp
SSC Eights!
SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)

Group: General Forum Members
Points: 872 Visits: 775
Checking the data.. there might be something wrong with the join too... I would expect that all this would be on one line... but we might be getting extra lines for some other reason...MRP Area, maybe.

0085177217 900003 0002619226 0002619226 55
0085177217 900003 0002619226 0002619226 55
0085177217 900003 0002619226 0002619226 55
0085177217 900003 0002619226 0002619226 55
0085177217 900003 0002619226 0002619226 55
0085177217 900003 0002619226 0002619226 55
0085177217 900003 0002619226 0002619226 55
0085177217 900003 0002619226 0002619226 55
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17222 Visits: 19557
SELECT 
d.Delivery,
d.Delivery_Item,
n.Sales_Order_Number,
d.REF_DOC,
n.Notification_cnt
FROM ztb_Carrier_Delivery_Metrics d
INNER JOIN (
SELECT
sales_order_number,
Notification_cnt = COUNT(sales_order_number)
FROM ztb_IMP_Notifications
GROUP BY sales_order_number
) n
ON n.Sales_Order_Number = d.REF_DOC
WHERE d.Ref_Doc > ''



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
dwilliscp
dwilliscp
SSC Eights!
SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)

Group: General Forum Members
Points: 872 Visits: 775
dwilliscp (8/14/2012)
Thanks for your help... I just could not figure out how to word the statement ... looking at some samples I found on the internet.

Thanks for the help from both of you.


yep, problem with the join.. I need to know that there were 55 Notifications, but not get 55 rows. I guess we could do a distinct on the outside query.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17474 Visits: 19122
Is REF_DOC a unique value? Or do you have it several times in ztb_Carrier_Delivery_Metrics?
As I see it, this is a problem with your data.

EDIT: Could you show us your code and sample data to be sure?


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17222 Visits: 19557
dwilliscp (8/14/2012)
dwilliscp (8/14/2012)
Thanks for your help... I just could not figure out how to word the statement ... looking at some samples I found on the internet.

Thanks for the help from both of you.


yep, problem with the join.. I need to know that there were 55 Notifications, but not get 55 rows. I guess we could do a distinct on the outside query.


How many rows does this return?

SELECT 
d.Delivery,
d.Delivery_Item,
--n.Sales_Order_Number,
d.REF_DOC--,
--n.Notification_cnt
FROM ztb_Carrier_Delivery_Metrics d
WHERE d.REF_DOC = '0002619226'



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
dwilliscp
dwilliscp
SSC Eights!
SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)SSC Eights! (872 reputation)

Group: General Forum Members
Points: 872 Visits: 775
ChrisM@Work (8/14/2012)
dwilliscp (8/14/2012)
dwilliscp (8/14/2012)
Thanks for your help... I just could not figure out how to word the statement ... looking at some samples I found on the internet.

Thanks for the help from both of you.


yep, problem with the join.. I need to know that there were 55 Notifications, but not get 55 rows. I guess we could do a distinct on the outside query.


How many rows does this return?

SELECT 
d.Delivery,
d.Delivery_Item,
--n.Sales_Order_Number,
d.REF_DOC--,
--n.Notification_cnt
FROM ztb_Carrier_Delivery_Metrics d
WHERE d.REF_DOC = '0002619226'



You get 6 rows, different deliveries though. (since it seems to omit tabs, I am going to try spaces. The Prime Key in the Carrier Delivery Metrics is Delivery, Line..but the Notifications file only has sales order. Ok does not like spaces so will Use underscores)

Delivery__________Line_________Sales Order
0085180733______900015_______0002619226
0085186304______900003_______0002619226
0085159667______900003_______0002619226
0085177217______900003_______0002619226
0085184918______900012_______0002619226
0085192416______900001_______0002619226
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search