将一个表中的所有记录与另一个表中的所有记录进行比较

时间:2017-05-24 14:59:36

标签: sql cursor frequency

我需要一些帮助。我在SQL中查询的目标是使用两个单独查询的结果(结果A + B)计算特定车库中一个变量(汽车)的频率一段时间。

第一组结果(结果A)提供车库中的车辆列表及其各自的到达日期和退出日期(每条记录是一辆车,一个到达日期和一个退出日期)。第二组结果是使用@PeriodStartDate作为日期和@PeriodEndDate作为日期的CTE递归,提供结果(结果B)以及从开始日期到结束日期的所有日期。

从这里,我想要做的是从结果A中取出每一条记录,并将其与结果B的每条记录进行比较,有效地确定结果B的第一条记录/日期是否为1)> =到达日期,和2)<比退出日期。例如。如果2016-02-04到2016-03-15期间车辆在车库,并且期间开始和结束日期是2016-01-01到2016-03-01,我想拍这张车的记录,将其到达和退出日期与2016-01-01至2016-03-01的所有日期进行比较,然后对结果A中的每辆车/记录重复该过程。

完成此练习后,我希望查询产生2016-01-01至2016-03-01每个日期车库中存在的车辆数量。

到目前为止,对A和B结果的查询已经完成,但老实说我不知道​​如何开始下一部分。

谢谢你, MIKEY

1 个答案:

答案 0 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    //Server paths.
    private static string masterserverPath = @"\\IPAddress\e$\BatchImport\";
    private static string personalPath = masterserverPath + "HR PersonalFile\\";
    private static string benefitsPath = masterserverPath + "HR Benefits\\";
    private static string workCompPath = masterserverPath + "HR WC\\";
    private static string LOAPath = masterserverPath + "HR LOA\\";
    //values to dynamicaly populate 4 division in subdiv dropdown list
    string[] personalSubDivArr = {"Employment Inquiry", "EBI", "W-4", "Driver's Application", "Direct Deposit",
                            "Address Changes", "Name Changes", "Employment Verifications", "New Hire-Rehire Datasheet",
                            "PAF's Status Changes", "Acknoledgement of Wages for NY and CA", "Counseling",
                            "Record of Conversation", "Written Warning-Attachments", "Probationary Counseling-Attachments",
                            "Final Warning", "Final Incident Documentation", "Commendations", "Goals and Objectives",
                            "performance Reviews", "Associate Acknowledgements", "Associate Develepment Programs",
                            "Unemployment", "EEOC forms", "I-9"};

    string[] benefitsSubDivArr = {"Associate Benefits Enrollments", "Dependant Eligibility Verifications", "HIPPA Forms","Physicians Documents",
                            "Repayment Agreements", "Beneficiary forms", "Medical Claims", "Acknowledgement Forms", "Cancellation Forms"};

    string[] WCSubDivArr = {"First Report of Injury", "Workers Comp. Medical Claims", "Adjuster Notes", "Wage Statements Verifications",
                     "State Forms", "Medical Improvement Reports", "Release", "Miscellaneous"};

    string[] LOASubDivArr = {"Request for LOA", "Initial Letter", "Medical Certification", "Approval Letter", "Medical Updates", "Extensions",
                      "Release", "Return Letter", "Notes"};
    string newFileName;


protected void Page_Load(object sender, EventArgs e)
{

}

//Button1 fires up validations before uploading the file to the server
protected void Button1_Click(object sender, EventArgs e)
{

        //verify if a file to upload exists
        if (FileUpload.HasFile)
        {
            string fileExtension = System.IO.Path.GetExtension(FileUpload.FileName).ToLower();
            string[] allowedExtensions = {".pdf"};
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                //make sure document is of appropiate type **pdf in this case
                if (fileExtension == allowedExtensions[i])
                {
                    //make sure none of the fields are empty
                    if (string.IsNullOrWhiteSpace(LnameTextBox.Text) || string.IsNullOrWhiteSpace(FnameTextBox.Text)
                        || string.IsNullOrWhiteSpace(SSNTextBox.Text) || string.IsNullOrWhiteSpace(FileNoTextBox.Text)
                        || SubDivDropDownList.Text == "Select an option" || MasterDropDownList.Text == "Select an option")
                    {


                        UploadMssgLabel.Text = "Employee and Document info cannot be empty.";
                    }
                    else
                    {
                    //string to rename file
                        newFileName = (FnameTextBox.Text + " " + LnameTextBox.Text + " " +  SubDivDropDownList.Text 
                                        + " " + "FileNo-" +FileNoTextBox.Text + fileExtension);
                    Response.Write("<script>alert('Inside save');</script>");
                        try
                        {

                            //select which path to save the file to
                            string destinationPath;
                            switch (MasterDropDownList.Text)
                            {
                                case "Personal":
                                    destinationPath = personalPath;
                                    break;

                                case "Benefits":
                                    destinationPath = benefitsPath;
                                    break;

                                case "WorkersComp":
                                    destinationPath = workCompPath;
                                    break;

                                case "LOA":
                                    destinationPath = LOAPath;
                                    break;

                                default:
                                    destinationPath = null;
                                    break;
                            }

                        Response.Write("<script>alert('Saving "+ newFileName +"');</script>");
                        FileUpload.PostedFile.SaveAs(Server.MapPath(destinationPath) + newFileName); //I have tried with Server.MapPath() an without it
                        UploadMssgLabel.ForeColor = System.Drawing.Color.Green;
                        UploadMssgLabel.Text = "Upload Successful!";

                        }
                        catch (Exception ex)
                        {
                            Response.Write("<script>alert('" + ex.Message + "');</script>");
                        }
                    Response.Write("<script>alert('ending save');</script>");
                }

                }
                else
                {

                    UploadMssgLabel.Text = "Cannot accept files of this kind.";
                }
            }

        }
        else
        {

            UploadMssgLabel.Text = "Please select a file.";
        }

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    //Clear the subdiv dropdown every selection. Otherwise, items would just keep being added on
    SubDivDropDownList.Items.Clear();

    //switch to dynamicaly populate the subdiv dropdown
    switch (MasterDropDownList.SelectedValue)
    {
        case "Personal":
            populateDropDown(SubDivDropDownList, personalSubDivArr);
            break;

        case "Benefits":
            populateDropDown(SubDivDropDownList, benefitsSubDivArr);
            break;

        case "WorkersComp":
            populateDropDown(SubDivDropDownList, WCSubDivArr);
            break;

        case "LOA":
            populateDropDown(SubDivDropDownList, LOASubDivArr);
            break;

        default:
            SubDivDropDownList.Items.Add("Select an option");
            break;

    }
}

//Method to populate a dropdown with an array
private void populateDropDown(DropDownList list, Array arr)
{
    list.Items.Add("Select an option");

    foreach (string s in arr)
    {
        list.Items.Add(s);
    }
}

}

这将返回在期间结束之前到达并在期间开始后退出的汽车总数

你可以改变

Select Count(*) AS TotalCars, B.PeriodStart, B.PeriodEnd
FROM A
JOIN B
ON A.Arrival <= B.PeriodEnd 
AND A.Exit > B.PeriodStart
Group By B.PeriodStart, B.PeriodEnd

AND A.Exit > PeriodStart

捕捉最后一段时间尚未退出的汽车