SQL Server query on two tables - sql

I have a SQL Server database with two tables, table 1 lists membership records and table 2 lists the names for each membership record. There can be more than one person in table 2 per record in table 1.
table_1.MembershipNumber
table_1.MemberType
table_1.StartDate
table_2.MembershipNumber
table_2.FirstName
table_2.LastName
table_2.Age
I would like to create a view which filters out records in table 1 where the age of anyone in table 2 is over the age of, say, 50.

CREATE VIEW [dbo].[vw_Membership]
AS
Select t.MembershipNumber ,tt.FirstName,tt.LastName
from MembershipNumber_table_1 t
INNER JOIN MembershipNumber_table_2 tt
ON t.MembershipNumber = tt.MembershipNumber
AND MemberType = 'A'
WHERE (DATEDIFF(yy,MembershipNumber_table_2.Age,GetDate()) -
CASE WHEN((MONTH(MembershipNumber_table_2.Age)*100 + DAY(MembershipNumber_table_2.Age)) > (MONTH(GetDate())*100 + DAY(GetDate()))) THEN 1 ELSE 0 END)>50
GO
OR
WHERE DATEDIFF(YEAR, MembershipNumber_table_2.Age, GETDATE())>50

Related

Is there a way to return a partial blank row in SQL when joining tables through automation?

For my project, I have 2 tables. Initially I inner joined both tables (table 1 and 2) via an inner join. However, I wanted an outcome as seen in table 4 where the repeated value from table 1 is left blank instead.
For table 2, the number of rows will always vary. There will always only be 1 department ID attached to numerous function IDs. Is there a query then where regardless of the number of function IDs, the department ID will only appear as the first row as seen in table 4?
(I think to many, this might seem weird and frankly not clean data, but for mail merges within word, it is easier to field code when the data is presented this way to refrain sections from 'reprinting itself'.)
Current Code:
SELECT Table1.*, Table2.* FROM
INNER JOIN Table 2 ON Table1.DepartmentID = Table2.DepartmentID
Table 1:
Department ID
Department
1
XYZ
Table 2:
Department ID
Function ID
Function
1
1
ABC
1
2
DEF
Table 3 (inner joined):
Department ID
Department
FunctionID
Function
1
XYZ
1
ABC
1
XYZ
2
DEF
Table 4 (desired):
Department ID
Department
FunctionID
Function
1
XYZ
1
ABC
2
DEF

Microsoft Access Query - Merging two queries into one

Using the query wizard, I built two different queries doing similar functionalities that I am trying to combine into one query. I have two tables (same structure) that I am matching to find duplicates:
Query #1 is as follows (Include ALL records from Table 1 and only those records from Table 2 where the joined fields are equal are applied to all the columns below):
Match Table 1 Column 3 to Table 2 Column 3
Match Table 1 Column 4 to Table 2 Column 4
Match Table 1 Column 5 to Table 2 Column 5
Match Table 1 Column 7 to Table 2 Column 7
If all of those columns from Table 1 match what’s in Table 2, it will identify the duplicates (I bring in Table 2 Column 7 which will show the duplicates I am looking for).
Query #2 is as follows (Include ALL records from Table 1 and only those records from Table 2 where the joined fields are equal are applied to all the columns below):
Match Table 1 Column 3 to Table 2 Column 3
Match Table 1 Column 4 to Table 2 Column 4
Match Table 1 Column 5 to Table 2 Column 5
Match Table 1 Column 8 to Table 2 Column 8
My second query has the same 3 columns, except the last column is different.
If all of those columns from Table 1, match what’s in Table 2, it will identify the duplicates (I bring in Table 2 Column 7/8 which will show the duplicates I am looking for).
What I am trying to do:
Add an OR statement for the query to show both duplicates on matches for Columns 8 and Columns 7. Such as if Table 1 Column 7 matches Table 2 Column 7 OR Table 1 Column 8 matches Table 2 Column 8, show the duplicates.
Would this require a UNION query?
Here is the query for one of them:
SELECT TABLE1.COLUMN_3, TABLE1.COLUMN_4, TABLE1. COLUMN_7,
TABLE2.COLUMN_7, TABLE1.COLUMN_5
FROM TABLE1
LEFT JOIN TABLE2
ON (TABLE1.COLUMN_7 = TABLE2.COLUMN_7)
AND (TABLE1.COLUMN_3 = TABLE2.COLUMN_3)
AND (TABLE1.COLUMN_4 = TABLE2.COLUMN_4)
AND (TABLE1.COLUMN_5 = TABLE2.COLUMN_5);
The given SQL will not run because of spaces in table and column names.
This example eliminates those spaces.
SELECT
Table1.Column3 , Table2.Column3
,Table1.Column4 , Table2.Column4
,Table1.Column5 , Table2.Column5
,Table1.Column7 , Table2.Column7
,Table1.Column8 , Table2.Column8
From Table1
Left Join Table2
On
( Table1.Column3 = Table2.Column3
AND Table1.Column4 = Table2.Column4
AND Table1.Column5 = Table2.Column5
AND ( Table1.Column7 = Table2.Column7
OR Table1.Column8 = Table2.Column8
)
)
Create query 3; add Q1 and Q2 - join them on F7. This will result in all matches of that field.
Create query 4; add Q1 and Q2 - join them on F8. This will result in all matches of that field.
You now have 2 data sets with those matches (you call duplicates). Then the decision is how to present/display. If you need them in a single record set - write those into a single common temp table. But otherwise you can easily present them together as sub reports/forms.

Finding non-matching data in two tables as per columns and arranging column data as row by row

i have a requirement like below
i have two tables in same data base, both table have same structure and column count.but the columns not present in the same position.
ex:
table 1
id name age
1 dhileep 22
2 uday 33
table 2
id age name
1 20 udayga
2 22 uday
i have id column is same for all tables, if i change the table also i have id same, but may columns name and column count and data count will change.
my final output is:
column_name id table1 table 2
name 1 dhileep udayga
note: i gave above as example, the count of columns is more than 500 and data exist approximately 50000+
use Sql JOIN .to join the 2 tables
use the following answer .i think it is useful for u.
SELECT t1.id,t1.name,t2.name FROM table1 AS t1 JOIN table2 AS t2 ON t1.id = t2.id

SQL Server 2014 Lookup value in Table2 between columns

I have two tables. In Table A I have a "StartDate" besides other values. I also have a column "TimeZone". In Table B I have TimeZone, StartDST, EndDST (DST=DaylightSavingTime) for several years.
I want to check - if StartDate is in Table B between B.StartDST and B.EndDST. If NOT... it should give me 0 else 1.
TimeZone exists multiple Times in B.
FROM DutyList A
WHERE NOT EXISTS (SELECT 1
FROM TimeZoneDST B
WHERE A.StartDuty between B.DSTstart and B.DSTend))
This does not give me all records. I need ALL records from DutyList A.
I need an extra column "DSTexists" 0 or 1 - if StartDuty is in Table B.
I am using SQL Server 2014.
SELECT A.*,
CASE WHEN EXISTS (SELECT 1 FROM TimeZoneDST B
WHERE A.StartDuty between B.DSTstart and B.DSTend)
THEN 1
ELSE 0
END as DSTexists
FROM DutyList A;

List Databases, Tables, Fields, StoredProcedures

My question is pretty much like the question here:
How to find column names for all tables in all databases in SQL Server
But I am not able to comment due to reputation not being 50 or > and I can't put anything in the answer box because you're supposed to put ANSWERS in the answer box.
I got this code from Stack Overflow:
SELECT *
FROM master..sysdatabases
where dbid > 4
--order by dbid
order by name
and it works fantastic - it lists all of our databases.
What I would like to do is get a result table that shows these columns:
DatabaseName, TableSPViewName, FieldName, TypeSize, Indexed
DatabaseName would be JUST the name of the database.
TableSPViewName would contain
the name of all tables in that database
the name of any Stored Procs in that database
the name of all Views in that database
FieldName would be a listing of all fields in that table (if it were a table or view)
TypeSize would be the type and size of that field (like: int, varchar(##), bit ...)
EDIT SEPT 20, 2013 ---------------
ON THIS SITE:
http://blog.sqlauthority.com/2009/04/26/sql-server-list-all-the-tables-for-all-databases-using-system-tables/
I FOUND THIS:
exec sp_msforeachdb 'select "?" AS db, * from ?.sys.tables'
But that SP puts puts up a separate query window for each DB with all its tables so you get
DB with all its tables
DB with all its tables
DB with all its tables
DB with all its tables
I need:
DB 1 table 1 field 1
DB 1 table 1 field 2
DB 1 table 1 field 3
DB 1 table 2 field 1
DB 1 table 2 field 2
DB 1 table 2 field 3
DB 1 table 3 field 1
DB 1 table 3 field 2
DB 1 table 3 field 3
DB 2 table 1 field 1
DB 2 table 1 field 2
DB 2 table 1 field 3
DB 2 table 2 field 1
DB 2 table 2 field 2
DB 2 table 2 field 3
DB 2 table 3 field 1
DB 2 table 3 field 2
DB 2 table 3 field 3
EDIT #2 SEPT 20, 2013 ---------------
ON THIS SITE:
http://blog.clicdata.com/2012/08/02/how-to-list-all-tables-and-columns-with-types-in-a-sql-server-database/
I FOUND THIS:
SELECT tTables.name AS table_name,
tCols.name AS column_name,
tTypes.name,
tTypes.max_length,
tTypes.precision,
tTypes.scale
FROM sys.tables AS tTables
INNER JOIN sys.columns AS tCols
ON tTables.OBJECT_ID = tCols.OBJECT_ID
JOIN sys.types AS tTypes
ON tCols.user_type_id = tTypes.user_type_id
ORDER BY tTables.name;
But that just works for the CURRENT database in use.
It gets me REALLY CLOSE to what I'm looking for but I still need to have one query that does what the above does but for ALL DATABASES we have. See my long list of "DB 1 table 1 field 1" shown above. If we can get the above query to tack on the DATABASENAME onto the left side of what the above produces AND do it for all DBs we'll be in business!
One way to do this is
EXEC sp_msforeachdb ‘SELECT tTables.name AS table_name,
tCols.name AS column_name,
tTypes.name,
tTypes.max_length,
tTypes.precision,
tTypes.scale
FROM sys.tables AS tTables
INNER JOIN sys.columns AS tCols
ON tTables.OBJECT_ID = tCols.OBJECT_ID
JOIN sys.types AS tTypes
ON tCols.user_type_id = tTypes.user_type_id
ORDER BY tTables.name;’
For one complete table, insert results into temp table.