union-all

SQL Server query assistance needed

雨燕双飞 提交于 2019-12-02 22:57:37
问题 I'm storing a bunch of data in a view (converted MS Access queries to views). Now what I'm trying to do is write a stored procedure to pull data based on when the data was added. Here is the query that I'm running Select Name, PlanID, ApptDate, 1stAppt, rn, from (Select *, row_number() over (partition by PlanID Order BY AddedonDate desc) as rn From vClientInfo) So this pulls all my data okay. The issue that I have to address is, the client actually comes in for the 1st appt and 2nd appt -

How to execute UNION without sorting? (SQL)

爱⌒轻易说出口 提交于 2019-12-02 16:36:50
UNION joins two results and remove duplicates, while UNION ALL does not remove duplicates. UNION also sort the final output. What I want is the UNION ALL without duplicates and without the sort. Is that possible? The reason for this is that I want the result of the first query to be on top of the final result, and the second query at the bottom (and each sorted as if they where run individually). I notice this question gets quite a lot of views so I'll first address a question you didn't ask! Regarding the title. To achieve a "Sql Union All with “distinct”" then simply replace UNION ALL with

SQL Server query assistance needed

Deadly 提交于 2019-12-02 13:05:49
I'm storing a bunch of data in a view (converted MS Access queries to views). Now what I'm trying to do is write a stored procedure to pull data based on when the data was added. Here is the query that I'm running Select Name, PlanID, ApptDate, 1stAppt, rn, from (Select *, row_number() over (partition by PlanID Order BY AddedonDate desc) as rn From vClientInfo) So this pulls all my data okay. The issue that I have to address is, the client actually comes in for the 1st appt and 2nd appt - Basically I need to pull the data for LATEST date, as well as EARLIEST, and include a '2ndAppt' in my

Union in JPA query - from the same table

只谈情不闲聊 提交于 2019-12-01 21:06:13
问题 I have a requirement where I need to restrict number of records returned from a table for a particular flag and all records for the other flag value. For example: contact-history table has an element called IorM_flg with possible values 'm' and 'o'. I need to return only 10 records of 'o' and all records of 'm'. I was about to write a query with union for this. Something like this: select ch from contact_history where ch.rownum <= 10 and ch.IorM_flg = 'o' Union All select ch from contact

How can I do a Union all in Entity Framework LINQ To Entities?

不羁岁月 提交于 2019-12-01 02:07:28
I came across a scenario where I had to use Union all, how can I achieve so in LINQ to entities ? Here is the answer you are looking for . Use the Concat keyword. From the example: var query = (from x in db.Table1 select new {A = x.A, B = x.B}) .Concat( from y in db.Table2 select new {A = y.A, B = y.B} ); I believe Concat is what you're looking for. var allResults = resultSet1.Concat(resultSet2); Obviously, both result sets must use the same type. And I believe there my be other requirements about how the result sets are constructed in the first place, but I don't know all the details. 来源:

How can I do a Union all in Entity Framework LINQ To Entities?

人走茶凉 提交于 2019-11-30 21:29:10
问题 I came across a scenario where I had to use Union all, how can I achieve so in LINQ to entities ? 回答1: Here is the answer you are looking for. Use the Concat keyword. From the example: var query = (from x in db.Table1 select new {A = x.A, B = x.B}) .Concat( from y in db.Table2 select new {A = y.A, B = y.B} ); 回答2: I believe Concat is what you're looking for. var allResults = resultSet1.Concat(resultSet2); Obviously, both result sets must use the same type. And I believe there my be other

performance of union versus union all

隐身守侯 提交于 2019-11-29 09:15:55
I have to run a select statement across several tables. I am sure the tables return different records. I am anyway using UNION ALL. Is it better to use UNION or of UNION ALL in performance terms when I am sure the tables return different records? UNION ALL will perform better than UNION when you're not concerned about eliminating duplicate records because you're avoiding an expensive distinct sort operation. See: SQL SERVER – Difference Between Union vs. Union All – Optimal Performance Comparison UNION ALL always is faster, because UNION exclude duplicated entries UNION implement internally

Combining ORDER BY AND UNION in SQL Server

僤鯓⒐⒋嵵緔 提交于 2019-11-28 20:03:49
How can I get first record of a table and last record of a table in one result-set? This Query fails SELECT TOP 1 Id,Name FROM Locations ORDER BY Id UNION ALL SELECT TOP 1 Id,Name FROM Locations ORDER BY Id DESC Any help? Put your order by and top statements into sub-queries: select first.Id, first.Name from ( select top 1 * from Locations order by Id) first union all select last.Id, last.Name from ( select top 1 * from Locations order by Id desc) last select * from ( SELECT TOP 1 Id,Name FROM Locations ORDER BY Id) X UNION ALL SELECT TOP 1 Id,Name FROM Locations ORDER BY Id DESC If you're

SQL Server: ORDER BY in subquery with UNION

被刻印的时光 ゝ 提交于 2019-11-28 13:23:43
i have two queries being combined with a UNION ALL 1 : --Query 1 SELECT Flavor, Color FROM Friends --Query 2 SELECT Flavor, (SELECT TOP 1 Color FROM Rainbows WHERE Rainbows.StrangerID = Strangers.StrangerID ORDER BY Wavelength DESC ) AS Color FROM Strangers Both of which, of course, work fine separately, but when combined with a UNION ALL : SELECT Flavor, Color FROM Friends UNION ALL SELECT Flavor, (SELECT TOP 1 Color FROM Rainbows WHERE Rainbows.StrangerID = Strangers.StrangerID ORDER BY Wavelength DESC ) AS Color FROM Strangers The query fails with the error: Msg 104, Level 15, State 1, Line

Merging 3 tables/queries using MS Access Union Query

柔情痞子 提交于 2019-11-28 05:55:34
问题 I have built a MySQL database to store bill payments. Everyone in my office has MS Access, so I am building a front-end database reporting tool using MS Access and linking to the MySQL tables on backend. I have created some Access queries that reference the MySQL tables, done some manipulation, and now want to merge three queries (with the same table structure) back into one that I can build my report on. Through my research (article1, article2, and others) , I have found that a Union query