Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Yes I am aware of parameterized queries however for simplicity this question is posted without utilization.


Sample Data Below Taken From Access Database
ID Ticket_Year Ticket_Number Ticket_Amount Amount_Paid Transaction_Date_Time
8 2013 06879 256.65 200 2/10/2018 11:02:00 AM
9 2013 06879 256.65 56 2/10/2018 11:03:00 AM
10 2013 06880 29.3 27 2/10/2018 11:55:47 AM
11 2013 06880 29.3 2 2/10/2018 11:55:56 AM
12 2013 06881 58.5 57 2/10/2018 11:57:44 AM
13 2013 06881 58.5 1 2/10/2018 11:57:53 AM
14 2013 06882 36 35 2/10/2018 12:05:05 PM
15 2013 06882 36 1 2/10/2018 12:05:15 PM
16 2013 06883 85.05 80 2/10/2018 12:19:12 PM
17 2013 06883 85.05 5 2/10/2018 12:19:28 PM
18 2013 06884 514.5 200 2/10/2018 1:47:01 PM
19 2013 06884 514.5 200 2/10/2018 2:05:46 PM
20 2013 06884 514.5 110 2/10/2018 2:09:58 PM
21 2013 06886 57.28 57 2/10/2018 7:16:39 PM
Sample of Data Is Above

I would like to select columns [Ticket_Number], [Ticket_Amount], SUM([Amount_Paid]), [Transaction_Date_Time], [Ticket_Year] and then Group The Data By [Ticket_Number] and then order the data by column [Transaction_Date_Time] ASC

For The Example above I would like the query to return
06879 256.65 256 2/10/2018 11:02:00 AM 2013
06880 29.3 29 2/10/2018 11:55:47 AM 2013
06881 58.5 58 2/10/2018 11:57:44 AM 2013
06882 36 36 2/10/2018 12:05:05 PM 2013
06883 85.05 85 2/10/2018 12:19:12 PM 2013
06884 514.5 519 2/10/2018 2:09:58 PM 2013
06886 57.28 57 2/10/2018 7:16:39 PM 2013

What I have tried:

SELECT [Ticket_Number], [Ticket_Amount], SUM([Amount_Paid]), [Transaction_Date_Time], [Ticket_Year] FROM Paid WHERE [Transaction_Date_Time] LIKE '%2018%' ORDER BY [Transaction_Date_Time] ASC GROUP BY [Ticket_Amount]

Above Gives Error About Aggregate Functions

SELECT [Ticket_Number], [Ticket_Amount], SUM([Amount_Paid]), [Transaction_Date_Time], [Ticket_Year] FROM Paid WHERE [Transaction_Date_Time] LIKE '%2018%' ORDER BY [Transaction_Date_Time] ASC GROUP BY [Ticket_Number], [Ticket_Amount], [Transaction_Date_Time], [Ticket_Year]

Which Completes With No Errors However Data Returned Is Identical To Data Orriginal
Posted
Updated 11-Feb-18 18:58pm

1 solution

If the column is within your query, it must be in your group by statement. Ex: your first query only has "Ticket_Amount" in your group by where your second query has all of your columns in the group by statement because they are all in your Select statement which makes sense why the data looks "normal" for you.

You might be better off doing this as a sub query or rethinking what data you need instead of querying all the columns. You may need to rethink your schema as, with my limited understanding, it seems you've got multiple records per ticket in your paid table...i would think you'd have another table called say..."Ticket" with a foreign key to your Paid table and then you'd be able to this query more easily.

Otherwise off the top of my head you'll probably end up just having to do a subquery that would look something like.

SELECT *, (SELECT SUM([Amount_Paid]) FROM Paid AS B WHERE A.Transaction_DateTime LIKE '%2018%' GROUP BY Ticket_Number) FROM Paid AS A ORDER BY Transaction_Date_Time


Given i don't know your schema this is the other alternative subquery form that I think may work better...possibly

SELECT *, (SELECT SUM([Amount_Paid]) FROM Paid AS B WHERE A.Ticket_Bumber = B.Ticket_Number GROUP BY Ticket_Number) FROM Paid AS A ORDER BY Transaction_Date_Time


If you can't alter your schema, the above subquery is the only way i can think of accomplishing what you want to accomplish.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900