Showing Results other than a column

Asked

Viewed 153 times

2

I have a table CAIXA, where in the column TIPO can have two results, the 00 and the 99, where 00 is the opening of the terminal and 99 the closing.

I’m trying to create a query where the result is to highlight only the branches (COD_FILIAL) where they did not close the box.

That would be about it:

WHERE TIPO = 99 < TIPO = 00 and DATA between '20160101' and '20160101'

But I’m not getting this thought to SQL.

  • You can place the complete structure of the tables(s)?

  • These fields are no longer enough?

1 answer

3

From what I understand you would have a box table similar to this:

| COD_FILIAL | TIPO | DATA     |
|------------|------|----------|
| 1          | 00   | 20160101 |
| 1          | 99   | 20160101 |
| 2          | 00   | 20160101 |
| 2          | 99   | 20160101 |
| 3          | 00   | 20160101 |
| 1          | 00   | 20160102 |
| 1          | 99   | 20160102 |
| 2          | 00   | 20160102 |
| 1          | 00   | 20160103 |

And you expect the following results (affiliates where there was box opening on a given day but had no closure):

| COD_FILIAL | TIPO | DATA     |
|------------|------|----------|
| 3          | 00   | 20160101 |
| 2          | 00   | 20160102 |
| 1          | 00   | 20160103 |

Considering that there should always be a box opening to have a closure, think that the boxes without closure are the difference of the subset of the closed boxes (the part highlighted in gray):

Conjunto

To get only the gray part, think that you have two lists: affiliates and days with open box and affiliates with closed box. By joining these lists, take out the part that intersects and you will have the days that the boxes have been open.

SELECT *
FROM (SELECT * FROM CAIXA WHERE STATUS = '00') AS abertura
LEFT JOIN (SELECT * FROM CAIXA WHERE STATUS = '99') AS fechamento ON
  abertura.cod_filial = fechamento.cod_filial AND
  abertura.data = fechamento.data
WHERE fechamento.status IS NULL;

See the query running on SQL Fiddle.

To do this query I used the concepts of subquery and joins. This query can be done in different ways using a SQL Server CVE.

I leave below some links that can help you better understand these concepts.

  • Friend, your answer solved 90% of my question, see the original code: __________________________________________________ SELECT Aperture.CODIGO_FILIAL, Aperture.DATA, Aperture.TIPO_LANCAMENTO_CAIXA FROM (SELECT * FROM LOJA_CAIXA_LANCAMENTOS WHERE TIPO_LANCAMENTO_CAIXA = '00') AS Aperture left JOIN (SELECT * FROM LOJA_CAIXA_LANCAMENTOS WHERE TIPO_LANCAMENTO_CAIXA = '99') AS Lock ON Opening.CODIGO_FILIAL = Closing.CODIGO_FILIAL AND Opening.DATE = Closing.DATE WHERE Closure.TIPO_LANCAMENTO_CAIXA IS NULL AND Opening.DATE BETWEEN '20160826' AND '20160826'

  • However, now remained the following question, has some branches that the box opening('00') has not yet been integrated, that is, I have not integrated neither the opening and nor the closing, and then this result does not display those branches that do not have the integrated box opening

  • Recalling that the differential in this case, is that the opening and closing were not integrated, but the branch already has record in LOJA_VENDA table. It is imported first the LOJA_VENDA table and then the LOJA_CAIXA_LANCAMENTO table, so I have record of sale, but for an error in receipt, I have not imported the cash movements yet.

  • @Elirodriguesdasilva this is another question. Do a [tour] and understand how the community works. The idea is to have questions that are useful to other people, not something that will solve 100% of your problems (which is the case you’re looking for).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.