dimanche 13 décembre 2015

SQLite Sum Column based on Date Range

Good evening, I have the following table

public class SalesRecord
{
    [PrimaryKey, AutoIncrement]
    public int SalesRecID { get; set; }

    [Indexed]
    public string SalesRecDate { get; set; }
    public string SalesRecCustName { get; set; }
    public string SalesRecProdName { get; set; }
    public int SalesRecProdQty { get; set; }
    public double SalesRecProdPrice { get; set; }
    public double SalesRecTotalPrice { get; set; }
}

and I was able to display rows of items in XAML Listbox.

<ListBox Name="listSalesRecord" Height="400" HorizontalAlignment="Left" Margin="0,5,0,0">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Height="18" VerticalAlignment="Top" >
                                <TextBlock Text="{Binding SalesRecID}" Margin="5,0,0,0" Width="30" FontSize="14"/>
                                <TextBlock Text="{Binding SalesRecDate}" Margin="5,0,0,0" Width="80" FontSize="14"/>
                                <TextBlock Text="{Binding SalesRecCustName}" Margin="5,0,0,0" Width="80" FontSize="14"/>
                                <TextBlock Text="{Binding SalesRecProdName}" Margin="5,0,0,0" Width="80" FontSize="14"/>
                                <TextBlock Text="{Binding SalesRecProdQty}" Margin="5,0,0,0" Width="60" FontSize="14"/>
                                <TextBlock Text="{Binding SalesRecProdPrice, Converter={StaticResource Converter}, ConverterParameter=\{0:0.00\}}" Margin="5,0,0,0" Width="60" FontSize="14"/>
                                <TextBlock Text="{Binding SalesRecTotalPrice, Converter={StaticResource Converter}, ConverterParameter=\{0:0.00\}}" Margin="5,0,0,0" Width="60" FontSize="14"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

using the following date range query. There was no problem because I was aware of the yyyy-MM-dd format.

private void btnSearchRecordDateRange_Click(object sender, RoutedEventArgs e)
    {
        var searchDateRange = conn.Query<SalesRecord>("SELECT * FROM SalesRecord WHERE date(SalesRecDate) BETWEEN date('" + tbxCDPFrom.Text + "') AND date('" + tbxCDPTo.Text + "')");

        listSalesRecord.ItemsSource = searchDateRange;
    }

but now I want to compute Sum of Product Quantity based on Date Range which I want to display in a textbox using the same query, with the only change I made was to replace SELECT * FROM with SELECT SUM(SalesRecProdQty) FROM. But this same query string failed to give me the sum required.

private void btnSearchSumDateRange_Click(object sender, RoutedEventArgs e)
   {
        var searchSumProdQty = conn.Query<SalesRecord>("SELECT SUM(SalesRecProdQty) FROM SalesRecord WHERE date(SalesRecDate) BETWEEN date('" +   tbxCDPFrom.Text + "') AND date('" + tbxCDPTo.Text + "')");

        tbxSearchQty.Text = searchSumProdQty.ToString();
   }

I got this error message in the textbox:

System.Collections.Generic.List`1[SalePad.Models.SalesRecord]

Can someone tell me where I did wrong.

SELECT * worked, but SELECT SUM(SalesRecProdQty) failed, even though they used same query string.

I am using SQLite.Net-PCL and I am on WinRT for UWP, not WPF.

Aucun commentaire:

Enregistrer un commentaire