how to select 2 tables data using single query in MSSQL [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am working on Library Management and for making a report I want to get data from 2 tables which are not interlinked i.e issueData and issueRecord.
The Record contains previous book issued data while the Data table contains only the current book issued Report.
Simply, I want to merge these two queries i.e
Select * From issueRecord where issueDate = '19/07/2013'.
Select * From issueData where issueDate = '19/07/2013'.
Please Help.

If your question was more clear we would be able to help more.
From what I understand you are trying for a union all or union. There is also a chance for cross join also, but that may not be the result you wanted.
There are answers for Union and union all, But I would suggest you to use like the below
Select 'Record', Field_1, Field_2, Field_3 From issueRecord where issueDate = '19/07/2013'
UNION
Select 'Data', Field_A, Field_B, Field_C From issueData where issueDate = '19/07/2013'
With this addition you can find which data is from which table.
In addition to this you can also use cross join
select * from issueRecord CROSS JOIN issueData
but check the data what you are getting.

Your question is not very clear but it seems like you are looking for a UNION or UNION ALL query.
Assuming that what you want is a single result set that includes record from each table, but you are not trying to combine the two tables onto a single line, the syntax would be something along the lines of:
Select Field_1, Field_2, Field_3 From issueRecord where issueDate = '19/07/2013'
UNION
Select Field_A, Field_B, Field_C From issueData where issueDate = '19/07/2013'
The type and order of the columns selected from both tables have to match. UNION will collapse identical records into one output row, just like a distinct clause, while UNION ALL will include every record from each query.

Related

Why can't we update a view which has DISTINCT Clause in its definition? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am not getting the logic behind the fact that a view can't be updated if it has DISTINCT Clause in its definition . Please guide with an example .
If you have the table:
CREATE TABLE table_name ( value1, value2 ) AS
SELECT 1, 1 FROM DUAL UNION ALL
SELECT 1, 1 FROM DUAL UNION ALL
SELECT 1, 2 FROM DUAL UNION ALL
SELECT 2, 1 FROM DUAL UNION ALL
SELECT 2, 2 FROM DUAL;
and the view:
CREATE VIEW view_name ( value1, value2 ) AS
SELECT DISTINCT value1, value2 FROM table_name;
and you try to do:
UPDATE view_name
SET value2 = 2
WHERE ( value1, value2 ) IN ( (1, 1) );
Which row should be updated?
Should it be the first row? or the second row? Either could be used as the DISTINCT value. In which case there would still be a DISTINCT row that has the 1, 1 pairing and it would appear that the update had done nothing.
Both the first and second row? So that afterwards there would be no 1, 1 pairing?
The expected behaviour is ambiguous as either result could be a valid solution.
Oracle removes the potential ambiguity by disallowing updates on aggregated/distinct views.
From the Oracle UPDATE documentation:
If you specify view, then the database updates the base table of the view. You cannot update a view except with INSTEAD OF triggers if the defining query of the view contains one of the following constructs:
A set operator
A DISTINCT operator
An aggregate or analytic function
A GROUP BY, ORDER BY, MODEL, CONNECT BY, or START WITH clause
...

SQL combine queries [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to do multiple queries in SQL at once, I know how to do each query separately but I can't find a way to combine all of them in a single query
for this I use two different tables the first on is called booking
For my first query I want to select only the rows of the Rownumber of which PerfDate=2015-12-10 and PerfTime=16:00:00
My second table is quite huge its called seat and it looks like this
For my second query I want to get all the rows from the seat table which have different RowNumber when combaring with the outcome of the first query
Then from the outcome of the two queries I want to take the rows only of which the zone is front stalls
Can anyone please help me, thanks in advance
You could use the 1st query as a criteria for the first as below. So you would say select everything from the seats table where RowNumber is not in your first query. You could also filter the result of this, so that you only get 'Front Stalls' as below:
Select * from YourSeatTable
where RowNumber not in (Select RowNumber from YourTable1
where PerfDate = '2015-12-10' and PerfTime='16:00:00')
and Zone = 'front stalls'
You could also try this:
select *
from seats s
where not exists (
select 1
from booking
where
perfdate = '2015-12-10'
and perftime = '16:00:00'
and rownumber = s.rownumber
)
and zone = 'front stalls'
This query might run faster than using where in.

Simple explanation for UNION [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What is the meaning (result) of this SQL statement in layman's term?:
SELECT mainTable.Year FROM mainTable
UNION SELECT mainTable.Title FROM mainTable
In set theory
if A={1,2,3} and B={2,3,4,5} then A union B={1,2,3,4,5} -- No duplicates here!
where as A union all B={1,2,3,2,3,4,5} --with duplicates
similarly in sql
its written like
select * from A
union -- or union all
select * from B
SEE HERE
The UNION combines two results.
When you have two SELECT statements, all of them has a result. If those results contain equivalent number of columns (and those - the ones which are at the same position - column's datatypes are equal or compatible with eachother), you can get those results combined into one result.
The UNION will filter out the duplications. You can use UNION ALL to prevent filtering.
See this SQLFiddle demo: http://sqlfiddle.com/#!2/42569/2

Query for selecting two different table having same columns [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I want to execute two queries having same number of columns. Here is my query, please help me to get the result:
SELECT A.*
FROM (SELECT *
FROM yh5lw_onlinecourse
WHERE state = 1
ORDER BY id DESC) A
union
SELECT A1.*
FROM (SELECT *
FROM yh5lw_practicetest
WHERE state = 1
ORDER BY id DESC) A1
I think you want this:
SELECT * FROM
(SELECT A.id as id, A.field1 as outputname1, A.field2 as outputname2, ... and so on
FROM yh5lw_onlinecourse A
WHERE state = 1
UNION
SELECT A1.*
FROM yh5lw_practicetest A1
WHERE state = 1) myUnion
order by myUnion.id DESC
Pay attention:
If you use UNION instead UNION ALL you discard all duplicated value-
First query must have alias because in this way you named yuor output table. The second query (after UNION) can use * wildcard (but the number of fields must be the same)

SQL find distinct values with same id [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I am trying to find duplicates with the same ID. I am not finding all of the duplicates. I would like to find distinct JOIN_FID values, and then duplicate JOIN FID values.
Here is my SQL select statements.
select OBJECTID,
Join_Count,
TARGET_FID,
JOIN_FID,
StrtConcat,
PermID,
Minutes,
FacilityID,
Shape
from
sde.gis.MFD_8_minute_response_ladder
where TARGET_FID
in (
select
JOIN_FID
from
sde.gis.MFD_8_minute_response_ladder
group by
JOIN_FID
having
COUNT(*) > 1
You're missing a closing parenthesis ()), but I assume that's a typo.
I suspect the problem may be that you're comparing two different fields:
where TARGET_FID
in (
select
JOIN_FID
Should that be:
where TARGET_FID
in (
select
TARGET_FID
or
where JOIN_FID
in (
select
JOIN_FID
?
For the query that you added ,an IN subquery can only return one value, so you need something like:
where PermID
in (
select
COUNT(1),
TARGET_FID
making sure you're comparing the right columns.