I have to run 1 query, then another with the results of the first. I realize that I COULD just throw the first result set inside of an array then iterate through the array, but is there a way to just nest the queries so that I don't have to mess with the array?

Here is my current code:

    SqlConnection conn2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["wesdb1SQL"].ToString());
    SqlCommand strSQL2;
    SqlDataReader itemReader2;

    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["wesdb1SQL"].ToString()))
    using (SqlCommand strSQL = conn.CreateCommand())
    {
        strSQL.CommandText = "SELECT item_id,item_lot,item_title,item_est_lo,item_est_hi,item_timed_start,item_reserve FROM tblItem WHERE (item_sale_id=@item_sale_id) ORDER BY item_lot";

        strSQL.Parameters.Add(new SqlParameter("@item_sale_id", SqlDbType.VarChar, 10, ParameterDirection.Input, true, 0, 0, "item_sale_id", DataRowVersion.Current, itemSaleId1_Var));

        try
        {
            conn.Open();
            using (SqlDataReader itemReader = strSQL.ExecuteReader())
            {
                if (itemReader.Read())
                {
                    using (conn2)
                    using (strSQL2 = conn2.CreateCommand())
                    {
                        strSQL2.CommandText = "SELECT TOP (1) MAX(tblBidHistory.bid_price) AS bid_price, tblMailList.mail_Email1, tblBidHistory.bid_bidder_id, tblMailList.mail_FirstName, tblMailList.mail_LastName, tblBidHistory.bid_item_id FROM tblBidHistory INNER JOIN tblBidder ON tblBidHistory.bid_bidder_id = tblBidder.bidder_number AND (tblBidder.bidder_sale_id=@item_sale_id) INNER JOIN tblMailList ON tblBidder.bidder_mail_id = tblMailList.mail_ID GROUP BY tblMailList.mail_Email1, tblBidHistory.bid_bidder_id, tblMailList.mail_FirstName, tblMailList.mail_LastName, tblBidHistory.bid_item_id,tblBidHistory.bid_type,tblBidHistory.bid_date HAVING (tblBidHistory.bid_item_id=@item_id) AND (tblBidHistory.bid_type = '2') ORDER BY bid_price DESC,bid_date DESC";

                        strSQL2.Parameters.Add(new SqlParameter("@item_sale_id", SqlDbType.VarChar, 10, ParameterDirection.Input, true, 0, 0, "item_sale_id", DataRowVersion.Current, itemSaleId1_Var));
                        strSQL2.Parameters.Add(new SqlParameter("@item_id", SqlDbType.VarChar, 10, ParameterDirection.Input, true, 0, 0, "item_id", DataRowVersion.Current, itemReader["item_id"].ToString()));

                        try
                        {
                            conn2.Open();
                            using (itemReader2 = strSQL2.ExecuteReader())
                            {
                                if (itemReader2.Read())
                                {
                                    if (count % 2 == 0)
                                    {
                                        results_Var += "<tr><td colspan=\"2\"><font size=\"2\">" + itemReader["item_lot"].ToString() + " - <u>" + itemReader["item_title"].ToString() + "</u></font></td><td><font size=\"2\">$" + itemReader["item_est_lo"].ToString() + " - $" + itemReader["item_est_hi"].ToString() + "</font></td><td>" + itemReader2["bid_price"].ToString() + " - " + itemReader2["bid_bidder_id"].ToString() + " (" + itemReader2["mail_FirstName"].ToString() + " " + itemReader2["mail_LastName"].ToString() + ")</td><td><font size=\"2\">$" + itemReader2["bid_price"].ToString() + "</font></td></tr>";
                                    }
                                    else
                                    {
                                        results_Var += "<tr><td colspan=\"2\" bgcolor=\"#b0e0e6\"><font size=\"2\">" + itemReader["item_lot"].ToString() + " - <u>" + itemReader["item_title"].ToString() + "</u></font></td><td><font size=\"2\">$" + itemReader["item_est_lo"].ToString() + " - $" + itemReader["item_est_hi"].ToString() + "</font></td><td>" + itemReader2["bid_price"].ToString() + " - " + itemReader2["bid_bidder_id"].ToString() + " (" + itemReader2["mail_FirstName"].ToString() + " " + itemReader2["mail_LastName"].ToString() + ")</td><td><font size=\"2\">$" + itemReader2["bid_price"].ToString() + "</font></td></tr>";
                                    }
                                }
                                itemReader2.Close();
                            }
                        }
                        catch (Exception e1)
                        {
                            throw new Exception(e1.Message);
                        }
                        finally
                        {
                            conn2.Close();
                        }
                    }
                }
                itemReader.Close();
            }
        }
        catch (Exception e2)
        {
            throw new Exception(e2.Message);
        }
        finally
        {
            conn.Close();
        }
    }

The output of both queries is used to create a table of multiple rows of data. Each row is one item in the database with the highest bidder's information including the bid itself, along with some category information. The category information is the first query, and the unique item information is the second query (which is where you can see the table rows being built using both result sets).

Edit

I changed xQbert query a little and ended up with:

SELECT MAX(BH.bid_price) AS bid_price, ML.mail_Email1, BH.bid_bidder_id,  ML.mail_FirstName, ML.mail_LastName, BH.bid_item_id, I.item_lot, I.item_title,  I.item_est_lo, I.item_est_hi, I.item_timed_start, I.item_reserve  
FROM tblBidHistory BH 
INNER JOIN tblBidder B  ON BH.bid_bidder_id = B.bidder_number   AND (B.bidder_sale_id=@item_sale_id) 
INNER JOIN tblMailList ML   ON B.bidder_mail_id = ML.mail_ID 
INNER JOIN tblItem I   ON I.Item_ID = BH.Bid_item_id 
WHERE (I.item_sale_id=@item_sale_id) And (BH.bid_type = '2') 
GROUP BY ML.mail_Email1, BH.bid_bidder_id, ML.mail_FirstName, ML.mail_LastName, BH.bid_item_id, BH.bid_type, BH.bid_date, I.item_lot, I.item_title, I.item_est_lo, I.item_est_hi, I.item_timed_start, I.item_reserve
ORDER BY I.Item_Lot

This gives me data, but it gives me every bidder for each item instead of the top bidder for each item. I'm not sure if I need to group differently, or maybe use a sub query.

Also, to do this, I'm just making 1 query. However, I would still like to know how to make nested queries in SQL.

有帮助吗?

解决方案

Try:

select * from
(SELECT i.item_id,
       i.item_lot,
       i.item_title,
       i.item_est_lo,
       i.item_est_hi,
       i.item_timed_start,
       i.item_reserve,
       h.bid_price,
       l.mail_Email1,
       h.bid_bidder_id,
       l.mail_FirstName,
       l.mail_LastName,
       h.bid_item_id,
       row_number() over (partition by i.item_id 
                          order by h.bid_price DESC, h.bid_date DESC) rn
 FROM tblItem i 
 INNER JOIN tblBidHistory h
         on i.item_id = h.bid_item_id=i.item_id AND h.bid_type = '2' 
 INNER JOIN tblBidder b
         ON h.bid_bidder_id = b.bidder_number AND b.bidder_sale_id=i.item_sale_id
 INNER JOIN tblMailList l
         ON b.bidder_mail_id = l.mail_ID 
 WHERE i.item_sale_id=@item_sale_id) v
where rn=1

其他提示

I highly doubt the below is correct: too many assumptions and lack of understanding of question: but this is one way to join them Assuming item joins to BH via Bid_Item_ID Added order by for I.Item_lot and added item data added item data to select and group by.

SELECT TOP (1) MAX(BH.bid_price) AS bid_price, ML.mail_Email1, BH.bid_bidder_id,
  ML.mail_FirstName, ML.mail_LastName, BH.bid_item_id, I.item_lot, I.item_title,
  I.item_est_lo, I.item_est_hi, I.item_timed_start, I.item_reserve  
FROM tblBidHistory BH
INNER JOIN tblBidder B
  ON BH.bid_bidder_id = B.bidder_number 
  AND (B.bidder_sale_id=@item_sale_id) 
INNER JOIN tblMailList ML 
  ON B.bidder_mail_id = ML.mail_ID
INNER JOIN tblItem I 
  ON I.Item_ID = BH.Bid_item_id
WHERE (I.item_sale_id=@item_sale_id)
GROUP BY ML.mail_Email1, BH.bid_bidder_id, 
  ML.mail_FirstName, ML.mail_LastName, BH.bid_item_id, BH.bid_type, BH.bid_date,
  I.item_lot, I.item_title, I.item_est_lo, I.item_est_hi, I.item_timed_start, 
  I.item_reserve  
HAVING (BH.bid_item_id=@item_id) AND (BH.bid_type = '2') 
ORDER BY I.Item_Lot, BH.bid_price DESC, BH.bid_date DESC
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top