I have a need to create a stock report, where I have article number in one stock table and Invoice number under which they were sold in another table.
Stock Table:
Select * from StockTable
result:
artno opening_Stock stock_received
30271472 1 50
Invoice Table:
Select * from InvoiceTable
result:
itemno invoicenumber QTYSold invoicedate
30271472 Inv_123 10 2018-10-06T00:00:00
30271472 Inv_234 20 2018-10-06T00:00:00
30271472 Inv_345 10 2018-10-06T00:00:00
30271472 Inv_567 10 2018-10-06T00:00:00
The Problem is that in Stock Table StockReceived is 50. Now this 50 Quantity can be sold to difference customers in multiple invoices.
My Objective is to show data in most presentation way and then query for that: a) Some option I can think of is to show all invoice numbers in comma saperated, Using XML Path or COALESCE..
b) Second option is to join two table and for each invoice number generate a new row, but in this case Opening_Stoc and Stock Received value will also repeat for each row.
c) Third is to generate dynamic columns for each invoice. Not even sure how to achive this..
Really confused, can somebody help me suggested the best possible way to present this to business and query to achive the same
Regards
Vipin
Related
I am stuck on something, which I have never used in my 10 years of SQL. I thought it would be useful if there was someway of doing this. Firstly I am running SQL Server Express (latest free version) on Windows. To talk to the database I am using SSMS.
There are three tables/queries.
1 table (A) has one data value I want to pull through.
2 tables (B)/(C) have multiple values.
Column common to all tables is CAMPAIGN NAME
Column common to (B)/(C) is PRODUCT NAME
This is an example of the data:
OUTPUT GOAL
I have tried the following:
UNION ALL (but this does not assist when I want to calculate AMOUNT - MARKETING - TOTAL INVESTMENT
I tried PARTITION (but I simple could now get it to work.
If I use joins, it brings through a head count / total investment and marketing cost per product, which when using SUM brings through the incorrect values for head count / total investment and marketing cost vs total amount, quantity.
I tried splitting the costs based on Quantity / Total Quantity or Amount / Total Amount, but the cost associated with the product is not correct or directly relating to the product this way.
Am I trying to do something impossible, or is there a way to do this in SQL?
The following comes pretty close to what you want:
select . . . -- select the columns you want here
from a join
b
on b.campaign_name = a.campaign_name join
c
on c.campaign_name = b.campaign_name and
c.product_name = b.product_name;
This produces a result set with a separate row for each campaign/product.
I am looking for a way to add up averages in SQL. Here is an example of the data I have:
product avg_price
phone 104.28
car 1000.00
And I'm looking to build something like this:
product avg_price
[all] 544.27
phone 104.28
car 1000.00
The way I'm currently doing it is to store the count and sum in two different columns, such as:
product cnt total
phone 203 20,304.32
car 404 304,323.30
And from that get the average. However, I was wondering if it is possible in SQL to just 'keep the fraction' and be able to add them as needed. For example:
product avg_price
[all] [add the fractions]
phone 20,304.32 / 203
car 304,323.30 / 404
Or do I need to use two columns in order to get an average of multiple aggregated rows?
You don't need 2 columns to get the average, but if you want to display as a fraction then you will need both numbers. They don't need to be in 2 columns though.
select product, sum(total) ||'/'||sum(count)
from table a
join table b on a.product=b.product
union
select product, total ||'/'||count
from table a
join table b on a.product=b.product;
In the tab above you can see several records in some cases the Material column of the record n is equal to the record n + 1 and in the Material column Desc the record n is equal to the record n + 1.
But it does not make the columns List Price USD and RVS-ZSEG what happens is that I need to make a group by or combine the pairs in some registers but that they become complementary.
For example that in register
1 and 2 would be converted by combining the values of List Price USD and RVS ZSEG should be shown as image below
Try this:
SELECT Material, MaterialDesc
,MAX([D-Chain-Spec(Status)])[D-Chain-Spec(Status)]
,MAX([List Price USD])[List Price USD]
,MAX([RVS - ZSEG])[RVS - ZSEG]
FROM YourTable
GROUP BY Material, MaterialDesc
From what I see you just need a sum on List Price USD and RVS-ZSEG right?
SELECT Material, MaterialDesc,[D-Chain-Spec(Status)]
,SUM([List Price USD]) as [List Price USD]
,SUM([RVS - ZSEG]) as [RVS - ZSEG]
FROM Table
GROUP BY Material, MaterialDesc,[D-Chain-Spec(Status)]
I'm sure if this is possible in SQL but my combination of case statements and wheres aren't working. This is some test data in the shape I'm using..
It shows items in an Order. In this order, the customer has amended the order for pens and increased the amount to 15. So the original order item, id 123, is marked as superceded and a new item row is created, id 158, and the PreviousVersion column is populated with the previous items itemId. AmendedStatusId is the status of the amended item. So in the example ItemId 158 is the updated version of ItemId 123. And the extra pens haven't been paid as they are AwaitingApproval. I know it's not the best laid out data but it's what I've to work with.
What I'm trying to do is when the amended items haven't been paid to select the old item, so in this example return ItemIds 123 and 124. When AmendedStatusId of ItemId 123 is updated to Paid, I would want to return ItemId 124 and 158. Is this possible?
Thanks in advance :)
This sort of structure should get you started.
select isnull(paidItemId, unpaidItemId) itemId
from yourTables
left join (subquery to identify paid items) paidItems on something
left join (subquery to identify unpaid items) unpaidItems on something
etc
Could you please help me figuring out how to accomplish the following.
I have a table containing the number of products available between one date and another as per below:
TABLE MyProducts
DateProduct ProductId Quantity Price
26/02/2016 7 2 100
27/02/2016 7 3 100
28/02/2016 7 4 100
I have created a form where users need to select a date range and the number of products they are looking for (in my example, the number of products is going to be 1).
In this example, let's say that a user makes the following selection:
SELECT SUM(MyProducts.Price) As TotalPrice
FROM MyProducts WHERE MyProducts.DateProduct
Between #2/26/2016# And #2/29/2016#-1 AND MyProducts.Quantity>=1
Now the user can see the total amount that 1 product costs: 300
For this date range, however, I want to allow users to select from a combobox also the number of products that they can still buy: if you give a look at the Quantity for this date rate, a user can only buy a maximum of 2 products because 2 is the lowest quantity available is in common for all the dates listed in the query.
First question: how can I feed the combobox with a "1 to 2" list (in this case) considering that 2 is lowest quantity available in common for all the dates queried by this user?
Second question: how can I manage the products that a user has purchased.
Let's say that a user has purchased 1 product within this date range and a second user has purchased for the very same date range the same quantity too (which is 1) for a total of 2 products purchased already in this date range. How can I see that for this date rate and giving this case the number of products actually available are:
DateProduct ProductId Quantity Price
26/02/2016 7 0 100
27/02/2016 7 1 100
28/02/2016 7 2 100
Thank you in advance and please let me know should you need further information.
You could create a table with an integer field counting from 1 to whatever max qty you could expect. Then create a query that will only return rows from your new table up to the min() qty in the MyProducts table. Use that query as the control source of your combobox.
EDIT: You will actually need two queries. The first should be:
SELECT Min(MyProducts.Quantity) AS MinQty FROM MyProducts;
which I called "qryMinimumProductQty". I create the table called "Numbering" with a single integer field called "Sequence". The second query:
SELECT Numbering.Sequence FROM Numbering, qryMinimumProductQty WHERE Numbering.Sequence<=qryMinimumProductQty.MinQty;
AFAIK there is no Access function/feature that will fill in a series of numbers in a combobox control source. You have to build the control source yourself. (Anyone with more VBA experience might have a solution to solve this, but I do not.)
It makes me ache thinking of an entire table with a single integer column only being used for a combobox though. A simpler approach to the combobox would just to show the qty available in a control on your form, give an unbound text box for the user to enter their order qty, and add a validation rule to stop the order and notify them if they have chosen a number greater than the qty on hand. (Just a thought)
As for your second question, I don't really understand what you're looking for either. It sounds like there may be another table of purchases? It should be a simple query to relate MyProducts to Purchases and take the difference between your MyProducts!qty and the Purchases!qty. If you don't have a table to store Purchases, it might be warranted based on my cursory understanding of your system.