Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Table Joins SQL query, group by clause error

$
0
0

We have a 4 tables this will be pulling information from in the statement that's currently not working.

 

What we do is select all the companies from the companies table (1 of each company).
SELECT      co_name AS [Dealer],

Then we do select statements to pull out the quanities of data that are tagged as Qualified etc in the leads table.
     (SELECT COUNT (*) from tblLeads where ld_status is null AND ld_co_id = co_idno) AS 'Qualified Prospects',
            (SELECT COUNT (*) from tblLeads where ld_status = 'Q' AND ld_co_id = co_idno) AS 'Active Prospects',
            (SELECT COUNT (*) from tblLeads where ld_status = 'T' AND ld_co_id = co_idno) AS 'Target Prospects',
            (SELECT COUNT (*) from tblLeads where ld_status = 'Z' AND ld_co_id = co_idno) AS 'Customers',

Following on from that, we then require to pull out information from an activities table. So each company will have data it owns in the leads table then following on from that the leads in that table own activity logged against it.
SO the history in the activities table contains entries in the row of data linked to lead references and users.
History is owned by a user and lead. THen obviously a user is tied to a company.
The link looks like so...

                      User 01 <--- lead 01 <--- activity logs

Company <--- User 02 <--- lead 03 <--- activity logs

                      User 03 <--- lead 06 <--- activity logs

SO the aim for the last column in the query is to pull out how many qualified prospect calls have been made in the history table by looking at what user entered it and only pulling the figure through if it matchs the company queried.
(SELECT COUNT(hs_idno) FROM tblHistory
         where hs_type = @DM AND hs_date > @Datebegin AND hs_date < @Dateend
         AND hs_ebus = (SELECT us_idno from tblUsers
         JOIN tblcompanies ON us_company = co_idno
         where co_idno = us_company))AS 'QPC


The query should pull up every company, with its quantities next to it. This is total not a breakdown by user.
The first bit where it pulls through data quantities from leads works but the history doesn't. Now this is because in the leads, they have company tags in the row of data to refer to. Whereas in history it refers to users and leads. What i'm trying to do is say pull through all history from all users from the company.

Query looks like the following.

 

1.

DECLARE
@Datebegin DATETIME,
@Dateend DATETIME,
@QPC INT,
@DM INT,
@1stappt INT,
@Demo INT,
@Prop INT,
@Order INT;

SET @Datebegin = '2013-05-01 00:00:00.000';
SET @Dateend = '2013-05-31 23:59:59.000';
SET @QPC = (select hs_type from tblHistory where hs_type = 134);
SET @DM = (select hs_type from tblHistory where hs_type = 135 or hs_type = 136);
SET @1stappt = (select hs_type from tblHistory where hs_type = 137 or hs_type = 138);
SET @Demo = (select hs_type from tblHistory where hs_type = 141 or hs_type = 142);
SET @Prop = (select hs_type from tblHistory where hs_type = 143 or hs_type = 144);
SET @Order = (select hs_type from tblHistory where hs_type = 145 or hs_type = 146);


SELECT      co_name AS [Dealer],
            (SELECT COUNT (*) from tblLeads where ld_status is null AND ld_co_id = co_idno) AS 'Qualified Prospects',
            (SELECT COUNT (*) from tblLeads where ld_status = 'Q' AND ld_co_id = co_idno) AS 'Active Prospects',
            (SELECT COUNT (*) from tblLeads where ld_status = 'T' AND ld_co_id = co_idno) AS 'Target Prospects',
            (SELECT COUNT (*) from tblLeads where ld_status = 'Z' AND ld_co_id = co_idno) AS 'Customers',
           
 {
            COUNT(hs_idno) AS 'QPC'
                        FROM tblHistory,tblcompanies
   where hs_type = @DM AND hs_date > @Datebegin AND hs_date < @Dateend
   AND hs_ebus = (SELECT us_idno from tblUsers
   JOIN tblcompanies ON us_company = co_idno
   where co_idno = us_company)
 } -----> THIS BIT DOES NOT WORK

ORDER BY co_name ASC

 

2.

DECLARE
@Datebegin DATETIME,
@Dateend DATETIME,
@QPC INT,
@DM INT,
@1stappt INT,
@Demo INT,
@Prop INT,
@Order INT;

SET @Datebegin = '2013-05-01 00:00:00.000';
SET @Dateend = '2013-05-31 23:59:59.000';
SET @QPC = (select hs_type from tblHistory where hs_type = 134);
SET @DM = (select hs_type from tblHistory where hs_type = 135 or hs_type = 136);
SET @1stappt = (select hs_type from tblHistory where hs_type = 137 or hs_type = 138);
SET @Demo = (select hs_type from tblHistory where hs_type = 141 or hs_type = 142);
SET @Prop = (select hs_type from tblHistory where hs_type = 143 or hs_type = 144);
SET @Order = (select hs_type from tblHistory where hs_type = 145 or hs_type = 146);


SELECT      co_name AS [Dealer],
            (SELECT COUNT (*) from tblLeads where ld_status is null AND ld_co_id = co_idno) AS 'Qualified Prospects',
            (SELECT COUNT (*) from tblLeads where ld_status = 'Q' AND ld_co_id = co_idno) AS 'Active Prospects',
            (SELECT COUNT (*) from tblLeads where ld_status = 'T' AND ld_co_id = co_idno) AS 'Target Prospects',
            (SELECT COUNT (*) from tblLeads where ld_status = 'Z' AND ld_co_id = co_idno) AS 'Customers',
           
            (SELECT COUNT(hs_idno) FROM tblHistory
   where hs_type = @DM AND hs_date > @Datebegin AND hs_date < @Dateend
   AND hs_ebus = (SELECT us_idno from tblUsers
   JOIN tblcompanies ON us_company = co_idno
   where co_idno = us_company))AS 'QPC'
           
FROM tblCompanies
ORDER BY co_name ASC

 

I do not get any data returned from this. :( or I get a group by clause error. If I remove the history count etc and just have the lead count, it works fine. It's just the history one that's cocking up.
ARGH!!

Help me please



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>