Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Query: Retrieving records based on criteria in multiple tables
    primarykey
    data
    text
    <h3>The Setup</h3> <p>I'm using a SQL Server DB to track various cases in production at a manufacturing environment. The database has two tables:</p> <ol> <li><em>Status Table</em>: this tracks the status of those cases, with one of the columns containing an integer code that represents the cases status. All cases with codes greater than 0 are "in production" while those with codes less than or equal to 0 are either "complete" or "canceled".</li> <li><em>Log Table</em>: this tracks what changes have happened to a particular case and when those changes happened. The "what" aspect is captured both as a string and as a code. The "when" is a datetime field.</li> </ol> <h3>The Problem / Task / Question</h3> <p>I would like to query all cases that are in production <em>AND</em> any cases that have been completed or canceled <strong><em>within the past two days</em></strong>. In the log, I have a datetime column and a code specific to both cancelling or completing a case. I assume the query would maybe <code>JOIN</code> any cases from the log that have either of those codes and have datetimes within 48 hours prior of now. But I'm not quite sure how to go about doing this. The first part of my query (to get cases that are in production) is as follows: </p> <pre><code>SELECT * FROM test.dbo.status WHERE status &gt; 0 </code></pre> <p>Any help would be greatly appreciated. Thanks in advance!</p> <p><em><strong>UPDATE</em></strong> To clarify, two things:</p> <ol> <li>I would like the query to return all of the columns in the <em>status table</em> for records that match the criteria above, not just the ID or something.</li> <li><p>The table structures: </p> <pre><code>CREATE TABLE [dbo].[status]( [serial] [varchar](10) NOT NULL, [type] [varchar](50) NULL, [commit_date] [date] NULL, [sent_to_prod] [date] NULL, [target] [date] NULL, [sent_to_mfg] [date] NULL, [status] [int] NULL, [notes] [bit] NULL, [hold] [bit] NULL, [canceled] [bit] NULL, [priority] [bit] NULL, [vendors] [varchar](150) NULL, [rework] [varchar](50) NULL, [created_on] [datetime] NULL, [modified_on] [datetime] NULL ) CREATE TABLE [dbo].[log]( [serial] [varchar](13) NOT NULL, [action] [varchar](200) NOT NULL, [who] [varchar](80) NOT NULL, [time] [datetime] NOT NULL, [code] [varchar](20) NOT NULL ) </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload