union

SQL UNION and ORDER BY

余生颓废 提交于 2019-12-25 14:08:17
问题 I have an annoying SQL statement that seem simple but it looks awfull. I want the sql to return a resultset with userdata ordered so that a certain user is the first row in the resultset if that users emailaddress is in the companies table. I have this SQL that returns what i want but i think it looks awful: select 1 as o, * from Users u where companyid = 1 and email = (select email from companies where id=1) union select 2 as o, * from Users u where companyid = 1 and email <> (select email

Union returning different outputs with limit postgresql

孤人 提交于 2019-12-25 08:43:52
问题 I have to fetch 12 questions from my question set based on the difficulty level. Following is the query I have written. (SELECT q.question_text, q.option_a, q.option_b, q.option_c, q.option_d, q.correct_answer, q.image_link, q.question_type FROM questions_bank q JOIN sports_type st ON st.id = q.sports_type_id JOIN difficulty_level dl ON dl.id = q.difficulty_level_id WHERE st.game_type = LOWER('cricket') AND dl.value = 'E' ORDER BY random() LIMIT 7) UNION (SELECT q.question_text, q.option_a, q

Prevent sort result of union in entity framework

妖精的绣舞 提交于 2019-12-25 08:19:54
问题 In SQL server union , result is sorted based on primary key column. I want to prevent this behavior in entity framework. In this post, @praveen has explained how to do this in pure sql. But I want to do this in entity framework. My code : public virtual ActionResult Search(string keyword) { var products = _db.Products .Where(x => x.IsActive) .AsQueryable(); var productExactlyTitle = products.Where(x => x.Title == keyword); var productStartTitle = products.Where(x => x.Title.StartsWith(keyword

mySQL: Multi-column join on several tables part II

孤街醉人 提交于 2019-12-25 08:13:47
问题 I am adding a 5th table to an existing join. The original query will always return a single row because the where clause specifies a unique ID. Here are the tables we are using: Table 1 carid, catid, makeid, modelid, caryear Table 2 makeid, makename Table 3 modelid, modelname Table 4 catid, catname Table 5 id, caryear, makename, modelname Here is the existing query I am using: SELECT a.*, e.citympg, e.hwympg FROM table1 a JOIN table2 b on a.makeid=b.makeid JOIN table3 c on a.modelid=c.modelid

Trying to get the sum of distinct values for distinct files for distinct software versions

萝らか妹 提交于 2019-12-25 06:46:55
问题 I'm working in SQL Server Management Studio. I've got a table that has simulation data in it. Four of the columns are "Version", "Scenario_Name", "Measure", and "Value". The "Version" column will have the software version number in it. The "Scenario_Name" column will have the file names for the scenario in them. The "Measure" column will have essentially a data type entry for the scenario something like Time, Money, Costs, etc. The "Value" column will record the number that is associated with

MySQL union and order by help

限于喜欢 提交于 2019-12-25 04:27:24
问题 The below code works but when i change Order by id to Order by s.id, i get this error Unknown column 's.id' in 'order clause' $construct = "SELECT child.* FROM products child LEFT JOIN products parent on parent.name=child.parent INNER JOIN subscribe s ON (s.productid = parent.id) WHERE s.username='$logged' AND s.type='0' UNION SELECT child.* FROM products child LEFT JOIN products parent on parent.sid=child.sid INNER JOIN subscribe s ON (s.productid = parent.id) WHERE s.username='$logged' AND

MySQL Refactoring Lengthy Query

吃可爱长大的小学妹 提交于 2019-12-25 03:07:50
问题 Alright, I totally hate myself for asking this; but I'm looking for another way to do this query, something that might be faster as well as more elegant (this looks like crap). Tell me what you think please: SELECT TRUNCATE(SUM(sub.Total),3) AS GrpTotal, sub.ActualDate, TRUNCATE(SUM(sub.BonusAmt),3) AS GrpBonusAmt, sub.UID, sub.CUSTID, YEAR(MIN(sub.ActualDate)) AS Year, pusers.username FROM ( SELECT a.UID, a.ActualDate, 'Global Report' AS Report, SUM(a.totalpayment) AS Total, a.CUSTID, ((SUM

ZF2 Union + Pagination

与世无争的帅哥 提交于 2019-12-25 02:10:33
问题 I'm using ZF2 and SQL Server 2012. I'm trying to write a union query with pagination. It seems like support for unions in ZF2 is not fully there. There is no union() method, but I see that there is a combine() method. It creates the query with the union, but it puts the ORDER BY on the first query instead of on the entire unioned query. So when the ZF2 paginator tries to add the limit/offset parts, SQL Server chokes because it has to have an ORDER BY on the whole query. Here is my code:

Combining two queries in mySQL

空扰寡人 提交于 2019-12-24 23:31:48
问题 I'm trying to combine the results of two queries. I'm not very proficient in mysql so I'm here for some help. The first query is as follows: select count(roomtypeid) as bookedrooms, day from event_guest_hotel where hotelid = 1 and roomtypeid = 1 group by day; This returns: The second query: SELECT ehr.reservationid, ehr.day, h.name AS hotelname, ehr.totalrooms as requested_rooms, r.name AS roomname FROM event_hotel_reservation ehr INNER JOIN hotel_room_type r ON ehr.roomtypeid = r.roomtypeid

How to unite two graphs in JUNG?

只谈情不闲聊 提交于 2019-12-24 21:20:24
问题 I have to graphs which I want to unite, that is, create a new graph composed by the union of both graph's edges and nodes (without repetition). Is there an implementation for that avaliable in JUNG or do I have do so on my own? 回答1: There isn't an implementation for that in JUNG, but it's about six lines of code assuming that the graphs, vertices, and edges are of the same types: // given Graph g1, g2 Graph g = new [appropriate Graph implementation] for (V v : Collections.union(g1.getVertices