Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hy,

The situation:
I have a table for warehouse item movements. The main columns are ID,Date,ItemID,Pallets,PFactor etc.

The columns that has to be calculated:
- MaxWeekCost - max (runingSum(pallets*pfactor)) for each week


In a week the items (it doesn't matter what) can have values:
4 , 4
5, 9
6 , 15
1, 16
-2, 14
1 , 15
-3 , 12

On the right side is the running sum with the BOLD max value.

I need to calculate the value (like 16) for each week in a year.

I´m representing the data in a rdlc report that is loaded from a tableadapter so it doesn't matter for me if i calculate it in a SQL statement of the tableadapter or in the rdlc report.

Thx.
Posted
Updated 10-Oct-14 4:59am
v2
Comments
Herman<T>.Instance 10-Oct-14 10:16am    
Max of Sum would lead to 1 answer. use Sum and group by in your query and the answer is split per month
TarikHuber 10-Oct-14 10:21am    
Maiby I was to fast by writing the question. I need the result table to be as it is with the value needet in a seperate column like this:
4 , 4,0
5, 9,0
6 , 15,0
1, 16 , 16
-2, 14,0
1 , 15,0
-3 , 12,0

And it´s not Max(Sum(Value)) it´s Max(runingSum(Value)). The "Runing" is making the problems :(
Maciej Los 10-Oct-14 11:07am    
Please, provide complete table structure and its sample data. Is there any unique field?
PhilLenoir 10-Oct-14 11:11am    
You don't say what partitions your running sum and you give us a sample output without giving us your source data.

This looks like it might be a candidate for a Table Valued Function and probably a CLR one at that. With a T-SQL TVF you would have to run a cursor, but with remembering the highest value and continually doing compares, I'd be inclined to go the CLR route.

 
Share this answer
 
Comments
Maciej Los 10-Oct-14 12:15pm    
+5!
TarikHuber 10-Oct-14 15:26pm    
Thx. I found that to. But it doesnt show hot to get the max value of the runing sum for a group.
Have a look at example:
SQL
DECLARE @tmp TABLE(ID INT IDENTITY(1,1), aDATE DATETIME, Pallets INT)

INSERT INTO @tmp (aDATE, Pallets)
VALUES('2014-09-29', 4),('2014-09-30', 5),('2014-10-01', 6),('2014-10-02', 1),('2014-10-06', -2),('2014-10-07', 1),('2014-10-08', -3)

SELECT t2.ID, t2.aDATE, DATEPART(wk, t2.aDATE) AS aWeek, SUM(t1.Pallets) AS RunningSum
FROM @tmp AS t1 INNER JOIN (
    SELECT ID, aDATE, DATEPART(wk, aDATE) AS aWeek, Pallets
    FROM @tmp
    ) AS t2 ON t1.ID <= t2.ID
GROUP BY t2.ID, t2.aDATE, DATEPART(wk, t2.aDATE)
ORDER BY t2.ID


Result:
ID   aDATE                     aWeek   RunningSum
1   2014-09-29 00:00:00.000    40     4
2   2014-09-30 00:00:00.000    40     9
3   2014-10-01 00:00:00.000    40     15
4   2014-10-02 00:00:00.000    40     16
5   2014-10-06 00:00:00.000    41     14
6   2014-10-07 00:00:00.000    41     15
7   2014-10-08 00:00:00.000    41     12


Now, you need to define field for rdlc report, on which the data should be grouped. In this case it's aWeek.
 
Share this answer
 
v2
Comments
Mehdi Gholam 10-Oct-14 15:10pm    
5'ed
Maciej Los 11-Oct-14 8:33am    
Thank you, Mehdi ;)
TarikHuber 13-Oct-14 2:17am    
Thank you. But I already am so far. I used this SQL Statement in the DataSet of the rdlc:

SELECT (SELECT SUM(b.Pallets * WarehouseItems.Factor) AS RSum
FROM WarehouseMovements AS b INNER JOIN
WarehouseItems ON b.WarehouseItemID = WarehouseItems.ID
WHERE (b.ID <= a.ID)) AS RuningSum, a.Date AS Datum, WarehouseMovementTypes.Name AS Bewegung, WarehouseItems_1.Name AS Artikel,
WarehouseItems_1.Number AS ANummer, a.LSNumber AS LSNummer, a.Pallets AS Paletten, WarehouseItems_1.Factor AS Faktor,
a.Pallets * WarehouseItems_1.Factor * 1.5 AS Handling, DATEPART(WW, a.Date) AS Week
FROM WarehouseMovements AS a INNER JOIN
WarehouseItems AS WarehouseItems_1 ON a.WarehouseItemID = WarehouseItems_1.ID LEFT OUTER JOIN
WarehouseMovementTypes ON a.WarehouseMovementTypeID = WarehouseMovementTypes.ID
ORDER BY a.ID

But then in the rdlc when I Group the RunnungSum per Week it returns the RunningSum only for that praticular week. I´dont get it how a rdlc can change data from a DataSet that is the source of ti???
Maciej Los 13-Oct-14 2:35am    
I have no idea. I can't see your screeen and formula used to group data ;(
TarikHuber 13-Oct-14 3:13am    
There is no Formula from my site. I just use a Tablix group.
with tt
as
(
select Raters,ROW_NUMBER() over (order by Raters) as [Rowp] from be_Posts
)
select Raters,(select Sum(Raters) from tt where rowp<=t.Rowp) from tt t
SQL

 
Share this answer
 
Comments
TarikHuber 13-Oct-14 2:19am    
Thx but where is there a Max value?
Er. Dinesh Sharma 13-Oct-14 3:17am    
with tt
as
(
select Raters,ROW_NUMBER() over (order by Raters) as [Rowp] from be_Posts
)
select max(RD) from (select Raters,(select Sum(Raters) from tt where rowp<=t.Rowp) as[RD] from tt t) p
I can´t find a direct solution withing SQL or RDLC so I had to make some more code and work with the data manualy. Here is code of the Form that represents the data in a rdlc report. The rest is just rdlc playing :)

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using ICSDB;
using Microsoft.Reporting.WinForms;



namespace ICS.EnterpriseResourcePlaning
{
    public partial class WarehouseMovementsReportForm : Form
    {

        private DataTable DtWarehouseMovements = new DataTable();
        int _WarehouseID = 0;
        double _RuningSum = 0;

        public WarehouseMovementsReportForm(int warehouseID=0)
        {
            _WarehouseID = warehouseID;
            InitializeComponent();
            AddColumnsToDataTable(DtWarehouseMovements);
        }

        private DataTable AddColumnsToDataTable(DataTable dt)
        {
            dt.Columns.Add("Datum", typeof(DateTime));
            dt.Columns.Add("Bewegung", typeof(string));
            dt.Columns.Add("Artikel", typeof(string));
            dt.Columns.Add("ANummer", typeof(string));
            dt.Columns.Add("LSNummer", typeof(string));
            dt.Columns.Add("Paletten", typeof(double));
            dt.Columns.Add("Faktor", typeof(double));
            dt.Columns.Add("Handling", typeof(double));
            dt.Columns.Add("Week", typeof(int));
            dt.Columns.Add("RuningSum", typeof(double));
            dt.Columns.Add("Lagergeld", typeof(double));
            return dt;
        }


        public void FillDataTable()
        {
            //Clear Rows in DataTable that Fills the Report
            DtWarehouseMovements.Rows.Clear();
            _RuningSum = 0;

            DateTime minDate = DateTime.Today;
            DateTime maxDate = DateTime.Today;
            minDate = dtpMinDate.Value;
            maxDate = dtpMaxDate.Value;

            WarehouseMovements tblWarehouseMovements = new WarehouseMovements();
            WarehouseMovementTypes tblWarehouseMovementTypes = new WarehouseMovementTypes();
            WarehouseItems tblWarehouseItems = new WarehouseItems();

            tblWarehouseMovements.Orderer = "Date,ID";

            tblWarehouseMovements.Parameters.Clear();
            tblWarehouseMovements.SetParameter("Deleted", false);
            tblWarehouseMovements.AddParameter("Date", minDate.AddDays(-1), "", SQLTable.Operator.Conditional.UND, SQLTable.Operator.Rational.größer);
            tblWarehouseMovements.AddParameter("Date", maxDate.AddDays(1), "", SQLTable.Operator.Conditional.UND, SQLTable.Operator.Rational.kleiner);
            tblWarehouseMovements.SetParameter("WarehouseID", _WarehouseID);

            DataTable dtWM = tblWarehouseMovements.DataTable;

            _RuningSum = SumToDate(minDate);

            foreach (DataRow row in dtWM.Rows)
            {              
                DataRow Nrow = DtWarehouseMovements.NewRow();

                double Lagergeld = 0;

                //Datum
                DateTime date = DateTime.MinValue;
                DateTime.TryParse(row["Date"].ToString(), out date);

                //Bewegung
                int movementID = 0;
                int.TryParse(row["WarehouseMovementTypeID"].ToString(), out movementID);
                string bewegung = tblWarehouseMovementTypes.MovementName(movementID);

                //Artikel data
                int itemID = 0;
                int.TryParse(row["WarehouseItemID"].ToString(), out itemID);
                string artikel = string.Empty;
                string aNummer = string.Empty;
                double pFaktor = 0;
                tblWarehouseItems.WarehouseItemData(itemID, out artikel, out aNummer, out pFaktor);

                //Paletten
                double palleten = 0;
                double.TryParse(row["Pallets"].ToString(), out palleten);

                int movementTypeFactor = tblWarehouseMovementTypes.MovementFactor(movementID);

                _RuningSum += palleten * movementTypeFactor;

                if (_RuningSum > Lagergeld)
                    Lagergeld = _RuningSum;
         
                //Week
                int week = 0;
                week = DateAdapter.Week.WeekNumber(date);


                Nrow["Datum"] = date;
                Nrow["Bewegung"] = bewegung;
                Nrow["Artikel"] = artikel;
                Nrow["ANummer"] = aNummer;
                Nrow["LSNummer"] = row["LSNumber"].ToString();
                Nrow["Paletten"] = palleten;
                Nrow["Faktor"] = pFaktor;
                Nrow["Week"] = week;
                Nrow["RuningSum"] = _RuningSum;
                Nrow["Lagergeld"] = Lagergeld;
                DtWarehouseMovements.Rows.Add(Nrow);
            }

        }

        private double SumToDate(DateTime date)
        {
            double sum = 0;

            string command = " SELECT SUM(dbo.WarehouseMovementTypes.Factor * dbo.WarehouseMovements.Pallets) AS Summe " +
                                " FROM     dbo.WarehouseMovements INNER JOIN " +
                                " dbo.WarehouseMovementTypes ON dbo.WarehouseMovements.WarehouseMovementTypeID = dbo.WarehouseMovementTypes.ID " +
                                " WHERE  (dbo.WarehouseMovements.WarehouseID = @WarehouseID) AND (dbo.WarehouseMovements.Date < @Date) AND (dbo.WarehouseMovements.Deleted = 0)";

            SqlConnection conn = ICSConnections.ICSDB;
            SqlCommand cmd = new SqlCommand(command, conn);
            cmd.Parameters.Add("@Date", SqlDbType.NVarChar).Value = date;
            cmd.Parameters.Add("@WarehouseID", SqlDbType.Int).Value = _WarehouseID;

            double.TryParse(cmd.ExecuteScalar().ToString(), out sum);

            return sum;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RefreshReport();
        }

        private void RefreshReport()
        {

            FillDataTable();

            rptWarehouseMovements.ProcessingMode = ProcessingMode.Local;
            ReportDataSource source = new ReportDataSource("WarehouseMovementsDS", DtWarehouseMovements);
            rptWarehouseMovements.LocalReport.DataSources.Clear();
            rptWarehouseMovements.LocalReport.DataSources.Add(source);

            this.rptWarehouseMovements.RefreshReport();
        }

    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900