union-all

Combining ORDER BY AND UNION in SQL Server

情到浓时终转凉″ 提交于 2019-12-17 22:38:37
问题 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? 回答1: 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 回答2: select * from ( SELECT TOP 1

SELECT TOP with multiple UNION and with ORDER BY

青春壹個敷衍的年華 提交于 2019-12-13 21:47:31
问题 I want to select the latest records from the DB in SQL Server. If only one item is selected the final output is this: SELECT TOP(10) * from dbo.Eventos WHERE Tipo LIKE '%OS%' AND Distrito LIKE '%' + always added at the end: ORDER BY Data DESC NOTE : Distrito LIKE '%' must stay as it sometimes is programatically changed to something other than % . If there are more items selected, the query gets one UNION line added programatically for each item . At the end, the ORDER BY is added as always.

How to sort the result of multiple queries alternatively?

与世无争的帅哥 提交于 2019-12-13 19:46:24
问题 I have a query made of three select clause like this: select id, colors from table1 union all select id, numbers from table2 union all select id, names from table3 Also here is the tables structure: // table1 // table2 //table3 +----+--------+ +----+---------+ +----+-------+ | id | colors | | id | numbers | | id | names | +----+--------+ +----+---------+ +----+-------+ | 1 | red | | 1 | ten | | 1 | jack | | 2 | green | | 2 | two | | 2 | peter | | 3 | blue | | 3 | one | +----+-------+ | 4 |

How to combine three queries?

会有一股神秘感。 提交于 2019-12-13 09:17:55
问题 I have three queries and i have to combine them together. Query 1 WITH ph AS (SELECT chrd, chwo, chse, chst, chvr, chfv, chrd, ROW_NUMBER () OVER(PARTITION BY chwo ORDER BY chse, chvr desc) TEMP FROM wrpd.wscl WHERE chaj > '20180901' AND chst = 'R' AND chstb IN ( 'L1', 'R2' ) ORDER BY chse) SELECT * FROM ph A WHERE A.temp = 1 Query 2 SELECT chrd, chwo, chse, chst, chvr, chfv, chrd, ROW_NUMBER () OVER( PARTITION BY chwo ORDER BY chse, chvr desc) TEMP FROM wrpd.wscl WHERE chajdt > '20180901'

How to find out duplicate records from multiple tables in MS Access based on two condition

眉间皱痕 提交于 2019-12-13 09:16:35
问题 How to find out duplicate records from multiple tables in MS Access based on month and the below columns ? Name, Text, Description, TestDescription select [table1].[Name], [table1].[Text], [table1].[Description], [table1].[TestDescription] From [table1] UNION ALL select [table2].[Name], [table2].[Text], [table2].[Description], [table2].[TestDescription] from [table2] WHERE Table1.month IN ("April","May") and Table2.month IN ("April","May") group by [table1].[Name], [table1].[Text], [table1].

Why outer order by does not work correctly?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 05:59:41
问题 I have a query like this: SELECT @rank := @rank + 3 `rank`, id, subject, name FROM quran, (select @rank := -2) q WHERE MATCH (subject, name) AGAINST ('anything') and aye IN ("10") UNION DISTINCT SELECT @rank1 := @rank1 + 3 `rank`, id, subject, name FROM quran, (select @rank1 := -1) q WHERE MATCH (subject, name) AGAINST ('anything') UNION ALL SELECT @rank2 := @rank2 + 3 `rank`, id, subject, byA FROM hadith, (select @rank2 := 0) q WHERE MATCH (subject) AGAINST ('anything') ORDER BY rank LIMIT 0

Is there a way to include a query that is non updateable in an UPDATE query? [duplicate]

大兔子大兔子 提交于 2019-12-13 00:39:24
问题 This question already has an answer here : Access SQL Update One Table In Join Based on Value in Same Table (1 answer) Closed 6 years ago . For the following query: UPDATE tempSpring_ASN AS t SET t.RECORD_TYPE = ( SELECT TOP 1 RECORD_TYPE FROM ( SELECT "A" AS RECORD_TYPE FROM TABLE5 UNION ALL SELECT "B" AS RECORD_TYPE FROM TABLE5 ) ); I'm getting, "Operation must use an updateable query." I don't understand. I'm not trying to update a union query. I'm just trying to update an otherwise

MySQL performance of VIEW for tables combined with UNION ALL

孤人 提交于 2019-12-12 12:00:21
问题 Let's say I have 2 tables in MySQL : create table `persons` ( `id` bigint unsigned not null auto_increment, `first_name` varchar(64), `surname` varchar(64), primary key(`id`) ); create table `companies` ( `id` bigint unsigned not null auto_increment, `name` varchar(128), primary key(`id`) ); Now, very often I need to treat them the same, that's why following query: select person.id as `id`, concat(person.first_name, ' ', person.surname) as `name`, 'person' as `person_type` from persons union

SQL Statement for Accessing Data from Multiple Tables

蓝咒 提交于 2019-12-12 06:32:05
问题 I have 7 Tables as per attached following Image. I will either enter Engine Number or Chassis Number and it should show the respective tables information (these tables have only mentioned fields) so all fields can be shown as result. I can use hard coded Engine Number or Chassis Number. Every time of execution of this Query, I will hard code the required Engine/Chassis Number and will get the result. Can anybody please help me to write this query for me? Click Here to See the Tables 回答1: This

Can't insert multiple values into DB2 by using UNION ALL and generate IDs from sequence

拜拜、爱过 提交于 2019-12-12 02:22:36
问题 I've created sequence by following statement: CREATE SEQUENCE MAIN.MY_SEQUENCE START WITH 1 INCREMENT BY 1 CACHE 50; And table by following statement: CREATE TABLE MAIN.EMPLOYEES( ID INTEGER NOT NULL, NAME VARCHAR(512), EMAIL VARCHAR(254), PRIMARY KEY (ID) ) Now when I try to insert a new record by using following statement: INSERT INTO MAIN EMPLOYEES (ID, NAME, EMAIL) VALUES (MAIN.MY_SEQUENCE.NEXTVAL, 'Name 1', 'email1@example.com') UNION ALL VALUES (MAIN.MY_SEQUENCE.NEXTVAL, 'Name 2',