I am trying to merge 2 rows in 1 in SQL Server.
This is what I have:
+---------------+-------+--------+
| Part Number | Code | value |
+---------------+-------+--------+
| 1 | 00 | 12 |
| 1 | 01 | 21 |
+---------------+-------+--------+
This is what I want :
+---------------+-------+--------+
| Part Number | Code | value |
+---------------+-------+--------+
| 1 | 00 | 33 |
I want that remain only the code 00.
You can try something like:
SELECT [Part Number], MIN(Code), SUM(value)
FROM myTable
GROUP BY [Part Number]
use GROUP BY clause
you can try
SELECT PartNumber, MIN(Code), SUM(value) FROM parts GROUP BY PartNumber
#EmanueleVerderame, This is a mock up below, to show you how to prevent errors.
It seems some column, either Code or Value has NULL/Blank values.
DECLARE #parts TABLE (PartNumber INT, Code Char(2), Value INT)
INSERT INTO #parts
SELECT 1,'00', 12 UNION ALL
SELECT 1, '01', 21 UNION ALL
SELECT 2, NULL, 5
--+---------------+-------+--------+
--| Part Number | Code | value |
--+---------------+-------+--------+
--| 1 | 00 | 12 |
--| 1 | 01 | 21 |
--| 2 | | 5 |
--+---------------+-------+--------+
SELECT PartNumber, MIN(isnull(Code, '00') ), SUM(isnull(value, 0)) FROM
#parts
GROUP BY PartNumber
If you want to display only first result then you should use where rownum = 1
Related
I want to get the batch id from stock table on the basic of fifo expiry date. How to get it. I write a query it give the exact batch but batch id not accurate. Basically I want to Fetch the batch is of the product which expired first
The Stock Table is Like as
Batch_ID | Product_ID | Quantity | Is_Expiry |Expiry_Date | Location_ID
6 | 148 | 90 | 1 | 2019-08-24 | 1
4 | 148 | 75 | 1 | 2019-09-13 | 1
2 | 148 | 0 | 1 | 2019-07-11 | 1
I write this Query
Select batch_id,min(datediff(expiry_date,now())) as Remaining_Days From Stock Where Product_ID = '148' and quantity > 0 and Location_ID = '1'
Current Output
Batch_ID | Remaining_Days
4 | 56
Expected Output:
Batch_ID | Remaining_Days
6 | 56
You are using aggregate min function it gives you wrong output.you can use order by function for sorting.
Select batch_id,(datediff(expiry_date,now())) as Remaining_Days From stock
Where Product_ID = '148' and quantity > 0 and Location_ID = '1'
order by Remaining_days
limit 1;
DEMO
I'm currently working on a stored procedure in T-SQL on SQL Server 2012. The task is really tricky: I need to merge (insert,update,delete) entries in multiple tables.
To facilitate, I try to explain my problem with 2 tables. I use Dapper in C# in an ASP.NET 4.5 MVC application and can control my Front-End. I'm passing into the stored procedure 2 Temporary Tables:
Temporary Table Target:
ID | TargetId | TargetGroupId | IsPublic | IsPrivate |
---+----------+---------------+----------+-----------+
0 | 42 | 1 | 0 | 1 |
1 | 0 | 1 | 1 | 0 |
2 | 0 | 1 | 0 | 1 |
Temporary Table Target Countries
ID |CountryId | TargetId |
---+----------+----------+
0 | CA | 42 |
1 | FR | 0 |
2 | AU | 0 |
The TargetId 0 corresponds to a new entry, thus if the TargetId = 0 I want to Insert or Delete an old record not from the list, if the TargetId > 0 I want to Update the record.
The final Result could look like this:
Table Target:
| TargetId | TargetGroupId | IsPublic | IsPrivate |
+----------+---------------+----------+-----------+
| 42 | 1 | 0 | 1 |
| 93 | 1 | 1 | 0 |
| 94 | 1 | 0 | 1 |
Table Country:
CId |CountryId | TargetId |
-----+----------+----------+
34 | CA | 42 |
35 | FR | 93 |
36 | AU | 94 |
So the new TargetId should be stored in Country as well. My current TSQL script looks like this:
DECLARE #TargetGroupId = 1;
MERGE [MySchema].[Target] AS [A]
USING #Targets AS [B]
ON ([A].TargetId = [B].TargetId)
WHEN NOT MATCHED BY TARGET THEN
INSERT (TargetGroupId, IsPrivate, IsPublic)
VALUES (#TargetGroupId, [B].IsPrivate, [B].IsPublic)
-- Desired behaviour: doesn't work unfortunately...
-- OUTPUT inserted.TargetId, [B].ID
-- INTO #tmp;
WHEN MATCHED THEN
UPDATE
SET [A].IsPrivate = [B].IsPrivate, [A].IsPublic = [B].IsPublic
WHEN NOT MATCHED BY SOURCE AND [A].TargetGroupId = #TargetGroupId
THEN DELETE;
This SQL script works fine for my table Target, but how can I update my second table Countries?
Do you know how solve this issue, thus to Insert, Update, Delete in a Merge Statement including several tables?
Thank you very much!!
I'm trying to do some SQL in C# along with an Access Database. I have two tables. Main Table and a Second Table:
MAIN TABLE
DoubleValue | | | |
----------------------------------------
1,40 | | | |
1,80 | | | |
2,00 | | | |
1,80 | | | |
1,60 | | | |
1,60 | | | |
----------------------------------------
SECOND TABLE
DoubleValue | Points | Weight |
-------------------------------
1,00 | 100 | 2 |
1,20 | 98 | 2 |
1,40 | 96 | 2 |
1,60 | 94 | 2 |
1,80 | 92 | 2 |
2,00 | 90 | 2 |
-------------------------------
I need to find all matching rows in "SECOND TABLE" based on the column "Double Value". Then for the rows that matches I want to get the value in columns "Points" and "Weight" as well as multplie those two columns and create a columns with the name "Sum" and add all three columns to the "MAIN TABLE":
MAIN TABLE - RESULT/OUTPUT
DoubleValue | Points | Weight | Sum |
-------------------------------------
1,40 | 96 | 2 | 192 |
1,80 | 92 | 2 | 184 |
2,00 | 90 | 2 | 180 |
1,80 | 92 | 2 | 184 |
1,60 | 94 | 2 | 188 |
1,60 | 94 | 2 | 188 |
-------------------------------------
The "MAIN TABLE" doesn't need to actually have the new columns "physically" inserted. I would very much prefer if they could just be displayed in the output very much like "SELECT Points * Weight AS Sum" would produce where "Sum" would be displayed but not actually inserted in the table. BUT OK, if it needs to actually be inserted then I will go with that.
How can this be done?
You are looking for a simpler INNER JOIN statement. Please notice that Sum is wrapped as it is a Reserved word in most SQL variants. Please try to avoid naming items with these words.
SELECT m.DoubleValue
, s.Points
, s.Weight
, [Sum] = s.Points * s.Weight
FROM MainTable AS m
INNER JOIN SecondTable AS s ON m.DoubleValue = s.DoubleValue
I'm using C# and quite new to SQLite .
I need to update the record field "Value1" by dividing it by a given denominator for a Certain User and date range (First record date to a spicific Date eg. 2012-03-01).
So I need to loop through the data set and if User=John and Date <= than 2012-03-01 then divide Value1 by lets say 10
This obviously can't be done in SQL alone as I need to know what Value1 is, so that I can devided it. How would I go about doing this ?
Below is a example of what data I need to modify
User | Date | Value1 | Value2 | Value3
----------------------------------------------
John | 2012-01-01 | *50 | 5 | 80
John | 2012-02-01 | *100 | 78 | 60
John | 2012-03-01 | *10 | 100 | 5
John | 2012-04-01 | 5 | 89 | 55
John | 2012-05-01 | 15 | 50 | 44
Max | 2012-01-01 | 40 | 25 | 90
Max | 2012-02-01 | 99 | 79 | 30
Max | 2012-03-01 | 100 | 110 | 59
Max | 2012-04-01 | 25 | 29 | 85
Max | 2012-05-01 | 15 | 30 | 88
Modified data divided by 10
User | Date | Value1 | Value2 | Value3
----------------------------------------------
John | 2012-01-01 | *5 | 5 | 80
John | 2012-02-01 | *10 | 78 | 60
John | 2012-03-01 | *1 | 100 | 5
John | 2012-04-01 | 5 | 89 | 55
John | 2012-05-01 | 15 | 50 | 44
Max | 2012-01-01 | 40 | 25 | 90
Max | 2012-02-01 | 99 | 79 | 30
Max | 2012-03-01 | 100 | 110 | 59
Max | 2012-04-01 | 25 | 29 | 85
Max | 2012-05-01 | 15 | 30 | 88
If Value1 was constant then I could use the code below. But it is'nt and it will update 2012-01-01 to 2012-03-01 Value1 will all be 5.
Value1 = 50
Val = Value1 / 10
sql_cmd = sql_con.CreateCommand();
sql_cmd.CommandText = "UPDATE Tablename SET Value1= '"+Val+"' WHERE User = 'John' and Date <= '2012-03-01'";
sql_cmd.ExecuteNonQuery();
You really need to brush up on your SQL syntax, but generally, if you want to use fileds in the current row in an UPDATE statement, you can just put the equation (or string function or whatever is appropriate) in the UPDATE statement itself.
So, for your example, to divide Value1 by 10 for a particular row (or set of rows):
UPDATE Tablename SET Value1 = Value1/10 WHERE User = 'John' and Date <= '2012-03-01'
From your latest comment above, if you need additional conditions to select different rows, put those in the WHERE clause:
UPDATE Tablename SET Value1 = 1 WHERE User = 'John' and Date <= '2012-03-01' and Value1 = 10
So, maybe do some reading about SQL syntax generally (it's a big subject) and then repost any specific questions about syntax.
I have been working on a web based report from an existing client/server apps mssql database. I have a working query, that pulls this information together from many tables, and can provide the query and a result set if necessary.
The results currently are similar to this.
ID | Name1 | Date1 | Veh | PO | Stops
_________________________________________
1 | Bob 1 | 12/1 | Car | 1234 | 4
2 | Sam | 12/3 | Car2 | 2245 | 3
2 | Joe | 12/4 | Van1 | 5568 | 2
3 | Mel | 1/4 | Van2 | 5678 | 5
4 | Mel | 2/2 | Car | 3456 | 4
4 | Sam | 2/3 | Bus | 4565 | 3
4 | Joe | 3/4 | Car | 6766 | 3
The Problem is that I don't know ahead of time if each ID will have 1 or many lines. I need the results to return a combined line, concatanate the data from several columns and add another.
Similar to
ID | Name1 | Date1 | Veh | PO | Stops
1 | Bob 1 | 12/1 | Car | 1234 | 4
2 | Sam, Joe | 12/3 | Car2, Van1 | 2245, 5568 | 5
3 | Mel | 1/4 | Van2 | 5678 | 5
4 | Mel, Sam, Joe | 2/2 | Car, Bus, Car | 3456, 4565, 6766 | 10
I've used a custom user defined function in SQL server that uses the COALESCE function in SQL Server. Here is an example.
Have a look at something like this (Full sql example)
DECLARE #Table TABLE(
ID INT,
Name1 VARCHAR(50),
Date1 VARCHAR(6),
Veh VARCHAR(50),
PO VARCHAR(10),
Stops INT
)
INSERT INTO #Table SELECT 1,'Bob 1','12/1','Car','1234',4
INSERT INTO #Table SELECT 2,'Sam','12/3','Car2','2245',3
INSERT INTO #Table SELECT 2,'Joe','12/4','Van1','5568',2
INSERT INTO #Table SELECT 3,'Mel','1/4','Van2','5678',5
INSERT INTO #Table SELECT 4,'Mel','2/2','Car','3456',4
INSERT INTO #Table SELECT 4,'Sam','2/3','Bus','4565',3
INSERT INTO #Table SELECT 4,'Joe','3/4','Car','6766',3
SELECT *
FROM #Table
SELECT t.ID,
(
SELECT tIn.Name1 + ', '
FROM #Table tIn
WHERE tIn.ID = t.ID
FOR XML PATH('')
) Name1,
MIN(Date1) Date1,
(
SELECT tIn.Veh + ', '
FROM #Table tIn
WHERE tIn.ID = t.ID
FOR XML PATH('')
) Veh,
(
SELECT tIn.PO + ', '
FROM #Table tIn
WHERE tIn.ID = t.ID
FOR XML PATH('')
) PO,
SUM(Stops) Stops
FROM #Table t
GROUP BY t.ID