SQL - 在同一个表中加入匹配字段

时间:2016-07-14 17:37:51

标签: sql sql-server join

我有一个包含6个字段(JobGUID, FunctionName,InFileName,InFileSize,OutFileName,OutFileSize)

的表格

一个FunctionName是 CheckFile ,另一个是 UnZipFile CheckFileInFileNameInFileSizeUnZipFile应该等于OutFileNameOutFileSizeSELECT CheckFile.InFileName, CheckFile.InFileSize, UnZipFile.OutFileName, UnZipFile.OutFileSize, CheckFile.InFileSize - UnZipFile.OutFileSize as 'FileSizeDifference' FROM [MY_DATABASE].[dbo].[MY_TABLE] CheckFile JOIN [MY_DATABASE].[dbo].[MY_TABLE] UnZipFile ON CheckFile.InFileName = UnzipFile.OutFileName WHERE CheckFile.JobGUID = 'Some GUID #' and CheckFile.FunctionName = 'CheckFile' and UnZipFile.FunctionName = 'UnZipFile' and CheckFile.InFileName like '%.txt' 。我想做一个JOIN,允许我看到每个并排,所以我可以比较文件大小匹配。

这是我到目前为止所拥有的。它似乎工作,但重复每个文件名和文件大小。 CheckFile有22行,UnZipFile有22行。我希望它只输出22行,但它输出44。

package bikurim.silverfix.com.bikurim.adapters;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filterable;
import android.widget.Toast;

import java.util.ArrayList;

import bikurim.silverfix.com.bikurim.Constants;
import bikurim.silverfix.com.bikurim.adapters.holders.FamilyViewHolder;
import bikurim.silverfix.com.bikurim.adapters.holders.GenericViewHolder;
import bikurim.silverfix.com.bikurim.adapters.holders.TimeUpViewHolder;
import bikurim.silverfix.com.bikurim.models.Family;
import bikurim.silverfix.com.bikurim.R;
import bikurim.silverfix.com.bikurim.utils.Utils;
import bikurim.silverfix.com.bikurim.utils.interfaces.HolderListener;
import bikurim.silverfix.com.bikurim.utils.managers.CountDownManager;
import bikurim.silverfix.com.bikurim.utils.interfaces.EventListener;

public class FamiliesAdapter extends GenericAdapter implements Filterable, EventListener, HolderListener {

    // last position holds the last position of the element that was added, for animation purposes
    private static int lastPosition = -1;

    private int[] viewTypes;

    private ItemTouchHelper touchHelper;

    private CountDownManager countDownManager;

    public FamiliesAdapter(Context context, ArrayList<Family> families, ItemTouchHelper touchHelper) {
        super(context, families);
        this.touchHelper = touchHelper;

        viewTypes = Constants.Tags.VIEW_TYPES;

        countDownManager = new CountDownManager(Constants.Values.TIME_INTERVAL, this);
        countDownManager.start();
    }


    @Override
    public GenericViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Inflating the view from a given layout resource file
        Log.d("A function has called", "onCreateViewHolder() was invoked");
        View v;
        switch (viewType) {
            case Constants.Tags.FAMILY_VIEW:
                v = LayoutInflater.from(context).inflate(R.layout.family_list_item, parent, false);
                FamilyViewHolder fvh = new FamilyViewHolder(context, v);
                return fvh;
            case Constants.Tags.TIME_UP_VIEW:
                v = LayoutInflater.from(context).inflate(R.layout.timeup_list_item, parent, false);
                TimeUpViewHolder tvh = new TimeUpViewHolder(context, v, this);
                return tvh;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(GenericViewHolder holder, int position) {
        // Binds the Family object to the holder view
        Family family = families.get(position);
        holder.bindData(family);

        Log.d("A function has called", "onBindViewHolder() was invoked, FAMILY NAME: " + family.name);

        if(holder instanceof FamilyViewHolder){
            countDownManager.addHolder((FamilyViewHolder) holder);
        }

        // Sets animation on the given view, in case it wasn't displayed before
        Utils.setSlideAnimation(context, holder.getView(), position, lastPosition, false);
    }
    // If the time of family has ended, return TIME_UP_VIEW
    // else, return FAMILY_VIEW
    @Override
    public int getItemViewType(int position) {
        if(families.get(position).whenInMillis <= System.currentTimeMillis())
            return viewTypes[1];
        return viewTypes[0];
    }

    /* Changes the UI of a holder to a time's up view with a flickering ImageButton and a TextView*/

    @Override
    public void onFinish(FamilyViewHolder holder) {
        // Switches between the clock icon to the alarm icon
        holder.reset();
        notifyItemChanged(holder.getAdapterPosition());
    }

    @Override
    public void onLessThanMinute(FamilyViewHolder holder) {
        Drawable stroke = ContextCompat.getDrawable(context, R.drawable.family_item_red_stroke);
        holder.frame.setBackground(stroke);

        Utils.setFadeAnimation(holder.frame);
        holder.isStrokeChanged = true;
    }

    @Override
    public void onRemoveTimeUpViewHolder(int pos, GenericViewHolder holder) {
        touchHelper.startSwipe(holder);
        removeData(pos, holder);
    }

    public void removeData(int pos, GenericViewHolder holder) {
        // Sets the last position to the given deleted position for animation purposes
        lastPosition = pos;

        // Removes the family object from the data set
        String name = Utils.getLastName(families.get(pos).name);

        families.remove(pos);
        notifyItemRemoved(pos);
        notifyItemRangeChanged(pos, getItemCount());

        if(holder instanceof FamilyViewHolder)
            // Cancels the the timer and removes it from the entry set
            countDownManager.removeHolder((FamilyViewHolder) holder);

        holder.reset();
        Toast.makeText(context, "משפחת "+name+" נמחקה מהרשימה", Toast.LENGTH_SHORT).show();
    }


    /* Cancels the timers and clears the entry set */
    public void cancelTimers() {
        countDownManager.reset();
        countDownManager.clear();
        countDownManager.stop();
    }

    /* Clears the adapter's data and resets the last position to -1 */
    @Override
    public void clearData() {
        super.clearData();
        cancelTimers();
        lastPosition = -1;
    }

    @Override
    public void onViewRecycled(GenericViewHolder holder) {
        holder.reset();
        super.onViewRecycled(holder);
    }
}

2 个答案:

答案 0 :(得分:1)

尝试

select distinct etc.

应该这样做。

答案 1 :(得分:1)

您需要使用INNER JOIN

SELECT CheckFile.InFileName, CheckFile.InFileSize,
       UnZipFile.OutFileName, UnZipFile.OutFileSize,
       CheckFile.InFileSize - UnZipFile.OutFileSize as 'FileSizeDifference'
FROM [MY_DATABASE].[dbo].[MY_TABLE] CheckFile
**INNER JOIN** [MY_DATABASE].[dbo].[MY_TABLE] UnZipFile ON CheckFile.InFileName = UnzipFile.OutFileName
WHERE CheckFile.JobGUID = 'Some GUID #'
  and CheckFile.FunctionName = 'CheckFile'
  and UnZipFile.FunctionName = 'UnZipFile'
  and CheckFile.InFileName like '%.txt'

根据:w3schools

  

INNER JOIN:当BOTH表中至少有一个匹配时返回所有行

     

LEFT JOIN:返回左表中的所有行,以及右表中匹配的行

     

RIGHT JOIN:返回右表中的所有行,以及左表中匹配的行

     

FULL JOIN:当其中一个表中存在匹配时返回所有行