how to draw data from multiple tables? - sql-server

I have 2 different tables. I need to get a name from the TMK table in table 1 as below, and I need to bring the total number from my 2nd table. I can't write join. can u help me
TMK Table;
| tmkName |
| George |
| Jacob |
flowNewStatus Table;
|statusId|
| 1 |
| 2 |
if george has number 1 status i want this join
| tmkName | |statusId|
| George | | 1 |

Before getting to possible SQL queries... from the tables you show you'd need an additional table that associates the person to status, a join table. Essentially a TMK_status table:
TMK_status table
| personID | statusID |
|----------|----------|
| 1 | 1 |
| 2 | 3 |
| 3 | 1 |
Alternatively, the statusID could be stored as a column of TMK thus,
TMK table
| personID | tmkName | statusID |
|----------|----------|----------|
| 1 | George | 1 |
| 2 | Jacob | 3 |
If by "I can't write join", you mean you don't know how, check this answer: What is the difference between "INNER JOIN" and "OUTER JOIN"? - you will need an inner join.
If, on on the other hand, you mean you can't use join statements, then you could write a subselect statement. There could be other solutions but they depend on how you decide to join/relate the 2 tables.

Related

TSQL query parser in TSQL

I would like to have something like a procedure that takes a query definition as input and output a set of tables containing the individual elements of the query.
Searching the internet for this yields me numerous results in various programming languages, but not in tsql itself. Is there such a resource around?
An example in order to illustrate what I mean by parser:
Input example (any query, really:)
'select t1.col1,t2.col2
from table1 t1
inner join table2.col2
on t1.t2ref=t2.key'
The output, of course, will be a multitude of data. I mentioned tables, but it could be in any form eg an xml. Here is a VERY SIMPLISTIC and arbitrary example of decomposition for the query above:
tables_used:
+----+-----------+--------+------------+
| id | object_id | name | alias used |
+----+-----------+--------+------------+
| 1 | 43252345 | table1 | t1 |
| 2 | 6542625 | table2 | t2 |
+----+-----------+--------+------------+
columns_used:
+----------+-------------+
| table_id | column name |
+----------+-------------+
| 1 | col1 |
| 1 | t2ref |
| 2 | key |
| 2 | col2 |
+----------+-------------+
joins_used:
+-----+-----+-------+-----------------+
| tb1 | tb2 | type | on |
+-----+-----+-------+-----------------+
| 1 | 2 | inner | t1.t2ref=t2.key |
+-----+-----+-------+-----------------+

ACCESS: Compare numbers from different tables

I don't know much Microsoft Access, but I need it to solve this issue:
Let's suppose that I have two tables:
Table A:
Zipcode Start | ZipCode End | Etc1 | Etc2
==============================================================
20000-000 | 29999-999 | Sample data 1 | Another Sample 1
30000-000 | 39999-999 | Sample data 2 | Another Sample 2
40000-000 | 49999-999 | Sample data 3 | Another Sample 3
Table B:
NAME | ZipCode | Etc1 | Etc2
=============================================
John Doe | 31564-888 | |
Johnny | 22559-010 | |
James | 44411-000 | |
How can I compare the Zipcodes on table B with the specified ranges on table A? And return the "Etc1" and "Etc2" that matches it?
Thank you ALL!!
You can do like this:
Select
TableB.Name,
TableB.ZipCode,
TableA.Etc1,
TableA.Etc2
From
TableA,
TableB
Where
TableB.ZipCode Between TableA.[ZipCode Start] And TableA.[ZipCode End]

Join two Select Statements into a single row when one select has n amount of entries?

Is it possible in SQL Server to take two select statements and combine them into a single row without knowing how many entries one of the select statements got?
I've been looking around at various Join solutions but they all seem to work on the basis that the amount of columns is predetermined. I have a case here where one table has a determined amount of columns (t1) and the other table have an undetermined amount of entries (t2) which all use a key that matches one entry in t1.
+----+------+-----+
| id | name | ... |
+----+------+-----+
| 1 | John | ... |
+----+------+-----+
And
+-------------+----------------+
| activity_id | account_number |
+-------------+----------------+
| 1 | 12345467879 |
| 1 | 98765432515 |
| ... | ... |
| ... | ... |
+-------------+----------------+
The number of account numbers belonging to the first query is unknown.
After the query it would become:
+----+------+-----+----------------+------------------+-----+------------------+
| id | name | ... | account_number | account_number_2 | ... | account_number_n |
+----+------+-----+----------------+------------------+-----+------------------+
| 1 | John | ... | 12345467879 | 98765432515 | ... | ... |
+----+------+-----+----------------+------------------+-----+------------------+
So I don't know how many account numbers could be associated with the id beforehand.

Is this a good design for a table?

Each user/person could know one or more languages.
All I can think is a table like
+----------+------+-----+------------+-----+-----+-----+-------+
| PersonID | Java | PHP | Javascript | C++ | C | CSS | HTML |
+----------+------+-----+------------+-----+-----+-----+-------+
| 1 | Yes | Yes | No | Yes | No | Yes | No |
| 2 | No | Yes | Yes | No | Yes | No | No |
| 3 | Yes | No | Yes | Yes | Yes | Yes | No |
+----------+------+-----+------------+-----+-----+-----+-------+
Considering I'm going to need at least 100 columns for all the languages, is it normal to have that many columns? Something tells me this is the wrong approach.
Thank you very much and sorry about my english!
I would suggest you to create three tables.
One table contains the information of the Person like his Name etc.
Second table contains two columns LanguageId and Language name.
+------------+-----------+
| LanguageID | Name |
+------------+-----------+
| 1 | Javascript|
| 2 | C |
| 3 | C++ |
+------------+-----------+
Third table contains the Id, PersonId, LanguageID. In this table you can join the above two tables record.
+---+----------+------------+
|ID | PersonID | LanguageID |
+---+----------+------------+
|1 | 1 | 1 |
|2 | 2 | 2 |
|3 | 3 | 3 |
+---+----------+------------+
Reasons to support my answer:
In future if you want to add any new language in your table then it
would be easier to add that in the main table.
You can join the two tables easily and get the result
A little improvement we can do over Rahul Tripathi response is to remove the "Known" column. You need only two tables for this case. One containing PersonId and LanguageId the person knows. The second table is for the languages only.
You know what languages one person knows by joining both tables. For example if you need to know the list of known languages you can do:
SELECT p.PersonId, l.Name
FROM Person p INNER JOIN Language l ON (p.LanguageId = l.LanguageId)
WHERE (p.PersonId = theIdYouNeedToKnow)

SQL Server. Join two tables n a view, take rows from one and turn into columns [duplicate]

This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 8 years ago.
I'm pretty new to SQL Server so don't really know what I'm doing with this. I have two tables, which might look like this:
table 1
| ID | customer | Date |
| 1 | company1 | 01/08/2014 |
| 2 | company2 | 10/08/2014 |
| 3 | company3 | 25/08/2014 |
table 2
| ID | Status | Days |
| 1 | New | 6 |
| 1 | In Work | 25 |
| 2 | New | 17 |
| 3 | New | 14 |
| 3 | In Work | 72 |
| 3 | Complete | 25 |
What I need to do is join based on the ID, and create new columns to show how long each ID has been in each status. Every time an order goes to a new status, a new line is added and the number of days is counted as in the 2nd table above. What I need to create from this, should look like this:
| ID | customer | Date | New | In Work | Complete |
| 1 | company1 | 01/08/2014 | 6 | 25 | |
| 2 | company2 | 10/08/2014 | 17 | | |
| 3 | company3 | 25/08/2014 | 14 | 72 | 25 |
So what do I need to to to create this?
Thanks for any help, as I say I'm pretty new to this.
I would suggest that AHiggins' link is a better candidate to mark this as a dupe rather than the one that's actually been selected because his link involves a join.
WITH [TimeTable] AS (
SELECT
T1.ID,
T1.[Date],
T2.[Status] AS [Status],
T2.[Days]
FROM
dbo.Table1 T1
INNER JOIN dbo.Table2 T2
ON T2.ID = T1.ID
)
SELECT *
FROM
[TimeTable]
PIVOT (MAX([Days]) FOR [Status] IN ([New], [Complete], [In Work])) TT
;

Resources