Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 2nd, 2008, 12:14 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: West Palm Beach, Florida, USA.
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default Query does not produce desired result

I have a query that selects job production data (see code below). The query works except for that I am not able to extract existing "notes" for a specific job.


Code:
SELECT          dbo.event.eventdate, dbo.Job.CompanyJobId, dbo.Item.CompanyItemId,
                dbo.JobNoteEvent.Note, dbo.productionevent.quantity   

FROM            dbo.Batch
        inner join dbo.event on dbo.event.batchguid = dbo.batch.batchguid
        inner join dbo.job on dbo.job.jobguid = dbo.event.jobguid
        inner join dbo.item on dbo.item.itemguid = dbo.event.itemguid
        inner join dbo.productionevent on dbo.productionevent.eventguid = dbo.event.eventguid
                left join dbo.JobNoteEvent on dbo.JobNoteEvent.EventGuid = dbo.event.eventguid

WHERE          dbo.Job.CompanyJobId = 3505048 and dbo.Item.CompanyItemId = '05001' 
           
ORDER BY       dbo.event.eventdat
Result:

EventDate = 2008-04-07
CompanyJobId = 3505048
CompanyItemId = 05001
Notes = Null
Quantity = 276.78


Now, I if comment out lines that extarcts the quantity like this:


SELECT dbo.event.eventdate, dbo.Job.CompanyJobId, dbo.Item.CompanyItemId,
                dbo.JobNoteEvent.Note <s>--, dbo.productionevent.quantity </s>
FROM dbo.Batch
        inner join dbo.event on dbo.event.batchguid = dbo.batch.batchguid
        inner join dbo.job on dbo.job.jobguid = dbo.event.jobguid
        inner join dbo.item on dbo.item.itemguid = dbo.event.itemguid
        <s>--inner join dbo.productionevent on dbo.productionevent.eventguid = dbo.event.eventguid </s>
                left join dbo.JobNoteEvent on dbo.JobNoteEvent.EventGuid = dbo.event.eventguid

WHERE dbo.Job.CompanyJobId = 3505048 and dbo.Item.CompanyItemId = '05001'
        
ORDER BY dbo.event.eventdate


Then I get the notes like below

EventDate = 2008-04-07
CompanyJobId = 3505048
CompanyItemId = 05001
Notes = LTL 1/1 7484-7915

Now, If I do this:

Code:
SELECT            i.eventdate, i.companyjobid, i.companyitemid, n.note, i.quantity 
FROM
(SELECT         dbo.event.eventdate, dbo.Job.CompanyJobId, dbo.Item.CompanyItemId,
                dbo.productionevent.quantity   
FROM            dbo.Batch
        inner join dbo.event on dbo.event.batchguid = dbo.batch.batchguid
        inner join dbo.job on dbo.job.jobguid = dbo.event.jobguid
        inner join dbo.item on dbo.item.itemguid = dbo.event.itemguid
        inner join dbo.productionevent on dbo.productionevent.eventguid = dbo.event.eventguid
                                 
WHERE           dbo.Job.CompanyJobId = 3505048 and dbo.Item.CompanyItemId = '05001' and dbo.productionevent.quantity > 0 
           
GROUP BY        dbo.event.eventdate, dbo.Job.CompanyJobId, dbo.Item.CompanyItemId, dbo.productionevent.quantity)i
Inner JOIN
(SELECT         dbo.Job.CompanyJobId, dbo.Item.CompanyItemId, dbo.JobNoteEvent.Note
FROM            dbo.Batch
                inner join dbo.event on dbo.event.batchguid = dbo.batch.batchguid
        inner join dbo.job on dbo.job.jobguid = dbo.event.jobguid
        inner join dbo.item on dbo.item.itemguid = dbo.event.itemguid
        left join dbo.JobNoteEvent on dbo.JobNoteEvent.EventGuid = dbo.event.eventguid
WHERE           dbo.Job.CompanyJobId = 3505048 and dbo.Item.CompanyItemId = '05001'
GROUP BY        dbo.event.eventdate, dbo.Job.CompanyJobId, dbo.Item.CompanyItemId, dbo.JobNoteEvent.Note)n
ON              n.CompanyJobId = i.CompanyJobId
Result: I get 2 records (I just want the record that have both qty and notes)

EventDate = 2008-04-07
CompanyJobId = 3505048
CompanyItemId = 05001
Notes = Null
Quantity = 276.78

EventDate = 2008-04-07
CompanyJobId = 3505048
CompanyItemId = 05001
Notes = LTL 1/1 7484-7915
Quantity = 276.78
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a Make Table Query produce a Linked table? kronik Access 5 May 16th, 2006 06:17 AM
Show result from query akibaMaila VB.NET 2002/2003 Basics 2 January 18th, 2005 12:43 PM
Unexpected result from the query tsimsha Classic ASP Databases 1 August 23rd, 2004 03:50 AM
Query Result mateenmohd SQL Server 2000 0 November 1st, 2003 03:53 AM
Archiving query result Ned SQL Server 2000 5 October 8th, 2003 03:34 PM



All times are GMT -4. The time now is 04:01 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.