简体   繁体   English

根据条件丢弃SQL结果行

[英]discard SQL result row based on condition

I have the following data: 我有以下数据:

Ord_Act_Deliv_DateTime  TrmnlGrp_Key    Ord_CustLoc_Key Ord_Driver_Key     Ord_Inv_No    Credits
2016-04-01 11:00:00     3               429             93                 INV-00055295  True
2016-04-01 11:00:00     3               135             93                 INV-00055295A False
2016-04-01 11:00:00     3               429             93                 INV-00055295C False

what I'm trying to accomplish is as follows 我想要完成的是如下

if credits = true
set @temp_invoice_no = Ord_inv_No + 'C'

SELECT * FROM Orders WHERE Ord_Inv_No NOT IN (original inv no with credit, inv no + 'C')

I may have many records like these in my selected date range and I need to make sure the original and credit do not make it into the final result set. 在我选择的日期范围内,我可能会有很多这样的记录,我需要确保原始和信用不会进入最终结果集。 From my research so far, it looks like most suggest CASE for simple comparisons (which I don't think this can fit that situation) or IF designating seperate select statements with a variable that is passed or declared in the statement. 从我到目前为止的研究来看,看起来大多数建议CASE用于简单比较(我认为这不适合这种情况)或IF指定单独的select语句,其中包含在语句中传递或声明的变量。 Maybe I'm misunderstanding the capabilities of CASE and IF. 也许我误解了CASE和IF的功能。 Please help, I'm stuck. 请帮忙,我被困住了。

I can also explore doing this in C#, but I'm still fairly green there. 我也可以在C#中探索这个,但我在那里仍然相当绿。 I've got the result in a DataTable but evaluating the table is not something I've figured out how to do other than to throw each row to a string. 我已经在DataTable中得到了结果,但是对表进行评估并不是我想出了除了将每一行抛出到字符串之外的其他方法。

So, you've got a set of records where the columns to focus on are Ord_Inv_No and Credits . 因此,您有一组记录,其中要关注的列是Ord_Inv_NoCredits

That first bit, you're setting a temporary variable with which you don't seem to be doing anything. 第一点,你正在设置一个临时变量,你似乎没有做任何事情。

As for the select query 至于选择查询

 SELECT * FROM Orders WHERE Ord_Inv_No NOT IN (original inv no with credit, inv no + 'C')

The only distinguishing thing I see in original inv no with credit, inv no + 'C' is that it's Credits column value is set to True (from the first bit). 我在original inv no with credit, inv no + 'C'看到的唯一区别是original inv no with credit, inv no + 'C'是它的Credits列值设置为True (从第一位开始)。 If that's the case, this query should work for you: 如果是这种情况,此查询应该适合您:

SELECT * FROM Orders WHERE Credits=true

I ended up doing the following in C#: 我最终在C#中做了以下事情:

 foreach(DataRow row in results.Rows)
        {
            credit = Convert.ToBoolean(row["Credits"].ToString());
            if(credit)
            {
                if (String.IsNullOrWhiteSpace(selectString))
                {
                    tempString = row["Ord_Inv_No"].ToString();
                    selectString = String.Format("'{0}', '{0}C'",tempString);
                }
                else
                {
                    tempString = row["Ord_Inv_No"].ToString();
                    selectString += String.Format(", '{0}', '{0}C'", tempString);
                }

            }
        }

        // The following three lines are for troubleshooting, shows the items being selected out and the initial count.
        // If DriverKey is 9 and dates are 5-2-16 - 5-8-16 String should show to INV numbers and count should be 19
        if (!String.IsNullOrWhiteSpace(selectString))
            MessageBox.Show(selectString);
        MessageBox.Show(results.Rows.Count.ToString());
        // only process if string is not empty, need code

        if (!String.IsNullOrWhiteSpace(selectString))
        {
            try
            {
                var rows = results.Select("Ord_Inv_No IN (" + selectString + ")");
                foreach (var row in rows)
                    row.Delete();
                results.AcceptChanges();
                // This is for troubleshooting, gets the final number of rows after edits 
                // If DriverKey is 9 and dates are 5-2-16 - 5-8-16 final count should be 17
                MessageBox.Show(results.Rows.Count.ToString());
                results.Columns.Remove("Credits");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM