I have 3 tables:
Table 1
CustomerID Name Sname Zipcode
1 test1 test1 xxx2
2 test2 test2 yyy2
3 test3 test3 xxx2
Table 2
StaffID Name sname
1 Jack Mack
2 Jhon Addison
Table 3
ID CustomerID StaffID
1 1 1
I am not sure if SQL can do this, So in C# what I want is if StaffID 2 is not in Table 3 and if a staff wants to visit all the customers in zipcode xx2 then by click on a visit button he should be inserted for all the zipcode. So if I select Zipcode xxx2 then the query should first check if I am not visiting same Zipcode for a specific, if not then add the person into table 3 so the updated table 3 will look something like this:
ID CustomerID StaffID
1 1 1
2 1 2
3 3 2
As you can see, StaffID 2 would like to visit xxx2 zipcode as a result he gets 2 entries into Table 3 because there are two CustomerID with Zipcode same as xxx2.
I have did some online research I would this IF EXISTS (SELECT but it just add one entry. Also I guess I will be expecting 3 parameter from C# zipcode, CustomerID and StaffID
From your description it appears you just need a simple insert statement.
INSERT INTO Table3 (CustomerID, StaffID)
select CustomerID, #StaffId from Table1
where Zipcode = #zipcode
and NOT EXISTS(select StaffID from Table3 where StaffID = #staffId)
Try this sql query, assuming you are passing #StaffId, #CustomerId and #Zip as parameter
INSERT INTO Table3 (CustomerID, StaffID)
SELECT CustomerID, #StaffId FROM Table1
WHERE Zipcode = #zip
AND NOT EXISTS(SELECT StaffID FROM Table3 WHERE StaffID = #staffId
AND CustomerID = #CustomerId)
Related
I want to generate a unique PrescriptionNo for each of the Prescription based on the shopid.
I have tried the following way
id PrescriptionNo Shopid Amount
1 PRES001 2 100
2 PRES002 2 200
3 PRES001 1 100
4 PRES003 2 200
select top 1 'PRES' + right('000' + CAST(ROW_NUMBER() over (order by id) + 1 AS VARCHAR(3)),3)
from prescription
where shopid = 2
order by id desc
Try this:
create table #tmp1 (id int, prescriptionno varchar(10), shopid int, amount int);
insert into #tmp1 values (1,'PRES001',2,100);
insert into #tmp1 values (2,'PRES002',2,200);
insert into #tmp1 values (3,'PRES001',1,100);
insert into #tmp1 values (4,'PRES003',2,200);
select 'PRES' + RIGHT(concat('000',ISNULL(max(right(prescriptionNo,3)),0)+1),3)
from #tmp1
where shopid = 3
=> Returns 'PRES0001'
select 'PRES' + RIGHT(concat('000',ISNULL(max(right(prescriptionNo,3)),0)+1),3)
from #tmp1
where shopid = 2
==> Returns 'PRES0004'
By using just max() allows you to use ISNULL(..,0).
I need to make an upsert to a table.
Using Oracle.
I created a temporary table that has the same columns as the original.
I want to merge the temp one into the original but without replacing null values from the temp table, i.e:
table1:
Id, First_Name, Last_Name, Salary
123, Bob, Foo, 100
table2:
Id, First_Name, Last_Name, Salary
123, NULL, NULL, 200
After merge:
table1:
Id, First_Name, Last_Name, Salary
123, Bob, Foo, 200
I tried constructing this:
MERGE INTO t1 wu USING t2 tmp ON (wu.Id = tmp.Id) WHEN MATCHED THEN
CASE WHEN tmp.First_Name IS NOT NULL THEN
UPDATE SET wu.First_Name = tmp.First_Name
ELSE
UPDATE SET wu.First_Name = wu.First_Name
END
CASE WHEN tmp.Last_Name IS NOT NULL THEN
UPDATE SET wu.Last_Name = tmp.Last_Name
ELSE
UPDATE SET wu.Last_Name = wu.Last_Name
END
CASE WHEN tmp.Salary IS NOT NULL THEN
UPDATE SET wu.Salary = tmp.Salary
ELSE
UPDATE SET wu.Salary = wu.Salary
END
WHEN NOT MATCHED THEN
INSERT VALUES(tmp.Id, tmp.First_Name, LastName, tmp.Salary);
But I get:
ORA-00927: missing equal sign
I do not want to use PL/SQL - all from c# directly.
NVL might help.
Test case:
SQL> create table t1 (id number, first_name varchar2(10),
2 last_name varchar2(10), salary number);
Table created.
SQL> create table t2 (id number, first_name varchar2(10),
2 last_name varchar2(10), salary number);
Table created.
SQL> insert all
2 into t1 values (123, 'Bob', 'Foo', 100)
3 into t2 values (123, null, null, 200)
4 into t2 values (555, 'Little', 'Foot', 20)
5 select * From dual;
3 rows created.
Merge:
SQL> merge into t1 using t2 on (t1.id = t2.id)
2 when matched then update set
3 t1.first_name = nvl(t2.first_name, t1.first_name),
4 t1.last_name = nvl(t2.last_name , t1.last_name),
5 t1.salary = nvl(t2.salary , t1.salary)
6 when not matched then insert
7 values (t2.id, t2.first_name, t2.last_name, t2.salary);
2 rows merged.
Result:
SQL> select * from t1 order by id;
ID FIRST_NAME LAST_NAME SALARY
---------- ---------- ---------- ----------
123 Bob Foo 200
555 Little Foot 20
SQL>
i want to fetch all the records having max product id from product table. the records in the product table are stored as category wise like following
Product id Category id Product name Product image
1 1 product 1 path
2 1 Product 2 path
1 2 Product 1 path
2 2 Product 2 path
3 2 Product 3 path
1 3 Product 1 path
and i have Category table like following
Category id Category name
1 Category 1
2 Category 2
3 Category 3
so i need to write query that return all records of max product in each category (1,2,3 here in this example) so we'll get the result like
# Product id Category id Product name Product image
1. 2 1 product 2 path
2. 3 2 product 2 path
3. 1 3 product 1 path
note:- product id is primary key and generated according to it's category
category id is foreign key in product table.
i am using this query/database in my C# application. I am using MS-Access.
Ok this will help:
"SELECT * From Product WHERE Categoryid=(SELECT Max(Count(*)) from product group by Categoryid)"
this should help
SQL Server -
select p.* from
(
select max(productid) productid,categoryid from products group by categoryid
) a join products p on p.productid = a.productid
and p.categoryid = a.categoryid
ACCESS -
select [p].* from
(
select max(productid) as pid,categoryid from products group by categoryid
) as [a] inner join [products] as [p] on [p].[productid] = [a].pid
and [p].categoryid = [a].categoryid
I am populating a combobox in C# & SQL.I have two tables to get data from them.
Table1
Id CategoryName
1 Salary
2 Utility
3 Other
Table2
Id SubCategoryName CatId
1 XYZ Salary 1
2 YY Salary 1
3 Car Exp 3
Now i want to show them all in a dropdown so that User select any of them and then i will save its Id.
How i will Know that selected Id is from Table1 OR Table2 ?
You will have to play around with fake id's. For example, you could create a new class with a new Id and map that new id to the tables manually. For example:
Class DisplayItem
Id 1 Name Salary
Id 2 Utility
Id 3 Other
Id 4 XYZ Salary
ID 5 Y Salary
Id 6 Car Exp
Then you will need code that looks like this
public int GetTableId(int DisplayItemId)
{
if (DisplayItemId < 4)
return 1;
return 2;
}
I have successfully simulated an Oracle CONNECT BY statement in SQL Server 2008 by following these 2 previous answers here and here and adjusting to get the results I need. But how do I do this in LINQ?
Here is an example of what I am doing using a dummy database:
CREATE TABLE Employee(
EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
Department INT NOT NULL,
EmployeeName VARCHAR(40) NOT NULL,
PeckingOrder INT NOT NULL,
HigherDepartment INT NULL)
INSERT INTO Employee (Department,EmployeeName,PeckingOrder,HigherDepartment)
VALUES (1,'Bart',1,NULL),(2,'Homer',1,1),(2,'Marge',2,NULL),
(3,'Lisa',1,2),(3,'Maggie',2,2),(3,'Santas Helper',3,1)
EmployeeID Department EmployeeName PeckingOrder HigherDepartment
1 1 Bart 1 NULL
2 2 Homer 1 1
3 2 Marge 2 NULL
4 3 Lisa 1 2
5 3 Maggie 2 2
6 3 Santas Helper 3 1
and this is the SQL used to return the heirachy:
WITH n(level, PeckingOrder, Department, EmployeeName, HigherDepartment) AS
(SELECT 1, PeckingOrder, Department, EmployeeName, HigherDepartment
FROM Test.dbo.Employee
WHERE Department = 3
UNION ALL
SELECT n.level + 1, nplus1.PeckingOrder, nplus1.Department, nplus1.EmployeeName, nplus1.HigherDepartment
FROM Test.dbo.Employee as nplus1
JOIN n ON n.HigherDepartment = nplus1.Department)
SELECT MAX(level) AS level, PeckingOrder, Department, EmployeeName, HigherDepartment
FROM n
GROUP BY PeckingOrder, Department, EmployeeName, HigherDepartment
ORDER BY MAX(level) DESC, PeckingOrder ASC
level PeckingOrder Department EmployeeName HigherDepartment
3 1 1 Bart NULL
2 1 2 Homer 1
2 2 2 Marge NULL
1 1 3 Lisa 2
1 2 3 Maggie 2
1 3 3 Santas Helper 1
You could use ExecuteQuery:
class YourRow
{
public int level {get; set;}
public int PeckingOrder {get; set;}
...
}
using (var db = new LinqDataContext())
{
var list = db.ExecuteQuery<YourRow>(
#"
WITH n(level, PeckingOrder, Department, EmployeeName, HigherDepartment) AS
(SELECT 1, PeckingOrder, Department, EmployeeName, HigherDepartment
...
";
}
Or perhaps better, create a view that contains the query, and use LINQ to read from the view.