I have two tables, first table is called 'submissions' and the second table is called 'area'
******** SUBMISSIONS TABLE ********
userid statusid no name area month year dateupdated
62 2 763 ABCD Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 7 2012 2013-11-26 15:10
62 2 869 ABC Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 6 2013 2013-11-26 14:50
61 1 763 ABCD Brazil 6 2013 2013-11-26 14:50
54 1 200 ABCDE US 12 2013 2013-11-26 21:02
32 2 200 ABCDE US 12 2013 2013-11-26 21:03
******* AREA TABLE ********
no name area
763 ABCD Brazil
869 ABC Brazil
869 ABC Brazil
869 ABC Brazil
763 ABCD Brazil
200 ABCDE US
200 ABCDE US
My Process:
A user selects a status
A user selects a month
A user select a year
All via dropdowns (SELECT)
Once they have selected the 3 fields, the filtered data is displayed.
What I am trying to achieve:
I want the records to show as per the SELECTS Above (this works correctly at the moment)
I also want to show all records from the 'Area' Table and where no data exists in the 'Submissions' table. An example of my desired output is below:
userid statusid no name area month year dateupdated
62 2 763 ABCD Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 7 2012 2013-11-26 15:10
62 2 869 ABC Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil
763 ABCD Brazil
200 ABCDE US
200 ABCDE US
In Summary, I need to show the selected records as the SELECTS, but also need to display all records from the area table.
You could use a left outer join here. Basically you want to select all records from area table and the matching rows from submission table.
The query would look like -
select sb.userid, sb.statusid, a.no,
a.name, sb.area, sb.month,
sb.year, sb.dateupdates
from area a left outer join submission sb
on (a.no = sb.no)
Related
I have 3 tables, and a query:
SELECT
DISTINCT assistent.id as id,
name,
events.client as client,
assistentprice.id as priceid,
value
FROM
`assistents`
LEFT JOIN `events` ON assistents.id = events.assistent
LEFT JOIN `assistentprice` ON assistents.id = assistentprice.id_assistente
ORDER BY
name
I got a result like:
id
name
client
priceid
value
88
MARK
44
12
7.00
88
MARK
27
14
8.00
88
MARK
44
15
11.00
88
MARK
27
11
10.00
88
MARK
44
10
9.00
16
OSCAR
49
21
8.00
16
OSCAR
14
23
9.00
16
OSCAR
14
22
7.00
16
OSCAR
49
19
9.00
So, table is ordered by name, but i want to see also ordered/grouped client for every assistent. For exampe, for Mark it have to be:
id
name
client
priceid
value
88
MARK
27
12
7.00
88
MARK
27
14
8.00
88
MARK
44
15
11.00
88
MARK
44
11
10.00
How can i do this?
I have this table that has the name of the employee and their phone time duration in mysql. The table looks like this:
Caller Emplid Calldate Call_Duration
Jack 333 1/1/2016 43
Jack 333 1/2/2016 45
Jack 333 1/3/2016 87
Jack 333 2/4/2016 44
Jack 333 2/5/2016 234
jack 333 2/6/2016 431
Jeff 111 1/1/2016 23
Jeff 111 1/2/2016 54
Jeff 111 1/3/2016 67 48
I am trying to calculate the running Daily average of each employee total_Duration by day each month. Suppose I have daily running average for the month of April, then the running average for the May should start from 1st of may and end on 31st of that month. I have tried doing many ways and mysql does not have pivot and partition function like sql server. The total employee who made the call changes daily, I need something that dynamically takes care of no of employees that makes call.
The output should look like this:
Caller Emplid Calldate Call_Duration Running_avg
Jack 333 1/1/2016 43 43
Jack 333 1/2/2016 45 44
Jack 333 1/3/2016 87 58.33333333
Jack 333 2/4/2016 44 44
Jack 333 2/5/2016 234 139
Jack 333 2/6/2016 431 236.3333333
Jeff 111 1/1/2016 23 23
Jeff 111 1/2/2016 54 38.5
Jeff 111 1/3/2016 67 48
This is the query that I started below:
SELECT row_number,Month_Year,Callername,Calldate,Caller_Emplid,`Sum of Call`,`Sum of Call`/row_number as AvgCall,
#`sum of call`:=#`sum of call`+ `sum of call` overallCall,
#row_number:=row_number overallrow_number,
#RunningTotal:=#`sum of call`/#row_number runningTotal
FROM
(SELECT
#row_number:=
CASE WHEN #CallerName=CallerName and date_format(calldate,'%d') = date_format(calldate,'%d') and
date_format(calldate,'%m') = date_format(calldate,'%m')
THEN #row_number+1
ELSE 1 END AS row_number,#CallerName:=CallerName AS Callername,Calldate,Caller_Emplid,Month_Year,`Sum of Call`
FROM transform_data_2, (SELECT #row_number:=0,#CallerName:='') AS t
ORDER BY callername) a
JOIN (SELECT #`Sum of call`:= 0) t
I have two tables, first table is called 'submissions' and the second table is called 'area'
******** SUBMISSIONS TABLE ********
userid statusid no name area month year dateupdated
62 2 763 ABCD Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 7 2012 2013-11-26 15:10
62 2 869 ABC Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 6 2013 2013-11-26 14:50
61 1 763 ABCD Brazil 6 2013 2013-11-26 14:50
54 1 200 ABCDE US 12 2013 2013-11-26 21:02
32 2 200 ABCDE US 12 2013 2013-11-26 21:03
******* AREA TABLE ********
no name area
763 ABCD Brazil
869 ABC Brazil
869 ABC Brazil
869 ABC Brazil
763 ABCD Brazil
200 ABCDE US
200 ABCDE US
I am trying to achieve the following: A user selects an Area, month and year via a dropdown (SELECT). Once they have selected the 3 fields, the filtered data is displayed.
I want to the display the selection as per the fields that are filtering, but also display all other records in that particular area where is no data. Almost like an outstanding Report.. Example Below:
userid statusid no name area month year dateupdated
62 2 763 ABCD Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 7 2012 2013-11-26 15:10
62 2 869 ABC Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil
61 1 763 ABCD Brazil
54 1 200 ABCDE US
32 2 200 ABCDE US
Does anyone know how i can achieve this? Please assist.
Try somthing like this
SELECT userid , statusid, a.no , a.name , a.area , month , year , dateupdated
FROM submission s
LEFT OUTER JOIN area a
ON s.no = a.no
WHERE month = 12 AND
year = 2013 AND
a.area = 'US'
SQLFIDDLE
I have a table that produces the following results:
userid statusid no name area month year dateupdated
62 2 763 ABCD Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 7 2012 2013-11-26 15:10
62 2 869 ABC Brazil 6 2013 2013-11-26 15:28
62 1 869 ABC Brazil 6 2013 2013-11-26 14:50
61 1 763 ABCD Brazil 6 2013 2013-11-26 14:50
54 1 200 ABCDE US 12 2013 2013-11-26 21:02
32 2 200 ABCDE US 12 2013 2013-11-26 21:03
I want to be able to show the latest 'no' per row. I have tried using max(dateupdated), but still shows all the records. I've also tried grouping, but also no luck.
I only need to see the latest record determined by the dateupdated.
Can anyone please assist me?
I'm not sure I understand your question, but you might be helped by something like this:
select * from mytable where dateupdated = (select max(dateupdated) from mytable);
EDIT:
Based on your clarification, you need in that case a correlated subquery. I haven't tried this, but perhaps something like:
select *
from mytable a
where dateupdated = (select max(dateupdated) from mytable where no = a.no);
Or this:
SELECT *
FROM table
ORDER BY dateupdated DESC
LIMIT 1;
Last for every no: (Is this what you want ?)
SELECT *
FROM table t1
WHERE NOT EXISTS(SELECT * FROM table t2
WHERE t1.no = t2.no
AND t2.dateupdated > t1.dateupdated)
Query:
SQLFIDDLEExample
SELECT t1.*
FROM Table1 t1
LEFT JOIN Table1 t2 ON t2.dateupdated > t1.dateupdated
AND t1.no = t2.no
WHERE t2.no IS NULL
Result:
| USERID | STATUSID | NO | NAME | AREA | MONTH | YEAR | DATEUPDATED |
|--------|----------|-----|-------|--------|-------|------|---------------------------------|
| 62 | 2 | 763 | ABCD | Brazil | 6 | 2013 | November, 26 2013 15:28:00+0000 |
| 62 | 2 | 869 | ABC | Brazil | 6 | 2013 | November, 26 2013 15:28:00+0000 |
| 32 | 2 | 200 | ABCDE | US | 12 | 2013 | November, 26 2013 21:03:00+0000 |
Tag along a LIMIT 1; at the end of your query and sort descending.
I have a following question regarding crosstabs in Access:
How do I create a subtotal columns?
What I want to see as a result of the query is this:
Nov 2010 Dec 2010 2010 Total Jan 2011 Feb 2011
Row1 2 4 17 3 2
Row2 8 6 35 7 5
How do I create these subtotals for the year? (It's ok, if the year data will be in the end, after all months)
The problem is that I need to do this without hardcoding each year, the query should work with any dataset
Thanks in advance!
Say we have raw [SalesData]
SalesYear SalesMonth Region SalesTotal
--------- ---------- ------ ----------
2010 11 East 45
2010 11 West 58
2010 12 East 55
2010 12 West 63
2011 1 East 51
2011 1 West 54
2011 2 East 55
2011 2 West 61
We can create a [SalesTotals] query to combine the monthly sales totals with the yearly totals...
SELECT SalesYear & "-" & Format(SalesMonth, "00") AS SalesPeriod, Region, SalesTotal FROM SalesData
UNION ALL
SELECT SalesYear & "-Total", Region, SUM(SalesTotal) FROM SalesData GROUP BY SalesYear, Region;
...which produces
SalesPeriod Region SalesTotal
----------- ------ ----------
2010-11 East 45
2010-11 West 58
2010-12 East 55
2010-12 West 63
2011-01 East 51
2011-01 West 54
2011-02 East 55
2011-02 West 61
2010-Total East 100
2010-Total West 121
2011-Total East 106
2011-Total West 115
Then we can do our crosstab query on the [SalesTotals] query...
TRANSFORM Sum(SalesTotals.[SalesTotal]) AS SumOfSalesTotal
SELECT SalesTotals.[Region]
FROM SalesTotals
GROUP BY SalesTotals.[Region]
PIVOT SalesTotals.[SalesPeriod];
...which produces
Region 2010-11 2010-12 2010-Total 2011-01 2011-02 2011-Total
------ ------- ------- ---------- ------- ------- ----------
East 45 55 100 51 55 106
West 58 63 121 54 61 115