操作命令查询字符串以返回1个COUNT子句?

时间:2013-12-10 15:59:22

标签: c# sql .net winforms ibm-midrange

我创建了一个winforms应用程序,允许用户设置他们的标准,查询ibm 400系统,查看结果,然后邮件合并选定的结果。作为一项额外的功能,我正在尝试设置一个标签,告诉用户选择标准(通过代码搜索,而不是单击搜索)从给定标准的总结果中打印了多少条记录。

示例:35个中的30个已打印。

这意味着在跟踪表中已标记了30份文件,共打印了35份。

目前我正在处理该字符串的总记录数,而不是“打印多少”部分。所以目前,我的代码将标签设置为“#of”+ RecCount +“打印”。

我遇到了一些问题。用户可以选择3种数据源: System1 System2 BOTH

以下详述我的查询和结果:

CRITERIA :20121209,System1,所有记录,旧地址

QUERY :SELECT COUNT(*)AS RecCount FROM Library2.Table1 a,Library2.Table2 b WHERE a.memno = b.memno and b.groupid ='N2'和b.type = 'B'和b.datec = 20131209 AND(a.addr1<> b.addr1或a.addr2<> bdadr2或a.city<> b.city或a.state<> ; b.state或a.zip<> b.zip)

返回CNT :24

CRITERIA :20121209,System2,所有记录,旧地址

QUERY :SELECT COUNT(*)AS RecCount FROM psrslib.pai000pf a,psrslib.pch010pf b WHERE a.memno = b.memno and b.groupid ='PAI0002'and b.type = 'B'和b.datec = 20131209 AND(a.addr1<> b.addr1或a.addr2<> bdadr2或a.city<> b.city或a.state<> ; b.state或a.zip<> b.zip)

返回CNT :14

上述两项似乎运作正常。但是当我来到BOTH数据源时:

标准:20121209,BOTH,ALL RECORDS,OLD ADDR

QUERY :SELECT COUNT()AS RecCount FROM psrslib.pai000pf a,psrslib.pch010pf b WHERE a.memno = b.memno and b.groupid ='PAI0002'and b。 type ='B'和b.datec = 20131209 AND(a.addr1<> b.addr1或a.addr2<> b.addr2或a.city<> b.city或a.state&lt ;> b.state或a.zip<> b.zip)UNION SELECT COUNT()AS RecCount FROM ntrslib.nai000pf a,ntrslib.nch010pf b WHERE a.memno = b.memno和b。 groupid ='NAI0002'和b.type ='B'和b.datec = 20131209 AND(a.addr1<> bdadr1或a.addr2<> b.addr2或a.city<> b.city或a.state<> b.state或a.zip<> b.zip)

返回CNT :24 - //这应该是38

在检查我的代码并使用服务器资源管理器时,我发现了以下内容:

我原来的搜索查询字符串操作之前只接收Count:

SELECT a.memno, 
a.name, 
a.addr1, 
a.addr2, 
a.city, 
a.state, 
a.zip, 
a.sex, 
a.lname, 
a.ssan, 
b.addr1 as old_addr1, 
b.addr2 as old_addr2, 
b.city as old_city, 
b.state as old_state, 
b.zip as old_zip, 
b.timec 

FROM Library1.Table1 a, Libary1.Table2 b 
WHERE a.memno = b.memno and 
b.groupid = 'P2' and 
b.type = 'B' and 
b.datec = 20131209 
AND (a.addr1 <> b.addr1 or a.addr2 <> b.addr2 or a.city <> b.city or a. state <> b.state or a.zip <> b.zip) 

UNION SELECT a.memno, 
a.name, 
a.addr1, 
a.addr2, 
a.city, 
a.state, 
a.zip, 
a.sex, 
a.lname, 
a.ssan, 
b.addr1 as old_addr1, 
b.addr2 as old_addr2, 
b.city as old_city, 
b.state as old_state, 
b.zip as old_zip, 
b.timec 

FROM Library2.Table1 a, Library2.Table2 b 
WHERE a.memno = b.memno and 
b.groupid = 'N2' and 
b.type = 'B' and 
b.datec = 20131209 
AND (a.addr1 <> b.addr1 or a.addr2 <> b.addr2 or a.city <> b.city or a. state <> b.state or a.zip <> b.zip) 

ORDER BY timec desc

我的代码:

    private void getNumPrinted()
            {
                string qryCmd = "";
                OdbcDataReader dr;

                if (cnt > 1)
                {
                    switch (cmbLetterType.SelectedIndex)
                    {
                        case 0:
                            docType = "oldAddr";
                            qryCmd = buildSearchQuery(docType);
                            break;
                        case 1:
                            docType = "newAddr";
                            qryCmd = buildSearchQuery(docType);
                            break;
                        case 2:
                            docType = "nameChg";
                            qryCmd = buildSearchQuery(docType);
                            break;
                    }

                    string query = "";

                    if (qryCmd.Contains("INNER JOIN"))
                    {
                        var newQry = qryCmd.Replace(qryCmd.Substring(0, qryCmd.IndexOf("FROM TableC c") - 1), "SELECT COUNT(a.memno) AS RecCount");
                        var orderByIndex = newQry.IndexOf("ORDER BY");
                        newQry = newQry.Replace(newQry.Substring(orderByIndex, newQry.Length - orderByIndex), "");
                        query = newQry;


   }
                else
                {
                    var newQry = qryCmd.Replace(qryCmd.Substring(0, qryCmd.IndexOf("FROM") - 1), "SELECT COUNT(*) AS RecCount");
                    var orderByIndex = newQry.IndexOf("ORDER BY");
                    newQry = newQry.Replace(newQry.Substring(orderByIndex, newQry.Length - orderByIndex), "");
                    query = newQry;
                }

                dr = mdl.GetData(query);
                while (dr.Read())
                {
                    lblNumPrinted.Text = "# out of " + dr["RecCount"].ToString() + " printed";
                }

                // NEED TO PERFORM ANOTHER QUERY COMBINED WITH TableC to see how many records have not been printed.

                mdl.closeConn();
                lblNumPrinted.Visible = true;
            }
            cnt++;
        }

字符串操作后:

SELECT COUNT(*) AS RecCount 
FROM Library1.Table1 a, Library1.Table2 b 
WHERE a.memno = b.memno and 
b.groupid = 'P2' and 
b.type = 'B' and 
b.datec = 20131209 
AND (a.addr1 <> b.addr1 or a.addr2 <> b.addr2 or a.city <> b.city or a. state <> b.state or a.zip <> b.zip) 

UNION SELECT COUNT(*) AS RecCount 
FROM Library2.Table1 a, Library2.Table2 b 
WHERE a.memno = b.memno and 
b.groupid = 'N2' and 
b.type = 'B' and 
b.datec = 20131209 
AND (a.addr1 <> b.addr1 or a.addr2 <> b.addr2 or a.city <> b.city or a. state <> b.state or a.zip <> b.zip)

我无法弄清楚为什么在字符串操作之后我的上述查询将标签设置为“ 24 ”的值。

但是,当我在Server Explorer中运行查询时,我看到了这个问题。在代码中,我尝试接收单个COUNT,并且我的替换代码行正在将UNION中的select子句更改为新的“ SELECT COUNT(*)AS RecCount ”。

Server Explorer结果:

[RECCOUNT]

[14]

[24]

有人可以向我展示我需要对使用BOTH数据源的代码/查询进行哪些修改,以便收到38个单RecCount

1 个答案:

答案 0 :(得分:3)

将您现有的查询对包含在公用表表达式中,并将其用作您想要总计的源。

with counts as (
SELECT COUNT(*) AS RecCount 
FROM Library1.Table1 a, Library1.Table2 b 
WHERE... 
UNION SELECT COUNT(*) AS RecCount 
FROM Library2.Table1 a, Library2.Table2 b 
WHERE...
)
select sum(RecCount) from counts;