How to find which table that contain specific data - sqlite

Lets say i have Database with to tables (division_1,division_2)
each table have the same columns ( id, name , salary)
and Jone is person in one of these tables
is there a query in SQL that return in which division Jone is?

Use a union to select from both tables and see which contains the record
select 'division1' as div from division_1 where name = 'Jone'
union
select 'division2' as div from division_2 where name = 'Jone'
BTW if you have the possibility to change the DB design, then do it. There should only be one division table with a column that indicates which one it is.

Related

What is the proper way to make a join query in mariadb?

I have a two table in the database, companies and line_of_business, I'd like to get all data from the table companies together with a line_of_business column in the table type_of_business.
This are the fields for the table companies:
id,
company,
address,
city,
tel_number,
fax_number,
line_of_business(FRK)
And this are the fields for the table line_of_business:
id,
type_of_business,
description
I tried to make a query on the database with the reference from here. http://www.w3schools.com/sql/sql_join_inner.asp, but the result is #1054 - Unknown column 'companies.company' in 'field list', which there exist a company column in the table companies
Here's the query I made,
SELECT `companies.id`, `companies.company`, `companies.address`, `companies.city`, `companies.tel_number`, `companies.fax_number`, `companies.tax_number`, `line_of_business.type_of_business` FROM `companies` INNER JOIN `line_of_business` ON `line_of_business.id`=`companies.line_of_business`
Any idea of the proper way to make a join query?
You can use companies.company instead companies.company for all column

select * from (select...) sqlite python

I'm working on a sqlite database and try to make a special request between two tables.
In the first table (table1 for example), i have two columns named "reference" and "ID". I want to search an ID in it, get it value in "reference" and display all informations from the table which have this value as name.
I try to find something on the internet but I didn't find an answer.
This is the request I made:
select * from (select Reference from table1 where Name='Value1')
It only give me the result of
select Reference from table1 where Name='Value1'
EDIT:
I want
select Reference from table1 where Name='Value1' => name of table
select * from name of table => show all elements
I'm new in sqlite but I hope you can help me.
Thank you by advance
Matt
If I understand your question correctly, I don't think there's a way to do it in sql completely (or at least not in a portable way). I'd recommend one of 3 solutions:
Do exactly what you want, but do some processing in Python. That means query your master table, then construct new query based on each of the rows returned.
If you have many tables, possibly changing dynamically - it may be a good idea to rethink your database design. Maybe you can move some of the changing table names into a new column and put your data in one table?
If you have only a few tables available as the Reference and they never change, you could join all the possible tables, like:
SELECT ... FROM table1
LEFT JOIN table2
ON table1.id = table2.id AND table1.Reference = "table2"
LEFT JOIN table3 ...
But you may need to explain it all a bit better...

How to find 2 matching records comparing two fields in one table to two fields in another table?

Using MS Access 2010, I have two tables that I need to compare and only retrieve the matches.
Table [EE] has 5 fields:
Field4, SSN, Birthdate, Address1, Address2
Table [UPDATED] has fields:
Field4, DOB
and several others not relevant to thus question.
I need to find all records in [EE] from fields Field4 AND Birthdate that have a matching values in BOTH Field4 and DOB in [UPDATED]. I have tried INNER JOIN and it is returning me several duplicates. I have tried:
SELECT EE.Birthdate, EE.Field4
FROM EE, UPDATED
WHERE (EE.Birthdate = UPDATED.DOB)
AND (EE.Field4 = UPDATED.FIELD4)
And
SELECT EE.Birthdate, EE.Field4
FROM INNER JOIN UPDATED ON EE.Birthdate = UPDATED.DOB)
AND (EE.Field4 = UPDATED.FIELD4)
I am getting a lot of duplicate records and only want the records that appear in BOTH tables.
There are probably actually duplicates. Add the DISTINCT keyword to eliminate duplicates:
SELECT DISTINCT EE.Birthdate, EE.Field4
FROM EE
JOIN UPDATED ON EE.Birthdate = UPDATED.DOB
AND EE.Field4 = UPDATED.FIELD4
I also fixed a few syntax errors.
Also, always prefer using proper join syntax.

SQL Server Stored Procedure, selecting rows from multiple tables

I have a stored procedure that is pulling data from one table, now I have got the same data collected in a different table, as the IDs are different I cannot put all data from the second table into the first one.
So now I have to have two select statements in one stored procedure.
Although the corresponding data is same but the column names in both table are different.
For instance BRIEF_TITLE would be briefTitle in the second table.
How can I merge the data from two different tables into one?
The result is bonded to ASP.net grid view control.
As the comments above state, you need something like this:
select BRIEF_TITLE from t1
union all
select briefTitle from t2
This will give you a column name of BRIEF_TITLE, but if you want something else, then add an alias to the first select
select BRIEF_TITLE as ShortTitle from t1
union all
select briefTitle from t2

How do I turn row data into column data?

I have a simple table of data that needs to be transposed from row data into column data. For example let me create a simple employee table:
I need to create a side-by-side comparison report structured like this using the above sql table:
Can someone show me the sql code using the sample table above? Or can it be done automatically using a built in ASP.net or DevExpress control?
Your feedback is always appreciated!
Thanks!
What you're looking for is a "pivot" function.
You can do them by hand inside of SQL, but it also looks like devexpress has a control for this...
http://www.devexpress.com/Products/NET/Controls/ASP/Pivot_Grid/
-- EDIT --
Like the commenter above posted, here's an introduction to the pivot function in SQL Server... What makes this tricky, is that unless you know exactly what values will comprise your columns, you'll have to use dynamic SQL to build the pivot.
Because it can be a little tricky to do in SQL, I'd try to stick to DevExpress since you already have it...
I think that if you were to add multiple data points to your pivot grid, that it would look like what you're expecting. Here's a screen shot from DevExpress that resembles what you're looking for...
Here's the page that shows this technique... In your case, instead of a row grouping, you could just do a "grand total", and then hide that column...
There it´s using SQL:
WITH FieldValueCte AS(
SELECT Name,
'Department' Field,
Department Value
FROM Table1 UNION ALL
SELECT Name,
'Sex' Field,
Sex Value
FROM Table1 UNION ALL
SELECT Name,
'HireDate' Field,
HireDate Value
FROM Table1 UNION ALL
SELECT Name,
'Salary' Field,
Salary Value
FROM Table1 UNION ALL
SELECT Name,
'Comments' Field,
Comments Value
FROM Table1)
SELECT [Field], [John Doe], [Jane Smith], [Peter Parker], [Jessica James]
FROM
(SELECT Field, Name, Value
FROM FieldValueCte) AS SourceTable
PIVOT
(
MIN(Value)
FOR Name IN ([John Doe], [Jane Smith], [Peter Parker], [Jessica James])
) AS PivotTable;
The solution is provided here
http://www.aspsnippets.com/Articles/Rotate-ASPNet-GridView---Convert-GridView-Columns-to-Rows-and-Rows-to-Columns.aspx

Resources