Subquery with data from the same table in sql server

1

I am using SQL SERVER 2012

I want to make a subquery with data from the same table that has the query relating three fields, I mean, the query is

SELECT m.COMPROBANT, COUNT (m.COMPROBANT)Item_ENTRADA, m.FECHA_TRS,m.COD_PROVEE,m.COD_AREA,
SUM(m.ENTRADAS) Total_ENTRADA
  FROM MOVIM AS m
WHERE m.COMPROBANT  LIKE '1%'
GROUP BY m.COMPROBANT,m.FECHA_TRS,m.COD_PROVEE,m.COD_AREA

And for the subquery data the fields

m.COMPROBANT, COUNT (m.COMPROBANT)Item_SALIDAS, m.FECHA_TRS,m.COD_PROVEE,m.COD_AREA,SUM(m.ENTRADAS) Total_SALIDAS

must be the same as the query but the WHERE of the subqueries must have

m.COMPROBANT  LIKE '2%'

Resumed as reflected in the image

In the MOVIM table there is a field called COMPROBANT but in this there is data that starts with '1' and data that starts with '2' so what I want is that a column all start with '1' and another column those that begin with '2' that's why I put LIKE but it only comes out in a single column, both what starts with '1' and those that start with '2'

Thank you very much for your help in advance

    
asked by Jose 26.07.2018 в 00:52
source

3 answers

0

You do not understand very well what you need or what you have, what I understand is the following ( correct me if I'm wrong ):

What you need is not a subquery, you need 2 queries:
   1- One that collects the data of enters in which the condition is LIKE '1%'
   2- Another one that collects the output data in which the condition is LIKE '2%'
   3- And these two queries join them horizontally in a single table

SELECT 
    COMPROBANT, 
    COUNT (COMPROBANT)item, 
    FECHA_TRS,
    COD_PROVEE,
    COD_AREA,
    SUM(ENTRADAS) total,
    entrada AS movimiento
  FROM MOVIM 
WHERE COMPROBANT  LIKE '1%'
GROUP BY COMPROBANT,FECHA_TRS,COD_PROVEE,COD_AREA

UNION 

SELECT 
    COMPROBANT, 
    COUNT (COMPROBANT)item,
    FECHA_TRS,
    COD_PROVEE,
    COD_AREA,
    SUM(ENTRADAS) total,
    salida AS movimiento
WHERE COMPROBANT  LIKE '2%'
GROUP BY COMPROBANT,FECHA_TRS,COD_PROVEE,COD_AREA

This query will return all the data you need the only thing that will be structured in another way, with the column movimiento you can control each line of data if it is input or output and each of them will have its own WHERE

My recommendation is as follows:
- If you really need to structure them in the way you proposed because it is a query for a view or something that stops in the query use the method PIVOT with this you can give it exactly this structure (I do not have all the data therefore not I can apply it to the query above)


- If you are going to use this data with a code other than the sql, avoid using the pivot and process the data of the query that I have put in the application itself, with a low amount of data you will not notice much difference . I use a similar query in my case and after a few tests a couple of days ago I removed the PIVOT from my query, in my case I deal with more than 1.2 million lines of data, in my case I rent more to try again the data in the program that receive them structured. Depending on how many lines you request you can mark many seconds of waiting difference.

(The above mentioned, if this is not what you asked for, update the question, give an example of the tables you have, the data format and the total result you want to obtain)

    
answered by 26.07.2018 в 12:31
0

try this! If it is not what you are looking for it is necessary to send a photo making a selection to your table MOVIM and only doing a filter for WHERE COMPROBANT LIKE '1%' OR COMPROBANT LIKE '2%' to see how you can work the data, luck!

SELECT x.FECHA_TRS,x.COD_PROVEE,x.COD_AREA
,x.Item_ENTRADA,x.Total_ENTRADA
,y.Item_SALIDA,y.Total_SALIDA
FROM (
SELECT 
    COUNT (m.COMPROBANT)Item_ENTRADA
    ,m.FECHA_TRS
    ,m.COD_PROVEE
    ,m.COD_AREA
    ,SUM(m.ENTRADAS) Total_ENTRADA
    FROM MOVIM AS m
    WHERE m.COMPROBANT  LIKE '1%'
    GROUP BY m.FECHA_TRS,m.COD_PROVEE,m.COD_AREA) AS x 
INNER JOIN (
    SELECT 
    COUNT (m.COMPROBANT)Item_SALIDA
    ,m.FECHA_TRS
    ,m.COD_PROVEE
    ,m.COD_AREA
    ,SUM(m.ENTRADAS) Total_SALIDA
    FROM MOVIM AS m
    WHERE m.COMPROBANT  LIKE '2%'
    GROUP BY m.FECHA_TRS,m.COD_PROVEE,m.COD_AREA) AS y
ON x.FECHA_TRS=y.FECHA_TRS AND x.COD_PROVEE=y.COD_PROVEE AND x.COD_AREA=y.COD_AREA 
    
answered by 26.07.2018 в 21:42
0

First of all, this is a problem that should be solved in the visualization layer and not in the data layer, it makes little sense since there is not just one logic that relates the two data sets, the logic is clearly of presentation. However, if the scenario forces you to do it from the data layer, what you could do is generate a id between the two queries to be able to relate them arbitrarily. We are going to do it by numbering each row, in such a way that after making FULL OUTER JOIN we will "join" row 1 of query 1 with row 1 of query 2, thus completing the data of both consultations. If there were more rows on one side than on the other, the FULL OUTER will allow us to show both without losing information:

SELECT  T2.COMPROBANT,
        T2.Item_SALIDA,
        T1.COMPROBANT,
        T1.Item_ENTRADA
        FROM (SELECT    COMPROBANT, 
                        COUNT (COMPROBANT) Item_ENTRADA,
                        ROW_NUMBER() OVER (ORDER BY COMPROBANT) NR
                        FROM MOVIM
                        WHERE LEFT(COMPROBANT,1) = '1'
                        GROUP BY COMPROBANT
            ) T1
        FULL OUTER JOIN ( SELECT    COMPROBANT, 
                                    COUNT (COMPROBANT) Item_SALIDA,
                                    ROW_NUMBER() OVER (ORDER BY COMPROBANT) NR
                                    FROM MOVIM
                                    WHERE LEFT(COMPROBANT,1) = '2'
                                    GROUP BY COMPROBANT
            ) T2
            ON T1.NR = T2.NR
    
answered by 27.07.2018 в 01:18