How to write a SQL function into Acces 2003 - sql

So I have an acces table : Data
Data is made of :
Client ID
Product ID
Sell Category ( exemple : Sell Dones, Sell Planed ,Forecast Sales) ,written all in letters
Quantity sold .
unitary price.
I am trying to build a single Acces query which would display things like :
|Client ID||Product ID||Total Sell Dones||Total Sell Planed||Total Forecast Sell||Quantity Sell Dones||Quantity Sell Planed||Quantity Forecast|
How can I do that ?
Like if I had only one type of sell and one single quantity I could simply do :
SELECT (sells*quantity) AS [Total Sells]
But the thing here is have different quantity and different types of sells could I put IF into the select ?
Something like :
Select (IF Data.Sell Category="Planned Sell" THEN quantity Sold*Unitary Price
ELSE 0) as [Total Planned Sell] , etc etc (for every single one ?
The thing is I am using acces 2003 and I dont think I can create a sql function in acces 2003 and use it in my select , but if I can I am willing to do so , else if I can only use "select,delete,update " in acces 2003 I guess I will have to do it like this

Related

Power Pivot / DAX - Add column that flags all entries for customers who meet criteria in any of their rows

I have a large database (2 million rows). There are many columns but the two relevant ones are CustomerID and AccountType. A customer can have more than one account type and if they do this will show as different rows in the database. If any of a customers accounts are AccountType = Premium, then they are a Premium customer.
I want to add a column in PowerPivot that will state whether a customer is Premium or not. So for example:
CustomerID Account Type Custom Column
1 Basic Premium
2 Deposit Not Premium
3 Savings Not Premium
1 Premium Premium
So in my example because customer 1 has a Premium account in the last row, the first row is also flagged as Premium. To make it one step trickier, there are actually a few codes, so it could be Premium1, Premium2 etc.
I think I could do this by creating a separate table and linking the two, but I would prefer to avoid this step if possible to keep the file size down.
Try this in the expression for the calculated column:
Custom Column =
IF (
COUNTROWS (
FILTER (
Table,
[CustomerID] = EARLIER ( Table[CustomerID] )
&& [Account Type] = "Premium"
)
)
> 0,
"Premium",
"Not Premium"
)
It is not tested but should work, let me know if this works for you.

SQL and VBScript

I want to show the quantity and Lbs purchased per customer on a web page without showing duplicate customers. The output value of total lbs is derived from retrieving the total lb in that type of package i.e. 6 for a 6 lbs per package, then in VBScript multiplying it by the quantity in that row.
<% Customer = ("CustomerName")
Qty = ("Qty")
ItemLbs = ("Lbs") * Qty
%>
It works fine without SUMing the customer names but when the duplicate customer names are SUMed the VB equation outputs incorrect information.
I would Like to corect this either via VBScript or SQL
Use GROUP BY and have SQL do it for you:
SELECT CustomerName,sum(qty*lbs) AS TotalWeight FROM yourtable GROUP BY CustomerName

Query cannot be parse SELECT avg_galoon, avg_galoon

I dont know if its the right way to multiply that way, I want to multiply the avg_galoon that is sold weekly by the price of it which is 20
SELECT avg_galoon,
avg_galoon * 20 + NVL(total income , 0)
FROM customer;
total income isn't a column name from the customer table.
If you disagree and can see that column name (via methods mentioned in the comments already) then you will need to enclose that column name in the appropriate syntax for the database engine you are using i.e. [total income] for Microsofts SQL Server, `total income` for MySQL etc.
In future avoid the use of spaces in column names.

SQL : how to Case depending of the result of the 2 latest values of one column

I am discovering SQL as I have to build queries in my new company.I have understood the basic but here is where I am stuck, maybe you could help me figure this out :
I would like to mention a product as unprocurable if sellers rejected my orders twice. Tricky part I aggregate the furniture orders for all our local offices, therefore even though I sent my purchase order(s) to one unique seller (the one with the best offer at the moment) I might have multiple lines for each item (one per office)
See below table for purchase orders, see REF1 item should be set as unprocurable as both on 21 and 31 december my orders have been rejected (no matter the seller)
http://i.stack.imgur.com/r3W3E.jpg
So to put it in logic I would like to have something like this:
For each items with 2 latest purchase orders that were both made at different dates and rejected(0 value in the table) THEN attach a note to it saying "unprocurable" else put as procurable.
IF it was only 1 value I think I could go with
Select
item
, MAX(date)
, case
when confirmed_units = 0
then 'Unprocurable'
else 'procurable'
end
From
purchase_table
Where
date between TO_DATE('01/01/2013', 'MM/DD/YYYY') AND TO_DATE('{RUN_DATE_YYYY/MM/DD}', 'YYYY/MM/DD')
But now I need to check the two latest purchase orders and that are not from the same day.
I am a bit lost, could you give a hand please?
Thanks !
Your question is a little unclear... have you tried using something along the lines of:
SELECT TOP 2 etc, etc... order by [column]

How to use SQL to find second-highest auction bids

I'm working on a small online auction site and I need some sql to determine what each item sold for. The bids table contains the following fields: bidID, itemID, bidderID, bidAmount, bidDate.
The site works basically like eBay, where if the item is currently at $10 and "bidder A" bids $50, the items price will remain $10 until a second bidder places a higher bid. Let's say "bidder B" places a $40 bid, then the item would be at ($40 + increment). The increment is, depending on the auction, either a fixed amount (say $5) or a percentage of the current price.
That's the overview. As for the sql, I think I need to find the highest and second-highest bids for each item and use those to determine the final price.
What's the best way to find each item's second-highest bid?
Also, just as a note, I'm stuck using SQL Server 2000, so the solution can't include ROW_NUMBER() or other more recent built-in functions.
Basically, you could do a TOP 2 and then wrap that in a SELECT statement and get only the one you want (the lower $ amount). Something like this:
SELECT TOP 1 *
FROM (
SELECT TOP 2 *
FROM table
WHERE <criteria match>
ORDER BY amount DESC
) AS newTable
ORDER BY amount ASC
I don't know if this is the most efficient solution but it may work:
SELECT TOP 2 * FROM bids WHERE itemID = ... ORDER BY bidAmount DESC