I have a table called Store Sales with the following columns
Date
Total Qty Sold
RRP
Total Value Sold
Branch No.
Barcodes
Unit Cost
Then I have another table called ESales that contains this
Inv Date
Our Ship Qty
Unit Price (RRP Inc VAT)
Line Total
Invoice
Order
Line
Brand
Part
Description
Our Order Qty
Unit Price (Exc VAT)
Discount %
Discount Amt (Inc VAT)
Discount Amount (Exc VAT)
Tax Category
Tax Exempt
Group
Sales Cat
Cust. ID
Title
Customer
Name
Tax ID
Rep. ID
Credit Memo
Unit Price
Amount
Category ID
Cust. Amount
Number01
ShortChar01
ShortChar02
Clubcard
There are matching fields but none with the same name. They are
Inv Date = Date
Our Ship Qty = Total Qty Sold
Unit Price (RRP Inc VAT) = RRP
Line Total = Total Value Sold
What I want to do in merge the values in StoreSales to ESales and create additional columns for the data that is not there, these are
Branch No
Barcodes
Unit Cost
Any ideas how to insert the matching values and create the three new ones?
Instead of creating a new table, I would just start with a query that shows you all the data you are describing. The way you merge data from multiple tables together is by using a JOIN command. Here is a example query that shows everything from the ESales table and the additional columns from the StoreSales table you described:
SELECT e.*, s.[Branch No], s.[Barcodes], s.[Unit Cost]
FROM StoreSales s
INNER JOIN ESales e ON
e.[Inv Date] = s.[Date] AND
e.[Our Ship Qty] = s.[Total Qty Sold] AND
e.[Unit Price (RRP Inc VAT)] = s.[RRP] AND
e.[Line Total] = s.[Total Value Sold]
Once you get a query you like, you can save it as a VIEW which essentially lets you interact with the result of this query as if it were a separate table.
I will caution you that you have to be very careful doing this. Usually, data will have some kind of common field like a Order ID or a product SKU that makes it very clear what unique item you are referencing in the database. Joining by things like date, order total, price, etc is bad practice. This is because there is no way to guarantee that there aren't two orders with the same date or the same order total. Those things are not unique to any one particular order.
Related
I am making a query for a price comparison site and I have to provide them a csv file with certain data. I have done the query and the first problem was that the prices that showed up in it were the full prices from sql database. So when we have for example a discount applied to the whole category of products the price shown is still the full price not the discounted one. I solved that by checking if an SKU is in a discounted category and the query calculates the price accordingly.
The new problem is now that a query checks through categories and if one sku is in multiple categories it creates a row with same SKU for each category and I end up with multiple rows with same SKU and only one has correct price (discounted one).
Example query result. NOTE that the Price column is a calculated result not the data from the table.
SKU
Product
Price
A
Ball
19
A
Ball
19
A
Ball
15
B
Cube
10
B
Cube
8
How do I filter it to just this
SKU
Product
Price
A
Ball
15
B
Cube
8
You need GROUP BY with aggregate function MIN()
SELECT SKU
, Product
, MIN(Price) as Price
FROM <table>
GROUP
BY SKU
, Product
We have set up GST application where I am getting difference between reports like monthly item wise sales report against monthly total sales report.
Please suggest me any changes I need to make for following rules:
We have 3 tables
A. Order master - single line for every order is stored with summarized values
B. Order product details - Each line item stored with respective qty, price etc.
C. Order tax details - line item wise tax details having separate row for CGST, SGST etc.
Here is how I am storing the data:
Tax is applicable on every line item [To generate final invoice all items in order group, we create some of all line items and storing value in Order Master.]
We are rounding 2 decimal value for 3 precision [eg. 4.657 = 4.66, 4.643 = 4.64] as per GST council
Rounding value is stored in Order Master table
For discount there are 2 cases
Percent based discount - Let's say if discount is 10% then from every individual line item 10% discount gets deducted than tax will be applicable for each line item
Flat discount - Let's say for 1000/- Rs order somebody wants to give 80/- Rs. discount than from each line item 8% is deducted and than tax will be applied, off course customer may not pay exact 920/- Rs. amount due to reversal of tax amount.
Now when I am generating following reports:
Monthly item wise sales report [Product wise sales report]
Monthly total sales report [Total sales]
Monthly payment type wise report [Bank, Cheque, Cash]
I am getting difference due to rounding of values. Can anyone suggest me which is the best way to set up rounding formula and up to which decimal point should I go to round the values.
I am using SQL server as backend and .net as front end technology.
i have 3 table - first one about supplier details second one about payment for supplier third one orders
all of them have relationship
table payment for supplier include 3 column for three date for payment with
amount in ever date
the question is how to collect date from three table like that:
the header of table
supplier name
first date (month)
from payment date in database
second date
from payment date in database
table contain
first supplier - [sum ] amount for first date - [sum ] amount for second date
second supplier - [sum ] amount for first date - [sum ] amount for second date
and this for all supplier
how to do that??
select
sup.suppliername, pay.firstdate, pay.seconddate
from suppliers sup
left join payments pay
on sup.supplierid = pay.supplierid
or somthing like that?
So I have a product for example cake (It's a soft for a cofeteria). I know that my cake expires after 48h.
Today, for example I have 10 cakes in my stock, and I want to know if some of my cakes are expired, to send it back to deposit, and eliminate it from my stock.
I have my product table which contains product and expiration date in hours (Example: 48 h)
I have another table where I calculate my stock now. (And a procedure who does that).
You might required to add new columns in Production tracking table and Sale table, that might be either BatchNumber or ProductionDate, then get list of cake produced and its batch number subtract with list of cake sold for each BatchNumber based on production time display it as expired or tasty.
SELECT
Product.ProductName,
Production.BatchNumber,
SUM(Production.Qty) - SUM(Sale.Qty) AS Stock
CASE WHEN DATEADD(hh, Product.ExpireInHrs, Production.ProductionTime) >= CURRENT_TIMESTAMP THEN 'Expired' ELSE 'Tasty' END
FROM Product
INNER JOIN Production ON Production.ProductId = Product.ProductId
INNER JOIN Sale ON Sale.ProductId = Product.ProductId AND Production.BatchNumber = Sale.BatchNumber
GROUP BY
Product.ProductName,
Production.BatchNumber
For the query above I presume that you have three tables Product, Production and Sale with BatchNumber columns in Production and Sale table, ProductionTime column in Production table.
I want to display total number of products sold for each product. There are around 30 products in my virtue mart and I want each one's total sold number.
Table named jos_vm_order_item contains product_id and product_quantity with all other information.
How can I display this?
SELECT O.product_id,
SUM(O.product_quantity) as Total_Sold
FROM jos_vm_order_item O
GROUP BY O.product_id