-2

I'm very new to DynamoDB, in SQL we could have done the below:

select * from Table where Section = 10 and Percentage between "90" and "100"

Need to the same with DynamoDB using Python Filter Expression. Below is the query which I have written.

fe = Key('Percentage').between("90", "100")

Need to add where Clause for the above as SQL query. How to do this?

EDIT 1 : The date values are stored like this in the image below. Date Column Values

The Query Which @LeeHannigan sent is selecting some random values and not between the dates.

response = table.scan(
    FilterExpression=Attr('Section').eq(10) & Attr('Percentage').between(90,100)
)

Any Suggestions on the to query on this date format? Or Whats the best way to store the date format?

  • Ideally your partition key is `Section` or you have a GSI on it. Avoid scan, if possible. – jarmod Mar 01 '23 at 14:01
  • We have only one partition key which is ID, that's a randomly generated GUID. Should date column be Secondary Index? –  Mar 02 '23 at 04:00
  • Your requirement appears to have changed to include date so you should update your question with the actual requirement. Does the range on date replace the range on percentage or do you need both? If you need both you will need a GSI on Section and either Date or Percentage and then filter on either Percentage or Date (whichever is not the sort key of the GSI).. – jarmod Mar 03 '23 at 15:01

1 Answers1

1

I will assume your DynamoDB table has a Partition Key of Section and a Sort Key of Percentage and both are Numbers.....

response = table.query(
    KeyConditionExpression=Key('Section').eq(10) & Key('Percentage').between(90,100),
)

If not, and you need to Scan:

response = table.scan(
    FilterExpression=Attr('Section').eq(10) & Attr('Percentage').between(90,100)
)
Leeroy Hannigan
  • 11,409
  • 3
  • 14
  • 31