Thursday 15 July 2010

python - SQLalchemy distinct, order_by different column -


I ask two tables with SQLalchemy, I want to use a specific feature on my query for a specific set client ID of

I have the following questions:

  order [n] = Dbession.query (order). \ Join (Customer). \ Filter (Order.oh_reqdate == Date_q). \ Filters (Order Vehicle_id == Vehicle.ID). \ Order_by (Customer.id). \ Distinct (Customer.id). \ Order_by (asc (order.power)). All ()   

If you can see what's happening here, then I am inquiring the order table for all orders for a specific vehicle, it works fine . However some customers may have more than one order for the same date. That's why I'm trying to filter out the results, to list only every customer once in the list. This work is fine, though, to do this, I have to sort out the results by the first column that has a different () function on it, I can add in the second order in the column, resulting in the result of the order without syntax error Want to But it is ignored and the result is ordered by the customer. ID.

I need to query my order table and connect to the client (not in other way) as the foreign key is set.

Is it possible within a query? Or will I need to loop my results again so that I can get the data I get in the right order?

You never need to "loop again" - if you load rows in Python So, this is it. You probably want to create a subquarium and you want to select from it, which you can get by using query.from_self (). Order_by (asc (order.position)) . You can use the subquery () in more specific circumstances.

In this case, I can not really tell what you want if a customer has more than one order with the requested vehicle ID and date, you will get two lines, for each order , And each order line will refer to the customer. What do you want instead? What is the first order line within each client group? I will do it like this:

  supreme_reader = s.query (Order.customer_id, func.max (order.position) .label ('position')). Filter (Order Oh_reqdate == date_q). \ Filters (Order Vehicle_id == vehicle.id). \ Group_by (Order.customer_id) \ Subquery () s.query (order). \ Join (Customer). \ Join (top_order, top_order.cccustomer_id == customer.id). \ Filters (order.homecdate == date_q). \ Filters (order.wall_id == vehicle.ed). \ Filters (order. Positive == supreme_order.cps)    

No comments:

Post a Comment