Derive a Week ending Date from an existing column - sql-server-2014

I have a table called Inventory, with one of the columns being report_dt, I want to add another column to the existing table called weekending_dt based on the report_dt. Report_dt : 20180108, 20180123, 20180129. Weekending_dt: 20180112, 20180126, 20180202.
If not able to add it to the same table I can make a copy of the table and add the columns to it. I need the logic to derive the Weekending_dt column. Thanks in advance your inputs are very much appreciated.
My initial approach was:
I thought of creating a table with 365 rows with dates (Jan through Dec) and deriving weekending_dt in the second column manually.
Then doing a lookup from the Inventory table of report_dt against the date column of the second table to pull it's corresponding weekending_dt.
I'm sure there is an easier approach. Once again appreciate all your inputs.

Related

Get a day of a week filled automatically to a new column with a WEEKDAY function MySQL

I am stuck on a problem I have with MySQL, I am quite new to it so please have patience :D. I have a table that includes a column with specific dates (each date has multiple entries) and I would like to create a new column with a specific day (Monday, Tuesday, etc.) based on the date I already have. I found a function WEEKDAY I would like to use, but I can't find a way to update all the rows automatically.
I already ADD COLUMN "day" (which is empty now) and need to add the values for all rows. The table has a thousand rows so there is no way I would do it manually one by one. I am sure there is a way around doing it by SQL code. Would appreciate it if anyone can give me a hint.
Thanks a lot!

SUM function needs automatically to adjust for changes to SQL DB entries

I'm using multiple Calc tables in a single document for ingoing and outgoing bills that are stored in a SQL DB. Table A has ingoing bills and Table B has outgoing bills. For example table A has data from B2-B39 and table B has data from B2-B15. I need the sum function to automatically adjust when data entries are added or subtracted.
I can use =SUM(B2:B39) in tableA.B40 and =SUM(B2:B15) in tableB.B16 to get the sums of the current data, at the end of the data entries. However, when the DB changes, for example Table A has data from B2-B44, I end up missing five entries. To further complicate things, I have Table C, where I evaluate the net income. For example in Table C I use tableB.B16-tableA.B40. When the number of entries in tables A and B change the function in Table C no longer works.
I need SUM in multiple tables to automatically adjust its position based on how many rows of data are found in the DB.
Maybe there is something about your layout that you have not mentioned but a solution would seem to be to apply whole column references (eg =SUM(B:B)), or if you must have the total in the same column move it to the top and sum like so:
=SUM(B2:B1048576)
Otherwise try something like LibreOffice Basic and search for the first empty cell in the column to put the formula in.

how to populate column from the content of other columns after a row is inserted in the same table in mysql

I have a table traffic with 7 columns, namely toll_id, date1, shift, car_single, car_return, car_local and car_total.
How could I populate first 5 columns manually, and then store a value in column car_total, which will be the sum of car_single and car_return?
Here is the image of my table:
Just to add a 3rd and 4th ways of achieving the desired outcome:
If you have at least MySQL v5.7.6, you can use a generated column as car_total.
Alternatively, you can choose not to store car_total at all, but calculate this value on the fly while querying the table.
Having a column to store the results of the calculation is good if you regularly have search based on that field because you can use indexes to speed up the searches. Calculating the results on the fly may be better, if you just need to display the result of the calculation, but there is no need to filter on it.
There are two ways to do this:
Add the logic in the application itself, so that it calculates total before inserting the record. (Recommended)
Write an after insert trigger (http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html) which calculates the count when record is inserted.

Display calculate total from the separate column

I work with CRM. Now I create some report from SSRS.
I need create some table like this:
I can not create last column of the table. This column must contains sum of all rows previous column from groupping. How to do it? I work with crm online. Reports to CRM online can not use SQL - only fetch. And even I would can calculate value of this column from query I can not place it in last column - only second column because we are using grouping.

How to check if there is a conflict in Day/Room/Start time/End time MySQL

So I have a table named tbl_fullsched. Here it is. As you can see, there are column named Day, Room, Start and End. They are all Varchar(). I want to compare all the data in this columns and display the data that has conflict. Like for example the "Monday" has two schedule in the same room but the time is conflicting with each other. Please help. Thanks