开发者

Sort DataAdapter Alphabetically - VB.Net

开发者 https://www.devze.com 2023-01-28 16:45 出处:网络
This should be a fairly simple one. I am creating a dataset which will contain a description field which I would like to sort by. The reason I want to sort the开发者_如何学Python dataadapter and not i

This should be a fairly simple one. I am creating a dataset which will contain a description field which I would like to sort by. The reason I want to sort the开发者_如何学Python dataadapter and not in my SQL is that I am already ordering by the results that have a particular value.

My SQL looks like this:

SELECT pif_desc, pif_fund, psf_end, (CASE WHEN SUM(pmi_units) IS Null THEN 0 ELSE SUM(pmi_units) END) As fundunits 
    FROM tbl_mem INNER JOIN tbl_sfunds
    ON pm_scheme = psf_scheme
    INNER JOIN tbl_invfun
    ON tbl_fund = tbl_fund
    LEFT JOIN pe_minv
    ON pmi_fund = pif_fund AND pm_member = pmi_member
    WHERE pm_member = @pm_member 
    GROUP BY pif_desc, pif_fund, psf_end
    ORDER BY fundunits DESC

My VB looks like this:

Dim cmd As New SqlCommand("getMembersFundsDCGENST", conn)
        cmd.CommandType = CommandType.StoredProcedure

        Dim p_pm_member As New SqlParameter("@pm_member", SqlDbType.Int)
        p_pm_member.Value = pm_member
        cmd.Parameters.Add(p_pm_member)

        Dim p_period_closing_date As New SqlParameter("@closingdate", SqlDbType.DateTime)
        p_period_closing_date.Value = period_closing_date
        cmd.Parameters.Add(p_closing_date)

        Dim da As New SqlDataAdapter(cmd)
        da.Fill(ds)

I want to sort the datarows is ds.tables(0) by pif_desc but still have the rows with fundunits > 0 listed first.

I am also open to other suggestions on how I can achieve the correct ordering.


Is the output you're looking for more like this?

pif_desc    fundunits
ABCDEF      6
CBCDEG      2
DEFGHI      4
ADFKHG      0
BFJSKL      0
XDFDKF      0

If so, try this SQL query:

SELECT  pif_desc,
        pif_fund,
        psf_end,
        (CASE WHEN fundunits IS Null THEN 0
        ELSE fundunits END) As fundunits,
        (CASE WHEN fundunits IS Null THEN 0
        ELSE 1 END) As pif_order
FROM    (
            SELECT  pif_desc,
                    pif_fund,
                    psf_end,
                    SUM(pmi_units) As fundunits
            FROM    tbl_mem
            INNER   JOIN tbl_sfunds
                    ON pm_scheme = psf_scheme
            INNER   JOIN tbl_invfun
                    ON tbl_fund = tbl_fund
            LEFT    JOIN pe_minv
                    ON pmi_fund = pif_fund AND pm_member = pmi_member
            WHERE   pm_member = @pm_member 
            GROUP   BY pif_desc, pif_fund, psf_end
        ) pifs
ORDER   BY pif_order,pif_desc


DataView should be used whenever sorting or filtering is to be applied in code. Take a look.

0

精彩评论

暂无评论...
验证码 换一张
取 消