Thank you for all the help and suggestions.
Here is what I ended up doing....
Simple query to return all the data. Then write the c# code to mangle it to what I really want. It was just easier for my skillset that way.
- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Reply to: SQL MAX
Collapse
You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:
- You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
- You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
- If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.
Logging in...
Previously on "SQL MAX"
Collapse
-
Originally posted by LondonManc View PostYou could do that as either a union query for the two types of event or use it as one daase expressions for each. I'd have to invoice you if I spent any longer on it though
Leave a comment:
-
That's what my query does as it orders by deployment descending and groups by batch_id.... I can't be sure that's the most efficient query at a glance but it yields the results.
Leave a comment:
-
Originally posted by stryker007 View PostAm I missing something? because simply this seems to work fine.
select d.batch_id, d.deployment, b.batch_type from deployment d
left join batch b on d.batch_id = b.batch_id
group by d.batch_id
order by d.deployment DESC
oops.. albeit under mysql :-)
Leave a comment:
-
Am I missing something? because simply this seems to work fine.
select d.batch_id, d.deployment, b.batch_type from deployment d
left join batch b on d.batch_id = b.batch_id
group by d.batch_id
order by d.deployment DESC
oops.. albeit under mysql :-)
Leave a comment:
-
Originally posted by LondonManc View PostGive this a try then:
select a.batch_id, a.deployment, b.batch_type
from deployment a join batch b on a.batch_id=b.batch_id
join
(select b1.batch_type, max(a1.deployment) maxdeploy
from deployment a1 join batch b1 on a1.batch_id=b1.batch_id
group by b1.batch_type) c
on b.batch_type=c.batch_type and a.deployment=c.maxdeploy
SQL Fiddle
Leave a comment:
-
Personally I would use a temp table. Or at least heavily comment the sql.
Far more maintainable.
Remember to use KISS methodology...
Leave a comment:
-
Originally posted by ASB View PostVery true. No I (intentionally) didn't. :-)
What I actually need in the final query is (for each batch type), the last one that has actually been deployed and all those that are yet to be deployed.
There are in fact various other tables where data is collected from in the real query I am trying to end up with.
But I have my start pointer thank you.
Leave a comment:
-
Originally posted by LondonManc View PostTo exclude forward dates, you just need an extra condition
Add this line before the one that starts "group by...." :
where a1.deployment < getdate()
The query includes future dates by default because you didn't specify that there could be future dates
What I actually need in the final query is (for each batch type), the last one that has actually been deployed and all those that are yet to be deployed.
There are in fact various other tables where data is collected from in the real query I am trying to end up with.
But I have my start pointer thank you.
Leave a comment:
-
Originally posted by ASB View PostI will give that a go tomorrow and then worry about the union I will need to add all the records with forwards dates.
Add this line before the one that starts "group by...." :
where a1.deployment < getdate()
The query includes future dates by default because you didn't specify that there could be future dates
Leave a comment:
-
Originally posted by LondonManc View PostGive this a try then:
select a.batch_id, a.deployment, b.batch_type
from deployment a join batch b on a.batch_id=b.batch_id
join
(select b1.batch_type, max(a1.deployment) maxdeploy
from deployment a1 join batch b1 on a1.batch_id=b1.batch_id
group by b1.batch_type) c
on b.batch_type=c.batch_type and a.deployment=c.maxdeploy
Leave a comment:
-
Originally posted by ASB View PostSQL server 2008.
select a.batch_id, a.deployment, b.batch_type
from deployment a join batch b on a.batch_id=b.batch_id
join
(select b1.batch_type, max(a1.deployment) maxdeploy
from deployment a1 join batch b1 on a1.batch_id=b1.batch_id
group by b1.batch_type) c
on b.batch_type=c.batch_type and a.deployment=c.maxdeploy
Leave a comment:
-
I had tried that (or at least what I believe to be logically the same) but it still returns each individual row.
Annoyingly of course I could easily just return the entire data set and process that in the consumer but I'm not supposed to do that.
Leave a comment:
-
Originally posted by ASB View PostI wish I didn't have to write this with SQL. But never mind.
I have a table:-
Deployment.
It contains:-
batch id (int)
deployment (datetime)
e.g.
1,13/09/2015
2,14/09/2015
3,15/09/2015
4,10/09/2015
2,11/09/2015
3,12/09/2015
Also:-
Batch.
Containing
batch id (int)
batch type(int)
I am trying to return the batch id, deployment, type
But I only want to return the most recent row for each batch type.
So I am completely stuck; mainly because I know stuff all about SQL. But the join would appear to need some sort of select max(deployment). No idea what though.
Leave a comment:
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- Streamline Your Retirement with iSIPP: A Solution for Contractor Pensions Sep 1 09:13
- Making the most of pension lump sums: overview for contractors Sep 1 08:36
- Umbrella company tribunal cases are opening up; are your wages subject to unlawful deductions, too? Aug 31 08:38
- Contractors, relabelling 'labour' as 'services' to appear 'fully contracted out' won't dupe IR35 inspectors Aug 31 08:30
- How often does HMRC check tax returns? Aug 30 08:27
- Work-life balance as an IT contractor: 5 top tips from a tech recruiter Aug 30 08:20
- Autumn Statement 2023 tipped to prioritise mental health, in a boost for UK workplaces Aug 29 08:33
- Final reminder for contractors to respond to the umbrella consultation (closing today) Aug 29 08:09
- Top 5 most in demand cyber security contract roles Aug 25 08:38
- Changes to the right to request flexible working are incoming, but how will contractors be affected? Aug 24 08:25
Leave a comment: