SQL: Output Row Values to Distinct Count for Number of Instances - sql

I'm trying to replicate an Access Formula that will output the Columns in Column "Distributions" to include distinct counts leading up to the value in Column "Count."
For example, if I have an order number that recurs in the data sheet for three lines, I would like the output in the "Distributions" column to count 1,2,3 for each line in the sheet. If there is one line to the order, I need only an output of 1 in the column, if there are 70 lines, I need an output of 1-70 for every matching Order Number.
I already have the "Count" column sorted out, but I can't wrap my head around the necessary code to make the output increment up in the "Distributions" column. The image below details the sort of output I'm looking for with Sample Data.

Related

SQLite3 Order by highest/lowest numerical value

I am trying to do a query in SQLite3 to order a column by numerical value. Instead of getting the rows ordered by the numerical value of the column, the rows are ordered alphabetically by the first digit's numerical value.
For example in the query below 110 appears before 2 because the first digit (1) is less than two. However the entire number 110 is greater than 2 and I need that to appear after 2.
sqlite> SELECT digit,text FROM test ORDER BY digit;
1|one
110|One Hundred Ten
2|TWO
3|Three
sqlite>
Is there a way to make 110 appear after 2?
It seems like digit is a stored as a string, not as a number. You need to convert it to a number to get the proper ordering. A simple approach uses:
SELECT digit, text
FROM test
ORDER BY digit + 0

Is there a way to select a range of cells at the end of each column on a cross table to perform a calculation

I need to select a range of the last 3 to 5 cells w/in each column and calculate an average to which the result needs to be output beneath the selected cells. webi results
Desired output modeled in Excel
Maybe you may try to add a rank dimension that you add in your table (then you hide) and you compute your avg(columnName where rank > value)
2 ways to do it
- the rank should be in desc order then in the avg --> rank<=5
- the rank is in esc order --> You will need to use count() to get the number of line - 5

How to find categorical outlier/noise rows in bigquery window

How would i detect outlier rows in bigquery and mark rows as outliers. For each row, I want to look at 5 rows before and 5 rows after and see if the value changed.
Here is an example of a table.
In this table I would want the highlighted rows to have some boolean flag to say outlier because the rows only change for a brief second. However, when it changes from id 19 to 5, that is okay (assuming that is a continued change). Basically I am trying to remove glitches where it just changes from one user id to another for 2 rows, so i want to mark those rows with some flag. I was thinking of doing this in bigquery by looking at lag rows and lead rows in a given window (over a partition), but am not sure if i am on the right path.
This isn't a typical "outlier" case as all rows should have the same value except for the glitches and it is more of a categorical variable, albeit a number.

How to combine a row of cells in VBA if certain column values are the same

I have a database where all of the input from the user (through a userform) gets stored. In the database, each column is a different category for the type of data (ex. date, shift, quantity, etc) and the data from the userform input gets put into its corresponding category. For some of the data, all the data is the same except for the quantity. I was wondering how I could combine these rows into one and add the quantities to each other for the whole database (ex. combining the first and third data entries). I have tried playing around with a couple different loops but can't seem to figure anything out.
Period Date Line Shift Type Quantity
4 x 2 4/3/18 A 3 14 18
4 x 2 4/3/18 A 3 13 12
4 x 2 4/3/18 A 3 14 15
Thank you!
If you're looking to modify the underlying database, you might be able to query the data into the format you want by including all the other columns in a GROUP BY statement, save the result to another table, then replace the original table with the properly formatted one.
If you have the data in Excel and you just want to view it with the duplicate rows summed, a Pivot Table would be a good choice. You can select all the other columns as rows for the Pivot Table and sum of Quantity as the values.

column headings getting truncated

i am running my query via a unix script. i need column headings. For this i have used set heading on. my requirement is to pull data from a table concatinate it using a pipe, write it in a text file. i need column headings along with the data.this file needs to be sent as feed to some other party. my query is something like this
select column1||'|'||column2||'|'||column3||'|'||column4......
from table;
I have more than 100 columns in my query
in my oputput the column data comes up correct but the column headings get truncated. i see dashes i.e '------' instead of column names after three column headings are displayed.
the output is something like
column1||'|'||column2||'|'||column3||'|'||col
--------------------------------------------------------------------------------
20-APR-13|0.990000|0|0|Y|voice|yes|0|0
20-APR-13|0.990000|0|0|Y|voice|yes|0|0
( in the above exmaple i tried with 9 columns. i can see data for 9 columns but the headings after 3 column headings the fourth one is truncated and rest are not displayed.)
I couldn't paste my original query with more than hundred columns here.
Can somebody let me know how to resolve it.
Use the configuration of the shell to set the column size:
COLUMNS=1024
stty columns 1024