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.
Related
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 4 years ago.
Improve this question
I have view that returns 5167 rows when doing
SELECT * FROM MY_VIEW -- returns 5167 rows
But, if I do the COUNT(*) on that, I get a totally different number
SELECT COUNT(*) FROM MY_VIEW -- returns 3112 rows
I've tried EXEC sp_refreshview 'MY_VIEW' but still keep getting above results.
I thought COUNT(*) is supposed to return a full row count (not filter out any null content as its not being executed for a column and thus should match number of rows returned using SELECT *
Any reason why that could be different?
I am using SQL Server 2012
** Updating with more info based on comments **
Did the following as mentioned in the comments
I modified the command to reflect server name and DB name.
confirmed that they are running in the same SSMS tab.
Added schema name 'dbo.MY_VIEW' to the query
Ran 'sp_updatestats'
Queries thus run in the SSMS tab are -
select ##SERVERNAME, DB_NAME(), count(*) from dbo.MY_VIEW;
-- returns 3112
select ##SERVERNAME, DB_NAME(), * from dbo.MY_VIEW;
-- returns 5167 rows in results
select ##SERVERNAME, DB_NAME(), *, COUNT(*) over () cnt FROM dbo.MY_VIEW;
-- returns 5167 rows in results and 5167 as the count
** Update 2 **
I found that if I run the COUNT('column_1'), then I get the result 3112, but if I do the COUNT('any_other_column'), am getting 5167! I checked and there are no NULLs in column_1. Also, COUNT(*) should count the NULL's anyways and should not be focussed on a column, but should count the number of rows. So, fail to further understand any of the above results.
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 5 years ago.
Improve this question
I am using 11g RDBMS and in one interview interviewer ask me that print 1 to 10 numbers using SQL query don't use loop if you are using PLSQL, means using select query he wants to see the result.
like Table:
1
2
3
Here are two approaches, both are SQL Server syntax, but you will find something similar for other RDBMs:
--ROW_NUMBER()
SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) FROM sys.objects; --any table with more rows than 10 will do
--recursive CTE
WITH recursiveCounter AS
(
SELECT 1 AS Nr
UNION ALL
SELECT r.Nr+1
FROM recursiveCounter AS r
WHERE r.Nr<10
)
SELECT * FROM recursiveCounter
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
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.
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.