-1

I have the following query and I want it to generate the result with just the latest date for the category for a store instead of giving out per date transaction:

SELECT c.store,d.node_name category, x.txn_dt, x.txn_tm time, count(c.txn_id) Buyer
FROM pos_swy.5_centerstore_triptype c
join pos_swy.3_txn_itm t on c.txn_id=t.txn_id
join pos_swy.1_upc_node_map d on t.upc_id=d.upc_id
join pos_swy.3_txn_hdr x on t.txn_id=x.txn_id
group by store,txn_dt,node_name;

I tried using max(x.txn_dt) but it really didn't solve the purpose.

  • possible duplicate of [Get top n records for each group of grouped results](http://stackoverflow.com/questions/12113699/get-top-n-records-for-each-group-of-grouped-results) – Marc B Jul 17 '13 at 14:25
  • you shouldn't group by field for which you are applying aggregate function – triclosan Jul 17 '13 at 14:27

1 Answers1

0

you may need the order by ?

    SELECT c.store,d.node_name category, max(x.txn_dt) max_date, x.txn_tm time, count(c.txn_id) Buyer
    FROM pos_swy.5_centerstore_triptype c
    join pos_swy.3_txn_itm t on c.txn_id=t.txn_id
    join pos_swy.1_upc_node_map d on t.upc_id=d.upc_id
    join pos_swy.3_txn_hdr x on t.txn_id=x.txn_id
    group by node_name
    order by max_date desc 

         -- you can change limit 1 to what ever you want to get results
echo_Me
  • 37,078
  • 5
  • 58
  • 78