having

Having a number in

Can someone give me a query that will return as a result rows ID 1 & 3? ID Name Hidden 1 Mika 1,4,2 2 Loca 0 3 Nosta 4 4 Like 2 Something like this SELECT * FROM table WHERE Hidden HAVING(4) ...

SQL statement HAVING MAX(some+thing)=some+thing

I'm having trouble with Microsoft Access 2003, it's complaining about this statement: select cardnr from change where year(date)<2009 group by cardnr having max(time+date) = (time+date) and cardto='VIP' What I want to do is, for every distinct cardnr in the table change, to find the row with the latest (time+date) that is before year...

SQL: HAVING clause

See the following SQL statement: SELECT datediff("d", MAX(invoice.date), Now) As Date_Diff , MAX(invoice.date) AS max_invoice_date , customer.number AS customer_number FROM invoice INNER JOIN customer ON invoice.customer_number = customer.number GROUP BY customer.number If the the following was added: HAVIN...

How Do I Update a Table From Another Table Only If the Result Count is 1?

I have a table of 2 tables in a one to many relationship. I want to run an update script that will update the table with the FK of the related table only if there is one result (because if there is multiple then we need to decide which one to use, in another method) Here is what I have so far: UPDATE import_hourly_event_reports i S...

NHibernate: SELECT MAX() with JOIN

I have a Vendor. Every Vendor has several Reservations, with a ReservationDate on it. I want a list of Vendors that have not made a reservation yet today. In SQL I would do something like this: SELECT v.Id, MAX(r.ReservationDate) AS MaxDate FROM Vendor v INNER JOIN DailyReservation r ON v.Id = r.Vendor_Id GROUP BY v.Id HAVING MAX(r....

Mysql: how to select groups having certain values?

Hello. Say there is such table: mysql> SELECT * FROM tags; +---------+--------+ | post_id | tag_id | +---------+--------+ | 1 | 2 | | 1 | 3 | | 1 | 1 | | 2 | 1 | | 2 | 2 | +---------+--------+ 5 rows in set (0.00 sec) Field names are pretty self-explanatory. I want to select post_...

SQL Group By - Select Both Columns

I have a table of users containing the following columns: | User_ID (int) | Name (varchar) | Age (int) | Experience_Level (int) | I would like to create an sql query to output all of the IDs of people who are not unique in the combination of age and experience. My code so far: SELECT Count(*), User_ID FROM Users GROUP BY Age,E...

MySQL / Ruby on Rails - How to "SUM" in a :has_many case

Hi there, I have the following tables: User :has_many Purchases Item :has_many Purchases Item has a column "amount" (can be + or -) and I need to find all Users that have a positive SUM of "Item.amounts" (over all Purchases each one has made). How does this query look like? (I'm not sure how to handle "SUM" correctly, in this c...

Using SQL Server 2005 I'm trying to do a HAVING in a WHERE clause sound crazy?

Ok, I have over a million records and I need to select all information from the database where the count of a column is greater or equal to 10. I know that using having I must use group by and using where I cannot use aggregate functions with operators so what I want in pseudo code is Select column1,column2,column3 From MYdatabase Wher...

SQL SELECT: value that occur > 1

I'm trying to select duplicates from this table: snr zip 01 83 02 82 03 43 04 28 Expected result is just empty table. Cuz it got no duplicates. I've tried with this query: SELECT snr, zip FROM student GRUOP BY snr HAVING (COUNT(zip) > 1) But it says that syntax is error, and that I'm trying to query an aggregate functions, etc.. ...

SQL Count where clause

I have the the following SQL statement: SELECT [l.LeagueId] AS LeagueId, [l.LeagueName] AS NAME, [lp.PositionId] FROM (Leagues l INNER JOIN Lineups lp ON l.LeagueId = lp.LeagueId) WHERE (lp.PositionId = 1) OR (lp.PositionId = 3) OR (lp.PositionId ...

Multiple Havings not working

I have a database with the following schema: ID PositionId LeagueId 1 4 5 3 4 5 3 8 5 4 1 6 I have this sql query in Access: SELECT lp.PositionId FROM Leagues l INNER JOIN Lineups lp ON (l.LeagueID = lp.LeagueId) GROUP BY PositionId HAVING sum(iif(lp...

LINQ query using join and aggregate functions

Urgh...my LINQ has suddenly taken a turn for the worse!! I have two tables and want to join and perform aggregate functions upon the tables. Firstly, is this possible, I am assuming it is, and secondly, how do I handle the aggregate functions? As part of the same LINQ statement? The SQL statement will look a bit like this: SELECT ...

Return all row having a maximum value

Hi! I'm using entity framework to connect the database. I've a table(Let's call it "File") that haves several fields: ID, Version, XYZ Primarky key is based on ID AND Version. so I can have several line with the same ID but different version(and inversly). The question is: How can I, with a LAMBDA expression, ask my Entity Framewor...

How do I count MySQL entries with a HAVING criterion

So usually you can just do SELECT COUNT(field.id) FROM table WHERE field.id > 100 and COUNT(field) will return the number of entries that has the criterion of field.id > 100 But then what if you what to count entries specified with the HAVING criterion such as SELECT COUNT(field.id), field.id * 10 AS foo FROM table HAVING foo > 100...