Count between 2 columns of 2 tables

2

I have these 2 tables

table 1

----------------------------------
|   articulo   |   Ubicacion     |
----------------------------------
|     200116    |   cy1112       | 
----------------------------------
|     200116    |   ee3091       |
----------------------------------

table 2

----------------------------------
|   articulo   |   Ubicacion     |
----------------------------------
|     200116    |   cy1112       | 
----------------------------------
|     200116    |   dd3091       |
----------------------------------

I'm trying to do it this way

SI_Ubicacion = COUNT(DISTINCT ISNULL(tabla1.SI_Ubicacion,tabla2.SI_Ubicacion))

is that you tell me the locations and if they are the same, do not count it twice and the result must be SI_Ubicacion = 3

At this moment you are only telling me the locations of the first table

The tables are related by articulo

I hope you can help me, Thanks.

    
asked by Eduard 25.07.2017 в 22:00
source

3 answers

2

Just make a UNION between both tables:

SELECT
     Articulo
    ,Ubicacion
FROM @t1
UNION
SELECT
     Articulo
    ,Ubicacion
FROM @t2

Which gives you the following result:

+----------+-----------+
| Articulo | Ubicacion |
+----------+-----------+
| 200116   | cy1112    |
+----------+-----------+
| 200116   | dd3091    |
+----------+-----------+
| 200116   | ee3091    |
+----------+-----------+

To get the total of records would be as follows:

SELECT SI_Ubicacion = COUNT(Ubicacion)
FROM (
    SELECT
         Articulo
        ,Ubicacion
    FROM @t1
    UNION
    SELECT
         Articulo
        ,Ubicacion
    FROM @t2
) AS T1

Which gives you the following results:

+--------------+
| SI_Ubicacion |
+--------------+
| 3            |
+--------------+

Here you can see the demo and its results

    
answered by 26.07.2017 / 00:45
source
1

It is not possible to understand the question well, but I assume that if there are the tables that you indicate are related by an article there must be a table article , then you can make a union of the table1 and table2 and then that union relate it to your article table to finally make a group by.

Here is an example of what I mention: see example

select a.nombre, count(ubicaciones.ubicacion)
from articulo a , (
    select * from tabla1 union select * from tabla2
    ) as ubicaciones
where a.codigo = ubicaciones.articulo
group by a.nombre;

Note: remember that UNION of two querys omits equal values

If it is not clear, try to improve your question, which is very little understood.

    
answered by 25.07.2017 в 23:35
1

As Andrés says, the answer lies in the use of the UNION to add the two location tables and take advantage of the fact that this sentence already takes away the duplicate records, but pay attention the UNION takes into account the entire row to determine duplicates so the SELECT should recover only articulo and ubicacion , if we add another field that does not repeat we would be counting the locations wrong.

The idea would be the following:

DECLARE @Articulo TABLE(articulo int, nombre VARCHAR(255))
DECLARE @t1 TABLE(articulo int, ubicacion varchar(30))
DECLARE @t2 TABLE(articulo int, ubicacion varchar(30)
)

INSERT INTO @Articulo VALUES(200116, 'Articulo 200116')
INSERT INTO @Articulo VALUES(200117, 'Articulo 200117')
INSERT INTO @Articulo VALUES(200118, 'Articulo 200118')

INSERT INTO @t1 VALUES(200116, 'cy1112')
INSERT INTO @t1 VALUES(200116, 'ee3091')
INSERT INTO @t1 VALUES(200116, 'xx3092')
INSERT INTO @t2 VALUES(200116, 'cy1112')
INSERT INTO @t2 VALUES(200116, 'dd3091')
INSERT INTO @t2 VALUES(200118, 'dd3091')

SELECT  A.articulo, 
    A.nombre,  
    ISNULL(COUNT(T.ubicacion),0) AS 'Ubicaciones'
        FROM @Articulo A
    LEFT JOIN ( SELECT articulo, ubicacion FROM @t1 UNION
                SELECT articulo, ubicacion FROM @t2) T
        ON A.articulo = T.articulo
    GROUP BY A.articulo,
        A.nombre

The exit:

articulo    nombre          ubicaciones
=========== =============== ===========
200.116,00  Articulo 200116 4,00
200.117,00  Articulo 200117 0,00
200.118,00  Articulo 200118 1,00

An eventual improvement with respect to the previous answers is in the use of the LEFT JOIN and the ISNULL that allows us to "count" also those articles that do not have locations.

    
answered by 26.07.2017 в 00:17