Tuesday, 30 September 2008

SQL queries

Oh woe is me, I know more about SQL than anyone else in the building, and I'm stuck. It can't be this difficult can it?

Can it possibly be the case that there is a minimum IQ below which SQL is witchcraft, and that this IQ level is something like the 99th percentile? That less than 1% of the world's population are smart enough to comprehend SQL?

There's my table:-

It says that on this date, for this part number, a quantity of this many were 'O' (ordered) or 'I' (received at goods in).

What I want is a wee query which tells me how many of each part number are still on order.

I can do this:-

Which neatly tells me how many of each part I've ever ordered and how many of each part I've ever received at goods in. I just can't figure out how to subtract one quantity from the other.

I'm stuck.

This isn't a puzzle like a Rubiks Cube where there is definitely a solution, this is a mystery where is possible that there is no way I can ever know the answer, no way I could ever have gotten to this stage in my life with the knowledge of how to find the answer, no way to save the company, and destined to have ever shorter and shallower relationships with women until the day I pass away.

Having sufficient SQL knowledge isn't in the usual Manufacturing Engineer's skillset, if I'd spent sufficient time learning SQL to have the answer, I wouldn't be a manufacturing engineer, I wouldn't have this job and so this problem would never have come about, etc.

Any help with these problems greatly appreciated, will pay for dinner.

*UPDATED*

I'm getting there slowly, in small bite-sized chunks

This is my query for listing parts ordered:-
SELECT "PARTNO", SUM( "QUANTITY" ) AS "QUANTITY ORDERED"
FROM "INVENTORYOP"
WHERE ( ( "FLAG" = 'O' ) )
GROUP BY "PARTNO"

And this is my query for listing parts received:-
SELECT "PARTNO", SUM( "QUANTITY" ) AS "QUANTITY RECIEVED"
FROM "INVENTORYOP"
WHERE ( ( "FLAG" = 'I' ) )
GROUP BY "PARTNO"

Now I just need to somehow put them into subqueries and subtract one from the other, aye?

No comments:

Post a Comment