SQL - Compare data from one table

michal2805

I am having trouble finding a way to compare data from one table (Table1).

Part of the Table1

Date        ID        Item        
----        -------   -----
2017-06-30  90        2200
2017-06-30  150       1200
2017-06-30  150       1201
2017-06-30  150       1202 
2017-06-30  150       1203 
2017-06-30  150       1204
2017-07-31  150       1201
2017-07-31  150       1202
2017-07-31  150       1203
2017-07-31  150       1204
2017-07-31  150       1205
2017-07-31  90        2200

The result I would like to get is 1205 as this is a new item in following month. It would be also nice if I could get item that would not be anymore in the following month, ie 1200

** EDITED: The one thing I should mention is that Table1 has also different IDs in ID Column. So the main goal is to compare exact ID=150 (not 160 or 180). **

I will be grateful for any advice.

Thank you

Strawberry

E.g.:

SELECT x.* 
  FROM my_table x 
  LEFT 
  JOIN my_table y 
    ON y.id = x.id 
   AND y.date = '2017-06-30' 
   AND y.item = x.item 
 WHERE x.date = '2017-07-31' 
   AND y.id IS NULL;

or

SELECT x.* 
  FROM my_table x 
  LEFT 
  JOIN my_table y  
    ON y.id = x.id AND y.date = x.date - INTERVAL 1 MONTH 
   AND y.item = x.item 
 WHERE x.date = '2017-07-31' 
   AND y.id IS NULL;

I would have left the remaining part as an exercise for the reader, but I see my plan has been scuppered.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL - Compare Data from the same table

From Dev

sql select from table then compare with other table in one statement

From Dev

Compare 2 tables and select data from one table

From Dev

Inserting data from one table to the next in sql

From Dev

How to compare one row of data to every row of data from another table in google sheet?

From Dev

sql server compare rows in one table

From Dev

sql server compare rows in one table

From Dev

Getting data from one table based on results of another SQL PHP

From Dev

SQL Server - Select data from one table based on a string value

From Dev

How to copy data from one table into another in Microsoft SQL Server

From Dev

Transfer data from one table to another in sql server

From Dev

How to restore SQL data from one table to another

From Dev

Oracle SQL Selecting data from one table that relates to another

From Dev

MS SQL retrieve related data from same table in one query

From Dev

SQL FOR XML multilevel from one pivoted table - grouping data

From Dev

Pulling data from one table to use in another query for SQL Server?

From Dev

SQL server : Inserting/updating missing data from one table to another

From Dev

Postgres SQL - Join data from 3 table to select one column

From Dev

SQL server 2008 query move table data from one to another

From Dev

SQL Server Copy Random data from one table to another

From Dev

SQL Query to compare two columns with one column from another table (and get two values)

From Dev

SQL Using row data from one table to choose data from a specific column in a different table

From Dev

Codeigniter SQL query : i want to retrieve data from one table using the data from other table

From Dev

Compare records of a table and filter their data in sql server

From Dev

SQL. Retrieve data from the table where data is duplicated from 1 column and sifferent from another one

From Dev

SQL help extracting data from one table and based on a backup table, inserting that data

From Dev

SQL help extracting data from one table and based on a backup table, inserting that data

From Dev

SQL Query to get data from one table where a specific column equals value from other table

From Dev

Cron php update/insert one sql table from another and delete data from old table

Related Related

  1. 1

    SQL - Compare Data from the same table

  2. 2

    sql select from table then compare with other table in one statement

  3. 3

    Compare 2 tables and select data from one table

  4. 4

    Inserting data from one table to the next in sql

  5. 5

    How to compare one row of data to every row of data from another table in google sheet?

  6. 6

    sql server compare rows in one table

  7. 7

    sql server compare rows in one table

  8. 8

    Getting data from one table based on results of another SQL PHP

  9. 9

    SQL Server - Select data from one table based on a string value

  10. 10

    How to copy data from one table into another in Microsoft SQL Server

  11. 11

    Transfer data from one table to another in sql server

  12. 12

    How to restore SQL data from one table to another

  13. 13

    Oracle SQL Selecting data from one table that relates to another

  14. 14

    MS SQL retrieve related data from same table in one query

  15. 15

    SQL FOR XML multilevel from one pivoted table - grouping data

  16. 16

    Pulling data from one table to use in another query for SQL Server?

  17. 17

    SQL server : Inserting/updating missing data from one table to another

  18. 18

    Postgres SQL - Join data from 3 table to select one column

  19. 19

    SQL server 2008 query move table data from one to another

  20. 20

    SQL Server Copy Random data from one table to another

  21. 21

    SQL Query to compare two columns with one column from another table (and get two values)

  22. 22

    SQL Using row data from one table to choose data from a specific column in a different table

  23. 23

    Codeigniter SQL query : i want to retrieve data from one table using the data from other table

  24. 24

    Compare records of a table and filter their data in sql server

  25. 25

    SQL. Retrieve data from the table where data is duplicated from 1 column and sifferent from another one

  26. 26

    SQL help extracting data from one table and based on a backup table, inserting that data

  27. 27

    SQL help extracting data from one table and based on a backup table, inserting that data

  28. 28

    SQL Query to get data from one table where a specific column equals value from other table

  29. 29

    Cron php update/insert one sql table from another and delete data from old table

HotTag

Archive