Get the Max Value from 3 columns in excel - excel-2007

I have a table with three columns of dates and three respective contacts made. From this table i would like to get the max date and contact relevant to that date. My data is as below:
DATE1 CONTACT1 DATE2 CONTACT2 DATE3 CONTACT3 MaxDate MaxContact
25/11/2014 Phone 21/03/2014 Visit 21/03/2014 Visit
30/09/2014 Phone 03/03/2014 Visit 10/10/2014 Visit
26/11/2014 Phone 18/02/2014 Visit 18/02/2014 Visit
13/08/2014 phone 14/12/2012 Visit 29/05/2014 Phone
Many thanks in advance.

With data like:
In G2 enter:
=MAX(A2,D2,E2)
and copy down and in H2 enter:
=INDEX(A2:F2,1,MATCH(G2,A2:E2,0)+1)
and copy down to yield:

Related

SQL - Monthly cumulative count of new customer based on a created date field

Thanks in advance.
I have Customer records that look like this:
Customer_Number
Create_Date
34343
01/22/2001
54554
03/03/2020
85296
01/01/2001
...
I have about a thousand of these records (customer number is unique) and the bossman wants to see how the number of customers has grown over time.
The output I need:
Customer_Count
Monthly_Bucket
7
01/01/2021
9
02/01/2021
13
03/01/2021
20
04/01/2021
The customer count is cumulative and the Monthly Bucket will just feed the graphing package to make a nice bar chart answering the question "how many customers to we have in total in a particular month and how is it growing over time".
Try the following SELECT SQL with a sub-query:
SELECT Customer_Count=
(
SELECT COUNT(s.[Create_Date])
FROM [Customer_Sales] s
WHERE MONTH(s.[Create_Date]) <= MONTH(t.[Create_Date])
), Monthly_Bucket=MONTH([Create_Date])
FROM Customer_Sales t
WHERE YEAR(t.[Create_Date]) = ????
GROUP BY MONTH(t.[Create_Date])
Where [Customer_Sales] is the sales table and ??? = your year

Always Include Certain Records in Daterange

Sorry if this is too general a question, but I couldn't find much material on this.
I'm wondering if there is any way in SQL or Tableau to always include certain records despite changes in a date range?
For example, I have 200 records that range from 1940-2004 and want 2 or 3 of these records to always be returned in the query (which includes a date range statement) is there a known method?
I'd like to avoid altering the date attributes based on the date range statement itself is possible.
Initial data:
Person_ID | Group | Date
ID 1 2 1-1-2003
ID 2 1 1-1-1994
ID 3 1 1-1-1985
ID 4 1 1-1-1992
ID 5 2 1-1-1991
ID 6 2 1-1-2002
ID 7 1 1-1-2003
ID 8 2 1-1-2005
ID 9 2 1-1-1999
ID 10 1-1-2002
ID 11 1-1-1989
For my results, I want it to be possible so that no matter the daterange I select, ID 10 and ID 11 are included.
SELECT Person_ID
FROM table
WHERE DATE BETWEEN date1 AND date2
Will always yield ID 10 and ID 11 no matter the dates inputted.
I don't know much about tableau but you can try this...
SELECT Person_ID
FROM table
WHERE (DATE BETWEEN date1 AND date2) OR Person_ID = 10 OR Person_ID = 11
If you want help on figuring out queries try and say what you want as literal as possible using sql words. So in this case you could say "I want to select the person_id from table where the date is between date1 and date2 or if the person_id is 10 or if the person_id is 11". If you ever say but (ex: date between date1 and date2 but if their id is 10 then also do it) then most likely you can put an or there : ). So if I were to do it without the or it sound more normal (in my opinion at least -> "where the date is between date1 and date2 but if the person_id is 10 or 11 also include it"). Hope that helps!

Count The number of Visit in SQL

I need to find out a way to record the number of the visit of a person in my table, so that if it is the first time then I need to have the number in the Visits equal to 1 and in the second time this person come the number in the visit should be 2.
Below is a description to the situation.
CNT PATID DATE PATName VISIT
----------------------------------------
300 3001 16/08/2015 Jason 1
300 3002 16/08/2015 Sayde 1
300 3003 20/08/2015 Sayde 2
300 3004 20/08/2015 wetni 1
300 3005 20/08/2015 Jason 2
The column Visit is the thing I want to be able to calculate and show.
The best way is to calculate this on the fly with row_number() function
select CNT, PATID, DATE, PATName,
row_number() over (partition by PATName order by PATID) as VISIT
from table
The above will work in SQL Server and ORACLE. If you use MySQL, you need to use a variable

Get Months between two Date columns

I have a table AreaValues with columns ID, Name, Value, startDate, EndDate.
I need to view a report that select the values of every month with month name or number
Example: this is my table AreaValues:
ID Name Value StartDate EndDate
-------------------------------------------
1 Test 200 05-06-2012 07-08-2016
I need report get the value using SQL Server Reporting Services or query or any way:
month value = (200 / count of months from startdate to end date)
ID Name Value year2012 year2013 year2014 year2015 year2016
1 test 200 6,7,8,9,10,11,12 1,2,3,....12 1,2,3,....12 1,2,3,....12 1,2,3,4,5,6,7
and do that for every record in the table
Please, I need help
It Solved
I used a Loop in SQl that get all months between two dates
Then
Create report with Matrix get the months above and ID left

SQL statement to get the most frequent hours and/or period for a user

I'm new to SQL Datetime.
Considering I have records in MySQL with Datetime data (as seen below),
what is the best way to get the most frequent occurring HOUR or range/period for these records for a particular user (i.e. john)?
ID | Datetime | User | Activity
0 2010-03-29 13:15:56 john visit
1 2010-03-29 13:13:14 ariel visit
2 2010-03-29 13:09:13 john visit
3 2010-03-29 13:07:21 john visit
4 2010-02-23 12:21:03 john visit
5 2010-02-23 12:01:03 john visit
6 2010-02-23 11:01:03 john visit
7 2010-02-23 02:01:03 john visit
With the above data,
the frequent hour for john doing visit would be 13, while period would be perhaps 12-13.
The goal is to find the period/time that the user does a certain activity most.
Thanks in advance for all the help!
It can depend on the database you're using, but in mysql :
SELECT COUNT(id) as count, HOUR(Datetime) as hour FROM table GROUP BY hour ORDER BY count DESC LIMIT 1
(see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html)
with oracle you can write something like
select TO_CHAR(Datetime, 'HH24'), count(ID) from Table group by TO_CHAR(Datetime, 'HH24')
on other RDBMS use equivalent function to extract hour part from Datetime field.