0

The code runs ok when i don't have the last line (GROUP BY ch.name). When I include the line I receive an error listed below - I am trying to get a query where I can identify and group many things such as grouping transactions by cardholder and grouping from 7:00 - 9:00 AM EST. Still new to coding so a bit frustrated. Any help is appreciated!

ERROR: column "t.date" must appear in the GROUP BY clause or be used in an aggregate function

LINE 1: SELECT ch.name, t.date, t.amount, t.card AS "Credit Card", t...
^
SQL state: 42803
Character: 17

My query:

SELECT 
    ch.name, t.date, t.amount, t.card AS "Credit Card", t.id_merchant,
    m.name AS "Merchan, mc.name AS "merchant category"
FROM
    transaction AS t
JOIN 
    credit_card AS cc ON (t.card = cc.card)
JOIN 
    card_holder AS ch ON (cc.cardholder_id = ch.id)
JOIN 
    merchant AS m ON (t.id_merchant = m.id)
JOIN 
    merchant_category AS mc ON (m.id_merchant_category = mc.id)
GROUP BY 
    ch.name;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You typically `GROUP BY` the same columns as you `SELECT`, except those who are arguments to set functions. – jarlh Feb 07 '20 at 15:45
  • 1
    Show us some sample table data and the expected result - all as formatted text (not images). Take a look at [mcve] too. – jarlh Feb 07 '20 at 15:47
  • You typically combine GROUP BY with set functions, to calculate values per group. – jarlh Feb 07 '20 at 15:48
  • Perhaps you want `ORDER BY ch.name;` instead? – jarlh Feb 07 '20 at 15:50

1 Answers1

0
SELECT ch.name, t.date, t.amount, t.card AS "Credit Card", t.id_merchant, m.name AS "Merchan mc.name AS "merchant category" 
FROM transaction AS t 
JOIN credit_card AS cc ON (t.card = cc.card) 
JOIN card_holder AS ch ON (cc.cardholder_id = ch.id) 
JOIN merchant AS m ON (t.id_merchant = m.id) 
JOIN merchant_category AS mc ON (m.id_merchant_category = mc.id) 
GROUP BY (ch.name, t.date, t.amount, t.card, t.id_merchant, m.name, mc.name);

Try above.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
lion_pankaj
  • 160
  • 1
  • 12