Table 1: ABC table 2: PQR
code|Name|Amount code|Name|Amount
----+----+----- ----+----+------
1 | A | 1000 1 | A | 1000
2 | B | 2000 2 | B | 2000
3 | C | 4000
4 | D | 1000
data in table 2 is insert from data based on table 1, now by pressing a button named "Remaining" i want to show data that is not present in table 2, to know which tuples i have missed to fill from table 1. How can i do it?
If the fields in the 2 tables are exactly the same and in the same order?
But you can't bother to put those fields in the SQL?
Then you could also use an EXCEPT
SELECT * FROM ABC
EXCEPT
SELECT * FROM PQR;
And if you're not certain that the fields are in the same order?
Then list them in the SQL.
That has also the benefit that the SQL will probably still work when one of the tables is altered.
SELECT [code], [Name], [Amount] from ABC
EXCEPT
SELECT [code], [Name], [Amount] FROM PQR;
It filters out the PQR records that are exactly the same as those found in ABC.
But normally, the methods that Tim Biegeleisen showed are more commonly used.
This answer assumes that you want to find all records in the first table which are not already present in the second table. One option uses a left join:
SELECT t1.*
FROM ABC t1
LEFT JOIN PQR t2
ON t1.code = t2.code AND t1.Name = t2.Name AND t1.Amount = t2.Amount
WHERE t2.code IS NULL;
We could also phrase this using EXISTS:
SELECT t1.*
FROM ABC t1
WHERE NOT EXISTS (SELECT 1 FROM PQR t2
WHERE t1.code = t2.code AND t1.Name = t2.Name AND
t1.Amount = t2.Amount);
Related
I am loading some data into a repeater which is coming from two tables. The query against the second table is only selecting the MAX record though, and because of this complexity, I'm having to create a child repeater to then go off and find the Max record to display.
Table A: Activity List
ID | Activity
----+-----------------------
1 | Change Oil Filter
2 | Change brake fluid
3 | Change brake rotors
Table B: Mechanics Log
ID | ActivityID | Date | Mechanic | Comment
---+-------------+-------------+-------------------------------------------
1 | 1 | 2019-27-06 | John | Changed the oil filter
2 | 1 | 2019-26-06 | Sally | No oil filters in stock.
3 | 2 | 2019-20-06 | Sally | Brake fluid flushed.
As stated above, I can produce the following table using two repeaters (one inside the other) and it looks like this.
ActivityID | Date | Mechanic | Comment
-------------+-------------+-----------------------------------------
1 | 2019-27-06 | John | Changed the oil filter
2 | 2019-20-06 | Sally | Brake fluid flushed.
3 | | |
My question is: How can I produce the same table but using only one repeater and 1 T-SQL query? Is it possible? The reason being is that this is a very simple list (shortened for this demonstration) of the full list I have to enable for my mechanics work log, and when i start going to 100+ activities that can be done on a vehicle, the page loads quite slow; assuming because it has to fire off the 2nd repeater + code for each record it has bound.
I also apologize I do not yet have a 'starting point' for you to work with, as nothing I have created has come even close to producing the result in one query. I am having trouble working out how I combine the first part of the query with the MAX(Date) of the 2nd table. Hoping for some assistance from the community to help.
You can use the below query to get the desired result -
Sample Data
Declare #ActivityList Table
(ID int, Activity varchar(100))
Insert into #ActivityList
values
(1 , 'Change Oil Filter' ),
(2 , 'Change brake fluid' ),
(3 , 'Change brake rotors' )
Declare #MechanicsLog Table
(ID int, ActivityID int, [Date] Date, Mechanic varchar(20), Comment varchar(50))
Insert into #MechanicsLog
values
(1 , 1 , '2019-06-27' , 'John' , 'Changed the oil filter' ),
(2 , 1 , '2019-06-26' , 'Sally' , 'No oil filters in stock.' ),
(3 , 2 , '2019-06-20' , 'Sally' , 'Brake fluid flushed.' )
Query
;With cte as
(select ActivityID, Max([Date]) [date] from #MechanicsLog ml
Group By ActivityID
)
Select al.ID, al.Activity, cte.[Date], Mechanic, Comment
from cte inner join #MechanicsLog ml
on cte.ActivityID = ml.ActivityID and cte.[date] = ml.[Date]
right join #ActivityList al on al.ID = ml.ActivityID
order by ID
If you add use the ROW_NUMBER function to add a sequence to each activity ID, you can then filter that to only get the most recent for each activity ID.
select ActivityID, Date, Mechanic, Comment
from
(
select *, ROW_NUMBER() OVER (PARTITION BY ActivityID order by Date desc) RowNumber
from MechanicsLog
) q1
where RowNumber = 1
This gives you the "MAX" record for each ActivityID but with the rest of the record, so you can join to the Activity List table if you want.
select
act.ActivityID, Max(log.[Date]) as [Date]
from
ActivityList act
inner join
MachineLog log on log.ActivityID = act.ActivityID
Group by
act.ActivityID
Here is my SQL query below. I want to select values from the column names given as variables. Is there any appropriate way of doing this except using a dynamic query?
SELECT EPV.EmployeeCode, #RateOfEmployee, #RateOfEmployer
FROM [HR_EmployeeProvisions] EPV
One way to do this without using dynamic sql is using CASE statement
But this is ugly
SELECT EPV.EmployeeCode, case #RateOfEmployee when 'RateOfEmployee' then RateOfEmployee
when 'X' then X
..
end , case #RateOfEmployer when 'RateOfEmployer' then RateOfEmployer
when 'Y' then Y
..
end
FROM [HR_EmployeeProvisions] EPV
You have to check all the column's in CASE statement.
You can't parameterize identifiers in Sql server, and I doubt it's possible in any other relational database.
Your best choice is to use dynamic Sql.
Note that dynamic sql is very often a security hazard and you must defend your code from sql injection attacks.
I would probably do something like this:
Declare #Sql nvarchar(500)
Declare numberOfColumns int;
select #numberOfColumns = count(1)
from information_schema.columns
where table_name = 'HR_EmployeeProvisions'
and column_name IN(#RateOfEmployee, #RateOfEmployer)
if #numberOfColumns = 2 begin
Select #Sql = 'SELECT EmployeeCode, '+ QUOTENAME(#RateOfEmployee) +' ,'+ QUOTENAME(#RateOfEmployer) +
'FROM HR_EmployeeProvisions'
exec(#Sql)
end
This way you make sure that the column names actually exists in the table, as well as using QUOTENAME as another layer of safety.
Note: in your presentation layer you should handle the option that the select will not be performed since the column names are invalid.
Have a look at UNPIVOT clause - I'm not sure it is applicable for your case but in some circumstances it can be used to query a value by the column name without dynamic SQL:
create table t1 (
a int,
b int,
c int
);
insert into t1 values
(1, 11, 111),
(2, 22, 222),
(3, 33, 333);
select a, col_name, col_value from t1
unpivot (col_value for col_name in (b, c)) as dt;
Result:
| a | col_name | col_value |
|---|----------|-----------|
| 1 | b | 11 |
| 1 | c | 111 |
| 2 | b | 22 |
| 2 | c | 222 |
| 3 | b | 33 |
| 3 | c | 333 |
(SQL Fiddle)
If you only need a value in a depending on some condition on (dynamically) either b or c, you can build the condition on that. If you need either values in column b or c, you can add ... WHERE col_name = ?. If you need more columns, you'd probably need to filter the column values on the un-pivoted table than pivot it again to get the values back in columns.
I have the following tables.
Table 1
Id | Values | Counts
1 | rock | 0
2 | tina | 0
3 | alex | 0
Table 2
Id | Values
1 | rock
2 | alex
3 | alex
4 | rock
5 | rock
6 | tina
As you can see, table 1 contains Values as rock, tina and alex. These column will always have unique values. Counts column should check the count of 'rock' in Table 2 and update it in Counts column. for e.g. rock is shown 3 times in table 2. The counts for rock should be then 3.
Similarly for other values. Can someone pls let me know how can i achieve this using SQL. Here is how the final table should look like.
Table 1
Id | Values | Counts
1 | rock | 3
2 | tina | 1
3 | alex | 2
Any help is appreciated. I searched online and couldnot find a possible solution for this scenario.
You can generally use a JOIN between 2 tables to update Table1 with values from Table2 (or further if you are using bridge tables).
UPDATE t1
SET t1.dataColumn = t2.dataColumn
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.keyColumn = t2.keyColumn
However, when you are using Aggregate functions (such as Count, Sum)you must utilize a subquery for the second table and perform the JOIN to that subquery
UPDATE t1
SET t1.Counts = sb.Counts
FROM Table1 AS t1
INNER JOIN (
SELECT [values], Counts = Count([values])
FROM Table2
GROUP BY [values]
) AS sb
ON t1.[values] = sb.[values]
Running this on your tables gave me this:
SELECT * FROM Table1
id values counts
---- ------- -------
1 rock 3
2 tina 1
3 alex 2
One thing concerning your table design; I generally recommend not using reserved/special/key words when naming tables, columns, or other database objects. I also try to avoid using the generic name id because it can get confusing when you start linking tables to one another, even idTable1 can make things a lot easier
In SQL Server, using a correlated subquery:
update t1
set t1.Counts = (
select count(*)
from t2
where t2.[Values] = t1.[Values]
);
rextester demo: http://rextester.com/SBYNB72372
In MySQL, using a correlated subquery:
update t1
set t1.Counts = (
select count(*)
from t2
where t2.`Values` = t1.`Values`
);
rextester demo: http://rextester.com/DDDC21719
Although this sort of thing might be better calculated in a view instead of stored in the t1 table.
In SQL Server:
create view dbo.t1_with_counts as
select t1.Id, t1.[Values], count(t2.[Values]) as Counts
from t1
left join t2
on t1.[Values] = t2.[Values]
group by t1.Id, t1.[Values]
go
select *
from dbo.t1_with_counts;
In MySQL:
create view t1_with_counts as
select t1.Id, t1.`Values`, count(t2.`Values`) as Counts
from t1
left join t2
on t1.`Values` = t2.`Values`
group by t1.Id, t1.`Values`;
select *
from t1_with_counts;
I would question the wisdom of keeping track of a count in a table like that. That leads to poor relational database structure and management. Instead, I suggest you remove the count column from Table 1. Then, whenever you need to see the counts you use a view:
SELECT t1.ID, t1.VALUES, COUNT(t2.ID) AS VALUE_COUNT
FROM TABLE1 t1 LEFT JOIN TABLE2 t2 ON t1.VALUES = t2.VALUES
This results in a dynamically updated view of your data instead of a static view that has the potential for going stale without your realizing it.
Let's say I have 3 tables - 1 header and 2 detail:
Header Table
id | label
1 | foo
2 | bar
Detail 1 Table
id | date | value
1 | 2015-01-01 | 5
Detail 2 Table
id | date | value
1 | 2015-01-01 | 7
2 | 2016-02-02 | 10
I want to make a linq query that joins all three, but does not eliminate data due to one detail table not having a record where the other one does. The result should look like:
Resulting Table
id | label | date | value1 | value2
1 | foo | 2015-01-01 | 5 | 7
2 | bar | 2016-02-02 | <null> | 10
So, a null for value1, instead of the entire row being removed.
If I were writing SQL, I could write
select
h.id,
h.label,
coalesce(d1.date, d2.date) as date,
d1.value as value1,
d2.value as value2
from
header h
left join detail1 d1
on d1.id = h.id
left join detail2 d2
on d2.id = h.id
and (
d2.date = d1.date
or d1.date is null
)
Is it possible to write this using Linq? I'm using the "on new equals new " syntax, and I cannot figure out how to preserve the detail2 record when there is no matching detail1 record.
Edit: I feel like the linked answer only answers the left join portion of my question. I know I can left join in linq, but the detail2 table is joining on to both header (not a problem) and detail1. If detail1 doesn't have a record for a date in detail2, the detail2 record will not appear in the result. Using "select new{} equals new{}" doesn't allow me to use the detail2 object before the equals, so I can't write
from
h in header.AsEnumerable()
join d1.AsEnumerable().DefaultIfEmpty()
on p.Id equals d1.Id
join d2.AsEnumerable().DefaultIfEmpty()
on new {
Id = h["Id"],
Date = d1["Date"] ?? d2["Date"], // Doesn't work, can't use d2 here.
} // d1 may not have a record, so there may not be a match
equals new {
Id = d2["Id"],
Date = d2["Date"],
}
select new {
// etc...
}
To implement a join with arbitrary conditions, you need to use another from clause with a where to handle your condition. I am not sure if used with Linq to SQL what type of SQL will be produced, you may be better off with my FullOuterJoin/LeftOuterJoin IQueryable extensions.
var ans = from h in header
join d1 in detail1 on h.id equals d1.id into hd1j
from hd1 in hd1j.DefaultIfEmpty()
from d2 in detail2 where h.id == d2.id && (hd1?.date == null || hd1.date == d2?.date)
select new { h.id, h.label, date = hd1?.date ?? d2?.date, value1 = hd1?.value, value2 = d2?.value };
For my Enumerable testing, I put in the conditional operators. You should remove them if testing against IQueryable (e.g. Linq to SQL).
I'm developing a system using WinForms C#.
I have two relational tables:
USER_PROFILE
| ID | NAME |
1 prof1
PERMISSIONS
| UPROFILE_ID | PERMISSION |
1 abc
1 acb
1 bca
I need to show the profiles on screen.
I dont know if it's the best fitting solution for my case, or how to implement it. My idea is to display in a DataGridView like this:
| Profile | Permission1 | Permission2 | Permission3 |
prof1 abc acb bca
Any suggestions?
Thanks in advance.
You can use 'case switch' to convert row into column. In actual select query you can construct column list dynamically.
Example:-
SQL> select id,name from user_profile;
ID NAME
1 U1
2 U2
SQL> select id,pname from per order by 1;
ID PNAME
1 A
1 B
2 C
SQL> select distinct u.name user_name, case when p.pname='A'then p.pname else null end permission1, case when p.pname='B' then p.pname else null end permission2 from user_profile u, per p where u.id=p.id order by 1;
USER_NAME PERMISSION1 PERMISSION2
-------------------- -------------------- --------------------
U1 A
U1 B
U2