0

我有一个 SQL 表,它存储向客户收取的不同费率。该表还包括一个“默认”费率列表,如果客户没有特定的费率,则使用这些费率。我需要的是,一旦用户从组合框中选择了一个客户,为该客户创建一个费率表,由默认值或特定于该客户的费率或两者的组合组成,如下所示:

Client     Type of Work     Rate
ABC Ltd       Type A        100
XYZ & Co      Type B        150
Default       Type A        125
Default       Type B        175

因此返回的费率如下所示:

ABC Ltd       Type A        100
              Type B        175   (default)
XYZ & Co      Type A        125   (default)
              Type B        150
Anyone else   Type A        125   (default)
              Type B        175   (default)

数据库中的信息结构如下

Table for clients
Table for Types of Work
Table for Rates   (foreign keys linking to clients and types of work)

谁能解释如何最好地使用 SQL 语句、winforms 应用程序中的 C# 例程或两者的组合来获得上述结果。

PS。我很抱歉,但我不确定如何在本网站的表格中设置我的数据。

到目前为止我已经尝试过什么

public static DataTable TableOfRates(string pIPName = "Default")
    {
        try
        {
            string sqlText1 = "SELECT RateID, IPName, RateName, Rate, DateFrom, DateTo, Active, " +
                              "(RateName + ': £' + CONVERT(VARCHAR, Rate) + CONVERT(VARCHAR, " +
                              "(CASE WHEN Active = '1' THEN ' (Active)' ELSE ' (Inactive)' END))) AS ListRate " +
                              "FROM tblRates WHERE IPName IS NULL AND Active = 'true';";
            aSqlQuery cQ1 = new aSqlQuery(sqlText1, "table");
            DataTable defaultRates = cQ1.TableResult;

            string sqlText2 = "SELECT RateID, IPName, RateName, Rate, DateFrom, DateTo, Active, " +
                              "(RateName + ': £' + CONVERT(VARCHAR, Rate) + CONVERT(VARCHAR, " +
                              "(CASE WHEN Active = '1' THEN ' (Active)' ELSE ' (Inactive)' END))) AS ListRate " +
                              "FROM tblRates WHERE IPName = '" + pIPName + "' " +
                              "AND Active = 'true';";
            aSqlQuery cQ2 = new aSqlQuery(sqlText2, "table");
            DataTable clientRates = cQ2.TableResult;

            foreach(DataRow defaultRow in defaultRates.Rows)
            {
                string defaultType = defaultRow["RateName"].ToString();
                foreach (DataRow clientRow in clientRates.Rows)
                {
                    string clientType = clientRow["RateName"].ToString();
                    if (defaultType == clientType)
                    {
                        clientRates.Rows.Add(defaultRow.ItemArray);
                    }
                }
            }

            return clientRates;
        }
        catch (Exception eX)
        {
            throw new Exception("cRate: Error compiling Table Of Rates (mode = '" + pIPName + "')" + Environment.NewLine + eX.Message);
        }
    }

但是,无论我使用 Add Row、Import Row 还是 Add Item Array,都会出现错误。它似乎不喜欢使用来自多个表的数据行。

4

2 回答 2

0

如果你真的需要加载一个 DataTable:

CREATE TABLE 
     #MyTempTable (myColumn1 DataType, myColumn2 DataType, myColumn3 DataType) 
INSERT INTO 
     #MyTempTable(myColumn1, myColumn2, myColumn3)
SELECT 
     f.Bar1, o.Bar2, o.Bar3 FROM FooTable AS f 
INNER JOIN 
     TableFoo o ON f.Bar1 = o.Bar1

通过一些调整,此脚本将创建一个临时表,只要您的连接保持活动状态并包含您需要的其他表中的数据,该表就会持续存在。

于 2012-11-19T16:43:36.840 回答
0

You must have a table that lists clients and jobs - with that in mind you might be doing something like the following

SELECT
  CASE R1.RATE WHEN NULL THEN R2.RATE else r1.rate AS RATE END
FROM
  CLIENTJOBS C LEFT JOIN RATES R1 ON
    R1.CLIENT = C.CLIENT AND R1.WORKTYPE = C.WORKTYPE
  JOIN RATES R2 ON
    R2.WORKTYPE = C.WORKTYPE AND R2.CLIENT = 'DEFAULT'

bear in mind this is untested guess-code, but hopefully points you in the right direction

于 2012-11-19T16:10:50.070 回答