SQL - Getting a column from another table to join this query

后端 未结 2 1976
野趣味
野趣味 2021-01-16 20:23

I\'ve got the code below which displays the location_id and total number of antisocial crimes but I would like to get the location_name from a diff

相关标签:
2条回答
  • 2021-01-16 20:38

    you need to find a relationship between the two tables to link a location to crime. that way you could use a "join" and select the fields from each table you are interested in.

    I suggest taking a step back and reading up on the fundamentals of relational databases. There are many good books out there which is the perfect place to start.

    0 讨论(0)
  • 2021-01-16 20:57

    You want to use join to lookup the location name. The query would probably look like this:

    SELECT ld.location_name, COUNT(cf.fk3_crime_id) as TOTAL_ANTISOCIAL_CRIMES
    from CRIME_FACT cf join
         LOCATION_DIM ld
         on cf.fk5_location_id = ld.location_id
    WHERE cf.fk1_time_id = 3 AND cf.fk3_crime_id = 1
    GROUP BY ld.location_name;
    

    You need to put in the right column names for ld.location_name and ld.location_id.

    0 讨论(0)
提交回复
热议问题