Dynamic work around for pivot function in hana - hana

I have a table that consists of the below:
Emp ID | Earning Type | Value
But instead of showing the employee in a # of lines based on the # of earning types they have i want to display all in one line dynamically.
I am aware i can do it with case whens but i don't wanna hard code the earning type, would it be possible to do that dynamically? insert rows as columns in a temp table with a loop based on the count of earning types?
Thanks in advance.

Related

BigQuery Create Table Query from Google Sheet with Variable item string field into Repeated Field

I hope I explain this adequately.
I have a series of Google Sheets with data from an Airtable database. Several of the fields are stringified arrays with recordIds to another table.
These fields can have between 0 and n - comma separated values.
I run a create/overwrite table SELECT statement to create native BigQuery tables for reporting. This works great.
Now I need to add the recordIds to a Repeated field.
I've manually written to a repeated field using:
INSERT INTO `robotic-vista-339622.Insurly_dataset.zzPOLICYTEST` (policyID, locations, carrier)
VALUES ('12334556',[STRUCT('recordId1'),STRUCT('recordId2')], 'name of policy');
However, I need to know how I to do this using SELECT statement rather than INSERT. I also need to know how to do this if you do not know the number of recordIds that have been retrieved from Airtable. One record could have none and another record could have 10 or more.
Any given sheet will look like the following, where "locations" contains the recordIds I want to add to a repeated field.
SHEETNAME: POLICIES
|policyId |carrier | locations |
|-----------|-----------|---------------------------------|
|recrTkk |Workman's | |
|rec45Yui |Workman's |recL45x32,recQz70,recPrjE3x |
|recQb17y |ABC Co. |rec5yUlt,recIrW34 |
In the above, the first row/record has no location Id's. And then three and two on the subsequent rows/records.
Any help is appreciated.
Thanks.
I'm unsure if answering my own question is the correct way to show that it was solved... but here is what it took.
I create a Native table in BigQuery. the field for locations is a string, mode repeated.
Then I just run an overwrite table SELECT statement.
SELECT recordId,Name, Amount, SPLIT(locations) as locations FROM `projectid.datasetid.googlesheetsdatatable`;
Tested and I run linked queries on the locations with unnest.

Temp columns in query sql

I have two tables, I have first table call projects and two is employees
Each employee pays his / her monthly salary divided by projects
I want to create a query that shows project names as columns and names of employees as records and contain the values of these percentages
Note Projects are changing each month
Is there a way to execute the query or not؟
example:
As there is no code the following will be brief. Join all projects, budgets and employees. This will return many rows and not the columns you desire. Next create a dynamic pivot statement. This will need a distinct list of projects supplying. Next execute the statement. simple talk has a good example.

repostioning column or copying column

I have imported a table from excel and called tblData, I already have table in access that is called tblAcc and I have to move the records from tblData to tblAcc. The problem is the fields are not on the same order.
ex:
tblData:
PARTNAME NUMBER COST
JK15 251 55
and in tblAcc:
PARTNAME COST NUMBER
CH61 14 6
So how can I transfer from table to table column by column OR how can I rearrange columns in tblData?
The easiest way to move the data from one table to the other would be to build what Access calls an Append query.
Using the query builder, you can line up the fields so that they map properly, and load the data that way.
SQL something like this should work, too:
INSERT INTO tblAcc (PARTNAME, COST, NUMBER)
SELECT PARTNAME, COST, NUMBER
FROM tblData;

Transpose to Count columns of Boolean values on Access SQL

Ok, so I have a Student table that has 6 fields, (StudentID, HasBamboo, HasFlower, HasAloe, HasFern, HasCactus) the "HasPlant" fields are boolean, so 1 for having the plant, 0 for not having the plant.
I want to find the average number of plants that a student has. There are hundreds of students in the table. I know this could involve transposing of some sort and of course counting the boolean values and getting an average. I did look at this question SQL to transpose row pairs to columns in MS ACCESS database for information on Transposing (never done it before), but I'm thinking there would be too many columns perhaps.
My first thought was using a for loop, but I'm not sure those exist in SQL in Access. Maybe a SELECT/FROM/WHERE/IN type structure?
Just hints on the logic and some possible reading material would be greatly appreciated.
you could just get individual totals per category:
SELECT COUNT(*) FROM STUDENTS WHERE HasBamboo
add them all up, and divide by
SELECT COUNT(*) FROM STUDENTS
It's not a great database design though... Better normalized would be:
Table Students; fields StudentID, StudentName
Table Plants; fields PlantID, PlantName
Table OwnedPlants; fields StudentID,PlantID
The last table then stores records for each student that owns a particular plant; but you could easily add different information at the right place (appartment number to Students; Latin name to Plants; date aquired to OwnedPlants) without completely redesigning table structure and add lots of fields. (DatAquiredBamboo, DateAquiredFlower, etc etc)

Hiding output on specific columns

I have written a package that has a stored procedure and a REF cursor. I am able to now display all of the columns in my table through this cursor. I would like to be able to insert a loop that if a certain condition is met, four of the seven columns will show four asterisks and the rest of the columns will show up with their normal data.
For example, I have a column called country. Any time that USA appears in a record, the four columns of (empid, ss, address, dept) will need to only show **** while the rest of the columns will appear as normal. If a country that is not USA is in a record, then all columns will show the data as normal. I know there is a noprint function but I can't seem to figure out how to just show the asterisks.
Instead of using something complicated for this, just use a CASE expression:
SELECT CASE WHEN Country = 'USA' THEN '*****' Else EmpID END as EmpID