Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
edelgado
Starting Member
2 Posts |
Posted - 2011-11-16 : 19:38:23
|
| Good evening:I have two tables named [@sys_pdevoluciones] and [@sys_psucursales]they can be joined with [@sys_pdevoluciones].u_sys_sucu = [@sys_psucursales].codewhen I run the following query...select t0.code, t0.name from [@sys_psucursales]...I get this:Code Name002 CABANAS003 MESONES004 CORREO MAYOR005 LOS REYES006 CHALCO007 LEON008 CELAYA009 CENTRO010 GONZALEZ ORTEGA011 SANTA TERE012 CENTRO DE DISTRIBUCION013 QUERETAROThose are my client's stores. But I need to check the returns and credit notes totals, I use the following query:select SUM(CASE WHEN T4.Name LIKE '%D%' THEN T4.U_SYS_TOTN/1.16 ELSE 0 END) AS 'DEV', SUM(CASE WHEN T4.NAME LIKE '%N%' THEN T4.U_SYS_TOTN/1.16 ELSE 0 END) AS 'NC', T5.Code from [@SYS_PDEVOLUCIONES] T4, [@SYS_PSUCURSALES] T5where T5.Code = T4.U_SYS_SUCU group by T5.Code ... and get thisDEV NC Code2820.6379310343 2152.1293103447 002488.8534482756 0.0000000000 003932.5775862068 0.0000000000 004107.1120689652 0.0000000000 005272.8103448275 0.0000000000 00619.6551724137 110.8534482758 008596.1637931031 0.0000000000 009832.8275862068 0.0000000000 0100.0000000000 360.3620689655 011However, what I need is thisDEV NC Code2820.6379310343 2152.1293103447 002488.8534482756 0.0000000000 003932.5775862068 0.0000000000 004107.1120689652 0.0000000000 005272.8103448275 0.0000000000 0060.0000000000 0.0000000000 00719.6551724137 110.8534482758 008596.1637931031 0.0000000000 009832.8275862068 0.0000000000 0100.0000000000 360.3620689655 0110.0000000000 0.0000000000 013Note that 012 is not present because it is a consolidator, not a real store, that's why I don't care for it. 007 and 013 are not present because there are no returns ot credit notes done by those stores.I've tried to use unions and changing my where clause, but i've only managed to multiply data or add duplicate rows for the data of a specific store.What I need to do here is to be able to check the totals by a specific range of dates, which, I will add after I solve this (T4.U_SYS_FECH). But right now what I need the most is to add a row containing 0, 0 and code when the store has no data for that specific range of dates.Sorry for the long question, but I need to be very specific to get help faster. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
edelgado
Starting Member
2 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|
|
|