i have to autonumer fields with same date an i honestly have no idea how to :(
(what i have)
ID|| DATE || PID
1 || 2014 01 01 || 1
2 || 2014 01 01 || 2
3 || 2014 01 02 || 3
4 || 2014 01 02 || 4
(what i need)
ID|| DATE || PID
1 || 2014 01 01 || 1
2 || 2014 01 01 || 2
3 || 2014 01 02 || 1
4 || 2014 01 02 || 2
If you want to add a new record and If ID is auto increment:
INSERT INTO table_name(DATE, PID)
VALUES ( [YOUR_DATE], (SELECT MAX(PID) from table_name where DATE=[YOUR_DATE]) + 1)
Change [YOUR_DATE] with the date you want to insert
Related
Data for every month is there till December. I want to get a new column as "sum" , which will sum till the previous month. Means, now it is august. So, the sum should be placed till july as shown in first entry in "Sum" column below.
Jan| Feb| Mar| Apr| May| Jun |Jul |Aug |Sum
21 | 28 | 26 | 31 | 54 | 67 |38 |29 |265
11 | 44 | 66 | 7 | 88 | 54 |90 |74 |
13 | 45 | 26 | 38 | 36 | 39 |67 |49 |
76 | 35 | 67 | 23 | 76 | 54 |35 |59 |
Since months are closed set, I prefer this simple solution:
SELECT *,
CASE WHEN DATEPART(MONTH, #date)<=1 THEN 0 ELSE Jan END +
CASE WHEN DATEPART(MONTH, #date)<=2 THEN 0 ELSE Feb END +
CASE WHEN DATEPART(MONTH, #date)<=3 THEN 0 ELSE Mar END +
CASE WHEN DATEPART(MONTH, #date)<=4 THEN 0 ELSE Apr END +
CASE WHEN DATEPART(MONTH, #date)<=5 THEN 0 ELSE May END +
CASE WHEN DATEPART(MONTH, #date)<=6 THEN 0 ELSE Jun END +
CASE WHEN DATEPART(MONTH, #date)<=7 THEN 0 ELSE Jul END +
CASE WHEN DATEPART(MONTH, #date)<=8 THEN 0 ELSE Aug END +
CASE WHEN DATEPART(MONTH, #date)<=9 THEN 0 ELSE Sep END Sum
--Append 3 more months to DECEMBER
FROM Months
SELECT
Jan,Feb,Mar,Apr, and so on...
,COALESCE(Jan,0) + COALESCE(Feb,0) + COALESCE(Mar,0) + COALESCE(Apr,0)..(till Dec) AS "Sum"
FROM MyTable
IF a row entrie is NULL the SUM will be NULL, therefor is the COALESCE-Function which will turn a NULL into ZERO
DECLARE #DATE date
SET #date = GETDATE()
SELECT LEFT(DATENAME(MM, #DATE), 3)
I have a database table WysTeacherattendance -table fields are(id t_adate t_amonth t_ayear t_attendance )
id t_adate t_amonth t_ayear t_attendance
1 15 07 2015 1
2 15 07 2015 1
3 15 07 2015 1
4 15 07 2015 1
5 15 07 2015 1
6 15 07 2015 1
7 15 07 2015 1
8 15 07 2015 1
9 15 07 2015 1
10 15 07 2015 1
i have 10 teachers and iam enter all attendances are 1, insert into database t_attendance is 1 for all 10 rows.
when im enter submit button values go to database and redirect to another page ,before check all t_attendance is 1 or not .
1st condition
if its all 1 go to home page.
id t_adate t_amonth t_ayear t_attendance
1 15 07 2015 1
2 15 07 2015 1
3 15 07 2015 1
4 15 07 2015 0
5 15 07 2015 1
6 15 07 2015 1
7 15 07 2015 0
8 15 07 2015 1
9 15 07 2015 1
10 15 07 2015 1
2nd condition
if any t_attendance value is 0 then go to attendance page.
how to check these 2 conditions.
iam us its controller code
foreach ($teachers as $teacher){
$teacheratt = new WysTeacherattendance();
$teacheratt->t_auserid = $teacher->user_id;
$teacheratt->t_adate=$date_exploded[0];
$teacheratt->t_amonth=$date_exploded[1];
$teacheratt->t_ayear=$date_exploded[2];
$teacheratt->t_attendance=Input::get($teacher->user_id);
$teacheratt ->save();
}
if($users = DB::table('wys_teacherattendances')
->where('t_adate',$date_exploded[0])
->whereIn('t_attendance', array(1))->get())
{return Redirect::route('GetTeachersearchAttendence')
->with('success',' teacher exist');
}
if($users = DB::table('wys_teacherattendances')
->where('t_adate',$date_exploded[0])
->where('t_attendance', '=',0)->get())
{return Redirect::route('AddTeacherabsentReason',$date_exploded[0])
->with('success',' teacher exist');
}
}`
but only work this condition `if($users = DB::table('wys_teacherattendances')
->where('t_adate',$date_exploded[0])
->whereIn('t_attendance', array(1))->get())
{return Redirect::route('GetTeachersearchAttendence')
->with('success',' teacher exist');
}
how solve it??
try this
if($users == DB::table('wys_teacherattendances')
->where('t_adate',$date_exploded[0])
->whereIn('t_attendance', array(1))->get())
{return Redirect::route('GetTeachersearchAttendence')
->with('success',' teacher exist');
}
for compare result, you must use ==, not = (= assigning value, not checked the value)
I think you must change all "=" you have typed with "=="
I only give the idea
You can check the number of that teacher
$teacher_number = "SELECT COUNT(teacher) from teacher"
You can check the number of that attendance
$attendance = "SELECT COUNT(attendance) from attendance where
attendance = 1 and date = yourdate"
if $teacher_number == $attendance
redirect home.page
else if $teacher_number > $attendance
redirect attendance.page
So, this is the challenge:
I have two tables:
Etalon:
+-----+-----+-----+-----+----+
| e1 | e2 | e3 | e4 | e5 |
+-----+-----+-----+-----+----+
| 01 | 02 | 03 | 04 | 05 |
+-----+-----+-----+-----+----+
And Candidates:
+-----+----+-----+-----+-----+----+----+
| ID | c1 | c2 | c3 | c4 | c5 | nn |
+-----+----+-----+-----+-----+----+----+
| 00 | 03 | 08 | 02 | 01 | 06 | ** |
+-----+----+-----+-----+-----+----+----+
| 01 | 05 | 04 | 03 | 02 | 01 | ** |
+-----+----+-----+-----+-----+----+----+
| 02 | 06 | 07 | 08 | 09 | 10 | ** |
+-----+----+-----+-----+-----+----+----+
| 03 | 08 | 06 | 09 | 02 | 07 | ** |
+-----+----+-----+-----+-----+----+----+
What request should I use, to find and save (in nn column) the number of matches between two rows (e1, e2, e3, e4, e5 and c1, c2, c3, c4, c5) for each row in table candidate?
Should be the next result:
Candidates:
|-----|----|-----|-----|-----|-----|----|
| ID | c1 | c2 | c3 | c4 | c5 | nn |
|-----|----|-----|-----|-----|-----|----|
| 00 | 03 | 08 | 02 | 01 | 06 | 03 |
|-----|----|-----|-----|-----|-----|----|
| 01 | 05 | 04 | 03 | 02 | 01 | 05 |
|-----|----|-----|-----|-----|-----|----|
| 02 | 06 | 07 | 08 | 09 | 10 | 00 |
|-----|----|-----|-----|-----|-----|----|
| 03 | 08 | 06 | 09 | 02 | 07 | 01 |
|-----|----|-----|-----|-----|-----|----|
The result for nn is:
0 - no matches
1,2,3,4,5 - numbers of matches
How can I achieve that?
The objective is to establish a maximal partial matching between the master row and each row of the client table without regard to the respective column identities.
The idea is to abstract away from the column ids by representing the column contents in another way. As you indicated that the value domain is {1, ..., 10}, one may choose the first 10 prime numbers {p_1, ...,p_10} = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }, mapping i to p_i. The comparisons will be based on the product of the mapped column values. This approach exploits the uniqueness of prime factorization, ie. every positive integer factorizes into a unique multi-set of prime numbers.
A one-pass standalone sql update statement is rather cumbersome to write down, therefore we create a temporary table that contains the products of the mapped values:
CREATE TEMPORARY TABLE t_pp (
id NUMBER
, mp_candidates NUMBER
, mp_etalon NUMBER
, nn NUMBER
);
INSERT INTO t_pp ( id, mp_candidates, mp_etalon )
SELECT id
, CASE c1
WHEN 1 THEN 2
WHEN 2 THEN 3
WHEN 3 THEN 5
WHEN 4 THEN 7
WHEN 5 THEN 11
WHEN 6 THEN 13
WHEN 7 THEN 17
WHEN 8 THEN 19
WHEN 9 THEN 23
WHEN 10 THEN 29
ELSE 31
END
* CASE c2 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
* CASE c3 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
* CASE c4 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
* CASE c5 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
mp_candidates
, CASE e1
WHEN 1 THEN 2
WHEN 2 THEN 3
WHEN 3 THEN 5
WHEN 4 THEN 7
WHEN 5 THEN 11
WHEN 6 THEN 13
WHEN 7 THEN 17
WHEN 8 THEN 19
WHEN 9 THEN 23
WHEN 10 THEN 29
ELSE 31
END
* CASE e2 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
* CASE e3 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
* CASE e4 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
* CASE e5 WHEN 2 THEN 3 WHEN 3 THEN 5 WHEN 4 THEN 7 WHEN 5 THEN 11 WHEN 6 THEN 13 WHEN 7 THEN 17 WHEN 8 THEN 19 WHEN 9 THEN 23 WHEN 10 THEN 29 ELSE 31 END
mp_etalon
, 0 nn
FROM candidates
CROSS JOIN etalon
;
Now for pass #2 - counting matches:
UPDATE t_pp
SET nn =
CASE WHEN mp_candidates MOD 2 = 0 AND mp_etalon MOD 2 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 3 = 0 AND mp_etalon MOD 3 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 5 = 0 AND mp_etalon MOD 5 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 7 = 0 AND mp_etalon MOD 7 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 11 = 0 AND mp_etalon MOD 11 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 13 = 0 AND mp_etalon MOD 13 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 17 = 0 AND mp_etalon MOD 17 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 19 = 0 AND mp_etalon MOD 19 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 23 = 0 AND mp_etalon MOD 23 = 0 THEN 1 ELSE 0 END
+ CASE WHEN mp_candidates MOD 29 = 0 AND mp_etalon MOD 29 = 0 THEN 1 ELSE 0 END
;
Finally, transferring the results to the original table and cleaning up:
UPDATE candidates c
set nn = ( SELECT p.nn FROM t_pp p WHERE p.id = c.id )
;
DELETE TEMPORARY TABLE t_pp;
Some more notes:
The scheme as shown assumes that cell values are unique within each row. However, it can easily be extended to allow formultiple occurrences of values.
In principle, this can be wrapped in a single sql statement - for obvious reasons this is not recommended.
Rdbms other than mysql follow the sql standard and provide the WITH clause that obviates the need for a temporaray table.
The value 31 in the ELSE branch of the above CASE expressions is a dummy value.
I have the following tables:
http://sqlfiddle.com/#!2/1e991/1
But I have a problem! The output of my query is:
DATE SLOT sum(SUCCESSFUL) SUCCESSFUL PERCENTAGE OTA_NAME USERS_SI
July, 02 2013 00:00:00+0000 2 120 120 41.6667 campana 2 50
July, 02 2013 00:00:00+0000 1 200 200 25 campana 2 50
July, 02 2013 00:00:00+0000 1 150 150 53.3333 campana 3 80
July, 01 2013 00:00:00+0000 2 100 100 20 campana 1 20
July, 01 2013 00:00:00+0000 3 440 440 4.5455 campana 1 20
July, 01 2013 00:00:00+0000 1 700 700 2.8571 campana 1 20
And I need a sum of successful with the same date and campaign, for example, I have in July 1 with campaign 1 three rows, then I need sum the successful of the three rows, and with campaign 2 two rows, then I need sum the successful of the two rows, and with campaign 3 one rows, then I need sum the successful of the one row that is the same.
And finally the percentage is the division between sum(SUCCESSFUL) and USERS_SI
The output that I need is:
DATE SLOT sum(SUCCESSFUL) SUCCESSFUL PERCENTAGE OTA_NAME USERS_SI
July, 02 2013 00:00:00+0000 2 320 120 41.6667 campana 2 50
July, 02 2013 00:00:00+0000 1 320 200 25 campana 2 50
July, 02 2013 00:00:00+0000 1 150 150 53.3333 campana 3 80
July, 01 2013 00:00:00+0000 2 1240 100 20 campana 1 20
July, 01 2013 00:00:00+0000 3 1240 440 4.5455 campana 1 20
July, 01 2013 00:00:00+0000 1 1240 700 2.8571 campana 1 20
Can you help me?
sqlfiddle here
SELECT other.date AS date,
other.slot,
other.successful,
calc.sum_successful,
max((case when (rule.tree_si = dms.tree) then dms.numberResponses end))/successful AS percentage,
other.name AS ota_name,
other.successful AS successful,
max((case when (rule.tree_si = dms.tree) then dms.numberResponses end)) AS users_si
FROM aca_ota_other other
join aca_dms_rules rule on other.name = rule.ota_name
join aca_dms dms on dms.date = other.date and rule.tree_si = dms.tree
join (SELECT name, sum(successful) as sum_successful FROM aca_ota_other GROUP BY name) as calc on other.name = calc.name
GROUP BY
other.date,
other.name,
other.successful
ORDER BY other.date desc
I'm not sure but if i understood well, you need something like this:
I'm sorry, I've made some little changes in your query.
SELECT other.date date,
other.slot,
other.name ota_name,
sum(successful) successful,
dms.numberResponses users_si,
SUM(successful)/dms.numberResponses AS percentage
FROM aca_ota_other other
JOIN aca_dms_rules rule ON other.name=rule.ota_name
JOIN aca_dms dms ON dms.date=other.date AND rule.tree_si=dms.tree
GROUP BY other.date,
other.name
ORDER BY other.date DESC
http://sqlfiddle.com/#!2/1e991/22
I have a query
SELECT ckt, setpt, clock FROM progs
WHERE feed = "80302" AND day=4 AND clock<"12:15:00"
ORDER BY ckt, clock DESC
That gets me this:
ckt setpt clock
0 69 06:06:00
0 62 00:30:00
1 57 10:30:00
1 67 04:30:00
1 57 01:30:00
2 69 11:00:00
2 62 00:30:00
How could I modify this query to give me the MAX(clock) for each CKT
In total:
Each cktand day has a number of setpt and clock entries. I am looking for the (1 or none) clock record < some_time for each ckt and day. ie
0 69 06:06:00
1 67 10:30:00
2 69 11:00:00
in mysql.
Something like this?
SELECT ckt, MAX(setpt), MAX(clock)
FROM progs
WHERE feed = "80302" AND day=4 AND clock<"12:15:00"
GROUP BY ckt
ORDER BY ckt, clock DESC
Result
| CKT | MAX(SETPT) | MAX(CLOCK) |
-----------------------------------------------------
| 0 | 69 | January, 01 1970 06:06:00+0000 |
| 1 | 67 | January, 01 1970 10:30:00+0000 |
| 2 | 69 | January, 01 1970 11:00:00+0000 |
See the demo
Sounds like you need to JOIN the table to itself, joining on the MAX(clock):
SELECT p.ckt, p.setpt, p.clock
FROM progs p
JOIN (
SELECT MAX(clock) maxClock, ckt
FROM progs
WHERE feed = "80302"
AND day=4
AND clock<"12:15:00"
GROUP BY ckt
) p2 on p.ckt = p2.ckt AND p.clock = p2.maxclock
ORDER BY p.ckt, p.clock DESC
SQL Fiddle Demo (borrowed from other post)
Producing:
0 69 January, 01 1970 06:06:00+0000
1 57 January, 01 1970 10:30:00+0000
2 69 January, 01 1970 11:00:00+0000
Please note row with id 1 is different than your desired results, but match your question's desired results...