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
Related
When I use the following Query:
SELECT
[OrderNumberComplete], [OrderDate], [OrderTotal],
[RollupItemCount], [RollupItemName], [RollupItemSKU],
[RollupItemCode], [RollupItemQuantity]
FROM
[Order]
WHERE
OrderDate BETWEEN '10/01/2014' AND '10/31/2014'
ORDER BY
[RollupItemSKU]
I get the following results for orders with multiple line items.
http://i.imgur.com/DNG7kjX.png
I need to be able to get this information to put it into a spread sheet.
I have a limited amount of query knowledge but I am willing to learn so anything anyone can suggest will go a long way.
Thanks in advanced for any help anyone can provide!
Since you added "sql-server-2012" tag to your question I will assume you are using SQL Server. In that case in SQL Server Management Studio right click the database. Go with Tasks -> Export Data. Select Excel or CSV as destination. Then select "Use SQL" option to extract data
try
ORDER BY
[RollupItemSKU] desc
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.
I have a table structure as below on Greenplum database:
Wish to change it to the following structure so as to support pie charts on Tableau.
Could some one help me out ? Thanks!
Export the table to a CSV file
Install the Tableau Excel add-in
Open CSV file in Excel and use the add-in to reshape the data
SQL Server 2008 : Convert column value to row
http://blog.devart.com/is-unpivot-the-best-way-for-converting-columns-into-rows.html
Just to make sure you know about this Tableau feature:
Once you have devised the SQL select statement that will unpivot the data the way you'd like, then you can tell Tableau to use that instead of a select * by editing the data connection and selecting the Custom SQL option.
The generic way to unpivot in your situation is to union together several select statements, unless your database offers a more efficient alternative as described in the blog entry that Revanayya cited.
The following would work for a static, known beforehand, set of metrics:
SELECT
t.Date,
x.Metric,
CASE x.Metric
WHEN 'metric1' THEN metric1_week
WHEN 'metric2' THEN metric2_week
END AS week_val,
CASE x.Metric
WHEN 'metric1' THEN metric1_13week
WHEN 'metric2' THEN metric2_13week
END AS "13week_val"
FROM
atable AS t
CROSS JOIN
(VALUES ('metric1'), ('metric2')) AS x (Metric)
;
You could build a dynamic query off the above to account for an unknown number of metrics. For that, you would need to read the metadata (probably the INFORMATION_SCHEMA.COLUMNS system view) to build the dynamic bits, which are the VALUES list and the two CASE expressions, before embedding them into the query.
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.
This is probably a simple question, but I really don't know what I'm doing in Excel, so hopefully someone can help me out.
I've been given an Excel spreadsheet that has two relevant columns to my task. The first column is an "External ID", and the second column is an "Internal ID". I need to select a bunch of data out of our databases (with various joins) using the Internal ID as the key, but then all of this data needs to be linked back to the External ID, and the only link between Internal/External is this spreadsheet.
For example, say a row of the spreadsheet looks like this:
ExtID IntID
AB1234 2
I need to select all the data relevant to the item with ID #2 in our database, but I have no way to get "AB1234" from the database, so I need to somehow relate this data back to "AB1234" using the spreadsheet.
What's the easiest way to accomplish this? The version of Excel is Excel 2007, and the database is Oracle, if that's relevant.
Note that I only have read permission to the production databases, so creating tables and importing the spreadsheet data to do a join is not an option.
Edited based on a comment
1 - Use MS Access to import the Excel sheet as a table.
2 - Link to your database table, also from within MS Access
External Data tab->other data sources->ODBC connection->choose yours->pick the table(s) you want
3 - Write an Access query to compare the values you want
Create->Query Design->Drop the tables you want, drag lines between them for relationships, click Run
Usually I use copy-paste and a good column-mode editor with macros to accomplish such tasks. It works fine if you only have a couple of Excel files.
Alot depends on how familiar you are with the tools you have available to you.
DO you have a tool you are familiar with that would make it easy to use the IntID to find those records? If so, can you do the query and paste the results back into the original spreadsheet in the column to the right of the column with the IntID?
If so, you will have what you want, a spreadsheet with the following columns:
ExtID (original)
IntID (original)
IntID (from Oracle)
Col1 (from Oracle)
Col2 (from Oracle) etc....
I'm not familiar with Oracle, but I know a lot of databases let you prepend a table name with # or something like that and create a temp table. Others have a temporary database where you can create things. Sometimes you can create a temp table even if you can't do anything else but select.
If you have access to do that, I would do the function as JosephStyons suggests (#2), insert your records into the temp table, and do a query based on that.
With Excel and VBA, you can use ActiveX Data Objects (ADO) as a high level way of using the OLE DB provider for a particular database. This lets you read the data from the database and you can then query that data and store the results in the spreadsheet.
Oracle OLE DB provider
ADO Guide