How to SUM by each one iteration with if condition - using mysql? - 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.

Related

MySQL return prioritizes value else return other value

I have a table (This is a mock-table),
+------------+------+--------+
| Name | Code | Active |
+------------+------+--------+
| Sales | 55 | 1 |
| Sales | 55 | 0 |
| IT | 22 | 1 |
| Production | 33 | 1 |
| Production | 33 | 0 |
| Marketing | 77 | 0 |
| Marketing | 77 | 0 |
+------------+------+--------+
And I want to return a list of distinct names and codes. However, I want to determine if the department is active or not. so if Sales has a 1 in active and a 0 in active they are active, but is they had only zeros then they are not.
I've tried a variety of methods and read through a few dozen SO post, but am not gaining any progress.
The output I am trying to achieve is:
+------------+------+--------+
| Name | Code | Active |
+------------+------+--------+
| Sales | 55 | 1 |
| IT | 22 | 1 |
| Production | 33 | 1 |
| Marketing | 77 | 0 |
+------------+------+--------+
How can I prioritize the Active column to a value of 1, but still return an entry if all entries with the same code have a value of 0 (such as marketing)?
GROUP BY name and code and get the maximum value of Active. (Assuming 0 and 1 are the possible values for Active column)
SELECT Name,Code,MAX(Active) active
FROM tablename
GROUP BY Name,Code

How to get sum for different entry in mysql

I have table in mysql like
| service_code | charges | caller_number | duration | minutes |
+--------------+---------+---------------+----------+---------+
| 10 | 15 | 8281490235 | 00:00:00 | 1.0000 |
| 11 | 12 | 9961621709 | 00:00:00 | 0.0000 |
| 10 | 15 | 8281490235 | 01:00:44 | 60.7333 |
| 11 | 2 | 9744944316 | 01:00:44 | 60.7333 |
+--------------+---------+---------------+----------+---------+
from this table I want to get charges*minutes for each separate caller_number.
I have done like this
SELECT sum(charges*minutes) as cost from t8_m4_bill groupby caller_number
but I am not getting expected output. Please help?
SELECT caller_number,sum(charges*minutes) as cost
from t8_m4_bill
group by caller_number
order by caller_number

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?

Calculating Date columns of MysQl in VB.Net?

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....

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.