Performing an operation within EACH GROUP of rows - mysql

I have a table like below.
+------------+------------------+-------------------------------+-------------+
| day | workflow_step_id | UNIX_TIMESTAMP(finished_time) | workflow_id |
+------------+------------------+-------------------------------+-------------+
| 2014-04-30 | 1 | 1398852780 | 1 |
| 2014-04-30 | 17 | 1398871213 | 2 |
| 2014-04-30 | 6 | 1398872807 | 1 |
| 2014-04-30 | 22 | 1398898676 | 2 |
| 2014-04-30 | 11 | 1398898234 | 1 |
| 2014-04-30 | 16 | 1398866813 | 2 |
| 2014-04-30 | 5 | 1398869940 | 1 |
| 2014-04-30 | 21 | 1398893419 | 2 |
| 2014-04-30 | 10 | 1398894136 | 1 |
| 2014-04-30 | 15 | 1398861385 | 2 |
| 2014-04-30 | 4 | 1398860271 | 1 |
| 2014-04-30 | 20 | 1398888252 | 2 |
| 2014-04-30 | 9 | 1398886916 | 1 |
| 2014-04-30 | 14 | 1398863922 | 2 |
| 2014-04-30 | 3 | 1398865682 | 1 |
| 2014-04-30 | 19 | 1398881994 | 2 |
| 2014-04-30 | 8 | 1398882497 | 1 |
| 2014-04-30 | 13 | 1398852785 | 2 |
| 2014-04-30 | 2 | 1398856674 | 1 |
| 2014-04-30 | 18 | 1398878836 | 2 |
| 2014-04-30 | 7 | 1398878949 | 1 |
| 2014-04-30 | 12 | 1398850920 | 2 |
| 2014-05-01 | 12 | 1398932040 | 2 |
| 2014-05-01 | 1 | 1398938880 | 1 |
| 2014-05-01 | 17 | 1398957830 | 2 |
| 2014-05-01 | 6 | 1398961385 | 1 |
| 2014-05-01 | 22 | 1398996388 | 2 |
| 2014-05-01 | 11 | 1398994543 | 1 |
| 2014-05-01 | 16 | 1398946714 | 2 |
| 2014-05-01 | 5 | 1398959182 | 1 |
| 2014-05-01 | 21 | 1398989773 | 2 |
| 2014-05-01 | 10 | 1398979568 | 1 |
| 2014-05-01 | 15 | 1398949939 | 2 |
| 2014-05-01 | 4 | 1398953732 | 1 |
| 2014-05-01 | 20 | 1398980246 | 2 |
| 2014-05-01 | 9 | 1398971281 | 1 |
| 2014-05-01 | 14 | 1398940775 | 2 |
| 2014-05-01 | 3 | 1398944207 | 1 |
| 2014-05-01 | 19 | 1398970295 | 2 |
| 2014-05-01 | 8 | 1398967778 | 1 |
| 2014-05-01 | 13 | 1398940935 | 2 |
| 2014-05-01 | 2 | 1398947433 | 1 |
| 2014-05-01 | 18 | 1398966959 | 2 |
| 2014-05-01 | 7 | 1398965931 | 1 |
-------------------------------------------------------------------------------
command:
SELECT day,
stats.workflow_step_id,
Max(Unix_timestamp(finished_time)) - Min(Unix_timestamp(finished_time)),
workflow_id
FROM modeling_dashboard_workflow_stats stats
INNER JOIN modeling_dashboard_workflow_step step
ON stats.workflow_step_id = step.workflow_step_id
ORDER BY day;
In this example only has two days and two workflow_ids, but it can have any number of days or workflow_ids.
I want to calculate the difference between maximum timestamp and minimum timestamp for each workflow_id for each day.
It should look something like this.
+------------+-------------------------------------------------------------+
| day | MAX(timestamp) - MIN(timestamp) |
+------------+-------------------------------------------------------------+
| 2014-04-30 | difference bw max&min timestamp with workflow_id=1 in 04-30 |
| 2014-04-30 | difference bw max&min timestamp with workflow_id=2 in 04-30 |
| 2014-05-01 | difference bw max&min timestamp with workflow_id=1 in 05-01 |
| 2014-05-01 | difference bw max&min timestamp with workflow_id=2 in 05-01 |
----------------------------------------------------------------------------
How can I do this?
Ideally it puts results for workflow_id = 1 and those for workflow_id = 2 in different columns, (day, MAX-MIN for id=1, MAX-MIN for id=2), but that's just my next step.

SELECT day, workflow_id
, MAX(UNIX_TIMESTAMP(finished_time)) - MAX(UNIX_TIMESTAMP(finished_time))
FROM modeling_dashboard_workflow_stats
GROUP BY day, workflow_id

try this:
select day
,workflow_step_id
,MAX(UNIX_TIMESTAMP(finished_time)) - MIN(UNIX_TIMESTAMP(finished_time))
from modeling_dashboard_workflow_stats
group by day
,workflow_step_id

Related

I want to retrieve which scheduler is scheduled for a particular day

+-----+--------------------------------------------------+--------+--------------------+---------------+---------------+---------------+--------------------------------------+-------------------------+-------------------+--------------------------------------------------------------------------------------------------------+--------------------------+--------+----------------+--------------------------------------+---------+
| ID | Name | Action | Created By Use Rid | Created Date | Days | Enterprise ID | Job ID | Last Updated By Use Rid | Last Updated Date | Relays | Sch Time | Status | Timezone | Trigger ID | User ID |
+-----+--------------------------------------------------+--------+--------------------+---------------+---------------+---------------+--------------------------------------+-------------------------+-------------------+--------------------------------------------------------------------------------------------------------+--------------------------+--------+----------------+--------------------------------------+---------+
| 30 | ios | 1 | 2 | 1559739565592 | | 2 | ef29baba-534d-4151-80d3-f5a589cf7c22 | 2 | 1559739565592 | 74 | 2019-06-05T13:04:00.000Z | 2 | Asia/Kolkata | 0492dc45-6671-4064-baef-8654761b67f2 | 2 |
| 31 | iot | 0 | 2 | 1559739617863 | 1,2,3,4,5,6,7 | 2 | 85e4a611-37dc-45d3-8a2f-243e6d4e0962 | 2 | 1575869484168 | 508,509,504,505,506,507 | 2019-12-09T13:50:00.000Z | 0 | Asia/Kolkata | b8a71ee4-94bd-4d2b-b875-7ac1468fad11 | 2 |
| 32 | Daily-8:50 Pm | 1 | 2 | 1559747777019 | | 2 | 3f87f532-6289-4016-a6d4-2a76baf4ffb5 | 2 | 1559749199040 | 45,46,47,48,50,51,52,61,62,63,64,65,302,303,73,74,75,76,77,53,54,55,56,57,58,59,60,49,66,67,68,304,305 | 2019-06-05T15:41:00.000Z | 2 | Asia/Kolkata | 6f1b873b-1e96-47bd-b723-4f50d5534f89 | 2 |
| 33 | TVOffSchedular | 0 | 2 | 1559933346322 | 1,2,3,4,5,6,7 | 4 | 1935f0cf-cdf9-4bdf-895d-9e6dc6c4ddff | 2 | 1560453392696 | 321,325 | 2019-06-13T19:20:00.000Z | 0 | Asia/Kolkata | c6bf383d-7bd7-4364-b077-e8bb3ad857f7 | 2 |
| 34 | TVOnMorning | 1 | 2 | 1559933392595 | 1,2,3,4,5,6,7 | 4 | d90d5c17-46b9-46cc-bb33-07557bf97684 | 2 | 1559933392595 | 321,325 | 1970-01-01T00:00:00.000Z | 2 | Asia/Kolkata | a131188d-aa7d-4132-995e-035e172566a7 | 2 |
| 35 | MorningOfficeON | 1 | 2 | 1560004379991 | 1,2,3,4,5,6 | 2 | 8b9163e7-bdea-4ee3-b827-3ab3d4728a0c | 2 | 1561881126773 | 49 | 2019-06-08T02:30:00.000Z | 0 | Asia/Kolkata | 926e809f-b1fb-4d3c-8da7-b048c0366a3a | 2 |
| 36 | EveningOfficeOFF | 0 | 2 | 1560005726195 | 1,2,3,4,5,6 | 2 | a7161d66-9ae4-4eb9-bc6e-ded6bd01a766 | 2 | 1560261033555 | 49 | 2019-06-11T14:00:00.000Z | 0 | Asia/Kolkata | dc45718f-aad4-4eb2-9b94-8a8bc4444385 | 2 |
| 37 | Good night | 1 | 3 | 1560088585521 | 1,2,3,4,5,6,7 | 3 | 4e8eedd1-956d-4922-9715-8f9b7bf221cb | 3 | 1560088585521 | 253,363 | 1970-01-01T14:00:00.000Z | 2 | Asia/Kolkata | 39a8fb76-06d6-4fdd-8c1f-827c3c1e2c24 | 3 |
| 38 | Good night off | 0 | 3 | 1560088719173 | 1,2,3,4,5,6,7 | 3 | c0a9dfdd-7b1b-4a9e-a9f1-b31d927d995e | 3 | 1560088719173 | 253,363 | 1969-12-31T19:30:00.000Z | 2 | Asia/Kolkata | 61cfacdd-f2c8-46c9-b192-228aef33cc59 | 3 |
| 39 | ApplicationON | 1 | 2 | 1560140790669 | 1,2,3,4,5,6,7 | 2 | 8f602cc4-8fe7-416a-b563-9b25cd008e5b | 2 | 1560141163056 | 45,46,47,48,50,51,52 | 2019-06-10T04:35:00.000Z | 0 | Asia/Kolkata | 5bab6ae0-7c7b-487c-9e72-d6200e0985a2 | 2 |
| 40 | ApplicationMorningOFF | 0 | 2 | 1560140849363 | 1,2,3,4,5,6,7 | 2 | 7d34c4f1-3638-4355-a14c-622bf733e23e | 2 | 1560141176600 | 45,46,47,48,50,51,52 | 2019-06-10T04:40:00.000Z | 0 | Asia/Kolkata | 4cd78892-c2b2-493e-a839-b270eff5aea5 | 2 |
| 41 | DesignNoonOFF | 0 | 2 | 1560241956756 | 1,2,3,4,5,6,7 | 2 | 00d3437b-a062-4f32-bb37-200574cac235 | 2 | 1567674559401 | 512,513,514,515 | 2019-09-05T08:15:00.000Z | 0 | Asia/Kolkata | b10d1ded-60a3-4adf-ba6c-743ec3dc4f29 | 2 |
| 42 | DesignNoonON | 1 | 2 | 1560241995428 | 1,2,3,4,5,6,7 | 2 | f3e3807c-7fad-4b08-bb18-439222f8d454 | 2 | 1567674587127 | 512,513,514,515 | 2019-09-05T08:30:00.000Z | 0 | Asia/Kolkata | c53e9069-164e-430a-89be-913c8127a767 | 2 |
| 43 | blockchain-test | 1 | 2 | 1560255684868 | 1,2,3,4,5,6,7 | 2 | 4ac65798-682e-447d-b6cf-813308f05efe | 2 | 1560257490068 | 45,46,47,48,50,51,52,61,62,63,64,65,302,73,74,75,76,53,54,57,58,59,60 | 2019-06-11T12:52:00.000Z | 2 | Asia/Calcutta | 8e7640ea-0223-4a9b-abdc-73c139f8e6fa | 2 |
| 44 | NightLampON | 1 | 2 | 1560360082007 | 1,2,3,4,5,6,7 | 4 | b5aee8d5-8013-4303-88df-6a5e3bc7330e | 2 | 1560360082007 | 322 | 1970-01-01T17:25:00.000Z | 0 | Asia/Kolkata | d0f3a1b5-93f3-4858-be5f-4837e62dd9bf | 2 |
| 45 | blockchain-off | 1 | 2 | 1560432958716 | 1,2,3,4,5,6,7 | 2 | a3d89bb2-3710-4f8e-9814-1e6942d1a405 | 2 | 1560432958716 | 53,54,57,59,60 | 2019-06-13T13:45:00.000Z | 0 | Asia/Calcutta | a0ccdcb6-a55e-48e2-9391-60d62e10fe4a | 2 |
| 46 | AllLightOffExceptOfficeMaster | 0 | 2 | 1560524588879 | | 2 | 64939311-522e-4877-b53f-e44ff78200b7 | 2 | 1560524588879 | 45,46,47,48,50,51,52,61,62,63,64,302,303,73,74,75,76,77,53,54,55,56,57,58,59,60 | 2019-06-14T15:10:00.000Z | 0 | Asia/Kolkata | 4a799d93-877c-4d19-b19a-40b6b9767564 | 2 |
| 47 | NightLampOFF | 0 | 2 | 1560533195543 | 1,2,3,4,5,6,7 | 4 | c844b235-3851-4692-ab14-4ee31b5d9404 | 2 | 1560620178500 | 322 | 2019-06-15T17:40:00.000Z | 0 | Asia/Kolkata | a48c0a99-76cd-4268-b946-8d63408aa234 | 2 |
| 48 | TVON | 1 | 2 | 1561124197968 | 1,2,3,4,5,6,7 | 4 | b0617f91-54b2-48bc-9cf9-2f42fb1526c4 | 2 | 1571035693157 | 321,325 | 2019-06-20T23:30:00.000Z | 0 | Asia/Kolkata | 93d07355-1c95-4faa-ac5c-73b0c68b5323 | 2 |
| 49 | panel light/Inside logo on | 1 | 3 | 1561492261954 | 1,2,3,4,5,6 | 3 | 736609ff-6391-474a-9556-85eff54f2fd8 | 3 | 1561492261954 | 359,360,361,364 | 2019-06-26T14:00:00.000Z | 2 | Asia/Kolkata | 84de41a7-a141-49d3-8bf2-8a9a229a8c56 | 3 |
| 50 | Outside Board/panel/focus light ON | 1 | 3 | 1562096267147 | 1,2,3,4,5,6,7 | 3 | 37d4f238-15b3-4a1a-bac1-a050d61792f7 | 3 | 1572442799792 | 894 | 1970-01-01T13:45:00.000Z | 2 | Asia/Kolkata | 3610c4b9-6180-4640-a078-3e310d80b4dd | 3 |
| 51 | Outside Board/Panel/Focus light OFF | 0 | 3 | 1562096428205 | 1,2,3,4,5,6,7 | 3 | 7ef0d450-39f9-4449-beec-c46416c3ebb2 | 3 | 1572442815806 | 894 | 2019-10-30T16:30:00.000Z | 2 | Asia/Kolkata | 8d9fe67d-7d39-4edb-8191-ea8af176b5d7 | 3 |
| 52 | Inside Logo/Panel/Display/Ganpati/Entry light ON | 1 | 3 | 1562096734383 | 1,2,3,4,5,6 | 3 | 4b6bab36-889a-45ca-969c-a1cecea1f42f | 3 | 1562096734383 | 110,111,359,360,361,364 | 2019-07-03T13:30:00.000Z | 2 | Asia/Kolkata | c08cc35a-8e64-4d17-a673-4bfddb2f1c92 | 3 |
| 53 | Inside Logo/Panel/Display/Ganpati/Entrance OFF | 0 | 3 | 1562097007454 | 1,2,3,4,5,6 | 3 | 54de42a4-f28e-494a-9ec3-56e22e7956e9 | 3 | 1562257875176 | 110,111,359,360,361,364 | 2019-07-03T16:30:00.000Z | 2 | Asia/Kolkata | ffbf0fcd-e6ae-41ba-8873-0d2bbd5d3cd0 | 3 |
| 54 | EveningOfficeON | 1 | 2 | 1562852792955 | | 2 | b4c3b59f-8157-4260-b0c0-a76b739e857b | 2 | 1562852792955 | 45,46,47,48,61,62,63,64,49 | 2019-07-11T14:01:00.000Z | 0 | Asia/Kolkata | 4be1a145-8e21-405e-990d-f85d0c3657bf | 2 |
| 55 | WaterCoolerON | 1 | 2 | 1564217226090 | 1,2,3,4,5,6,7 | 2 | 1e9c429f-9a51-4147-815b-efad95330baa | 2 | 1564217226090 | 489 | 1970-01-01T00:30:00.000Z | 0 | Asia/Kolkata | 4b9413ae-6d13-4117-bc8d-36f02c462b05 | 2 |
| 56 | WaterCoolerOff | 0 | 2 | 1564217269330 | 1,2,3,4,5,6,7 | 2 | 0f2c1112-dd26-43eb-8390-a1bba53789de | 2 | 1564217269330 | 489 | 1970-01-01T14:30:00.000Z | 0 | Asia/Kolkata | 907e4ee5-367a-4b4b-bc6b-716eeed4c8b8 | 2 |
| 57 | Test 1 | 1 | 28 | 1567236412976 | | 6 | 2c8276b3-a946-475c-a0e9-7e55569cadae | 28 | 1567236412976 | 553 | 2019-08-31T07:28:00.000Z | 2 | IST | dc1fec52-8c98-4832-b01a-a3a91f0b6058 | 28 |
| 58 | Test 3 | 1 | 28 | 1567236499938 | | 6 | fc820b65-a04c-4ecf-9876-2bd4bb8a8816 | 28 | 1567237094910 | 556,557 | 2019-08-31T07:39:00.000Z | 2 | IST | ceca92e5-765c-4f54-97ba-77447147276f | 28 |
| 59 | Test 1.1 | 1 | 28 | 1567236608884 | | 6 | 985e07c0-3a53-4884-a169-19e8a9ae630b | 28 | 1567236608884 | 553 | 2019-08-31T07:32:00.000Z | 2 | IST | f1a18b94-ef35-46a7-afbe-c75e743185fe | 28 |
| 60 | Outside C&C Ledlight On | 1 | 3 | 1570365557312 | 1,2,3,4,5,6,7 | 3 | ad17d61e-549e-44f1-99db-b50105d069b3 | 3 | 1574947645835 | 893 | 2019-11-28T12:30:00.000Z | 0 | Asia/Kolkata | 82b0fc02-c98c-4b12-9d8a-3ba2897d1aa7 | 3 |
| 61 | test | 0 | 2 | 1570532481972 | 1 | 2 | 2d058c32-89f8-4709-ba77-cca2c677334d | 2 | 1570532481972 | 524,525,526,527,529,530,531 | 2019-10-08T11:00:00.000Z | 2 | Africa/Lusaka | 722aece6-39d5-44be-bace-967180cd449f | 2 |
| 62 | Outside C&C Ledlight Off | 0 | 3 | 1570647085459 | 1,2,3,4,5,6,7 | 3 | 24c4831d-3491-4fa9-a8e2-dab21a94aa93 | 3 | 1572636959040 | 893 | 2019-10-29T19:00:00.000Z | 0 | Asia/Kolkata | 41c21970-faf8-4699-9a97-a49e1b28f3ce | 3 |
| 63 | bedroom allout 1 | 1 | 2 | 1571346220354 | 1,2,3,4,5,6,7 | 7 | 9ba4ec5b-c645-4cf6-bede-59fa79073134 | 2 | 1574015422525 | 662 | 1970-01-01T17:03:00.000Z | 0 | Asia/Calcutta | 4aff33ae-6af6-45f3-8407-f67a6f344e4b | 2 |
| 64 | bedroom allout off | 0 | 2 | 1571346263753 | 1,2,3,4,5,6,7 | 7 | 8cd3c6ac-0ea6-49b9-af09-10e9dcb3847b | 2 | 1571346263753 | 662 | 1970-01-01T01:04:00.000Z | 0 | Asia/Calcutta | 4ecfcf3f-110c-44b2-9a8d-3f35f1d5a295 | 2 |
| 65 | PIR Enable | 1 | 25 | 1571649294987 | 1,2,3,4,5,6,7 | 9 | c142b8f1-77d6-46a2-8f45-46ad3770a3c4 | 25 | 1571661763643 | 756 | 2019-10-21T13:00:00.000Z | 2 | Asia/Kolkata | 05fc4108-f65a-449f-a211-10e76e080196 | 25 |
| 66 | PIR Disable | 0 | 25 | 1571649351212 | 1,2,3,4,5,6,7 | 9 | 894ef781-cb8f-4139-907f-5d20fc5a0861 | 25 | 1571649351212 | 756 | 2019-10-21T01:30:00.000Z | 2 | Asia/Kolkata | 0f927aca-f592-44bb-9a7f-67544aa32223 | 25 |
| 67 | testing 22 | 1 | 2 | 1571734869588 | | 7 | 2fe74039-d25e-4384-bc4a-dd63843de063 | 2 | 1574015556036 | 639 | 2019-11-20T09:03:00.000Z | 0 | Asia/Calcutta | 86cfd858-367a-4a0b-b7eb-35e1096b56f4 | 2 |
| 68 | mobile charger | 0 | 23 | 1572199190164 | 1,2,3,4,5,6,7 | 11 | 7d536ef8-d24a-4a53-94c0-6c52a441312a | 23 | 1575485933026 | 773,774 | 2019-12-04T21:00:00.000Z | 0 | Asia/Calcutta | fc8b10b3-43a5-497c-86b9-bd55a80cfe4a | 23 |
| 69 | Outside Focus light On | 1 | 3 | 1572637078856 | 1,2,3,4,5,6,7 | 3 | 9a889394-8102-42a0-836a-43a7ffb5c7bf | 3 | 1572983697418 | 922 | 2019-11-06T12:30:00.000Z | 0 | Asia/Kolkata | 4747ca5e-736b-4b80-b7a4-9abfe33f0e77 | 3 |
| 70 | Outside Focus light Off | 0 | 3 | 1572637169312 | 1,2,3,4,5,6,7 | 3 | 1a283075-37e6-4152-a716-a8b63ef62f56 | 3 | 1572637169312 | 922 | 1969-12-31T18:35:00.000Z | 0 | Asia/Kolkata | fa79f7f6-bde4-4d87-96fd-b570ad47a57c | 3 |
| 71 | Ganpati/Display On | 1 | 3 | 1572637346879 | 2,3,4,5,6,7 | 3 | 6ae8c9ca-e26b-48b8-a3be-ef2d41b0a0d6 | 3 | 1574943671391 | 924,926,894 | 2019-11-28T12:30:00.000Z | 0 | Asia/Kolkata | 0027b2eb-efa2-41ff-ac24-50483eec4632 | 3 |
| 72 | Ganpati/Display Off | 0 | 3 | 1572637442853 | 2,3,4,5,6,7 | 3 | 0bb73aca-c82d-4459-922b-cb3592783ae9 | 3 | 1574943713949 | 924,926,894 | 2019-11-28T16:30:00.000Z | 0 | Asia/Kolkata | f8fb596e-dd42-4227-98cb-03a8dcbee9bb | 3 |
| 73 | panel/Logo/Table logo/ Light On | 1 | 3 | 1572638062814 | 2,3,4,5,6,7 | 3 | ab78a500-af55-4c8c-84a8-fdbbe76404f4 | 3 | 1572638089623 | 853,855 | 2019-11-02T12:30:00.000Z | 0 | Asia/Kolkata | 0ef71b02-744c-4755-b7dc-bc51dd727fe9 | 3 |
| 74 | all-out-off | 0 | 13 | 1572887848318 | 2,3,4,5,6 | 10 | 213f3bab-332a-4992-9d2d-7e38cf48dbfd | 13 | 1572887848318 | 838 | 2019-11-04T01:30:00.000Z | 0 | Asia/Calcutta | 70938fed-0053-443e-b302-e0f8e25826bb | 13 |
| 75 | mbroom-fan-off | 0 | 13 | 1572888838651 | 1,2,3,4,5,6,7 | 10 | ae1b74c2-2ad7-4182-9b4d-3f15812edbe8 | 13 | 1572974583661 | 834 | 2019-11-04T20:45:57.000Z | 0 | Asia/Calcutta | e87e19e4-d8ae-45b4-b575-1110d1368648 | 13 |
| 76 | PIR Enable | 1 | 25 | 1572944519175 | 1,2,3,4,5,6,7 | 9 | 6fc17694-4fc5-443e-92ee-ed1132a2ec35 | 25 | 1572944519175 | 758 | 2019-11-05T12:46:00.000Z | 0 | Asia/Kolkata | 46ea68cd-7e11-4003-bdfb-54aab6ba3923 | 25 |
| 77 | PIR Disable | 0 | 25 | 1572944690715 | 1,2,3,4,5,6,7 | 9 | ae7a333c-ff75-4c8b-bcae-92b308180b93 | 25 | 1573197064859 | 758 | 2019-11-06T00:20:00.000Z | 0 | Asia/Kolkata | 6a41e2f1-d029-4d28-9ce7-b2adebb59dab | 25 |
| 78 | test 1 | 1 | 2 | 1573129815561 | | 16 | ab60b929-48ed-43ab-9a89-45bfc0a10ee3 | 2 | 1573129815561 | 1030 | 2019-11-07T12:32:13.000Z | 0 | Asia/Kolkata | 80c7d1b2-68b4-425e-91b4-d82544d66dfc | 2 |
| 79 | pir en test | 1 | 25 | 1573196361376 | 1,2,3,4,5,6,7 | 9 | a206b1f6-f1e8-4c12-a97f-b7d217fd344a | 25 | 1573196739778 | 758 | 2019-11-08T07:08:00.000Z | 2 | Asia/Kolkata | dc23b8ee-2da0-42a3-a6d0-35a328035e12 | 25 |
| 80 | pir dbtest | 0 | 25 | 1573196399643 | 1,2,3,4,5,6,7 | 9 | 5d133d17-8495-4e34-aa6c-ea13344ddfdc | 25 | 1573196765147 | 758 | 2019-11-08T07:09:00.000Z | 2 | Asia/Kolkata | b431f3fc-f541-4912-b698-83f574c1c811 | 25 |
| 81 | living-off | 0 | 13 | 1573408027950 | | 10 | cac70a87-98b3-405f-a6da-2ac6f1c3a344 | 13 | 1573408137563 | 813 | 2019-11-10T20:30:56.000Z | 0 | Asia/Calcutta | 7b91172a-da7b-4f94-bf92-7915b51fce98 | 13 |
| 82 | ro on | 1 | 2 | 1573416862256 | 1,2,3,4,5,6,7 | 7 | 22e7e1ab-d2f3-483e-bcc5-3aa595feedb8 | 2 | 1573416862256 | 672 | 1970-01-01T00:08:00.000Z | 0 | Asia/Calcutta | 747f6396-77b7-4bab-93a2-6c6715b3e87f | 2 |
| 83 | RO off | 0 | 2 | 1573473725301 | 1,2,3,4,5,6,7 | 7 | eed1acbd-0850-4dc1-ad65-2528eb630389 | 2 | 1573473725301 | 672 | 1969-12-31T19:01:00.000Z | 0 | Asia/Calcutta | 89a511e2-52f6-45f3-9257-78b7f9f41bc2 | 2 |
| 84 | Test | 1 | 42 | 1573738374195 | | 17 | fca106ba-af83-4bae-8855-aa4d9edd4484 | 42 | 1573738590203 | 1133,1134,1135,1136 | 2019-11-14T13:38:29.000Z | 0 | Asia/Kolkata | f668dc34-db5b-4f34-a5a1-ee7649bc093d | 42 |
| 85 | entrance | 0 | 42 | 1573738946510 | | 17 | ce332a00-b3ca-45cc-9c1d-c3ead60845fd | 42 | 1573738946510 | 1133 | 2019-11-14T13:47:25.000Z | 0 | Asia/Kolkata | c7b912db-096a-4836-8b1d-0b52197922c5 | 42 |
| 86 | ronak bedroom charger | 1 | 15 | 1573827203905 | 1,2,3,4,5,6,7 | 15 | a31cc0a3-cfcd-4a57-8a72-f292893c4de9 | 15 | 1574274253149 | 1103 | 2019-11-14T23:30:00.000Z | 0 | Asia/Kolkata | 91a3e0c7-f750-42ca-84e0-af4bbb7ef839 | 15 |
| 87 | ronak bedroom plug off | 0 | 15 | 1573827259380 | 1,2,3,4,5,6,7 | 15 | 7f9e1f0f-e126-4bdd-acce-967bfb785c25 | 15 | 1574274268343 | 1103 | 2019-11-15T01:30:00.000Z | 0 | Asia/Kolkata | dc3b7742-711c-46c5-9395-38c9568d32dc | 15 |
| 88 | fan off | 0 | 15 | 1573845123332 | 1,2,3,4,5,6,7 | 15 | d5c68535-499a-436d-844f-8d0d7140d2da | 15 | 1573845123332 | 1096 | 2019-11-15T20:32:00.000Z | 0 | Asia/Kolkata | a014d04c-48dc-4ec4-a56f-90383387c241 | 15 |
| 89 | master beroom all out on | 1 | 20 | 1574014827858 | 1,2,3,4,5,6,7 | 8 | f25ae2d2-9414-418a-878b-1545a956d5fe | 20 | 1574014827858 | 700 | 2019-11-17T17:00:00.000Z | 0 | Asia/Kolkata | 3cded4f6-9740-43fa-b921-b5b9deb5bdb5 | 20 |
| 90 | master bedroom plug 4 | 0 | 20 | 1574015013470 | 1,2,3,4,5,6,7 | 8 | 1daabfe0-ccaa-4821-8658-c556541771a4 | 20 | 1574042395372 | 700 | 2019-11-18T00:30:00.000Z | 0 | Asia/Kolkata | fa0aad18-fe38-4e2f-8753-caef702ab74c | 20 |
| 91 | light on | 0 | 22 |
+-----+--------------------------------------------------+--------+--------------------+---------------+---------------+---------------+--------------------------------------+-------------------------+-------------------+--------------------------------------------------------------------------------------------------------+--------------------------+--------+----------------+--------------------------------------+---------+
In the below data the sch time needs to be compared with Days ie when the Days column is null then sch Time should be the output and when it is 1,2,3,4,5,6,7 then case when 1 then return sunday, case when 2 then return monday,case when 3 then return tues, case when 4 then return wed, case when 5 then return thur, case when 6 then return fri, case when 7 then return sat. should be the output The data should be time and date sorted decreasing
Please check the below query and put your table name at the place Your_table.
ALTER TABLE dbo.dates ADD update_stat BIT DEFAULT(0) --You can add this in current table or temp table
WHILE EXISTS (
SELECT TOP 1 update_stat
FROM Your_Table
WHERE update_stat = 0
ORDER BY Id
)
BEGIN
DECLARE #days VARCHAR(100)
DECLARE #datepart VARCHAR(10)
DECLARE #Id INT
SET #datepart = DATEPART(dw, getdate())
SET #days = (
SELECT TOP 1 days
FROM dbo.dates
WHERE days IS NOT NULL
ORDER BY ID
)
SET #ID = (
SELECT TOP 1 ID
FROM dbo.dates
WHERE days IS NOT NULL
ORDER BY ID
)
IF CHARINDEX(#datepart, #days) > 0
BEGIN
UPDATE Your_Table
SET update_stat = 1
WHERE id = #Id
END
SELECT
SELECT DATEPART(d, getdate()) AS DaysNo
,CASE DATEPART(WEEKDAY, GETDATE())
WHEN 1
THEN 'SUNDAY'
WHEN 2
THEN 'MONDAY'
WHEN 3
THEN 'TUESDAY'
WHEN 4
THEN 'WEDNESDAY'
WHEN 5
THEN 'THURSDAY'
WHEN 6
THEN 'FRIDAY'
WHEN 7
THEN 'SATURDAY'
END AS DaysName
,*
FROM Your_Table
WHERE update_stat = 1
You can insert the new column the update_stat either in the same table or you can insert all the existing table data into temp table and then add a update_stat column using below query and rest are same start from WHILE Loop.
SELECT * INTO ##tempTable
FROM Your_Table
ALTER TABLE dbo.dates ADD update_stat BIT DEFAULT(0)---rest are same..

Creating a log having the date of purchase

I need to create a log having the purchase date of an item.
Items can be owned by only one buyer at time. So, for example, if item1 was purchased by buyer2 in 2009 and after by buyer1 in 2015, then between 2009 and 2015 was owned by buyer2.
Here is my table:
+--------+------------+-----------+----------+
| id_doc | date | id_item | id_buyer |
+--------+------------+-----------+----------+
| 11 | 2016-06-07 | 1 | 4 |
| 10 | 2016-06-06 | 1 | 4 |
| 1 | 2015-11-30 | 1 | 1 |
| 9 | 2009-01-01 | 1 | 2 |
| 4 | 2001-01-12 | 1 | 2 |
| 8 | 1996-06-06 | 1 | 2 |
| 3 | 1995-05-29 | 1 | 1 |
| 2 | 1998-05-23 | 2 | 2 |
| 7 | 2014-10-10 | 3 | 2 |
| 6 | 2003-12-12 | 3 | 3 |
| 5 | 1991-01-12 | 3 | 2 |
+--------+------------+-----------+----------+
Here is a kind of table/view I need:
+------------+------------+-----------+----------+--------+
| date_from | date_to | id_item | id_buyer | id_doc |
+------------+------------+-----------+----------+--------+
| 2016-06-07 | - | 1 | 4 | 11 |
| 2016-06-06 | 2016-06-07 | 1 | 4 | 10 |
| 2015-11-30 | 2016-06-06 | 1 | 1 | 1 |
| 2009-01-01 | 2015-11-30 | 1 | 2 | 9 |
| 2001-01-12 | 2009-01-01 | 1 | 2 | 4 |
| 1996-06-06 | 2001-01-12 | 1 | 2 | 8 |
| 1995-05-29 | 1996-06-06 | 1 | 1 | 3 |
| 1998-05-23 | - | 2 | 2 | 2 |
| 2014-10-10 | - | 3 | 2 | 7 |
| 2003-12-12 | 2014-10-10 | 3 | 3 | 6 |
| 1991-01-12 | 2003-12-12 | 3 | 2 | 5 |
+------------+------------+-----------+----------+--------+
I've tried a lot with GROUP BY, GROUP_CONCAT, trying to access next record date, etc ... but I can't found out how to solve the problem.
Thanks in advance.
I finally found out the solution only for past purchases.
SELECT
main.id_doc, main.id_item, main.date AS "date_from", bi.date AS "date_to", main.id_buyer
FROM
MyTable main, MyTable bi
WHERE
bi.id_doc =
(
SELECT sub.id_doc
FROM MyTable sub
WHERE sub.id_item = main.id_item AND sub.date > main.date ORDER BY sub.date ASC LIMIT 1
);

MYSQL GROUP BY HAVING clause unsing MIN and MAX for a range

The query:
SELECT product_cd, SUM(avail_balance) prod_balance
FROM account
WHERE status = 'ACTIVE'
GROUP BY product_cd
HAVING MIN(avail_balance) >= 1000
AND MAX(avail_balance) <= 10000;
The table:
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| account_id | product_cd | cust_id | open_date | close_date | last_activity_date | status | open_branch_id | open_emp_id | avail_balance | pending_balance |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| 1 | CHK | 1 | 2000-01-15 | NULL | 2005-01-04 | ACTIVE | 2 | 10 | 1057.75 | 1057.75 |
| 2 | SAV | 1 | 2000-01-15 | NULL | 2004-12-19 | ACTIVE | 2 | 10 | 500.00 | 500.00 |
| 3 | CD | 1 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 2 | 10 | 3000.00 | 3000.00 |
| 4 | CHK | 2 | 2001-03-12 | NULL | 2004-12-27 | ACTIVE | 2 | 10 | 2258.02 | 2258.02 |
| 5 | SAV | 2 | 2001-03-12 | NULL | 2004-12-11 | ACTIVE | 2 | 10 | 200.00 | 200.00 |
| 7 | CHK | 3 | 2002-11-23 | NULL | 2004-11-30 | ACTIVE | 3 | 13 | 1057.75 | 1057.75 |
| 8 | MM | 3 | 2002-12-15 | NULL | 2004-12-05 | ACTIVE | 3 | 13 | 2212.50 | 2212.50 |
| 10 | CHK | 4 | 2003-09-12 | NULL | 2005-01-03 | ACTIVE | 1 | 1 | 534.12 | 534.12 |
| 11 | SAV | 4 | 2000-01-15 | NULL | 2004-10-24 | ACTIVE | 1 | 1 | 767.77 | 767.77 |
| 12 | MM | 4 | 2004-09-30 | NULL | 2004-11-11 | ACTIVE | 1 | 1 | 5487.09 | 5487.09 |
| 13 | CHK | 5 | 2004-01-27 | NULL | 2005-01-05 | ACTIVE | 4 | 16 | 2237.97 | 2897.97 |
| 14 | CHK | 6 | 2002-08-24 | NULL | 2004-11-29 | ACTIVE | 1 | 1 | 122.37 | 122.37 |
| 15 | CD | 6 | 2004-12-28 | NULL | 2004-12-28 | ACTIVE | 1 | 1 | 10000.00 | 10000.00 |
| 17 | CD | 7 | 2004-01-12 | NULL | 2004-01-12 | ACTIVE | 2 | 10 | 5000.00 | 5000.00 |
| 18 | CHK | 8 | 2001-05-23 | NULL | 2005-01-03 | ACTIVE | 4 | 16 | 3487.19 | 3487.19 |
| 19 | SAV | 8 | 2001-05-23 | NULL | 2004-10-12 | ACTIVE | 4 | 16 | 387.99 | 387.99 |
| 21 | CHK | 9 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 1 | 1 | 125.67 | 125.67 |
| 22 | MM | 9 | 2004-10-28 | NULL | 2004-10-28 | ACTIVE | 1 | 1 | 9345.55 | 9845.55 |
| 23 | CD | 9 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 1 | 1 | 1500.00 | 1500.00 |
| 24 | CHK | 10 | 2002-09-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 23575.12 | 23575.12 |
| 25 | BUS | 10 | 2002-10-01 | NULL | 2004-08-28 | ACTIVE | 4 | 16 | 0.00 | 0.00 |
| 27 | BUS | 11 | 2004-03-22 | NULL | 2004-11-14 | ACTIVE | 2 | 10 | 9345.55 | 9345.55 |
| 28 | CHK | 12 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 38552.05 | 38552.05 |
| 29 | SBL | 13 | 2004-02-22 | NULL | 2004-12-17 | ACTIVE | 3 | 13 | 50000.00 | 50000.00 |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
Result:
+------------+--------------+
| product_cd | prod_balance |
+------------+--------------+
| CD | 19500.00 |
| MM | 17045.14 |
+------------+--------------+
My understanding goes as far as this, from the query above:
SELECT product_cd, SUM(avail_balance) prod_balance
FROM account
WHERE status = 'ACTIVE'
GROUP BY product_cd
Which results in:
+------------+--------------+
| product_cd | prod_balance |
+------------+--------------+
| BUS | 9345.55 |
| CD | 19500.00 |
| CHK | 73008.01 |
| MM | 17045.14 |
| SAV | 1855.76 |
| SBL | 50000.00 |
+------------+--------------+
My confusion is with:
HAVING MIN(avail_balance) >= 1000
AND MAX(avail_balance) <= 10000;
Which I interpret as: "Include only the ones having a balance that is greater than or equal to 1,000 and lesser than or equal to 10,000" which would result in
BUS: 9345.55
SAV: 1855.76
Which is completely wrong when I ran the query as above. Any explanations as to what is going on would help.
instead of MIN(avail_balance) and MIN(avail_balance), try
HAVING MIN(SUM(avail_balance)) >=1000
and MAX(SUM(avail_balance)) <=10000
You are getting CD and MM in the result beacause all of their values are in between 1000 to 10000
Do you want the total or the minimum or maximum avail_balance match a certain range? They are different things.
The product CD for instance has a total of 19500, a minimum of 1500 and a maximum of 10000. You are showing the total (SUM), but want to see results where the product's minimum (MIN) is >= 1000 and the product's maximum (MAX) is <= 10000. This is true for CD.
So your query shows the correct results.
Maybe you want these, or maybe you want
SELECT
product_cd,
SUM(avail_balance) AS prod_balance
FROM account
WHERE status = 'ACTIVE'
GROUP BY product_cd
HAVING SUM(avail_balance) >= 1000
AND SUM(avail_balance) <= 10000;
instead.

Mysql rollup issue when grouping with quarter and date

I have this simple query:
select quarter(invitation_date) as `iQuarter`, invitation_date as `iDate`,count(*) as count from data group by iQuarter,iDate with rollup
I would expect to get a table like this:
+----------+------------+-------+
| iQuarter | iDate | count |
+----------+------------+-------+
| 1 | 2010-02-23 | 133 |
| 1 | 2010-03-02 | 14 |
| 1 | 2010-03-09 | 74 |
| 1 | 2010-03-15 | 102 |
| 1 | 2010-03-22 | 76 |
| 1 | 2010-03-30 | 16 |
| NULL | NULL | 415 |
| 2 | 2010-04-06 | 20 |
| 2 | 2010-04-12 | 84 |
| 2 | 2010-04-19 | 85 |
| 2 | 2010-04-26 | 51 |
| 2 | 2010-05-03 | 73 |
| 2 | 2010-05-11 | 76 |
| 2 | 2010-05-18 | 54 |
| 2 | 2010-05-24 | 83 |
| 2 | 2010-06-01 | 73 |
| 2 | 2010-06-07 | 74 |
| 2 | 2010-06-24 | 81 |
| 2 | 2010-06-28 | 110 |
| NULL | NULL | 864 |
+----------+------------+-------+
But what I get has the total in the wrong position and before every quarters total we have a row with the next quarter with a count of one.
+----------+------------+-------+
| iQuarter | iDate | count |
+----------+------------+-------+
| 1 | 2010-02-23 | 1 |
| NULL | NULL | 1 |
| 1 | 2010-02-23 | 132 |
| 1 | 2010-03-02 | 14 |
| 1 | 2010-03-09 | 74 |
| 1 | 2010-03-15 | 102 |
| 1 | 2010-03-22 | 76 |
| 1 | 2010-03-30 | 16 |
| 2 | 2010-04-06 | 1 |
| NULL | NULL | 415 |
| 2 | 2010-04-06 | 19 |
| 2 | 2010-04-12 | 84 |
| 2 | 2010-04-19 | 85 |
| 2 | 2010-04-26 | 51 |
| 2 | 2010-05-03 | 73 |
| 2 | 2010-05-11 | 76 |
| 2 | 2010-05-18 | 54 |
| 2 | 2010-05-24 | 83 |
| 2 | 2010-06-01 | 73 |
| 2 | 2010-06-07 | 74 |
| 2 | 2010-06-24 | 81 |
| 2 | 2010-06-28 | 110 |
| 3 | 2010-07-06 | 1 |
| NULL | NULL | 864 |
+----------+------------+-------+
Invitation date is defined as:
+-------------------+---------------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------------+------+-----+---------------------+-------+
| invitation_date | date | NO | | | |
+-------------------+---------------------------+------+-----+---------------------+-------+
Anyone have a clue on why this is happening? Mysql version: 5.0.27

SQL joining three tables and split into columns

I have three tables, mess_stock, mess_voucher, add_grocery.
Mess_stock table is below,
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| sno | voucher_id | particular_name | opening_balance | inward | outward | balance | pay_amount | pay_type |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| 49 | 5 | 4 | 100 | 10 | 100 | 10 | 10.00 | 1 |
| 50 | 17 | 5 | 111 | 10 | 20 | 101 | 60.00 | 1 |
| 51 | 7 | 3 | 123 | 2 | 1 | 124 | 300.00 | 1 |
| 52 | 7 | 1 | 123 | 20 | 20 | 123 | 500.00 | 2 |
| 53 | 14 | 8 | 100 | 5 | 95 | 10 | 60.00 | 2 |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
Mess_voucher table is below
+------------+--------------+--------------+
| voucher_id | voucher_name | voucher_date |
+------------+--------------+--------------+
| 5 | VG1001 | 2015-02-19 |
| 6 | VG1001 | 2015-02-20 |
| 7 | VG1002 | 2015-02-20 |
| 8 | VG1002 | 2015-02-19 |
| 9 | MS1001 | 2015-02-20 |
| 10 | VG10012 | 2015-02-19 |
| 11 | 0 | 2015-02-23 |
| 12 | 1 | 2015-02-24 |
| 13 | MS1001 | 2015-02-25 |
| 14 | MS1001 | 2015-02-28 |
| 15 | VG1003 | 2015-02-28 |
| 16 | MS1001 | 2015-02-19 |
| 17 | MS1001 | 2015-02-21 |
+------------+--------------+--------------+
Add_grocery table is below
+-----+-----------------+------------------+
| sno | particular_name | particular_price |
+-----+-----------------+------------------+
| 1 | Rice | 25.00 |
| 3 | Mango | 150.00 |
| 4 | Coconut | 22.00 |
| 5 | Banana | 6.00 |
| 6 | Raddish | 12.00 |
| 7 | Apple | 150.00 |
| 8 | Pumkin | 12.00 |
+-----+-----------------+------------------+
I want to group the sum of pay_amount of mess_stock table. I have used the below query
SELECT opening_balance AS ope_stock,
balance AS clo_stock,
SUM(IF(pay_type = 1, pay_amount, 0)) mess_pay,
SUM(IF(pay_type=2, pay_amount, 0)) est_pay
FROM mess_stock;
That works fine. The particular_name is the auto increment id of add_grocery table. I need the inward outward amount total. For example the inward amount 10 means it has to get the particular_price from add_grocery using the particular_name provided in the mess_stock table, similarly I need all the answer. And I want to sort that by date wise. The date of the entry is stored in the mess_voucher table that is connected to mess_stock table.
Try this it will work :
Use Inner Join :
SELECT t2.`particular_name`,t1.`inward`,t1.`outward`,t2.`particular_price`,t3.`voucher_date` from Mess_stock t1 JOIN Add_grocery t2 ON t1.`particular_name`=t2.`sno` JOIN Mess_voucher t3 ON t3.`voucher_id`=t1.`voucher_id` ORDER BY t3.`voucher_date` DESC