Cumulative aggregates produce a token unknown error - sum

I have a problem with Firebird (embedded - version 3.0) database with cumulative aggregates. I have the following table:
+----+---------+
|ID |Salary +
+----+---------+
|1 |10.00 |
+----+---------+
|2 |20.00 |
+----+---------+
|3 |35.00 |
+----+---------+
|4 |10.00 |
+----+---------+
I would like to add a third column (cum_sum) that will contain a cumulative sum, i.e .:
+----+---------+---------+
|ID +Salary +cum_sum |
+----+---------+---------+
|1 |10.00 |10.00 |
+----+---------+---------+
|2 |20.00 |30.00 |
+----+---------+---------+
|3 |35.00 |65.00 |
+----+---------+---------+
|4 |10.00 |75.00 |
+----+---------+---------+
When I try to use the following code:
select id, salary, sum(salary) over (order by salary) cum_sum
from employee
order by salary
I get a message:
Invalid token.
Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 1, column 37.
Where is the problem?

You get this error if you are using Firebird 2.5 or earlier. Window functions were introduced in Firebird 3. You will need to upgrade to Firebird 3 if you want to run the query as shown in your question.
As you claim to use Firebird 3 Embedded, I suggest that you carefully check
if your Firebird embedded version is actually version 3 (eg use select rdb$get_context('SYSTEM', 'ENGINE_VERSION') from rdb$database; this works for Firebird 2.1 and higher and will report an error for earlier versions).
You may have loaded a different version of Firebird. This can happen if you have a different version of the embedded library earlier on the search path. This can also happen if you have a Firebird 2.5 fbembed.dll and a Firebird 3 fbclient.dll in the same location. In Firebird 3, embedded was unified with the normal client, and no longer has a separate library (it requires additional libraries, the engine plugin, instead). However most Firebird drivers will first attempt to load fbembed.dll before falling back to fbclient.dll.
if you are actually connecting with Firebird embedded, and not accidentally to a Firebird server instance of a different version (eg select rdb$get_context('SYSTEM', 'NETWORK_PROTOCOL') from rdb$database will report NULL for embedded, but a value for other connection methods).
This can happen if you use the wrong connection string or otherwise incorrectly configured your driver.
Otherwise, if you can't upgrade, you will need to use the more painful option of writing a stored procedure (or block) that does this for you, or resort to equally painful recursive CTE-based solutions (that have other limitations that might get in the way).

Related

Is it efficient to use Cucumber Datatables when lot of test data has to be created

I have to automate the scenarios that require lot of pre-Req data before verification of expected results.
For Example
Scenario: Scenario1
When I add Data for Order of type 1
|name | Quantity| values|
|A |1 | 2 |
And I add Data for Order of type 2
|name | Quantity| values|UOM |
|A |1 | 2 | mg |
And I add Data for Order of type 3
|name | Quantity| values|UOM |Deliver|
|A |1 | 2 | mg |Home|
Can I use datatables for each Order creation steps. Will it be efficient to use datatable or i should use list.
When cuking you should not put your data in your feature files. Instead you should name your data and refer to the data in your feature files by using the name.
You can then define your data in code in your step definitions, (or better still in a helper method).
This allows you to write features that focus on WHAT you are doing and WHY that is important, rather than on HOW you are doing something.
If you aren't interested in expressing WHAT you are doing and WHY its important you should probably not bother using Cucumber and use a unit testing tool instead. Its much easier to deal with complex data in unit tests because unit tests are written in code, so you can easily do things like
import data from elsewhere
use loops to construct data
use structures to define data
...

Bigquery select column only if not null

I am an absolute beginner in Bigquery and SQL so apologies if this is a dumb question. I have a bigquery table like this
|Name|Value1|Value2|Value3|Value4|Value5|Value6|
|Ben |19 |45 |null |19 |13 |null |
|Bob |34 |null |12 |null |45 |43 |
My query only selects one row that matches the name in Name column. I want the result to only display columns that have non null values. For example if I do
SELECT * FROM mytable WHERE Name = "Bob"
I want the result to look like
|Name|Value1|Value3|Value5|Value6|
|Bob |34 |12 |45 |43 |
Similarly, if I select for Ben I want the result to look like
|Name|Value1|Value2|Value4|Value5|
|Ben |19 |45 |19 |13 |
I have tried SELECT IF but don't seem to get the syntax right.
You cannot select a variable amount of columns, but you may be able to create a SQL, with a combination of aggregate/pivot functions. You may be spending more time than it's worth trying to do it. I spend about two hours on the documentation, and I still feel almost clueless (If doesn't help that I don't have an account there, and my own database does not have the same exact functions).
See Google's BigQuery Documentation for examples.
I think you may be able to do it with UNNEST() and ARRAY(), but you'll lose the original column header information in the process.
I doubt if it can be achieved, because any SQL statement will act on record(s),i.e various columns, so if a column is null, it will affect all columns in the record that are to be retrieved. SQL STATEMENTS RETRIEVE ROWS(COLUMNS REFERENCED)
You can not do that dynamically in SQL. If you need a query like that you could create it manually but it depends on the results you want to achieve.
In the case you showed for example, the query below would work but you would lose the table's header reference.
SELECT value1,value2,value4,value5 FROM mytable WHERE value3 IS NULL AND value6 is NULL
UNION ALL
SELECT value1,value3,value5,value6 FROM mytable WHERE value2 IS NULL AND value4 is NULL
In this example it's possible to see that this kind of query is complicated to build if you have many conditions. Besides that, UNION ALL will always need the same number of columns in each separate query to work. If you need to create a generic query to do that, it's not gonna be possible.
I hope it helps

SQL Server Management Studio: returning multiple max values from a subquery

I am currently learning SQL and have done a sub query on a database and now want to take the highest version number. But have found out you can't use max on the subquery
Day|Version
---+-------
1 |1
1 |1
1 |2
1 |2
Along with a bunch of other data in the row. I want to select the rows with the highest version number.
Any suggestions? First time poster so sorry for the poor formatting
Cheers
Andrew

How to use string_agg in SQL in Excel Query

I know that in SQL I can use the 'String_agg(Test1, ',')' function for grouping rows and concatenate values in a selected field ('Test1' in this case).
For Example:
I have a query that the result without using String_agg on 'Buyer' field is:
**Key** | **Buyer** | **MP**
1 | Josh | Gregory
1 | Bred | Gregory
2 | John | Ethan
The expected results when using String_agg is:
**Key** | **Buyer** | **MP**
1 | Josh, Bred | Gregory
2 | John | Ethan
But the problem is that I'm trying to execute it in SQL query which retrieves data to Excel file from another Excel file and it fails because of an error that seems like the Excel query doesn't know the String_agg function.
The query is:
SELECT `Sheet1$`.Key, string_agg(`Sheet1$`.Buyer, `, `) AS `Buyer`, `Sheet1$`.MP
FROM `C:\Input\Data.xls`.`Sheet1$` `Sheet1$`
GROUP BY 2
ORDER BY `Sheet1$`.Key
Screenshot:
Query screenshot
Error:
Error Screenshot
Someone can help me and tell me how should I correct my query to make it works?
Thank you!
Problem: Excel is not a database.
You are trying to used advanced query functionality in a spreadsheeting package, which is sometimes somewhat supported in some versions of excel, uses lots of processor power, causes serious issues as soon as a user moves anything on the sheet, or the file itself, and is not really what it was designed to do.
Solution: Use a database.
Have a bit of a look at the excel 'concatenate' function.
I believe you can use it as CONCAT() also.
Also see this SO question: Concatenation in SQL select query on Excel sheet
Hope this helps.

Remove Text from a String in an SSIS Package

I am currently updating an already existing SSIS package.
The current Package pulls data from an Excel Spread Sheet that is provided by our IT Department. It lists Machine Names of Computers and counts it for a License Report.
I currently have the Job (derived column) strip off the M (Mobile) or D (Desktop) from the first part of the machine name so that it returns just the user name, which is what I need for the report.
MBRUBAKERBR => BRUBAKERBR
However, our IT Department just implemented Windows 7 and with it a new Naming convention.
Now there is a 76A, B, C or D that is added to the end of all of the updated machines. If the machine has not been updated then it stays with the older Naming Convention (seen Above).
There are also machines that have to stay on XP, their names have been update to have X3A, B, C or D at the end of theirs.
MBRUBAKERBR76A or DBRUBAKERX3C
What I need is to remove the last part of the name so that I just get the user name out of it for reporting.
The issues is I can't use a LEFT, RIGHT, LTRIM or RTRIM expression as some of the computer names will only have the M or D in front (as they have not yet been upgraded).
What can I do to remove these characters without rebuilding this package?
UPDATE: I would really like to update the existing Expression that Removed the M and D.
Here is the Expression that I am using.
SUBSTRING(Name,2,50)
this is in a Derived Column in my SSIS Package.
As for Sample Data here is what it looks like coming in.
| Name |
| MBrubakerBR76A |
| MBROCKSKX3A |
| DGOLDBERGZA |
| MWILLIAMSEL |
| DEASTST76C |
| DCUSICKEVX3D |
This is what I want it to return.
| Name |
| BRUBAKERBR |
| BROCKSK |
| GOLDBERGZA |
| WILLIAMSEL |
| EASTST |
| CUSICKEV |
Let me know if you need any more information or examples.
First determine if the machine has been upgraded, if it is then strip out last 3 and the first letter. If it has not been upgraded then just strip out the first letter. I avoided Trim functions to keep the code clear.
SELECT
machineName,
CASE WHEN RIGHT(machineName, 3) Like '%[0-9]%' THEN
SUBSTRING(machineName, 2, len(machineName) - 4)
ELSE
RIGHT(machineName, len(machineName)-1)
END AS UserName
From MachineList
SQL Fiddle Example
SSIS Expression
As pattern matching not working in SSIS expression, try this
LEFT(RIGHT(machineName, 3),2)="X3"||LEFT(RIGHT(machineName, 3),2)="76"?SUBSTRING(machineName, 2, len(machineName) - 4):RIGHT(machineName, len(machineName)-1)