How to use SUM and GROUP BY(SQL) with Visual FoxPro in VB.NET? - sql

I do not have much experience with VB. I have built a VB app that can retrieve data from a table(FoxPro). The code is given below:
Dim dBaseCommand As New System.Data.OleDb.OleDbCommand("SELECT * FROM inventory", con)
Dim dBaseDataReader As System.Data.OleDb.OleDbDataReader = dBaseCommand.ExecuteReader(CommandBehavior.SequentialAccess)
But I need to run the following SQL instead of SELECT * FROM inventory
SELECT itemnumber, mfgr, SUM(qty) AS cqty, unitcost, description
FROM inventory
GROUP BY itemnumber
I replaced the SQL and it does not work. How can I implement that SQL or GROUP BY statement into my VB code?
Update:
The SQL was a sample sql from MySQL. But I have updated the SQL that I am now actually using in my VB app. It does not show any error or does not close the app itself and it does not generate the file which is supposed to be produced by this app. If I use the sql from first sample code, it generates the file nicely from that table.
I went to database explorer(data sources) to run this query too. The following issues came up. Any ideas? not supported by Foxpro or any other workarounds?
Executing SQL: "GROUP BY clause is missing or invalid"
Verify SQL syntax: This command is not supported by this provider
Does this have something to do with this link where group by is not there?
If I use ODBC data provider, will it support the SQL?
Update 2:
It looks like ODBC driver supports GROUP BY. But Microsoft recommends to use OLEDB data provider instead of ODBC which does not support GROUP BY. In Visual basic data connections, I dont see ODBC as available data provider. It has only SQL server data provider and OLEDB data provider. Is there any way so that I can use the ODBC data provider and then use the GROUP BY statement?
*My reputation did not allow me including more references for citing the last information.

I had to change the SQL to the following code and it worked:
SELECT itemnumber, SUM(qty) AS qty, description
FROM inventory
GROUP BY itemnumber, description
From VFP 8, SELECT with GROUP BY clause can select only columns which are associated with either GROUP BY or fields with Aggregate functions. If we need to use GROUP BY clause, we need to be careful with those conditions. So if the "description" from GROUP BY clause is removed, then the SQL does not work. Similarly, if SUM function is removed from qty column, the SQL wont work.
These conditions explain why "GROUP BY clause is missing or invalid" popped up. The good thing is that we can verify the queries on Visual studio before embedding into application.

Remove the single quotes from around Inventory, to make the statement:
SELECT partnumber, SUM(quantity) FROM inventory GROUP BY partnumber
Inventory is a SQL object, not a text literal.

Related

Querying Microsoft Jet Database Date Parameters

I am trying to query a table using Microsoft Jet Database. This is the code I am using
SELECT orderID, OriginCustomer, OriginAddress, DestinationCustomer, DestinationAddress, purchaseOrder, productSize, referenceNumber, actionDescription, actionNote, actionDriver, actionStatus, actionColorNumber, actionDate FROM actionView
WHERE (actionDriver = ?)
AND ( actionDate between #(?)# and #(?)#)
The error I get is
Cannot convert entry to valid date/time
TO_DATE function might be required
I am trying to find the proper sql syntax but I can not find it or figure it out....
I am using access database
Ohh yeah forgot to mention that... I am making a Winform application through visual studio using .NET and C#.

SQL Query from SQL novice

I am trying to generate several spreadsheets, sourcing the data from an SQL database. I am new to SQL. The only tool I have for accessing the database is MS Query.
I have managed a lot by the copy & modify process, but now am stuck. I have the following code which allows me to select values from a specified Ac for a specified period.
SELECT Table1.Date, Table1.Ac, Table1.Ref, Table1.Text, Table1.Value
FROM Main.dbo.Table1 Table1
WHERE (Table1.Ac=?) AND (Table1.Date>=? And Table1.Date<=?)
ORDER BY Table1.Date
What I now want to do is:
Delete the Table1.Ac criterium so that I get all records between selected dates
Group by the Table1.Ac field, sorted ascending
In a new column, Display the sum of all the values for each Table1.Ac
This would be very similar to a Summary TB in accounts parlance
As soon as I start modifying the code, I get the message: Parameters are not allowed in queries that can't be displayed graphically.
I would appreciate any help on the SQL code, and on any better tools that I can integrate into Excel. The company is standardising on SQL and converting all its old databases (Access, Accounts, Btrieve, etc) into SQL
I'd recommend working in SQL Server Management Studio if using Microsoft SQL Server.
Does this query suffice?
SELECT Table1.Ac, Sum(Table1.Value)
FROM Main.dbo.Table1 Table1
WHERE (Table1.Date>=? And Table1.Date<=?)
ORDER BY Table1.Date GROUP BY Table1.Ac
Here's a link outlining using MS Query to get data into Excel..
http://office.microsoft.com/en-gb/excel-help/use-microsoft-query-to-retrieve-external-data-HA010099664.aspx

How to limit select result rows in Progress 9.1D09?

I'm trying to extract data from a Progress 9.1D09 database. I understand that this isn't the latest version of Progress but upgrading isn't an option. The database is used by a dying program and I am moving the data to its successor.
One table has 162000 rows. I want to work with a small number of rows.
In SQL Server I would change my query to "select top 100 * from ...". In MySQL I would do "select * from ... limit 0,100".
Neither of these syntaxes work and googling for the right syntax has failed me so far.
How can I limit the number of rows in the source data using SQL?
Rule #1 -- Progress is not SQL. Progress does support a SQL-92 interface and it also has SQL-89 syntax embedded -- but SQL is not the native tongue for a Progress DB.
If you are using the embedded SQL-89 there is no support for TOP. The embedded SQL is accessed via the 4GL engine. If you are not using ODBC or JDBC you are probably trying to use embedded SQL-89. If syntax like:
FOR EACH customer NO-LOCK:
DISPLAY customer.
END.
works then you are using the 4GL and thus the embedded SQL-89.
A 4GL solution might look like this:
Getting first 100 records from the table in Progress OpenEdge database (e.g. SELECT TOP 100..)
If you are using SQL-92 via ODBC drivers -- TOP support was added sometime in 10.1B
http://knowledgebase.progress.com/articles/Article/P13258
So you're out of luck with v9.
You can try rownumber something like
SELECT FirstName, LastName, ROW_NUMBER()
OVER (ORDER BY PostalCode) AS 'RowNumber'
WHERE RowNumber BETWEEN(4,100)

Query error in MS Access 2007 when trying to connect to SQL Tables

This is my first post and hopefully I am asking the right question.
I am trying to pull data from a SQL server linked table into a ms access 2007 table using the following query
SELECT PTNT.PTNT_SEQ_NBR, PTNT.INTERFERON_RECEIVED_FROM_OTHER_SOURCE
INTO tblGetNonCMKfills.PTNT_SEQ_NBR, tblGetNonCMKfills.InterferonReceivedFromOtherSource
FROM PTNT
GROUP BY PTNT.PTNT_SEQ_NBR;
Although when I try to even save the query it gives me the error message
"Query input must contain at least one table or query"
Maybe I'm missing something obvious here, can anyone help?
In your INTO clause you just need to provide the name of the table you want to be created to insert the data into, not the column names.
SELECT PTNT.PTNT_SEQ_NBR, PTNT.INTERFERON_RECEIVED_FROM_OTHER_SOURCE
INTO tblGetNonCMKfills
FROM PTNT
ORDER BY PTNT.PTNT_SEQ_NBR;
Additionally you cannot use a GROUP BY clause in a query unless you are performaing some sort of aggregation in the SELECT. I am assuming what you really wanted here was an ORDER BY clause.

Why Does Access Return Different ResultSet When Query Run Programmatically?

I've written a query in MS Access. This is a simplified version:
SELECT IIf([category] LIKE "*abc*","DEF",category) AS category
, Month
, Sum(qty) AS [qty]
FROM [tableX]
GROUP BY category, Month
The purpose of the query is to sum quantities of a product in different categories for different months. I want to aggregate categories like abc into a single category called ABC. When I view the query in Access the categories are correctly aggregated, but if I select from the query in C# code no aggregation is done.
Any ideas why this is this happening?
The wildcard for when using the Access database engine's ANSI-92 Query Mode is %, not *.
The Access database engine's OLE DB providers (e.g. via ADO classic, ADO.NET, etc) always use ANSI-92 Query Mode.
The Access UI uses ANSI-89 Query Mode by default but can be put into ANSI-92 Query Mode.
DAO always uses ANSI-89 Query Mode.
Using the (unsupported) ALIKE keyword always uses the '%' wildcard regardless of Query Mode.
if it's working in Access so try to make it as a Query in Access and use it from your APP.