Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a query that gives me what i need, but is there a way to rewrite the query so that it gives me the result for all users (distinct) in the table?
Really confused now. Is it even possible? Join/subquery? Eighter - i get stuck. Can variables do?

The query gives a total unique areas and the date for the last unique one.
Current query lists only selected user (mycall).

Important in table:
Datetime, sm, mm, conf.
Areacode can be in both sm and mm but is counted uniqely.

Sorry if question is somewhat duplicate, but on the other hand it's quite different too.

What I have tried:

SELECT COUNT(mycall) AS kmn, MAX(KJ_tid) FROM (SELECT t1.*, KJ_log.tid AS KJ_tid, KJ_log.sm AS KJ_sm, KJ_log.mm AS KJ_mm FROM (SELECT DISTINCT mycall, sm FROM KJ_log WHERE mycall = 'SM6TOB' AND sm !='' AND conf = '1' UNION DISTINCT SELECT mycall, mm FROM KJ_log WHERE mycall = 'SM6TOB' AND mm !='' AND conf = '1') AS t1 LEFT JOIN KJ_log ON (t1.sm = KJ_log.sm) OR (t1.sm = KJ_log.mm) where KJ_log.mycall = 'SM6TOB' AND conf = '1' GROUP BY t1.sm) AS latestdate
Posted
Updated 25-Apr-22 1:16am
v3
Comments
Richard MacCutchan 24-Apr-22 11:45am    
There is no need for obscenities in question; all we need are the facts.
SM6TOB 24-Apr-22 12:01pm    
Here in Sweden we use all kinf of "extra" words, but my misstake. The intended word was "horse"

If this a continuation of this thread: Count unique ocurrence sorted by date in myqsl[^], the description of your issue is not clear...

If you would like to loop through the collection of the data, you can use MySQL :: MySQL 8.0 Reference Manual :: 13.2.15 WITH (Common Table Expressions)[^]
 
Share this answer
 
That query requires some attention as I get a syntax error when I try to run it - that GROUP BY needs to be outside the brackets. You should always provide some sample data with questions like this, as well as your expected results and actual results.

First and foremost, MySQL is a set-based language - there is no need to "loop" through anything.

I used the following sample data derived from your other question (you owe thanks to @maciej-los for that)
SQL
CREATE TABLE KJ_log (tid datetime, mycall varchar(10), mm varchar(10), conf varchar(10), sm varchar(10));

insert into KJ_log(mycall ,tid, mm, conf, sm) values
('SM6ABC', '2022-04-01 09:00:00', 'AAAA', '1','xxx'),
('SM6ABC', '2022-04-01 09:01:00', 'AAAB', '1','xxx'),
('SM6ABC', '2022-04-01 09:12:00', 'AAAA', '1','xxx'),
('SM6CBA', '2022-04-01 09:10:00', 'BBBB', '1','xxx'),
('SM6CBA', '2022-04-01 09:11:00', 'AAAA', '1','xxx'),
('SM6AAA', '2022-04-01 09:12:00', 'AAAA', '1','xxx');
Next you can either use a CTE (use the link provided in Solution 1) or a temporary table - I chose to put the data into a table while I was checking it out
SQL
create table t1 (mycall varchar(10), sm varchar(10));

insert into t1 
(
	select mycall, sm from 
	(
		SELECT DISTINCT mycall
		, sm FROM KJ_log 
		WHERE 
			sm !='' 
			AND conf = '1' 
		
		UNION DISTINCT 
		SELECT mycall
		, mm FROM KJ_log 
		WHERE 
			mm !='' AND conf = '1'
	) AS x
);
NOTE - I have removed the part of the WHERE clause that refers to mycall
I then used the following
SQL
SELECT sm, COUNT(MYCALL), MAX(KJ_tid)
FROM
(
	SELECT 
		t1.*
		, KJ_log.tid AS KJ_tid
		, KJ_log.sm AS KJ_sm
		, KJ_log.mm AS KJ_mm 
  FROM t1 
  LEFT JOIN KJ_log ON KJ_log.mycall = t1.mycall AND (t1.sm = KJ_log.sm OR t1.sm = KJ_log.mm) 
  where conf = '1' 
) as LatestDate
GROUP BY sm
NOTE the addition of the filter on KJ_log.mycall in the ON clause of the join. If you omit that you will effectively end up with a cross-join and many many repeated rows.

NOTE also the addition of sm to the outer SELECT clause - it is usual to show the items that you have grouped by in such scripts
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900