select one or more rows for each group

I have a set of supply operation organised like this: op_type | time_stamp | product | in | out ---------------------------------------------------- 01 | 08:00:00 | p1 | 50 | 0 02 | 08:01:00 | p1 | 0 | 10 02 | 08:02:00 | p1 | 0 | 35 03 | 08:03:00 | p

How to get only one of each value in a column

I have this query: SELECT r.rev_id, rs.name, COUNT(ws.user_id) as likes FROM Reviews AS r LEFT JOIN Wasliked AS ws ON r.rev_id = ws.rev_id LEFT JOIN Restaurants AS rs ON rs.rid = r.rest_id GROUP BY rs.name, r.rev_id ORDER BY likes DESC and the result

Calculate the average of two tables

I am new to SQL and I just can't figure out how to calculate the average of a certain value in two tables. I have one table that is having all the information of the lakes in the world and i have another tables including all the information of the se

Sql Server Returns bool if the item is available in the table

This question already has an answer here: Solutions for INSERT OR UPDATE on SQL Server 21 answers i have a table named tblStockManagement in my database. i have two columns named Client_ID and FoldingID. i want to insert values to the columns, but fi

sql If statement or case

I think I may be missing the obvious here but im trying to create a sql query to pull data out in a particular way but cant work it out. I have a table which is made up with the following columns: Name, StageDate, Stage I want to create in sql an out

How to create a composite primary key using migration in Yii2?

I tried to run yii migrate, but it showed the following error: create table news-cate ...Exception: SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key

how to add a check constraint in Oracle

I have a table, create table a( id_a number(5) not null, name varchar2(15) not null, address varchar2(30), phone varchar2(15), constraint pk_a primary key (id_a) ); I want add constraint check at phone. example. phone is 08175210868 I want to only be

MySQL: return the first line of the join

I have 2 tables "quizzes" & "users", I need to return list of first user took each quiz, with quiz_id: tables structure "quizzes" structure: id name 1 England 2 france 3 Japan 4 USA 5 UAE 6 Sweden 7 Italy 8 Brazil 9 South

Enter data into the database by many users

Each of the salesmen should make a forecast of his sales. I know how he may input data directly from Excel sheet to SQL table. Do I need to create different tables - one table per salesman? At the end I need to aggregate all the forecasts. Is it poss

Priority of SQL parentheses

I'm using SQL Server 2008 R2 and trying to do a query like this: SELECT * FROM finance WHERE paid > 1000 and not (city <> 'RJ' and (flag2 is null or flag2 = '0')) But i'm not getting the desire solution. My idea is to search all payments bigger t

From month Name and year Get this month's date range in SQL

I have Month Name And Year from that I want Date Range of that Month. Input @Input=August 2014 Output @fromDate=08/01/2014(MM/dd/YYYY) @toDate=08/31/2014 Please Help me to do thisTry This DECLARE @SystemDate DateTime, @StartDate DateTime, @EndDate Da

How to join two tables with a different pass number? mysql

I want to run this query : SELECT firstName , secondName , lastName FROM (SELECT firstName,secondName,lastName FROM table1 UNION SELECT firstName,NULL,NULL FROM table2 )resutls I want add two Null columns to the second test, How could I do that ? or

Select multiple values ​​in where the time clause is equal

My table looks like this: Table 1: Note: This table is very large in reality, with lots more columns (20ish) and rows (in the millions) | Time | tmp | productionID | | 10:00:00 | 2.2 | 5 | | 10:00:05 | 5.2 | 5 | | 10:00:11 | 7.4 | 5 | | ...... | 3.2

The FORMAT function does not work in SQL Server 2008 R2

DECLARE @d DATETIME = '01/01/2011'; SELECT FORMAT ( @d, 'd', 'en-US' ) AS US_Result; I am using above code in SQL Server 2008 R2 but it is encountering an error: 'FORMAT' is not a recognized built-in function name. How can I use FORMAT function?FORMA

Generic LIKE with multiple fields and spaces in MYSQL

I'm having some trouble searching for any similar match in two fields. For example I have a table with the values: CAR MAKE CAR MODEL Ford Mustang (Shelby) Toyota Corolla Seat Leon etc etc. I want to be able to get the result "Ford, Mustang (Shelby)&

INSERT IGNORE in MS SQL Server 2005?

Is there mysql's INSERT IGNORE equivalent in MS SQL Server 2005 or something similar?you can implement something like this with an INSTEAD OF trigger, intercepting the default insert action to perform updates where existing rows are detected. Not qui

Join several (4) tables in MYSQL

I have four tables I want to join and get data from. The tables look something like... Employees (EmployeeID, GroupID[fk], EmployeeName, PhoneNum) Positions (PositionID, PositionName) EmployeePositions (EployeePositionID, EmployeeID[fk], PositionID[f