连接的SQL过滤

时间:2017-04-21 19:28:37

标签: sql sql-server

我正在编写我的第一个SQL查询,所以请原谅我对此事缺乏了解。

我希望从每次加入过滤多次,看起来执行的查询总行数正在增长,而不是缩小。

/*  

Reads Order Status, determines if it's OPEN
Pulls all OPEN orders to Time Tickets
Reads the Time Ticket TicketDate, determines if it's > 90 days old
Compares PODet JobNo, joins PO table
Reads the PO DateMod, determines if it's > 90 days old

*/


DECLARE @now DATETIME
DECLARE @90daysago DATETIME


SET @now = GETDATE()
SET @90daysago = DATEADD(day, -90, @now)

SELECT
    o.JobNo,
    o.OrderNo,
    o.PartNo,
    o.Status,
    o.JobNo,
    t.TicketDate,
    p.Status,
    p.OutSideService,
    p.PONum,
    po.DateEnt,
    po.DateMod


FROM
    RBCBEMD.dbo.OrderDet AS o       /* OrderDet = o */

INNER JOIN RBCBEMD.dbo.TimeTicketDet AS t       /* TimeTicket = t */
    ON o.JobNo = t.JobNo

INNER JOIN RBCBEMD.dbo.PODet AS p           /* PODet = p */
    ON o.JobNo = p.JobNo

INNER JOIN RBCBEMD.dbo.PO AS po         /* PO = po */
    ON p.PONum = po.PONum

WHERE 
    o.Status = 'Open' AND
    t.TicketDate <= @90daysago AND
    po.DateMod <= @90daysago

ORDER BY
    cast(t.TicketDate as DATETIME) DESC

该查询应该从OrderDet表中查找OPEN订单。从那里,如果它是OPEN,那么从TimeTicketDet表中拉入最后一个TicketDate。确定TicketDate是否> 90天。如果它> 90天,请从PO表中提取PONum,找到其DateMod并确定它是否> 90天。

如果(o.status ='打开')AND(t.ticketDate&gt; 90天)和(po.DateMod&gt; 90天),则发布JobNo的结果。

2 个答案:

答案 0 :(得分:0)

让我先简单回答你的问题:你的表有一对多或多对多的关系导致重复的行返回。您将需要创建表,用于对多个连接进行排序,或者在where语句中应用更强大的过滤器以消除它们。

如果你看看你的结果,你会看到重复的键,可能是这样的:

O.Jobno | P.Status 1 |打开 1 |关闭

请注意第一个表(O.Jobno)中的主键将多次出现。

这是一篇很好的文章,可以帮助您朝着正确的方向前进: Resolve many to many relationship

答案 1 :(得分:0)

哦,第二次看完你的需求后,我发现了问题。您只想从TimeTicketDet表中提取最后一个票证日期。您需要一个简单的不存在来删除重复的行:

import { NgZone } from '@angular/core';
import { LoadingController } from 'ionic-angular';

...

@Injectable()
export class AppService {
    private loader: any;

    constructor ( private ngZone: NgZone, private loadingCtrl: LoadingController, ... ) {

    }

    presentLoading() {
        this.loader = this.loadingCtrl.create({
            content: "Please wait...",
            showBackdrop: true, //dark background while loading
            dismissOnPageChange: true
        });
        this.loader.present();
    }

    updateLoadingText(msg: string) {
        this.loader.data.content = content; //change content of loader
    }

    dismissLoading() {
        this.loader.dismiss();
    }

    onProgress(msg: string) 
    {
        this.ngZone.run(() => 
        {
            this.updateLoadingText(msg);
        });
    }

    fetchNotificationListAferUserDataget() {

        this.presentLoading();
        this.onProgress("Please wait... Fetching online notifications");

        this._userDataService.getNotificationList().subscribe((data) => {

            this.dismissLoading();
                ...
                    this.presentLoading();
                    this.onProgress("Please wait... Fetching your purchased packages");

                    this._userDataService.getAllPackageByUser(this.userData.user_id).subscribe(
                      (data) => this.populateUserPackages(data),
                      (err) => this.showDataFetchErrorFromServer('Unable to fetch user packages')
                    )
                ...
            },
            (err) => {
              this.dismissLoading(); //dismiss is also needed in error section(s)
              this.showDataFetchErrorFromServer('Unable to fetch notifications')
            }
        );
    };
}