Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SELECT 
all_combos.user_id, 
all_combos.item_id, 
COALESCE(T.stat, 0) AS stat, 
COALESCE(T.dup) AS dup, 
t2.item_group
FROM 
>   (
SELECT 
user_id, 
item_id 
FROM 
>       (SELECT DISTINCT user_id FROM table3) u, 
>       (SELECT DISTINCT item_id FROM table3) i
AS all_combos 
LEFT OUTER JOIN table3 T ON T.user_id = all_combos.user_id AND T.item_id = all_combos.item_id
LEFT OUTER JOIN table2 t2 ON t2.id = all_combos.item_id
WHERE t2.item_group= 'LAMPS';




SELECT t8.username AS 'Username', 
            GROUP_CONCAT(CASE WHEN t3.dup=1 AND t4.stat=0 AND t2.item_group='LAMPS' THEN item_no END ORDER BY item_id SEPARATOR ', ') `My Items List`,
            GROUP_CONCAT(CASE WHEN t4.dup=1 AND t3.stat=0 AND t2.item_group='LAMPS' THEN item_no END ORDER BY item_id SEPARATOR ', ') `Item List`,
            SUM(CASE WHEN t4.dup=1 AND t3.stat=0 AND t2.item_group='LAMPS' THEN 1 ELSE 0 END) as `All_dup_sum`
            FROM table3 t3
            LEFT JOIN table3 t4 USING (item_id)
            JOIN table2 t2 ON t2.id = t4.item_id
            JOIN accounts t8 ON t8.id = t4.user_id
            WHERE t3.user_id = 23
              AND t4.user_id <> 23
              GROUP BY t4.user_id
              HAVING `Item List` is not null or `My Items List` is not null
              ORDER BY SUM(CASE WHEN t4.dup=1 AND t3.stat=0 AND t2.item_group='LAMPS' THEN 1 ELSE 0 END) DESC,
                       SUM(CASE WHEN t3.dup=1 AND t4.stat=0 AND t2.item_group='LAMPS' THEN 1 ELSE 0 END);


What I have tried:

SQL - Merge 2 queries into 1
Posted
Updated 19-Apr-23 2:42am
v4
Comments
Richard Deeming 19-Apr-23 4:48am    
VisitStartDate >= '2023-03-01'and VisitEndDate < '2023-03-31'

That assumes your visits are entirely within March, excluding 31st for some reason.

If any visit spans month-end - eg: 25th February — 5th March - then it won't be counted.

If you want to get every visit that is at least partially within March, you should probably use:
VisitStartDate <= '2023-03-31'and VisitEndDate >= '2023-03-01'
Maciej Los 19-Apr-23 10:25am    
You should provide sample data and expected output...
CHill60 20-Apr-23 6:10am    
As you have already been prompted - provide some sample data and your expected output and we are more likely to be able to help you

1 solution

Without knowing what data you actually expect to get, we can't help you with specifics on optimization - but two things do occur:

1) Format your queries. one long line of text is not easy to work out: break it into multiple lines so it's a lot easier to read and it becomes more maintainable, as well as better for the people who didn't throw it together!

2) At a guess, you probably need to start by looking at SQL Joins[^] as well as (or instead of ) SQL GROUP BY[^]

I'd start by taking your sample data and deciding what you want to produce, and where each element of the output should come from. Then look at JOINing and GROUPing instead of multiple subqueries.
 
Share this answer
 

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