使用Pivot设置0而不是NULL值

时间:2017-01-11 15:31:37

标签: sql pivot-table

我有一个带有pivot的请求,当它返回一个值时,它只显示一个值而不是表中的所有行。 我想如果他没有找到所有的值,他会返回所有的行,而不仅仅是有值的行。 这是代码:

DECLARE @P_A_USER VARCHAR(10)
SET @P_A_USER = 'BBE' -- Here with the user BBE

set language us_english
select * from (
select datename(month, D1) as 'Mois', COALESCE(count(*), 0) as 'Total_nouveaux_contrats', 'Nouveaux contrats' as 'Donnee', 'a' as 'NumLigne' from CONTRAT
where C22 = @P_A_USER and year(D1) = year(getdate()) and C27 = 01
group by datename(month, D1)) as NvContrats


Pivot(
SUM(Total_nouveaux_contrats)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt1
UNION
select * from (
select datename(month, D1) as 'Mois', COALESCE(count(*), 0) as 'Total_contrats_renew_1an_perim', 'Renew 1 an avec modification périmètre et FI 30%' as 'Donnee', 'b' as 'NumLigne' from CONTRAT
where C22 = @P_A_USER and year(D1) = year(getdate()) and C27 = 02
group by datename(month, D1)) as NvContrats


Pivot(
SUM(Total_contrats_renew_1an_perim)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt2
UNION
select * from (
select datename(month, D1) as 'Mois', COALESCE(count(*), 0) as 'Total_contrats_renew_1an', 'Renew 1 an sans modification périmètre' as 'Donnee', 'c' as 'NumLigne' from CONTRAT
where C22 = @P_A_USER and year(D1) = year(getdate()) and C27 = 03
group by datename(month, D1)) as NvContrats


Pivot(
SUM(Total_contrats_renew_1an)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt3
UNION
select * from (
select datename(month, D1) as 'Mois', COALESCE(count(*), 0) as 'Total_contrats_renew_3ans', 'Renew 3 ans sans modification périmètre' as 'Donnee', 'd' as 'NumLigne' from CONTRAT
where C22 = @P_A_USER and year(D1) = year(getdate()) and C27 = 04
group by datename(month, D1)) as NvContrats


Pivot(
SUM(Total_contrats_renew_3ans)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt4
UNION
select * from (
select datename(month, DAT_ACT_PA) as 'Mois', COALESCE(count(*), 0) as 'RDV_realises', 'RDV réalisés' as 'Donnee', 'e' as 'NumLigne' from ACTION
where COD_COM = @P_A_USER and CO_T_CO_PA = 'RDVCL' and year(DAT_ACT_PA) = year(getdate())
group by datename(month, DAT_ACT_PA)) as RDVrealise


Pivot(
SUM(RDV_realises)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt5
UNION
select * from (
select datename(month, DAT_ACT_PR) as 'Mois', COALESCE(count(*), 0) as 'RDV_planifies', 'RDV planifiés' as 'Donnee', 'f' as 'NumLigne' from ACTION
where COD_COM = @P_A_USER and CO_T_CO_PA = 'RDVCL' and year(DAT_ACT_PR) = year(getdate()) and DAT_ACT_PA is NULL
group by datename(month, DAT_ACT_PR)) as RDVplanifie


Pivot(
SUM(RDV_planifies)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt6
UNION
select * from (
select datename(month, DATE_COMMANDE) as 'Mois', COALESCE(count(distinct SOCIETE.NO_SOCIETE), 0) as 'Nouveaux_comptes', 'Nouveaux comptes ouverts' as 'Donnee', 'g' as 'NumLigne' from SOCIETE
left join V_COMMANDE on V_COMMANDE.NO_SOCIETE = SOCIETE.NO_SOCIETE
where SOCIETE.COD_COM = @P_A_USER and year(DATE_COMMANDE) = year(getdate()) and (SOCIETE.CA = 0 and SOCIETE.CAN_1 = 0)
group by datename(month, DATE_COMMANDE)) as NvComptes


Pivot(
SUM(Nouveaux_comptes)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt7
UNION
select * from (
select datename(month, CREER_LE) as 'Mois', COALESCE(count(*), 0) as 'Nouvelles_affaires', 'Nouvelles affaires' as 'Donnee', 'h' as 'NumLigne' from AFFAIRE
where AFFAIRE.COD_COM = @P_A_USER and year(CREER_LE) = year(getdate())
group by datename(month, CREER_LE)) as NvAffaires


Pivot(
SUM(Nouvelles_affaires)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt8
UNION
select * from (
select datename(month, DATE_AFF) as 'Mois', COALESCE(count(*), 0) as 'Nouvelles_affaires_exploitation', 'Nouvelles affaires avec exploitation' as 'Donnee', 'i' as 'NumLigne' from AFFAIRE
where AFFAIRE.COD_COM = @P_A_USER and year(DATE_AFF) = year(getdate()) and N3 != 0
group by datename(month, DATE_AFF)) as NvAffairesExp


Pivot(
SUM(Nouvelles_affaires_exploitation)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt9
UNION
select * from (
select datename(month, DATE_AFF) as 'Mois', cast(COALESCE(SUM(N1), 0) as integer) as 'CA_nouvelles_affaires', 'CA nouvelles affaires (€)' as 'Donnee', 'j' as 'NumLigne' from AFFAIRE
where AFFAIRE.COD_COM = @P_A_USER and year(CREER_LE) = year(getdate())
group by datename(month, DATE_AFF)) as CaNvAffaires


Pivot(
SUM(CA_nouvelles_affaires)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt10
UNION
select * from (
select datename(month, DATE_AFF) as 'Mois', COALESCE(count(*), 0) as 'Nb_affaires_gagnees', 'Affaires gagnées' as 'Donnee', 'k' as 'NumLigne' from AFFAIRE
where AFFAIRE.COD_COM = @P_A_USER and year(DATE_AFF) = year(getdate()) and COD_ETAT = 02
group by datename(month, DATE_AFF)) as NbAffG


Pivot(
SUM(Nb_affaires_gagnees)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt11
UNION
select * from (
select datename(month, DATE_AFF) as 'Mois', COALESCE(count(*), 0) as 'Nb_affaires_perdues', 'Affaires perdues' as 'Donnee', 'l' as 'NumLigne' from AFFAIRE
where AFFAIRE.COD_COM = @P_A_USER and year(DATE_AFF) = year(getdate()) and COD_ETAT = 03
group by datename(month, DATE_AFF)) as NbAffP


Pivot(
SUM(Nb_affaires_perdues)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt12

order by NumLigne asc

这是用户BBE的结果: http://imgur.com/a/Fdiwz

我希望他能给我所有的线路并将0代替Null ...... 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

Did you use 0 as char or as number


DECLARE @P_A_USER VARCHAR(10)
SET @P_A_USER = 'BBE' -- Here with the user BBE

set language us_english
select * from (
select datename(month, D1) as 'Mois', COALESCE(count(*), '0') as 'Total_nouveaux_contrats', 'Nouveaux contrats' as 'Donnee', 'a' as 'NumLigne' from CONTRAT
where C22 = @P_A_USER and year(D1) = year(getdate()) and C27 = 01
group by datename(month, D1)) as NvContrats


Pivot(
SUM(Total_nouveaux_contrats)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt1
UNION
select * from (
select datename(month, D1) as 'Mois', COALESCE(count(*), 0) as 'Total_contrats_renew_1an_perim', 'Renew 1 an avec modification périmètre et FI 30%' as 'Donnee', 'b' as 'NumLigne' from CONTRAT
where C22 = @P_A_USER and year(D1) = year(getdate()) and C27 = 02
group by datename(month, D1)) as NvContrats


Pivot(
SUM(Total_contrats_renew_1an_perim)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt2
UNION
select * from (
select datename(month, D1) as 'Mois', COALESCE(count(*), 0) as 'Total_contrats_renew_1an', 'Renew 1 an sans modification périmètre' as 'Donnee', 'c' as 'NumLigne' from CONTRAT
where C22 = @P_A_USER and year(D1) = year(getdate()) and C27 = 03
group by datename(month, D1)) as NvContrats


Pivot(
SUM(Total_contrats_renew_1an)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt3
UNION
select * from (
select datename(month, D1) as 'Mois', COALESCE(count(*), 0) as 'Total_contrats_renew_3ans', 'Renew 3 ans sans modification périmètre' as 'Donnee', 'd' as 'NumLigne' from CONTRAT
where C22 = @P_A_USER and year(D1) = year(getdate()) and C27 = 04
group by datename(month, D1)) as NvContrats


Pivot(
SUM(Total_contrats_renew_3ans)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt4
UNION
select * from (
select datename(month, DAT_ACT_PA) as 'Mois', COALESCE(count(*), 0) as 'RDV_realises', 'RDV réalisés' as 'Donnee', 'e' as 'NumLigne' from ACTION
where COD_COM = @P_A_USER and CO_T_CO_PA = 'RDVCL' and year(DAT_ACT_PA) = year(getdate())
group by datename(month, DAT_ACT_PA)) as RDVrealise


Pivot(
SUM(RDV_realises)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt5
UNION
select * from (
select datename(month, DAT_ACT_PR) as 'Mois', COALESCE(count(*), 0) as 'RDV_planifies', 'RDV planifiés' as 'Donnee', 'f' as 'NumLigne' from ACTION
where COD_COM = @P_A_USER and CO_T_CO_PA = 'RDVCL' and year(DAT_ACT_PR) = year(getdate()) and DAT_ACT_PA is NULL
group by datename(month, DAT_ACT_PR)) as RDVplanifie


Pivot(
SUM(RDV_planifies)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt6
UNION
select * from (
select datename(month, DATE_COMMANDE) as 'Mois', COALESCE(count(distinct SOCIETE.NO_SOCIETE), 0) as 'Nouveaux_comptes', 'Nouveaux comptes ouverts' as 'Donnee', 'g' as 'NumLigne' from SOCIETE
left join V_COMMANDE on V_COMMANDE.NO_SOCIETE = SOCIETE.NO_SOCIETE
where SOCIETE.COD_COM = @P_A_USER and year(DATE_COMMANDE) = year(getdate()) and (SOCIETE.CA = 0 and SOCIETE.CAN_1 = 0)
group by datename(month, DATE_COMMANDE)) as NvComptes


Pivot(
SUM(Nouveaux_comptes)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt7
UNION
select * from (
select datename(month, CREER_LE) as 'Mois', COALESCE(count(*), 0) as 'Nouvelles_affaires', 'Nouvelles affaires' as 'Donnee', 'h' as 'NumLigne' from AFFAIRE
where AFFAIRE.COD_COM = @P_A_USER and year(CREER_LE) = year(getdate())
group by datename(month, CREER_LE)) as NvAffaires


Pivot(
SUM(Nouvelles_affaires)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt8
UNION
select * from (
select datename(month, DATE_AFF) as 'Mois', COALESCE(count(*), 0) as 'Nouvelles_affaires_exploitation', 'Nouvelles affaires avec exploitation' as 'Donnee', 'i' as 'NumLigne' from AFFAIRE
where AFFAIRE.COD_COM = @P_A_USER and year(DATE_AFF) = year(getdate()) and N3 != 0
group by datename(month, DATE_AFF)) as NvAffairesExp


Pivot(
SUM(Nouvelles_affaires_exploitation)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt9
UNION
select * from (
select datename(month, DATE_AFF) as 'Mois', cast(COALESCE(SUM(N1), 0) as integer) as 'CA_nouvelles_affaires', 'CA nouvelles affaires (€)' as 'Donnee', 'j' as 'NumLigne' from AFFAIRE
where AFFAIRE.COD_COM = @P_A_USER and year(CREER_LE) = year(getdate())
group by datename(month, DATE_AFF)) as CaNvAffaires


Pivot(
SUM(CA_nouvelles_affaires)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt10
UNION
select * from (
select datename(month, DATE_AFF) as 'Mois', COALESCE(count(*), 0) as 'Nb_affaires_gagnees', 'Affaires gagnées' as 'Donnee', 'k' as 'NumLigne' from AFFAIRE
where AFFAIRE.COD_COM = @P_A_USER and year(DATE_AFF) = year(getdate()) and COD_ETAT = 02
group by datename(month, DATE_AFF)) as NbAffG


Pivot(
SUM(Nb_affaires_gagnees)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt11
UNION
select * from (
select datename(month, DATE_AFF) as 'Mois', COALESCE(count(*), 0) as 'Nb_affaires_perdues', 'Affaires perdues' as 'Donnee', 'l' as 'NumLigne' from AFFAIRE
where AFFAIRE.COD_COM = @P_A_USER and year(DATE_AFF) = year(getdate()) and COD_ETAT = 03
group by datename(month, DATE_AFF)) as NbAffP


Pivot(
SUM(Nb_affaires_perdues)
for Mois in (January, February, March, April, May, June, July, August, September, October, November, December)) as pvt12

order by NumLigne asc