2011-07-12 5 views
1

Salut peut-être quelqu'un peut m'aider ici ... J'ai un léger problème avec une déclaration SQL. (Sur MS - SQL Server 2008) j'ai donc 6 tableaux ressemblant à ceSQL - Joints multiples de table

ID/Société/Mois/ClosedTimeStamp/informations différentes

Maintenant, j'ai besoin (preferrable dans une déclaration: P) le nombre de datasets de chaque table regroupée par Société et Mois au moment où elle ressemble à ceci. Et il y a une autre chose pas toutes les tables doivent avoir des données pour cette entreprise et ce mois donc il peut y avoir 0 comme résultat pour le comptage (*)

SELECT COUNT(*) as c, Month, Company 
FROM Table1 WHERE ClosedTimeStamp IS NULL 
GROUP BY Company, Month 
ORDER BY Company 

Je peux le faire pour toutes les tables et juste choisir les résultats pour chaque entreprise ... Eh bien, si quelqu'un a une idée que je voudrais vraiment l'apprécier :)

Désolé oublié quelque chose ... le résultat devrait ressembler à ceci:

société/Mois/CountTable1/CountTable2/CountTable3/..... Test 02 1 0 50

Si ce n'est pas possible dans une déclaration bien, alors je dois le faire fonctionner d'une autre manière. :)

Merci

Lim

+0

Vous avez six tables avec presque la même structure? Et votre 'ORDER BY' ci-dessus devrait probablement commander par' Company' et non 'Firma';) – Jacob

+0

Ah désolé: P oublié de remplacer cela – Lim

+0

Bien sûr, il est possible dans une déclaration. Vérifiez ma réponse –

Répondre

1

Si votre base de données était normalisée, la requête serait beaucoup plus simple.

Parce que, votre company et Month sont réparties sur 6 tables, nous devons faire union de ces tables afin d'obtenir l'ensemble de données distinct de tous company + month, en tant que tel:

select company, month from table1 
union 
select company, month from table2 
union 
select company, month from table3 
union 
select company, month from table4 
union 
select company, month from table5 
union 
select company, month from table6 

Notez que nous avons besoin union, pas union all, parce que nous ne voulons pas la même société + paire de mois répété.

Ensuite, il suffit d'utiliser cet ensemble de données pour interroger les quantités pour chaque table:

select t.company, t.month, 
    (select count(*) from table1 
     where company = t.company 
     and month = t.month 
     and ClosedTimeStamp is null) as qt1, 
    (select count(*) from table2 
     where company = t.company 
     and month = t.month 
     and ClosedTimeStamp is null) as qt2, 
    (select count(*) from table3 
     where company = t.company 
     and month = t.month 
     and ClosedTimeStamp is null) as qt3, 
    (select count(*) from table4 
     where company = t.company 
     and month = t.month 
     and ClosedTimeStamp is null) as qt4, 
    (select count(*) from table5 
     where company = t.company 
     and month = t.month 
     and ClosedTimeStamp is null) as qt5, 
    (select count(*) from table6 
     where company = t.company 
     and month = t.month 
     and ClosedTimeStamp is null) as qt6 
from (
    select company, month from table1 
    union 
    select company, month from table2 
    union 
    select company, month from table3 
    union 
    select company, month from table4 
    union 
    select company, month from table5 
    union 
    select company, month from table6 
) t 
order by t.company 
+0

wow merci ça marche :) – Lim

2

UNION TOUTES les lignes de table et puis faire le décompte

SELECT COUNT(*) as c, Month, Company 
FROM 
(
SELECT Month,Company FROM Table1 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company FROM Table2 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company FROM Table3 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company FROM Table4 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company FROM Table5 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company FROM Table6 WHERE ClosedTimeStamp IS NULL 
) AS t 
GROUP BY Company, Month 
ORDER BY Company 

Si vous voulez que le total pour chaque table, société dans une rangée

SELECT SUM(t1) t1,SUM(t2) t2,SUM(t3) t3,SUM(t4) t4,SUM(t5) t5,SUM(t6) t6, Month, Company 
FROM 
(
SELECT Month,Company, 1 t1,0 t2, 0 t3, 0 t4, 0 t5, 0 t6 FROM Table1 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company, 0 t1,1 t2, 0 t3, 0 t4, 0 t5, 0 t6 FROM Table2 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company, 0 t1,0 t2, 1 t3, 0 t4, 0 t5, 0 t6 FROM Table3 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company, 0 t1,0 t2, 0 t3, 1 t4, 0 t5, 0 t6 FROM Table4 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company, 0 t1,0 t2, 0 t3, 0 t4, 1 t5, 0 t6 FROM Table5 WHERE ClosedTimeStamp IS NULL 
UNION ALL 
SELECT Month,Company, 0 t1,0 t2, 0 t3, 0 t4, 0 t5, 1 t6 FROM Table6 WHERE ClosedTimeStamp IS NULL 
) AS t 
GROUP BY Company, Month 
ORDER BY Company 
+0

Cela me fait juste un compte sur toutes les tables/Companys pas ce que je cherche Le résultat devrait ressembler à ceci Société/Mois/CountTable1/CountTable2/CountTable3/..... Test 02 1 0 50 – Lim

+0

Mis à jour. Voir l'alternative – niktrs

+0

Vous avez oublié de corriger le nom des tables. Ce n'est pas toujours 'Table1' –

Questions connexes