Query Sql Count and Business Object


Author
Message
Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Olivier,
You have to change the way you are thinking from VFP to a SQL DBA so you can build your queries better in SQL.

I would build your query this way, for testing purpose set the value of the ProductName and ProductPrice parameters then comment them for production.  What makes this query generic in a way that could accept a Product Name or a Product Price is the values of the parameters which are expected as '%' or zero, and in this case since you are using the "LIKE" attribute, then we will test for the string '%' to test for empty, or you can create whatever condition you want to test as empty for the ProductName.


-- spGetProductByPriceOrName
declare @ProductName varchar(30)
declare @ProductPrice DECIMAL

set @ProductName = 'm.dupont'
set @ProductPrice = 120000

SELECT
    p.*
FROM
    Product p
WHERE
    (p.Name LIKE @ProductName OR @ProductName = '%') AND
    (p.Price >= @ProductPrice OR @ProductPrice = 0)


In your VB program you would have a method or function like this:

        Public Sub MyName(ProductName As String, ProductPrice As Decimal)
            Using cmd As New SqlClient.SqlCommand()
                '-- Create the params
                cmd.CommandTimeout = 0
                cmd.CommandType = CommandType.StoredProcedure
                cmd.CommandText = "dbo.spGetProductByPriceOrName"

                cmd.Parameters.AddWithValue("@ProductName", ProductName.Trim + "%").SqlDbType = SqlDbType.VarChar
                cmd.Parameters.AddWithValue("@ProductPrice", ProductPrice).SqlDbType = SqlDbType.Int

                Me.ExcecuteScalar(cmd)
            End Using 'cmd
        End Sub


Edhy Rijo

Edited 10 Years Ago by Edhy Rijo
Olivier
Olivier
StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)
Group: StrataFrame Users
Posts: 96, Visits: 805
Hello Edhy

What do you think about this ! i want to build a dynamic query !
and do you think it's secure ?


declare @cName varchar(30);
declare @cPrice varchar(12);
declare @cCondition varchar(max);
declare @MyQuery varchar(max)

set @cName = 'm.dupont';
set @cPrice = '120000';

IF @cName <>''
     set @cCondition = 'name like '+@cName+'%';

IF @cPrice <>''
     set @cCondition = 'price>='+@cPrice;


SET @MyQuery = 'select * from product where ' + @cCondition
EXEC (@MyQuery)



Do you see the way what i need ?

thanks
Olivier

==============================================
Asp.net C# - Strataframe - telerik
==============================================
Olivier
Olivier
StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)
Group: StrataFrame Users
Posts: 96, Visits: 805
Hello Edhy,

I ll test tomorrow (it's night on france),
but i have already build a view before, and i m no way cause the bo ask primary key and i don't put this,
cause i want to group a record.

may be it's override primary key the solution,

i test and i come back to tell you !

thanks
Olivier

==============================================
Asp.net C# - Strataframe - telerik
==============================================
Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Olivier,

Here is a sample of a query I used to test before creating the stored procedures:

DECLARE @StartDate AS DATE = '1/1/2013'
DECLARE @EndDate AS DATE = '12/31/2013'
DECLARE @PK_InsuranceCompany AS INT = 0

SELECT
    v.CompanyName,
    SUM(v.BrokerCommissionValue) AS BrokerCommissionvalue
FROM
    vCompanyPayments v
WHERE
    CAST(v.ReceiptDate AS DATE) >= @StartDate AND CAST(v.ReceiptDate AS DATE) <= @EndDate AND
    BrokerCommissionvalue > 0 AND
    (v.PK_InsuranceCompany = @PK_InsuranceCompany OR @PK_InsuranceCompany = 0)

GROUP BY
    v.CompanyName

ORDER BY
    v.CompanyName


Notice how I test for the value of the parameter @PK_InsuranceCompany to be = 0 in the WHERE condition, in this case in my VB application I pass either a real value greater than zero or just zero and the I will get all records for that Insurance Company PK or for all.
You can use similar code to replace your VFP coding and in VB you can control how to pass the parameters to your SQLCommand.


Edhy Rijo

Olivier
Olivier
StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)
Group: StrataFrame Users
Posts: 96, Visits: 805
thanks michel,

i ll check , the override primary key option.

Can you see the way to build an procedure stored like this ?
to get a condition to put in the Where ?
(i give you a code in vfp cause i dont' get a vb code)



If ! empty(m.nNBPIECES)
        m.cCondition = m.cCondition + " .AND. CLI_PIECMN<="+Alltr(str(m.nNBPIECES))
    Endif
    If ! empty(m.nSURFACE)
        m.cCondition = m.cCondition + " .AND. CLI_SURF<="+Alltr(str(m.nSURFACE+(m.nSURFACE*M.XPCKORL1/100)))
    Endif

select myfield from mytable where 1=1 and &cCondition



it's the last line i don't get in tsql for my proc stor !

thanks
Olivier


==============================================
Asp.net C# - Strataframe - telerik
==============================================
Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Olivier, Michel,

Michel is right, sometimes a view and a stored procedure to fill the view is what is needed, also if all the aggregated fields are in the view the SQL to query the view may be pretty simple no needing to have a stored procedure for it.

My preference is to try to get all calculations done at the SQL Server level so I can simply use the views in my reports or graphs with a SF Business Binding Source as clean as possible.

Edhy Rijo

Michel Levy
Michel Levy
StrataFrame User (437 reputation)StrataFrame User (437 reputation)StrataFrame User (437 reputation)StrataFrame User (437 reputation)StrataFrame User (437 reputation)StrataFrame User (437 reputation)StrataFrame User (437 reputation)StrataFrame User (437 reputation)StrataFrame User (437 reputation)
Group: StrataFrame Users
Posts: 193, Visits: 9K
Hi Olivier,

If you don't want to use the agregating functions provided in your report engine, it is possible (I often prefer to agregate on the SQL Server).

here is a solution (there are also some other ways):

  1. create a view on the server with the count(*) and grouping you need, map a BO on that view, you have the structure (don't forget to enforce a pseudo primary key on the BO - look at "override primary key specification" in the Advanced otions of the BO, choose the columns that may identify a line) 
  2. if you need some parameters, create a stored procedure resulting in the same strcuture, having th parameters you need
  3. fill your BO with the stored procedure
That's all, guy!




Salut Olivier,

Si tu ne veux pas utiliser les fonctions d'aggrégation de ton moteur de rapport, c'est faisable (je préfère souvent faire les agrégations sur le serveur SQL).

Voilà une solution (il y en a d'autres):

  1. tu crées une vue sur le serveur, avec ton count(*) et tes group by, tu mappes un BO sur cette vue, ça te donne ta structure (n'oublie pas de forcer une pseudo PK sur le BO - regarde "override primary key specification" dans les options avancées du BO, tu choisis une combinaison de colonnes qui peuvent servir d'identifiant unique pour une ligne)
  2. si tu as besoin de paramètres, tu crées une procédure stockée qui te donne le résultat voulu, avec les paramètres nécessaires
  3. tu remplis ton BO avec la procédure stockée
Et c'est fini, mon ami!
Olivier
Olivier
StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)StrataFrame User (324 reputation)
Group: StrataFrame Users
Posts: 96, Visits: 805
Hello

I have a simple query to count in a sales graph and i group by seller

but Strata Bo ask me all primary key in my view and my query is not disctint or grouping.

Can you tell me , how you start your query with count(*) and some group by on Stratraframe
to learn and reproduce this.

thanks
Olivier,

==============================================
Asp.net C# - Strataframe - telerik
==============================================
Tags
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search