Question

I am having that the hardest time trying to combine these two simple queries (I'm fairly new to SQL and everything). I've tried a number of ways to combine them (sum case when...then 1 else 0) and using joins and tried implementing Exist but still giving me an error.

In any case the two queries I'm trying to combine are as follows:

Select
Count (*) as TotalA,
z.zname
From CTable c, STable s, SLTable sl, ZTable z
Where c.ID=s.sID
and s.sID=sl.ID
and sl.zID=z.zID
Group by z.zname
Order by z.zname

Select 
Count (*) as TotalB,
z.zname
From STable s, SLTable sl, ZTable z
Where s.sID=sl.sID
and sl.zID=z.zID
Group By z.zName
Order By z.zName

Here is one of my attempts to try and get the info

Select 
Count (*) as TotalB,
sum (case when sl.sID=s.sID and sl.zID=z.zID then 1 else 0 end) as  TotalA,
z.zname
From STable s, SLTable sl, ZTable z, CTable c
Where s.sID=sl.sID
and sl.zID=z.zID
and c.sID=s.sID
Group By z.zname
Order By z.zname

Pretty much this gives me three Columns of Total A, Total B and Zname - but Total A and Total B are exactly the same to one another - taking all the correct info from the first query and just copying it over to the Total B area.

I'm sure I'm just crudely trying to smash them together but I really have not a clue how to get them combined.

Any help is appreciated!

Était-ce utile?

La solution 3

Here is how I answered this - with some research - and cumulative answers from you guys:

SELECT
    ts.zname,
    TotalA ,
    TotalB
FROM
(
    Select
    Count (*) as TotalA,
    z.zname
    From CTable c, STable s, SLTable sl, ZTable z
    Where c.sID=s.sID
    and s.sID=sl.sID
    and sl.zID=z.zID
    Group by z.zname
) TC
INNER JOIN 
(
    Select 
    Count (*) as TotalB,
    z.zname
    From STable s, SLTable sl, ZTable z
    Where s.sID=sl.sID
    and sl.zID=z.zID
    Group By z.zname
) ts ON ts.zname = tc.zname
Order By ts.ZoneName

Autres conseils

How about the following:

SELECT COUNT(*) AS TotalA, 
(
  SELECT COUNT(*) as TotalB
  FROM STable ss
    INNER JOIN SLTable sssl ON ss.sID = sssl.sID 
    INNER JOIN ZTable sz ON sssl.zID = sz.zID
  WHERE sz.zname = z.zname 
) AS TotalB, z.zname
FROM CTable c
  INNER JOIN STable s ON c.ID = s.sID
  INNER JOIN SLTable sl ON s.sID = sl.ID 
  INNER JOIN ZTable z ON sl.zID = z.zID
GROUP BY z.zname

How about smashing them by CTE?

with

TableA as
(Select
Count (*) as TotalA,
z.zname
From CTable c, STable s, SLTable sl, ZTable z
Where c.ID=s.sID
and s.sID=sl.ID
and sl.zID=z.zID
Group by z.zname),

TableB as
(Select 
Count (*) as TotalB,
z.zname
From STable s, SLTable sl, ZTable z
Where s.sID=sl.sID
and sl.zID=z.zID
Group By z.zname)

/* --I've tested with this sample to avoid filling CTable,STable,SLTable,ZTable:
create table TableA(zname varchar(100),TotalA int)
create table TableB(zname varchar(100),TotalB int)
insert TableA(zname,TotalA) select 'a',10 union all select 'b',null
insert TableB(zname,TotalB) select 'a',20 union all select 'c',10 union all select 'd',50
select * from TableA
select * from TableB
*/

select COALESCE(TableA.zname,TableB.zname) as zname,TableA.TotalA,TableB.TotalB
from TableA full join TableB on TableA.zname=TableB.zname

if your SQL Server is quite old to use CTEs, just create temp tables and operate with them. Like this one:

Select
    Count (*) as TotalA,
    z.zname
    into #TableA
    From CTable c, STable s, SLTable sl, ZTable z
    Where c.ID=s.sID
    and s.sID=sl.ID
    and sl.zID=z.zID
    Group by z.zname
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top