SQL Select a column field without adding additional column - mysql

Basically I have a table named DEPOSITS with columns amount, date and time. Lets say I want to select all the columns of DEPOSITS and add in an additional column named Data source with the values deposits. Can I do this without altering the actual database table column?
So my select query output will be like
Table Deposits
amount date time dataSource
1000 1/1/2017 5am Deposits
2000 2/1/2017 10pm Deposits
Note that I don't wish to alter the table to insert a column dataSource. I just want to select it as shown. Please help thanks!

Yes you can:
select amount, date, time, 'Deposits' as dataSource
from Deposits

Related

How to update a row value in one table using the rencently inserted value in another table in MySQL

Ok, first of all I'm pretty new to MySQL. I'm currently designing a demo database in which i have multiple tables already. What I want to do is that I have these two tables, one named loans, and the other revenue. in the loan table columns are ( transaction, customer-id, video-id, loan-date, return-date, total-loan, period(this is a date value with just month and year))
On the revenue table I have ( period(same as loan), no_of_transactions, total_revenue) The period column in this table is UNIQUE constrained. So I was thinking of adding a trigger, every time there's a new INSERT into the transaction table, the total_revenue column in the revenue table should update. I designed this code:
CREATE TRIGGER UpdateRevenue
AFTER INSERT ON loan
FOR EACH ROW
UPDATE revenue
SET total_revenue = total_revenue + loan.total-loan
WHERE period = NEW.period;
But I think is not working because I'm not referring to the new insert. Any guidance on this would be good. Thanks.

How to add data from 2 queries into 1 table in webi

I have 2 queries.
The first query is from the webi universe which has:
Staff ID
Arrival Date
Leave Date
The 2nd query is an Excel data query that has:
Staff ID
Test Date
What I am want to do is to create a single table that has:
Staff ID
Arrival Date
Leave Date
Test Date
How can I do that?
Create two data providers. First Data Provider using Universe will select Staff ID, Arrival Date, Leave Date. Second Data Provider using Excel Data Source will select Staff ID, Test Date. Run the queries. Once the data is fetched. Merge the common column (Staff ID). Then create a new table and drag the columns as required.

SQL: How to update values of table one and implement the change in table 2 as welll?

I have two tables, and i am trying to update the first table based on the value from second table. Table 1 contains the Sum of total weight and table two contains weight. when added up these values in table 2, it then shows total in table 1
For example if I update the foreign key in table 2 then the Amount in table should update. Please refer the screenshot attached.
My first table sql:
select LodingZoneID, Finaltotal from TransitList
My Second table sql:
SELECT `Suburb`, `LodingZoneID`, Total FROM `GenerateRun`
There is no reason for table 1. You can just run the query:
select fk, sum(val)
from table2
group by fk;
If you really need to store the sum in table 1, then you would be using triggers on table2 to handle insert/update/delete. This is rather cumbersome and it is generally better to do the calculation when needed, rather than trying to store the results in advance.
Or, You can create a View with desired output
-CREATE VIEW 'TransitList' AS
SELECT LodingZoneID, SUM(Total) FinalTotal FROM GenerateRun
GROUP BY LodingZoneID;
Whenever you want to access data, you can use
-SELECT LodingZoneID, FinalTotal FROM TransitList;

Insert/Update on table with autoincrement and foreign key

I have a table as such:
id entity_id first_year last_year sessions_attended age
1 2020 1996 2008 3 34.7
2 2024 1993 2005 2 45.1
3 ... ... ...
id is auto-increment primary key, and entity_id is a foreign key that must be unique for the table.
I have a query that calculates first and last year of attendance, and I want to be able to update this table with fresh data each time it is run, only updating the first and last year columns:
This is my insert/update for "first year":
insert into my_table (entity_id, first_year)
( select contact_id, #sd:= year(start_date)
from
( select contact_id, event_id, start_date from participations
join events on participations.event_id = events.id where events.event_type_id = 7
group by contact_id order by event_id ASC) as starter)
ON DUPLICATE KEY UPDATE first_year_85 = #sd;
I have one similar that does "last year", identical except for the target column and the order by.
The queries alone return the desired values, but I am having issues with the insert/update queries. When I run them, I end up with the same values for both fields (the correct first_year value).
Does anything stand out as the cause for this?
Anecdotal Note: This seems to work on MySQL 5.5.54, but when run on my local MariaDB, it just exhibits the above behavior...
Update:
Not my table design to dictate. This is a CRM that allows custom fields to be defined by end-users, I am populating the data via external queries.
The participations table holds all event registrations for all entity_ids, but the start dates are held in a separate events table, hence the join.
The variable is there because the ON DUPLICATE UPDATE will not accept a reference to the column without it.
Age is actually slightly more involved: It is age by the start date of the next active event of a certain type.
Fields are being "hard" updated as the values in this table are being pulled by in-CRM reports and searches, they need to be present, can't be dynamically calculated.
Since you have a 'natural' PK (entity_id), why have the id?
age? Are you going to have to change that column daily, or at least monthly? Not a good design. It would be better to have the constant birth_date in the table, then compute the ages in SELECT.
"calculates first and last year of attendance" -- This implies you have a table that lists all years of attendance (yoa)? If so, MAX(yoa) and MIN(yoa) would probably a better way to compute things.
One rarely needs #variables in queries.
Munch on my comments; come back for more thoughts after you provide a new query, SHOW CREATE TABLE, EXPLAIN, and some sample data.

Database table design with money and periods

Suppose I have database table like Employee and each employee has associated wage. Wages can be different in different periods of time. How can I design a table storing these wages? ("wage periods" cannot overlap, but this constraint does not have to be handled by the database)
Employee will not be the only table needing such a date-modal currency data.
I was thinking about a table with two primary keys (a "wageId" integer and a datetime) and a wage column (integer, counting pennies as suggested). Employee (or any other) table would have a column "wage" referencing the "wageId". Datetime would be the date up to which the wage is valid. Does that make any sense? Could it be improved?
I'm using mysql.
This is an example of a slowly changing dimension. A good reference on database design are books by Ralph Kimball.
In any case, what you want is a table called something like EmployeeWage. This would have columns such as:
EmployeeWageId, a unique id for the row
EmployeeId, identifier for the employee
Wage, the wage
EffDate, date the wage took effect
EndDate, date the wage stopped being effective.
I would also add columns such as who created each row and the exact time, but that is outside the scope of your question.