Looping/Iterating a table query in Bigquery - sql

I am using BigQuery and I don´t know how to loop a table that is in a database here. For example, lets suppose we have schema_A.tableA with the following information
Table A
Originally the TableA.columnA holds the information for the rest row. The columnE is the calculation of the other three columns. But what I am looking for is to iterate/loop in a column the result coming from E (LAG(columnE)) and generate the calculation for the second row. The third row would take the result of columnE_2row and so on.
The desired output is like this :
For example the 2 row- columnA is using 500 because the result of the previous row is 500. In the third row is 300 because that was the result of columnE_row2 and so on. I don´t know how looping works in BigQuery, I would really appreciate your knowledge
Please help!!!
So far, I read some threads but none of them shows how to set a variable from a query, all are loops from 0. https://towardsdatascience.com/loops-in-bigquery-db137e128d2d

Related

Forward fill in spark SQL based on column value condition

Please can someone help me how to forward fill values in a case statement based on another column value in SPARK SQL.
I am basically trying to detect outliers in the SQL dataset and so far how I have identified these outliers is identifying standard deviation of a value far from the mean of the dataset.
Now the problem statement is wherever these outliers fall, I have to fill the value in a new column the value which was last valid/authentic.
For example: after 1 in the first column, I want to append 556 in third column and for 3 in the first column, I want to append 561 in the third column
So far, I have identified the outliers and based on the value, I am guessing I can use lag function and go back 1 row. But I also know, this is not a good approach. For example, I get 10 outliers in a sequence, I will have to write 10 CASE statement for that.
Please if someone have any better/efficient approach, please help.

GCP Bigquery - query empty values from a record type value

I'm trying to query all resources that has empty records on a specific column but I'm unable to make it work. Here's the query that I'm using:
SELECT
service.description,
project.labels,
cost AS cost
FROM
`xxxxxx.xxxxx.xxxx.xxxx`
WHERE
service.description = 'BigQuery' ;
Here's the results:
As you can see, I'm getting everything with that query, but as mentioned, I'm looking to get resources with empty records only for example record 229,230 so on.
Worth to mention that schema for the column I'm trying to query is:
project.labels RECORD REPEATED
The above was mentioned because I tried using several combinations of WHERE but everything ends up in error.
To identify empty repeated record - you can use ARRAY_LENGTH in WHERE clause like in below example
WHERE ARRAY_LENGTH(project.labels) = 0

How do I get MS Access 2007 to calculate row percentages?

How do I write a query in MS Access 2007 that displays both the count AND the percentage of the total records in a table for each row in a specified field?
For instance, if my table has a field called "gender," what is the query I should write to see the row count and percentage of Men and the row count and percentage of Women?
So, if the table had 1000 records, the result would look something like:
Men.....600.....60%
Women...400.....40%
I can easily write a query that just gives me the row count, but I can't figure out how to also see the percentages.
I should mention that I do not know SQL. I use the Design View when creating queries. But if you can give me the text for the query in SQL, I can copy and paste it into a new query and then save it.
It's not part of SQL itself, but you can use a VBA function in a query to count the amount of records in a table: DCount (MSDN).
Copy and paste the following lines in the query designer (top row, one line per column):
TotalMen: DCount("*";"[TableNameHere]";"[Gender]='Male'")
TotalWomen: DCount("*";"[TableNameHere]";"[Gender]='Female'")
You can then get the percentage by performing some simple math on the return values (again one line per column):
TotalPersons: [TotalMen]+[TotalWomen]
PercentageMen: [TotalMen]/[TotalPersons]
PercentageWomen: [TotalWomen]/[TotalPersons]
Don't forget to set the format to Percentage or the values will show up as 0,6 and 0,4 instead of 60% and 40%.
By the way, there are probably more consise ways to do it. I tried to make it readable.
PS: don't forget to replace the names of the tables and fields.

vb.net microsoft reportviewer - I need help to make a table fit in one page

How can I fill the page with empty table rows? For example; I have 2 records in the dataset but I want to the page with empty rows (draw the table borders until the end of page)
I have;
I want;
Are you getting your report data from a database? If so you might look at returning empty rows from your database call.
If you can describe your data source and query that you are using for this report, we might be able to suggest changes to the query to return blank rows.
UPDATE:
OK, Based on how you describe the data you are getting, perhaps you can change your database call to do something like this:
select * from Subeler where Subeler.FirmaId = 10
UNION ALL
select TOP 5 ' ' from Subeler where 1 = 1
That will give you your original data, plus 5 "blank" rows to help pad your report. Of course you would have to make sure the second query has the same number and type of columns as your first, but hopefully this will point you in the right direction.

MS SQL 2000 - How to efficiently walk through a set of previous records and process them in groups. Large table

I'd like to consult one thing. I have table in DB. It has 2 columns and looks like this:
Name...bilance
Jane...+3
Jane...-5
Jane...0
Jane...-8
Jane...-2
Paul...-1
Paul...2
Paul....9
Paul...1
...
I have to walk through this table and if I find record with different "name" (than was on previous row) I process all rows with the previous "name". (If I step on the first Paul row I process all Jane rows)
The processing goes like this:
Now I work only with Jane records and walk through them one by one. On each record I stop and compare it with all previous Jane rows one by one.
The task is to sumarize "bilance" column (in the scope of actual person) if they have different signs
Summary:
I loop through this table in 3 levels paralelly (nested loops)
1st level = search for changes of "name" column
2nd level = if change was found, get all rows with previous "name" and walk through them
3rd level = on each row stop and walk through all previous rows with current "name"
Can this be solved only using CURSOR and FETCHING, or is there some smoother solution?
My real table has 30 000 rows and 1500 people and If I do the logic in PHP, it takes long minutes and than timeouts. So I would like to rewrite it to MS SQL 2000 (no other DB is allowed). Are cursors fast solution or is it better to use something else?
Thank you for your opinions.
UPDATE:
There are lots of questions about my "summarization". Problem is a little bit more difficult than I explained. I simplified it just to describe my algorithm.
Each row of my table contains much more columns. The most important is month. That's why there are more rows for each person. Each is for different month.
"Bilances" are "working overtimes" and "arrear hours" of workers. And I need to sumarize + and - bilances to neutralize them using values from previous months. I want to have as many zeroes as possible. All the table must stay as it is, just bilances must be changed to zeroes.
Example:
Row (Jane -5) will be summarized with row (Jane +3). Instead of 3 I will get 0 and instead of -5 I will get -2. Because I used this -5 to reduce +3.
Next row (Jane 0) won't be affected
Next row (Jane -8) can not be used, because all previous bilances are negative
etc.
You can sum all the values per name using a single SQL statement:
select
name,
sum(bilance) as bilance_sum
from
my_table
group by
name
order by
name
On the face of it, it sounds like this should do what you want:
select Name, sum(bilance)
from table
group by Name
order by Name
If not, you might need to elaborate on how the Names are sorted and what you mean by "summarize".
I'm not sure what you mean by this line... "The task is to sumarize "bilance" column (in the scope of actual person) if they have different signs".
But, it may be possible to use a group by query to get a lot of what you need.
select name, case when bilance < 0 then 'negative' when bilance >= 0 then 'positive', count(*)
from table
group by name, bilance
That might not be perfect syntax for the case statement, but it should get you really close.