Calculating Date columns of MysQl in VB.Net? - mysql

I'm using VB.Net 2010 and MySQL.
I have two tables in MySQL database 'CAR' and 'CAR_RENT'.
From the VB.Net I want to do the following calculations:
I want to calculate the total_fee Column in CAR_RENT. Which can be multiplying the rental_fee column from 'CAR' table with the date difference of Issue_date and return_date from 'CAR_RENT' table.
I want to calculate the penalty_fee column of 'CAR_RENT' table by finding the exceeded date from the return_date. That should be rental_fee*number_of_exceeded_date for specified client.
That should be automatically calculated when the program is run.
I know that the code I tried is completely not formal way so no need to post it here. Please I need your help??
TABLE:CAR
+-----------+----------+---------------+--------+----------------+
| Car_id | Plate_no | Model | color | Rental_fee_day |
| 100 | 25534 | Tesla Model S | Black | $3500 |
| 101 | 25535 | Audi A6 | Black | $2100 |
| 103 | 35625 | BMW 3 Series | silver | $2000 |
+-----------+----------+---------------+--------+----------------+
TABLE:CAR_RENT
+-----------+--------+------------+-------------+-----------+-------------+
| Client_id | Car_id | Issue_date | Return_date | Total_fee | Penalty_fee |
+-----------+--------+------------+-------------+-----------+-------------+
| 1 | 103 | 2014-02-01 | 2014-02-10 | | |
| 1 | 100 | 2014-02-01 | 2014-02-15 | | |
| 3 | 101 | 2014-02-18 | 2014-02-30 | | |
+-----------+--------+------------+-------------+-----------+-------------+

you should check here this is the DateDiff function for mysql.
you can use this and a join to get the info you need....

Related

How to SUM by each one iteration with if condition - using mysql?

I want to sum each value by iteration and when the condition matches they trigger the date value and total sum.
+------------+----------------------------------+------------+--------+
| id | EMAIL | created | Amt |
+------------+----------------------------------+------------+--------+
| 61 | abc#gmail.com | 1514909390 | 57.00 |
| 25 | xyz#gmail.com | 1515534837 | 360.00 |
| 36 | zccc#abv.com | 1515645391 | 240.00 |
| 22 | vv#aa.com | 1516419622 | 320.40 |
| 48 | aa#xyz.com | 1516706121 | 240.00 |
+------------+----------------------------------+------------+--------+
Expected Output:
if(Amt=>600) then I need the (Created (1515645391)) the third row created date.
Above is sample data of result. Thanks in advance.

How to make a pivot table by multiple unique ID numbers?

I'm trying to break up a SQL table that needs to take a users name and find the unique user ID's from up to 4 systems.
The data is currently like this:
| Name | User_ID |
-----------------
| A | 10 |
| A | 110 |
| A | 1500 |
| A | 4 |
| B | 20 |
| B | 100 |
| B | 2 |
| C | 10 |
I need to pivot it around the user's name to look like this (the id's don't need to be in numerical order as the SYS#_ID for each doesn't matter):
| Name | SYS1_ID | SYS2_ID | SYS3_ID | SYS4_ID |
------------------------------------------------
| A | 4 | 10 | 110 | 1500 |
| B | 2 | 20 | 100 | NULL |
| C | 10 | NULL | NULL | NULL |
This is the code I have tried on MySQL:
PIVOT(
COUNT(User_ID)
FOR Name
IN (SYS1_ID, SYS2_ID, SYS3_ID, SYS4_ID)
)
AS PivotedUsers
ORDER BY PivotedUsers.User_Name;
I'm unsure if PIVOT works on MySQL as I keep getting an error "PIVOT unknown". Is there a way to find the values that each user has and if they do not appear in the table already add them to the next column with a max of 4 values?

Finding MAX Date of Two Fields in an Access Query

In my access database, we keep track of two sets of dates. One set is for date of membership dues payments, the other set is date of other contributions (a non-membership donation.) There are multiple dates for each person depending on number of payments made for each type.
Example:
+----+---------------+---------------+
| ID | Dues_Date | Cont_Date |
+----+---------------+---------------+
| 1 | 01/01/15 | 09/12/11 |
| | 01/01/14 | |
| | 01/01/13 | |
| 2 | 07/30/14 | 06/20/13 |
| | | 11/12/11 |
+----+---------------+---------------+
First I needed to know the most recent payment for each of the two fields so I ran a query that tells me the MAX (most recent) date for each field.
Example Query:
+----+---------------+---------------+
| ID | Max Dues_Date | Max Cont_Date |
+----+---------------+---------------+
| 1 | 01/01/15 | 09/12/11 |
| 2 | 07/30/14 | 06/20/13 |
| 3 | 02/11/13 | 09/16/14 |
| 4 | 07/30/12 | 06/20/11 |
| 5 | 12/13/13 | 11/12/14 |
+----+---------------+---------------+
Now I need a third field in the same query to compare the results of the first two fields and show which is the MAX of those two.
I have column 2 and 3 in the query; how can I take that and create column 4 in the same query?
Example Query:
+----+---------------+---------------+-----------------+
| ID | Max Dues_Date | Max Cont_Date | Max Date(DD&CD) |
+----+---------------+---------------+-----------------+
| 1 | 01/01/15 | 09/12/11 | 01/01/15 |
| 2 | 07/30/14 | 06/20/13 | 07/30/14 |
| 3 | 02/11/13 | 09/16/14 | 09/16/14 |
| 4 | 07/30/12 | 06/20/11 | 07/30/12 |
| 5 | 12/13/13 | 11/12/14 | 11/12/14 |
+----+---------------+---------------+-----------------+
Try adapting this to your own scenario:
SELECT tblTest.DueDate, tblTest.ContDate, [DueDate]-[ContDate] AS Test, IIf([Test]<0,[ContDate],[DueDate]) AS MaxRes
FROM tblTest;
"Test" finds which is the later date, ContDate or Due Date. The IIf statement selects the later date.
Does this help?

how to insert average of a query into mysql table

I am trying to get average of latency for each items that holds into two separate mysql table. Let me more clarify that I have two mysql tables as below,
table: monitor_servers
+-----------+-----------------+
| server_id | label |
+-----------+-----------------+
| 1 | a.com |
| 2 | b.com |
+-----------+-----------------+
table: monitor_servers_uptime
+-------------------+-----------+-----------+
| servers_uptime_id | server_id | latency |
+-------------------+-----------+-----------+
| 1 | 1 | 0.4132809 |
| 3 | 1 | 0.4157769 |
| 6 | 1 | 0.4194210 |
| 9 | 1 | 0.4140880 |
| 12 | 2 | 0.4779439 |
| 15 | 2 | 0.4751789 |
| 18 | 2 | 0.4762829 |
| 22 | 2 | 0.4706681 |
+-------------------+-----------+-----------+
Basically, each domains associated with the same id_number in both tables. While I am running the query below, getting average of each items.
select monitor_servers.label, avg(monitor_servers_uptime.latency)
from monitor_servers,monitor_servers_uptime
where monitor_servers.server_id = monitor_servers_uptime.server_id
group by monitor_servers.server_id;
The query ended up,
+---------------------+-------------------------------------+
| label | avg(monitor_servers_uptime.latency) |
+---------------------+-------------------------------------+
| a.com | 0.41393792995 |
| b.com | 0.47551423171 |
+---------------------+-------------------------------------+
My questions are doing am i in wright way while getting average of the each items and how can i insert new average result of each items into a new column on table monitor_servers ? And also what happens if some of latency rows are NULL ?
**Edit : What i am trying to achieve in one query result is **
+-----------+----------+------------------+
| server_id | label | avg. |
+-----------+----------+------------------+
| 1 | a.com | 0.41393792995 |
| 2 | b.com | 0.47551423171 |
+-----------+-----------------------------+
Thanks in advance,
Your calculation seems to be correct.
You could add another column to the monitor_servers using sql:
ALTER TABLE monitor_servers ADD avg_latency DEFAULT 0.0 NOT NULL
For doing the AVG calculation check this answer.

Including a value once in a summed SSRS group

I have a data source which is pulling in event attendance information which looks like the following table.
Note that one booking can contain multiple attendees, causing duplication in the Amount column.
+------------+-----------+---------------+----------+
| Date | Booking | Booking Price | Attendee |
+------------+-----------+---------------+----------+
| 01/01/2011 | Booking 1 | £300.00 | Alice |
| 01/01/2011 | Booking 1 | £300.00 | Bob |
| 01/01/2011 | Booking 1 | £300.00 | Dave |
| 01/01/2011 | Booking 2 | £200.00 | Frank |
| 01/01/2011 | Booking 2 | £200.00 | Julie |
| 02/01/2011 | Booking 3 | £100.00 | Anne |
+------------+-----------+---------------+----------+
The Report should end up a bit like this:
+------------+-----------+---------+
| Date | Booking | Amount |
+------------+-----------+---------+
| 01/01/2011 | Booking 1 | £300 |
| | Alice | |
| | Bob | |
| | Dave | |
+------------+-----------+---------+
| | Booking 2 | £200 |
| | Frank | |
| | Julie | |
+============+===========+=========+
| TOTAL FOR 01/01/2011 | £500 |
| |
| |
+============+===========+=========+
| 02/01/2011 | Booking 3 | £100 |
| | Anne | |
+============+===========+=========+
| TOTAL FOR 02/01/2011 | £100 |
| |
| |
+============+===========+=========+
(That pretty much exhausts my ascii-art table skills!)
The problem I have is that because the amount is showing for each delegate, the aggregate functions count them all, so date for 01/01/2011 shows as £1300, instead of £500
I don't need any values next to the attendees, so I could pull it in a separate data set, but I can't seem to add a table into a tablix cell, so don't know how that would work.
The version of SSRS is key here. In SSRS 2008R2, you can now nest aggregate functions:
So this would now be a valid expression:
=SUM(FIRST(Fields!BookingPrice.Value , "BookingGroupName") , "DateGroupName")
If you aren't yet on 2008R2 then you can do some tricks with embedded code to keep a running total: http://beyondrelational.com/blogs/jason/archive/2010/07/03/aggregate-of-an-aggregate-function-in-ssrs.aspx
The key here is that you add to the total in the embedded code once per group. Then retrieve the total and clear it out at the end of the group.