I have some data within a table I am importing into an ERP solution. The data is presented like this:
buyer name,order id,shipment item id,sku,quantity shipped, price etc
The shipment item ID is a unique value and is the primary key along with the order id.
My problem is this:
I need to locate each distinct order and create a sales order based upon that information.
The issue I am having is:
Each shipment item ID has its own row within the database and a simple
while(reader.read()){
...logic here...
}
will not work as it will attempt to create n amount of sales orders for the same customer.
I need to formate a query that will take each shipment item ID and put its SKU, quantity etc into one row so I can attach it properly but I am unsure how this would work.
Any help would be much appreciated.
You need to use a group by clause to deduplicate the rows to get the top level information
SELECT [buyer name], [order id], etc
FROM orderTable
GROUP BY [buyer name], [order id], etc
WHERE [order id] = 1
then select all the rows seperately
SELECT *
FROM orderTable
WHERE [order id] = 1
I have found a solution with the ERP software provider which will allow me to use a basic SQL statement and do an update on the any sales order which has a primary key of the order ID. This will allow each corresponding shipment item id to be added to the sales order without having to do extra SQL "massaging" as if the record exists it will update it, otherwise it will create a new one.
Thanks everyone for your suggestions.
Related
I am making an attendance management system where users/employees can mark their attendance once a day. The admin can see the overall attendance of the employees. I have to show this in a tabular form.
I have made 2 tables in SSMS: tblUser and tblAttendence. tblUser contains RegNo(int), Name(varchar) and JoiningDate(date). tblAttendence has DateAndTime(datetime) and RegNo(int) as columns. So whenever a user opens the application and marks the attendance, it will be recorded in tblAttendance with the current DateTime and the RegNo of the user.
Now, I have to display this using a DataGridView with the columns as RegNo, Name and Attendance(this will be the number of entries found in tblAttendance corresponding to the RegNo)
Can this be done? I can't seem to find out a way to do this. Is there a way to do it?
You can obtain the count of the attendances by user with
SELECT u.RegNo, u.Name, Count(t.RegNo) AS Attendance
FROM tblUser u JOIN tblAttendance t ON u.RegNo = t.RegNo
GROUP BY u.RegNo, u.Name
and if you like an order you can add
ORDER BY Count(t.RegNo) DESC
Im making an ordering system where there is a product,supplier and order table. What I'm trying to do is when you order, it can have multiple product and one supplier. Example us OrderID 001 it can have 3 products from product table and 1 supplier from supplier table. How can I do this?
Sorry for asking too much but I don't have a code yet for this part of the system as I don't know where to begin. Thank you.
Create an Orders Table.
Add all ordered products to the order Table.
In the table the three products would all have the same order_id, but a different or (in case someone bought two of the same) same product. You will also need to track the amount they purchase with each row, in case the amount changes.
Select Sum(purchase_amount) from orders where order_id = "YOUR_ORDER_ID"
Select * from orders where order_id = "YOUR_ORDER_ID"
...3 rows show up
You may want to have an order_summary table as well that contains the total amount, etc.
I have a database table like this:
I want to get the the row number of the second row. I use the following code:
SELECT ROW_NUMBER() OVER(ORDER BY Name) From Deposit WHERE Name='Murali'
But, its not working. Whats wrong with the code?
Thanks in advance.
The ROW_NUMBER function returns the row number in the resulting dataset.
In your query you restricted the results to only those whose name is Murali. Since you have only one such record, it is normal that it will return 1.
In SQL there's no such notion as row number. Table rows do not have an order. The notion of order only makes sense when you make a SQL query. Without SQL query you simply cannot talk about order and row numbers.
It appears that you need to introduce some order number for each user. The correct way to implement this is to add an Order column to your Deposit table. Now in order to retrieve it you would use the following query:
SELECT [Order] From Deposit WHERE Name = 'Murali'
All that's left is make the Order column to autoincrement and you are good to go. Everytime a new record is inserted the value will be automatically incremented. So there you go, now you have an order which represents the order in which the records have been inserted into the table. You now have context.
Perhaps something like this (if I understood you correctly):
SELECT Q.RN FROM (
SELECT ROW_NUMBER() OVER(ORDER BY Name) AS RN, * From Deposit
) AS Q
WHERE Q.Name = 'Murali'
Try this
WITH TempTable AS
(
SELECT Name,ROW_NUMBER() OVER (ORDER BY Name) AS 'RowNumber'
FROM Deposit
)
SELECT RowNumber,Name
FROM TempTable
WHERE Name='Murali'
I have the following query that I use to pull data out of a mysql database.
SELECT ProductCode, ProductName, ProductCategory
FROM Product
WHERE <<Criteria goes here>>
The data returned by the query is consumed by the .NET Application and I am getting a million rows which I am going through one by one through the DataReader.
Now I have to show the progress bar telling users how many percentage of rows have been received. Which means I need a total number
A friend suggested me to add COUNT(*) as an extra column, which will have the repeated number but we don't have to do two round-trips one to get count and other to get the rows.
SELECT ProductCode, ProductName, ProductCategory, COUNT(*)
FROM Product
WHERE <<Criteria goes here>>
mysql is giving back only one record when I add COUNT(*).
And 2nd, I am not sure if this is a good idea.
Please share what you know about this Thanks
You can use SQL_CALC_FOUND_ROWS
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
e.g.
SELECT SQL_CALC_FOUND_ROWS, ProductCode, ProductName, ProductCategory
FROM Product
WHERE <>
and then run this query to get number of rows without filtering
'select FOUND_ROWS();'
A DBMS might start returning rows to the client before the answer set is fully created, then the only way to get a 100% correct value is to run the select with a
SELECT COUNT(*) FROM (your query) x;
before you run the query or to use count as a window function, but this is not supported by MySQL:
SELECT ProductCode, ProductName, ProductCategory, COUNT(*) OVER ()
FROM Product
WHERE <<Criteria goes here>>
Assuming you are loading the retrieved records in a DataTable, just check the DataTable.Rows.Count() property to see how many you have.
In my application, i want to show the newly added RECORDS by an import operation in a gridview. Is there is any method in sql to retrive newly added rows.
I tried to do it in using code and tried to get the difference before and after the insertion and its working perfectly but makes the application very slow. So, i want to do it in database itself.
Im using Mysql, ASP.NET.
Eg:
table may have these records before the import operation
ID Name
1 A
2 B
3 C
and after import the table may be like this.
ID Name
1 A
2 B
3 C
4 D
5 E
6 F
I want result like
ID Name
4 D
5 E
6 F
You need to have AUTO_INCREMENT column defined on table or alternatively you can use TIMESTAMP field to retrieve newly added records, try this:
SELECT *
FROM table_name
ORDER BY id DESC
LIMIT 10;
For single row insert you can use LAST_INSERT_ID after you INSERT query:
SELECT LAST_INSERT_ID();
For multi-row insert you can follow these steps:
START TRANSACTION;
SELECT MAX(id) INTO #var_max_id FROM table_name;
INSERT INTO table_name VALUES(..),(..),...;
SELECT MAX(id) INTO #var_max_id_new FROM table_name;
COMMIT;
SELECT *
FROM table_name
WHERE id BETWEEN (#var_max_id + 1) AND #var_max_id_new;
i think this will be more simple:
SELECT MAX(id) INTO #Max_table_Id FROM table;
// Insert operation here//////
SELECT * FROM table WHERE id>#Max_table_Id;
In case you use auto incremental IDs for your records, you can use:
SELECT * FROM [table] ORDER BY [id column] DESC LIMIT [number of records]
Otherwise you should add a TIMESTAMP colum to your records for this purpose and select by this column.
Personally, if there is an option, I wouldn't use the record IDs for this, as it is not what they are for. Record IDs can change throughout the lifetime of an application and they don't necessarily represent the order in which the items were added. Especially in data import/export scenarios. I'd prefer to create special columns to store such information, e.g. "CreatedAt", "ModifiedAt".