Access 2010 SQL query works fine by itself but not when linked to access db - sql-server-2005

Access 2010 inventory db of items (item, location, qty etc...).
Linked SQL server 2005 tables of orders.
I've created an Access SQL query to retrieve a list of items for a particular order number. This runs fine by itself, but when I add links to my Access db, asking it for the location and qty, it only shows results for 1 item (last item)?
I feel like I'm missing something simple. I somewhat know my way around access but not so much in SQL and basically new to any programming languages.
Since I can't post photos yet I'll attempt to show examples of what is returned.
SQL Order query
Headers Order_no, Item_no
Results 00123, 77000; 00123, 77013; 00123, 77006; 00123, 77010; etc...
SQL order query with inventory
Headers Order_no, Item_no, Location, Qty
Results 00123, 77006, bin3, 24; 00123, 77006, bin4, 32; 00123, 77006, bin7, 21; 00123, 77006, bin14, 10.
Any ideas would be appreciated. Thanks!

Related

Suiteql/NetSuite query Pivot

I'm fairly new to netsuite and was replicating a sql query to work for netsuite, I was able to replicate the entire query except for the last part that uses pivot.
The snippet of what I am trying is something like;
select * from (select item, price, pricelevelname from itemprice)t PIVOT( sum(price) FOR (pricelevelname) IN ([STOCKING DEALER], [VOLUME DEALER], [WHOLESALE]) as PVT
I am either getting a null_pivoted error or an internal error with any modifications i try, any help would be really appreciated.
Thanks!

MS Access subquery from identical table

I am currently working on an access database where we collect customers feedback.
I have one table with the following structure and data :
And I want to display the following result :
Indeed, what I want is a MS Access Request that displays, for every date value in my table, the amount of records that matches the same date on the column "date_import" (2nd column of the result) and the amount of records that matches this criteria on the column "date_answered" (3rd column of the result).
I have no idea how to do this since all the subqueries should be aware of each other.
Has anyone ever faced this issue and might be able to help me ?
Thanks in advance,
P.S. : I'm using the 2016 version of MS Access but I'm pretty sure what I'm trying to do is also achievable in previous versions of Access, this is what I added several tags.
Hmmm . . . I think this will work:
select dte, sum(is_contact), sum(is_answer)
from (select date_import as dte, 1 as is_contact, 0 as is_answer
from t
union all
select date_answers, 0 as is_contact, 1 as is_answer
from t
) t
group by dte;
Not all versions of MS Access allow union all in the FROM clause. If that is a problem, you can create a view and then select from the view.

SQL query (T-SQL)

I'm looking for a little assistance trying to write an SQL query because, well... I suck at it.
This is a snippet of what my table looks like:
CUSTOMERID DEVICEID STATE
GUID-1 DEVICE-1 UP
GUID-1 DEVICE-2 UP
GUID-1 DEVICE-3 UP
GUID-2 DEVICE-1 DOWN
GUID-2 DEVICE-2 DOWN
GUID-2 DEVICE-3 DOWN
GUID-3 DEVICE-1 UP
GUID-3 DEVICE-2 UP
GUID-3 DEVICE-3 DOWN
In the above example, the customer GUID-1 and GUID-2 have all of their devices UP or all of them DOWN. I want to suppress those results from showing in my query. It is customer GUID-3 that I'm trying to find. This is the customer I want to show in my query results because they have devices that are both UP and DOWN.
For what it is worth, all clients have the same number of devices, in the above example 3, but in my live database more than that.
Any query writing gurus out there?
Chuck
If you just want the customerid, then this is a group by query:
select customerid
from snippet t
group by customerid
having min(state) <> max(state);

SQL multiple aliases alongside query

I'm currently working on a query which pulls out all the items from a trolley that a user has added. The below code demonstrates what I am trying to achieve, where I take all the trolley items and total the quantity up, aswell as multiplying the cost + quantity of each item against eachother and summing those up too. I also want to be able to call out bog standard column names from this query. I'm not too sure about how I can do this other than create 3 queries, one for the trolley itself, one for the total amount of items for the user and one for the total cost of user, however surely it can all be done in one query right?
<cfquery datasoure="#application.datasource#" name="trolley">
Select *, IsNull(Sum(trolley_amount), 0) As trolly_items, IsNull(Sum(trolley_cost * trolley_amount), 0) As trolley_totalcost
From trolley
</cfquery>
I'll give you a coldfusion answer. You can do this in one query to the DB and 1 or 2 query of a query queries. This will "look like" 2 or 3 queries in your code but in reality it will be 1 query (trip to the DB) and 2 "array filtering or aggreegating" operations. In short it would look like this:
<cfquery name="myTrolley" datasource="myDSN">
SELECT Item, quantity, cost, quantity * cost AS totalItemCost
FROM trolley
WHERE userID = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#userid#"/>
<cfquery name="itemTotals" dbtype="query">
SELECT SUM(totalItemCost) AS grandTotal
FROMY myTrolley
</cfquery>
Of course your actual query will differ, but if your goal is to reduce traffic to the DB (a laudable goal that can reap dividends sometimes) then this might be the way to go. Q of a Q is pretty lean and efficient for this sort of thing - though of course it does break down when you try to get overly complex with it.
As Adam said, this is an SQL question.
From the question, I take it that you want to get items in the trolley, the total cost and the total number (total amount) of items being bought.
From my experience and little digging, you cannot do all that in a single SQL statement. Rather, you need two statements. First to get items in trolley and the second will combine total cost and total amount just as you did in the SQL in your question because they are both aggregate functions.
So remove that * in your SQL and create a new SQL.

Calculate result from multi table in database

I have a question in my VB.NET POS Development and could't find a solution myself. I'm currently using VS2010 and MS Access as my database.
I have two database table as shown below:
SalesReceipt(#Receipt_ID, Sales_Date, Receipt_Number, Customer_ID, Sales_BDiscount, Sales_ADiscount, Sales_Payment)
Customer(#Customer_ID, Customer_Name, Customer_Contact, Customer_Debt)
NOTE : BDiscount = Before Discount / ADiscount = After Discount
In my system, one customer can have many SalesReceipt. Now my problem is how can I update the correct Customer_Debt for customer? My logic is update the respective customer's Customer_Debt by looping every row in SalesReceipt and calculate the debt by doing something like :
totalDebt = totalDebt + (Sales_Payment - Sales_ADiscount)
But I not sure how can I make sure it only loop and calculate the result for the selected customer only. How can I achieve this correctly in .NET?
If you want to calculate totalDebt per customer you can use query
SELECT Customer_ID, sum(Sales_Payment - Sales_ADiscount) as totalDebt FROM SalesReceipt
GROUP BY Customer_ID
Result contains totalDebts aggregated by Customer_ID and can be used to update Customer (all the looping and calculating is done by a database engine).
The query can also update be more complex to do even update for you.
Couldn't you just write a query in your Access db that performs your calculation (Sales_Payment - Sales_ADiscount) on your SalesReceipt table grouped by CustomerID?