join multiple queries error

Karuppiah RK

First query

SELECT a.*,
       ifnull(cnt_all,0) total_drivers,
       ifnull(cnt_active,0) active_drivers,
       ifnull(cnt_idle,0) idle_drivers
FROM ta_agent a
LEFT JOIN
  (SELECT agent_id,
          count(*) cnt_all
   FROM ta_drivers
   GROUP BY agent_id) cnt ON a.agent_id=cnt.agent_id
LEFT JOIN
  (SELECT agent_id,
          count(*) cnt_idle
   FROM ta_drivers
   WHERE last_viewed=0
   GROUP BY agent_id) idle ON a.agent_id=idle.agent_id
LEFT JOIN
  (SELECT agent_id,
          count(*) cnt_active
   FROM ta_drivers
   WHERE last_viewed=1
   GROUP BY agent_id) active ON a.agent_id=active.agent_id

Second query

SELECT FROM_UNIXTIME(date_of_registration, '%Y-%m-%d %H:%i:%s') AS user_registeredon
FROM ta_agent a,
     ta_subscription s
WHERE s.agent_id = a.agent_id

These two queries working fine when I run separately. I want to join these two queries. I have tried to join these two queries but I got this error Every derived table must have its own alias

I have tried this

select a.*, ifnull(cnt_all,0) total_drivers,ifnull(cnt_active,0) active_drivers, ifnull(cnt_idle,0) idle_drivers
    from ta_agent a left join (select agent_id, count(*) cnt_all
                       from ta_drivers
                       group by agent_id) cnt on a.agent_id=cnt.agent_id

    left join (select agent_id, count(*) cnt_idle
                       from ta_drivers
                       where last_viewed=0
                       group by agent_id) idle on a.agent_id=idle.agent_id

    left join (select agent_id, count(*) cnt_active
                       from ta_drivers
                       where last_viewed=1
                       group by agent_id) active on a.agent_id=active.agent_id

    left join(SELECT FROM_UNIXTIME( date_of_registration, '%Y-%m-%d %H:%i:%s' ) AS user_registeredon FROM ta_subscription WHERE agent_id = a.agent_id)

I don't think this is the correct method to get the result..

Ronak Shah

Please mentioned how do you want to join this two query.. Looking for something like below:

SELECT *
FROM
  (SELECT a.*,
          ifnull(cnt_all,0) total_drivers,
          ifnull(cnt_active,0) active_drivers,
          ifnull(cnt_idle,0) idle_drivers
   FROM ta_agent a
   LEFT JOIN
     (SELECT agent_id,
             count(*) cnt_all
      FROM ta_drivers
      GROUP BY agent_id) cnt ON a.agent_id=cnt.agent_id
   LEFT JOIN
     (SELECT agent_id,
             count(*) cnt_idle
      FROM ta_drivers
      WHERE last_viewed=0
      GROUP BY agent_id) idle ON a.agent_id=idle.agent_id
   LEFT JOIN
     (SELECT agent_id,
             count(*) cnt_active
      FROM ta_drivers
      WHERE last_viewed=1
      GROUP BY agent_id) active ON a.agent_id=active.agent_id)tempaliasA
LEFT JOIN
  (SELECT FROM_UNIXTIME(date_of_registration, '%Y-%m-%d %H:%i:%s') AS user_registeredon,
          agent_id
   FROM ta_agent a,
        ta_subscription s
   WHERE s.agent_id = a.agent_id)tempaliasB ON tempaliasA.agent_id = tempaliasB.agent_id

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JOIN queries vs multiple queries

Is multiple queries or LEFT JOIN faster?

SQL: Except Join on multiple queries

MySQL - How to join multiple queries?

LEFT JOIN multiple sub queries

How to join multiple count(*) queries?

How to join multiple Apollo queries in the state with React?

Hibernate does multiple queries on @OneToOne join

Search multiple queries oninner join table

Multiple SQL Queries with INNER JOIN different parameters?

Eloquent Join, multiple where queries, change AND to OR

Join multiple complex regex queries with pipe

Join Multiple Select Queries into One in MySql

SQL left Join Error on multiple JOIN

Error when trying to run multiple mysqli queries

Syntax error on multiple join statement

large single join queries vs multiple smaller ones

PSQL: Combine/Join Results of Multiple Queries Into One Table

multi-tenancy Join or multiple Select queries postgres

Select where not exists with: multiple select queries & concat query & join query

In SQL, how to you join multiple aggregate queries (count or sum specifically)?

In SQL, how to you join multiple aggregate queries (count or sum specifically)?

What is the optimal solution, use Inner Join or multiple queries?

How to optimize MySQL "Order By Limit 1" in queries that join multiple tables?

JOIN queries in DataTable using AJAX throwing Unknown column error

Getting Queries of Queries error

How to Join Multiple Queries with Multiple Conditions in Mysql from Single Table in PHP?

WordPress: multiple search terms or join results from multiple WP_Queries

Multiple Foreign key SqlAlchemy join error in flask

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive