I'm a newbie to Access.
I have two tables (Tp and Temp). I want to update a field in the Temp table with the max of that field in the Tp table.
Max([TP]. [QCTNO]) this returns null in an update query, but if I use it in a select query, it returns a value.
TP table
id
TESTPACKAGE
QC SEND
QCTNO
QC RECIVE
QC RESULT
1
AG-TP-520-AIR-0001
400.08.15
320
400.08.15
UPDATE
2
AG-TP-520-AIRP-0001
400.01.24
250
401.01.28
UPDATE
3
AG-TP-538-BUT-0001
400.04.14
254
401.01.28
UPDATE
4
AG-TP-538-BUT-0002
400.04.14
251
401.01.28
UPDATE
5
AG-TP-538-BUT-0003
400.07.05
152
401.01.28
ACC
6
AG-TP-538-BUT-0004
400.07.05
254
401.01.28
ACC
7
AG-TP-538-BUT-0005
400.05.17
245
401.01.28
ACC
8
AG-TP-538-BUT-0006
400.05.17
142
401.01.28
ACC
9
AG-TP-538-BUT-0007
401.01.28
175
401.01.28
ACC
10
AG-TP-538-BUT-0008
401.01.28
198
401.01.28
ACC
11
AG-TP-501-BUT-0001
401.01.28
250
401.01.28
Return
12
AG-TP-537-BUT-0002
400.05.17
254
401.01.28
Return
13
AG-TP-502-BUT-0003
401.01.28
574
401.01.28
Return
14
AG-TP-502-BUT-0004
401.01.28
245
401.01.28
Return
15
AG-TP-502-BUT-0005
400.05.17
283
401.01.28
Return
16
AG-TP-502-BUT-0006
400.05.17
282
401.01.28
Return
17
AG-TP-502-BUT-0007
400.05.17
247
401.01.28
Return
Temp table
id
TESTPACKAGE
QC SEND
QCTNO
QC RECIVE
QC RESULT
9
AG-TP-538-BUT-0007
401.01.28
175
401.01.28
ACC
10
AG-TP-538-BUT-0008
401.01.28
198
401.01.28
ACC
11
AG-TP-501-BUT-0001
401.01.28
250
401.01.28
Return
what I need
id
TESTPACKAGE
QC SEND
QCTNO
QC RECIVE
QC RESULT
9
AG-TP-538-BUT-0007
401.01.28
574
10
AG-TP-538-BUT-0008
401.01.28
574
11
AG-TP-501-BUT-0001
401.01.28
574
Although saving calculated value is strongly discouraged (not recommended) but if you really interested to do it then use domain aggregate function DMAX() to update query. Try-
UPDATE TempTable SET [QCTNO]=DMAX("[QCTNO]","[TP]");
Related
I have a query that I need to SUM the trid column after summing the raised amount
SELECT
`ppm_campaign_id`.`meta_value` AS `campaign_id`,
`trasnlations`.`trid` AS `trid`,
`ppm_campaign_value`.`meta_value` AS `raised`
FROM (((`thf_posts` `posts`
LEFT JOIN `thf_postmeta` `ppm_campaign_id`
ON (((`ppm_campaign_id`.`meta_key` = 'campaign_id')
AND (`ppm_campaign_id`.`post_id` = `posts`.`ID`))))
LEFT JOIN `thf_postmeta` `ppm_campaign_value`
ON (((`ppm_campaign_value`.`meta_key` = 'campaign_value')
AND (`ppm_campaign_value`.`post_id` = `posts`.`ID`))))
LEFT JOIN `thf_icl_translations` `trasnlations`
ON ((`trasnlations`.`element_id` = `ppm_campaign_id`.`meta_value`)))
WHERE ((`ppm_campaign_id`.`meta_value` IS NOT NULL)
AND (`trasnlations`.`trid` IS NOT NULL))
GROUP BY `ppm_campaign_id`.`meta_value`
Result
campaign_id trid raised
1022 564 4137.5
1031 564 3937.5
1698 653 3010
1700 655 10
1702 657 750
1712 653 3030
1713 655 20
1727 657 20
2163 682 0.9
2164 682 50
2166 683 200
2168 684 50
Now I want the sum of trid combined so summing trid = 682 the result of raised should be 50.9 in both campaign_id
Expected Result
campaign_id trid raised
1022 564 8075
1031 564 8075
1698 653 3010
1700 655 10
1702 657 750
1712 653 3030
1713 655 20
1727 657 20
2163 682 50.9
2164 682 50.9
2166 683 200
2168 684 50
Considering the output of your query is stored in temptable. Please try the below query. Hope it helps.
SELECT t1.campaign_id, t1.trid, t2.raised_sum
FROM temptable t1 JOIN (
SELECT trid, SUM(raised) as raised_sum
FROM temptable GROUP BY trid) t2
ON t1.trid=t2.trid
GROUP BY t1.campaign_id, t1.trid, t2.raised_sum;
If more details on the tables included in your query and sample data can be given, we can try to get the data directly from them.
Say below is my table
entryID ID Number locationId timestamp
331 136 7 1 5/30/2018 9:23
332 136 7 1 5/30/2018 9:25
333 136 9 1 5/30/2018 9:25
334 136 11 1 5/30/2018 9:25
335 136 11 1 5/30/2018 9:25
336 136 9 1 5/30/2018 9:26
337 137 1 2 6/4/2018 8:45
338 137 2 2 6/4/2018 8:47
339 137 7 2 6/4/2018 8:47
340 137 7 2 6/4/2018 8:47
I would want to filter them by ID and locationId, delete the duplicate rows ("Number" is the column which carries duplicate values) and see the results as shown below after querying the table filtered by locationId = 1.
entryID ID Number locationId timestamp
332 136 7 1 5/30/2018 9:25
335 136 11 1 5/30/2018 9:25
336 136 9 1 5/30/2018 9:26
337 137 1 2 6/4/2018 8:45
338 137 2 2 6/4/2018 8:47
339 137 7 2 6/4/2018 8:47
340 137 7 2 6/4/2018 8:47
If locationId = 2, below is the result I would expect to see.
entryID ID Number locationId timestamp
331 136 7 1 5/30/2018 9:23
332 136 7 1 5/30/2018 9:25
333 136 9 1 5/30/2018 9:25
334 136 11 1 5/30/2018 9:25
335 136 11 1 5/30/2018 9:25
336 136 9 1 5/30/2018 9:26
337 137 1 2 6/4/2018 8:45
338 137 2 2 6/4/2018 8:47
340 137 7 2 6/4/2018 8:47
The requirement is poorly explained. Nevertheless, assuming that timestamp really is a timestamp, then I suspect that you're after something like this...
(Remember to back up your data prior to deleting anything!!!)
DELETE x
FROM my_table x
LEFT
JOIN
( SELECT locationid
, number
, MAX(entryid) entryid
FROM my_table
GROUP
BY locationid
, number
) y
ON y.locationid = x.locationid
AND y.number = x.number
AND y.entryid = x.entryid
WHERE y.entryid IS NULL;
MEMO TYPE ID
2442 11 52
33658 4 52
823 6 56
825 4 56
826 7 56
85 4 57
3298 7 57
87 4 141
377 7 141
88 4 142
378 7 142
98 1 143
99 2 143
194 7 143
7586 5 143
1451 4 143
7781 6 143
3252 4 167
3249 6 167
3915 7 167
13666 5 167
115 4 168
9253 9 168
9254 10 168
138 1 194
139 2 194
1951 4 194
8650 7 194
8191 6 197
8192 7 197
9687 8 197
9930 9 197
I need to select those records from above table where for every unique value in column 'ID', column 'TYPE' have value 6 and 7 both.
Result of this select query would be as below:
MEMO TYPE ID
823 6 56
826 7 56
7781 6 143
194 7 143
3249 6 167
3915 7 167
8191 6 197
8192 7 197
I hope this data is not too much.
select t.*
from your_table t
inner join
(
select id, min(type) as mint, max(type) as maxt
from your_table
where type in (6,7)
group by id
having count(distinct type) = 2
) x on x.id = t.id and t.type in (x.maxt,x.mint)
SQLFiddle demo
I'm trying to write a query that will return data grouped by date ranges and am wondering if there's a way to do it in one query (including the calculation), or if I need to write three separate ones? The dates in my table are stored as unix timestamps.
For example, my records look like this:
id type timestamp
312 87 1299218991
313 87 1299299232
314 90 1299337639
315 87 1299344130
316 87 1299348977
497 343 1304280210
498 343 1304280392
499 343 1304280725
500 343 1304280856
501 343 1304281015
502 343 1304281200
503 343 1304281287
504 343 1304281447
505 343 1304281874
566 90 1305222137
567 343 1305250276
568 343 1305387869
569 343 1305401114
570 343 1305405062
571 343 1305415659
573 343 1305421418
574 343 1305431457
575 90 1305431756
576 343 1305432456
577 259 1305441833
578 259 1305442234
580 343 1305456152
581 343 1305467261
582 343 1305483902
I'm trying to write a query that will find all records with a "created" date between:
2011-05-01 and 2011-06-01 (Month)
2011-03-01 and 2011-06-01 (Quarter)
2010-05-01 AND 2011-06-01 (Year)
I tried the following (in this case, I hardcoded the unix value for just the month to see if I could get it to work... ):
SELECT COUNT(id) AS idCount,
MIN(FROM_UNIXTIME(timestamp)) AS fromValue,
MAX(FROM_UNIXTIME(timestamp)) AS toValue
FROM uc_items
WHERE ADDDATE(FROM_UNIXTIME(timestamp), INTERVAL 1 MONTH)
>= FROM_UNIXTIME(1304233200)
But, it doesn't seem to work because the fromValue is: 2011-04-02 21:12:56 and the toValue is 2011-10-25 06:20:14, which, obviously, isn't a date between 5/1/2011 and 6/1/2011.
This aught to work:
SELECT COUNT(id) AS idCount,
FROM_UNIXTIME(MIN(timestamp)) AS fromValue,
FROM_UNIXTIME(MAX(timestamp)) AS toValue
FROM uc_items
WHERE timestamp BETWEEN UNIX_TIMESTAMP('2011-05-01') AND UNIX_TIMESTAMP('2011-06-01 23:59:59')
Also, as a performance tip - avoid applying functions to columns in your tables in a WHERE clause (e.g you have WHERE ADDDATE(FROM_UNIXTIME(timestamp))). Doing that will prevent MySQL from using any indexes on the timestamp column.
I have a table like this.
id day1 day2 day3
1 411 523 223
2 413 554 245
3 417 511 209
4 420 515 232
5 422 522 212
6 483 567 212
7 456 512 256
8 433 578 209
9 438 532 234
10 418 555 223
11 460 510 263
12 453 509 245
13 441 524 233
14 430 543 261
15 456 582 222
16 444 524 241
17 478 511 211
18 421 583 222
I want to select all the IDs that have duplicate values in day2.
I'm doing
select day2,count(*) from resultater group by day having count(*)>1;
Is it possible to list all the IDs within the groups?
select day2,count(*), group_concat(id)
from resultater
group by day
having count(*)>1;
should do the trick.