Access Query 2010 grouping

Hello,

I have a query that pulls dates for different work cells.  Here's the code:

SELECT dbo_projects.job_number, dbo_projects.jobid, dbo_projects_schedule.series, Format(DateAdd("s",[act_start_cut],#1/1/1970#),"mm/dd/yy") AS [CUTTING START], Format(DateAdd("s",[act_end_cut],#1/1/1970#),"mm/dd/yy") AS [CUTTING END], Format(DateAdd("s",[act_start_mach],#1/1/1970#),"mm/dd/yy") AS [MACHINING START], Format(DateAdd("s",[act_end_mach],#1/1/1970#),"mm/dd/yy") AS [MACHINING END], Format(DateAdd("s",[act_start_weld],#1/1/1970#),"mm/dd/yy") AS [WELDING START], Format(DateAdd("s",[act_end_weld],#1/1/1970#),"mm/dd/yy") AS [WELDING END], Format(DateAdd("s",[act_start_prep],#1/1/1970#),"mm/dd/yy") AS [FAB PREP START], Format(DateAdd("s",[act_end_prep],#1/1/1970#),"mm/dd/yy") AS [FAB PREP END], Format(DateAdd("s",[act_start_coat],#1/1/1970#),"mm/dd/yy") AS [COATING START], Format(DateAdd("s",[act_end_coat],#1/1/1970#),"mm/dd/yy") AS [COATING END], Format(DateAdd("s",[act_start_assy],#1/1/1970#),"mm/dd/yy") AS [ASSEMBLY START], Format(DateAdd("s",[act_end_assy],#1/1/1970#),"mm/dd/yy") AS [ASSEMBLY END]
FROM dbo_projects LEFT JOIN dbo_projects_schedule ON dbo_projects.jobid = dbo_projects_schedule.jobid
GROUP BY dbo_projects.job_number, dbo_projects.jobid, dbo_projects_schedule.series, Format(DateAdd("s",[act_start_cut],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_end_cut],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_start_mach],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_end_mach],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_start_weld],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_end_weld],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_start_prep],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_end_prep],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_start_coat],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_end_coat],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_start_assy],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_end_assy],#1/1/1970#),"mm/dd/yy")
HAVING (((dbo_projects.job_number)="12203216") AND ((dbo_projects_schedule.series)="A2"));


and here's the result:

result 1
Notice the highlighted fields.  I want group it somehow to get one record like this:

result 2
Thanks for your help,
Joel
Genius123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
perhaps with Max Clause will make the output... try:

select 
job_number, jobid, series,
IIF(max([CUTTING START]) = #1/1/1970#, 'na', max([CUTTING START])) as [CUTTING START],
IIF(max([CUTTING END]) = #1/1/1970#, 'na', max([CUTTING END])) as [CUTTING END],
IIF(max([MACHINING START]) = #1/1/1970#, 'na', max([MACHINING START])) as [MACHINING START],
IIF(max([MACHINING END]) = #1/1/1970#, 'na', max([MACHINING END])) as [MACHINING END],
IIF(max([WELDING START]) = #1/1/1970#, 'na', max([WELDING START])) as [WELDING START],
IIF(max([WELDING END]) = #1/1/1970#, 'na', max([WELDING END])) as [WELDING END],
IIF(max([ASSEMBLY START]) = #1/1/1970#, 'na', max([ASSEMBLY START])) as [ASSEMBLY START],
IIF(max([ASSEMBLY END]) = #1/1/1970#, 'na', max([ASSEMBLY END])) as [ASSEMBLY END]
from
(
SELECT dbo_projects.job_number, dbo_projects.jobid, dbo_projects_schedule.series, Format(DateAdd("s",[act_start_cut],#1/1/1970#),"mm/dd/yy") AS [CUTTING START], Format(DateAdd("s",[act_end_cut],#1/1/1970#),"mm/dd/yy") AS [CUTTING END], Format(DateAdd("s",[act_start_mach],#1/1/1970#),"mm/dd/yy") AS [MACHINING START], Format(DateAdd("s",[act_end_mach],#1/1/1970#),"mm/dd/yy") AS [MACHINING END], Format(DateAdd("s",[act_start_weld],#1/1/1970#),"mm/dd/yy") AS [WELDING START], Format(DateAdd("s",[act_end_weld],#1/1/1970#),"mm/dd/yy") AS [WELDING END], Format(DateAdd("s",[act_start_prep],#1/1/1970#),"mm/dd/yy") AS [FAB PREP START], Format(DateAdd("s",[act_end_prep],#1/1/1970#),"mm/dd/yy") AS [FAB PREP END], Format(DateAdd("s",[act_start_coat],#1/1/1970#),"mm/dd/yy") AS [COATING START], Format(DateAdd("s",[act_end_coat],#1/1/1970#),"mm/dd/yy") AS [COATING END], Format(DateAdd("s",[act_start_assy],#1/1/1970#),"mm/dd/yy") AS [ASSEMBLY START], Format(DateAdd("s",[act_end_assy],#1/1/1970#),"mm/dd/yy") AS [ASSEMBLY END]
FROM dbo_projects LEFT JOIN dbo_projects_schedule ON dbo_projects.jobid = dbo_projects_schedule.jobid
GROUP BY dbo_projects.job_number, dbo_projects.jobid, dbo_projects_schedule.series, Format(DateAdd("s",[act_start_cut],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_end_cut],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_start_mach],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_end_mach],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_start_weld],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_end_weld],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_start_prep],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_end_prep],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_start_coat],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_end_coat],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_start_assy],#1/1/1970#),"mm/dd/yy"), Format(DateAdd("s",[act_end_assy],#1/1/1970#),"mm/dd/yy")
HAVING (((dbo_projects.job_number)="12203216") AND ((dbo_projects_schedule.series)="A2"))
) as a
Group By job_number, jobid, series

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PatHartmanCommented:
I don't know what the dateadd()s and format()s are all about but your table is not normalized.  If each of the dates were on a separate row, you would use a cross tab to pivot them.

Staying with your current format, assuming that only one row in a set would have any given date column filled in, you could use Max() for each date column

SELECT dbo_projects.job_number, dbo_projects.jobid, dbo_projects_schedule.series, Max([act_start_cut]) AS [CUTTING START], Max([act_end_cut]) as [CUTTING END], ... rest of the dates
Group by dbo_projects.job_number, dbo_projects.jobid, dbo_projects_schedule.series
Order by dbo_projects.job_number, dbo_projects.jobid, dbo_projects_schedule.series;
0
Gustav BrockCIOCommented:
Remove all instances of Format in your SQL, or the "dates" will be text which you can't do any meaningful "Max" on.

Then you can do something like:

IIf(Max([ASSEMBLY START]) = #1/1/1970#, Null, Max([ASSEMBLY START])) As [ASSEMBLY START]

Open in new window


and apply a format like this in the form:

mm/dd/yyyy,,,"N/A"

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Genius123Author Commented:
Thank you.  The Max worked.
0
PatHartmanCommented:
Just FYI, Ryan's SQL will result in the dates being strings.
1
Gustav BrockCIOCommented:
Pat is right. It won't work as is. See my previous note, please.
0
Genius123Author Commented:
Thanks.  I modified Ryan's solution slightly to bring them in as dates.  Here's an example:

CUTTING END: Max(IIf([act_end_cut]<>0,Format(DateAdd("s",[act_end_cut],#1/1/1970#),"mm/dd/yy")))
0
Gustav BrockCIOCommented:
That doesn't help. It is still text you try to "Max".
Study again my comment above. That is how to do it.
0
Genius123Author Commented:
Gustav, you are right.  I changed it and it works great.  Thank you.  I'll try to change the points awarded.
0
Ryan ChongCommented:
eventually you stil need to convert it to string if you want the same field to display as "na" for some of the date values
0
Genius123Author Commented:
Ryan, I just used Null instead of "NA".
0
Gustav BrockCIOCommented:
you stil need to convert it to string if you want the same field to display as "na"

No, the format takes care of that in the form.
0
PatHartmanCommented:
There is one and only one place where you ever have to convert a date to a string and that is when you are creating an SQL string that includes a date value AND you are not using the US mm/dd/yyyy standard.  That is because SQL ASSUMES mm/dd/yyyy format for dates.

Therefore:
if you create a Querydef with:
Where MyDate = Forms!someform!somedate
AND the form field is a datetime datatype, there is no reason to convert the date to a string.

However, if you build the SQL String in VBA,

strSQL = "Select ...From ... Where MyDate = #" Format(& Me.somedate, "mm/dd/yyyy")  & "#"

You do need to reformat to m/d/y order.

SQL will also recognize the unambiguous yyyy/mm/dd order.
0
Ryan ChongCommented:
No, the format takes care of that in the form.
you can do that if you want. there are many ways we can do/suggest here.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.