I am trying to write a script to update the column values if they are null with incremental values if the column id is the same - sql-update

I am trying to write a SQL script to set the column values with incremental values based on the values of the id of the previous column. If the Job Id column is the same I would like the Employee column to be populated with incremental values.
I have this so far, but I'm not sure how to group it by JobID and reset the counter if they JobID changes:
DECLARE #employee INT
SET #employee = 0
UPDATE dbo.JobEmployees
SET Employees= #employees, #employees = #employees + 1 where Employees is null

Related

SQLite how to limit the number of records

I want to limit the number of records in my SQLite table to for example 100 records, and then when I INSERT the 101th record, the first record (the oldest) be removed from the table. In other word, I want to prevent the table from growing more than 100 records and always have the last 100 records. Is there any setting or query with SQLite or should I handle it manually?
thanks in advance
You can do it with a trigger.
Say that your table is this:
CREATE TABLE tablename (
id INTEGER PRIMARY KEY,
name TEXT,
inserted_at TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now'))
);
In the column inserted_at you will have the timestamp of the insertion of each row.
This is not necessary if you declared the column id as:
id INTEGER PRIMARY KEY AUTOINCREMENT
because in this case you could identify the 1st inserted row by the minimum value of the id.
Now create this trigger:
CREATE TRIGGER keep_100_rows AFTER INSERT ON tablename
WHEN (SELECT COUNT(*) FROM tablename) > 100
BEGIN
DELETE FROM tablename
WHERE id = (SELECT id FROM tablename ORDER BY inserted_at, id LIMIT 1);
-- or if you define id as AUTOINCREMENT
-- WHERE id = (SELECT MIN(id) FROM tablename);
END;
END;
Every time that you insert a new row, the trigger will check if the table has more than 100 rows and if it does it will delete the 1st inserted row.
See the demo (for max 3 rows).

How to copy highest value name as new column value in power bi?

I'm trying to create a new column which is filled by the highest value name and condition is today date and if suppose two values are the same then we have to pick the first one value name.
My table is
My Expected table is
Condition is
select screen name has new column value from the most viewed screen by using a count value, selected customer count should be 1 group by id and start date
Best approach here would be that you add a blank query in the query editor. Make the blank query the same table as your first table.
=FirstTableName
Call the blank query SecondTable for example.
Add a filter with the current date.
Group your table by Screen, by count and by the start date. (I think you need a count value that the grouping will work)
Add a sort order for the column count (descanding).
Delete all rows expect the first row.
The result is now one row with the highest value in the count column on todays date.
Join this one row now with your first table. The column screen and the start date are the join columns and expand the screen column from your joined table.
This gives you the result like your second picture.

Add column with hardcoded values in PowerBI

It's probably extremely simple but I can't find an answer. I have created a new column and I would like to use the DAX syntax to fill the column with hardcoded values.
I can write this: Column = 10 and I will get a column of 10s but let's say my table has 3 rows and I would like to insert a column with [10, 17, 155]. How can I do that?
Try using DATATABLE function
Table = DATATABLE("Column Name",INTEGER,{{10},{17},{155}})
You can also put more columns with their own data if you want to, check this
https://learn.microsoft.com/en-us/dax/datatable-function
Assuming your table has a primary key column, say, ID, you could create a new table with just the column you want to manually input.
ID Value
---------
1 10
2 17
3 155
You can create this table either through the Enter Data button or create it using the DAX DATATABLE function as #Deltapimol suggests.
Once you have this table you can create a relationship to your existing table in the data model at which point you can either use this new table in your report to get the values you need or if you really need them in the existing table for some reason, you can pull them over using the RELATED function in a calculated column.
Table1 = GENERATESERIES(1, 3)
Table2 = DATATABLE(
"ID", INTEGER,
"Value" INTEGER,
{{1, 10},{2, 17},{3, 155}}
)
Now you can create a relationship from Table1 to Table2[ID] and then define a calculated column on Table1 as follows:
ValueFromTable2 = RELATED(Table2[Value])
If you don't want to create a relationship, then you could use the LOOKUPVALUE function instead in a calculated column on Table11.

Filter sql query results using static values from "Select list" page item in Oracle APEX 5.0

I'm looking for a way to filter and display records using static values from a "Select list" page item. I have created a bar chart in APEX 5.0 using the following query:
select to_char(to_date(time_stamp,'YYYY-MM-DD-HH24:MI:SS'),'YYYY-MM-DD-HH24:MI:SS') as label, col2 as value from table1 where :P5_NEW_1 = col1 ;
The time_stamp column of table1 is of datatype varchar2 in my database and contains the date values in the format YYYY-MM-DD-HH24:MI:SS
e.g., values like below are stored in the time_stamp column
2015-08-26-10:17:15
2015-08-26-13:17:15
2015-09-17-12:45:54
2015-09-17-14:12:32
2015-10-06-10:01:42
2015-10-06-11:01:28
2015-10-06-05:01:28
and so on...
I have added a "Select list" item named "interval" on my form that contains a pre-populated list of values like 1hr, 6hrs.
Now I want to modify the above query such that following should happen:
For the value of 1hr selected from the drop down list, the query should check the time_stamp column and display the records for the last one hour
(i.e., records which fall in range to_char(sysdate - 1/24 ,'YYYY-MM-DD-HH24:MI:SS') to to_char(sysdate ,'YYYY-MM-DD-HH24:MI:SS')
And for the value of 6hrs selected from the drop-down list, the query should display records for the last 6 hours.
(i.e., records which fall in range to_char(sysdate - 6/24 ,'YYYY-MM-DD-HH24:MI:SS' to to_char(sysdate ,'YYYY-MM-DD-HH24:MI:SS') )
How do I modify my SQL query to add the above condition in my current SQL query, using static values from page item select list?
Hi you can try this:
SELECT to_char(to_date(time_stamp,'YYYY-MM-DD-HH24:MI:SS'),'YYYY-MM-DD-HH24:MI:SS') AS label, col2 AS VALUE
FROM table1
WHERE :P5_NEW_1 = col1 AND
to_date(time_stamp,'yyyy-mm-dd hh24:mi:ss') BETWEEN
(SYSDATE - (to_number(:SELECT_LIST_ID,'99')/24)) AND SYSDATE;
Just make sure that the static values has a return value equal to hr itself
For example, if static value is equal to 1HR then return value should be 1
:SELECT_LIST_ID is the select list id which holds the static values for the interval.
Hope this helps.

How to Update a single column through informatica?

I have a target table with the following attributes:
PARTY_ID PK
START_DATE PK
STATUS_CD PK
END_DATE
I have a dynamic lookup which is returning me 1(insert) 2(update) 0 (duplicate) for each row from source table.
What i want is when i get 2(update) to add an END_DATE to the updated row without changing anything else.
For example i have the following row in my target table:
1 12/01/2014 2 NULL
and i get this row from my source table:
1 14/01/2014 6 NULL
What i want is to add ONLY the end date to the target table without anything else. LIKE:
1 12/01/2014 2 14/01/2014
I know how to update the whole row but i dont know how to update only one column.
Schema:
CREATE SET TABLE IND_MAR_STATUS ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
INDIVIDUAL_PARTY_ID DECIMAL(18,0) NOT NULL,
INDIV_MARITAL_STAT_START_DTTM DATE FORMAT 'YYYY-MM-DD' NOT NULL,
MARITAL_STATUS_CD VARCHAR(100) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
INDIV_MARITAL_STAT_END_DTTM DATE FORMAT 'YYYY-MM-DD',
ETL_SOURCE_ID DECIMAL(18,0) NOT NULL,
ETL_EXTRACT_SPEC_ID DECIMAL(18,0),
ETL_JOB_RUN_ID DECIMAL(18,0))
PRIMARY INDEX ( INDIVIDUAL_PARTY_ID );
Simply disconnect the target ports you don't want to update (i.e. only PARTY_ID and END_DATE should be connected).