I have a little query about Codeigniter's db queries. (Order by)
$this->db->select('*, MONTH(date) as mnth');
$this->db->where('school_id', $school_id);
$this->db->group_by('MONTH(date)');
$this->db->order_by('date', 'desc');
$query = $this->db->get('events');
In above piece of code i want to get events with respect to each month but it is returning me only the first event of each month.. I have tried it with php loops but i want it without loops.. Hoping for good responses.
Best Regards
Muhammad Saud
GROUP BY will aggregate all of the column values that match the parameter into one row. You generally need to use aggregate functions like SUM in the SELECT statement with GROUP BY. If you're selecting columns that aren't being used with an aggregate function, then you'd include them in the GROUP BY. I'm not sure exactly what you're trying to achieve, but I don't think the GROUP BY is doing what you'd expect - maybe try removing it (or add more column names to it).
Related
If i run a select query on a MS Sql database, without Order by clause, will it return the order of insertion?
I need to be sure that a specific row in my database has been inserted prior to another. By running a select all without any order by, this statement above would be true. I am not sure tough that the order of the rows returned is in fact the order in that they have been added to the database. All tests I made point in that direction and seem to confirm this assumption, but I was not able to find a official statement or confirmation of this anywhere.
I was able to solve the problem by using this:
select mycolumns, %%physloc%% as pl from mytable order by pl desc
Rows that have been written one after the other will have very similar values in this column, unless it has been written something totally different. In my case it solved the problem.
I'm not an Oracle developer. We have a job with steps which retrieve data from Oracle and publish (update and insert) it into another database. The weird problem is in the select query which gives error
ORA-01427: single-row subquery returns more than one row
after investigating the query was
SELECT DISTINCT CUSTOMER, CUSTOMER_STATUS, CUST_ACCT_CREATION_DATE,.... FROM table_CUSTOMER
and when I remove certain columns from the query it gives me results with no problems, but unfortunately these columns is needed for other purposes.
How can these columns generate this error?
How? Because they don't make a distinct result set, that's how.
If you need to fetch a single row, apply WHERE clause to that query which will make sure that only one row is returned.
The simplest way to restrict number of rows is to use WHERE rownum = 1, but it'll return one, random row. Will it satisfy business rules? I don't know. Maybe; maybe not.
We don't have your tables nor data. But, if you could provide test case, someone might assist.
I have a query I'm running daily and I'd like to exclude certain items from this query that I've already identified as not wanting to see without adding another table to the database. Below is my attempt at this, which works with one TaskID but not multiple TaskIDs as I'm trying to do.
I feel its also important to note that this list could grow into roughly 150 ID's but not necessarily over 200 if that makes a difference. Obviously the way I did it is not the best way. Can anyone recommend the best way to accomplish this?
Direct question: what is the best way to exclude a large number of TaskID's from the below query without creating another table?
SELECT
TaskID, MAX(timeended) AS 'Last Run'
FROM
[moveitautomationagain].[dbo].taskruns
WHERE
TaskID <> 222300 OR TaskID <> 103439128
GROUP BY
TaskID
HAVING
DATEDIFF(HOUR, MAX(timeended), SYSDATETIME()) > 24
For your query you need AND not OR. However I would use NOT IN i.e.
where TaskID not in (222300, 103439128)
I have a difficult (if not impossible) problem to ask a solution for. I have an Oracle query, and I have to translate it to T-SQL syntax.
The query looks like this in Oracle:
__select_wrapper=select * from(
select rownum row_num, inner__c3p__query.*
from ({0}) inner__c3p__query)
where row_num>#offset and (row_num<=(#offset+#count) or (#count<1))
This comes from a properties file. Basically, this one line executes any SQL query, names the resultset "inner__c3p__query", and selects everything from it and also a rownum. Then, it filters the result by the rownum: >#offset and <=#offset + #count. So, practically, it executes any SQL query, and returns a limited rowset, e.g. from the 10th row to the 20th.
Now, my job is to transform this into T-SQL. I spent one and a half day looking up every possible idea, but so far, I haven't succeeded. I tried a great many things, neither of them worked. The problem is, I can't use ROW_NUMBER() function, because it requires at least one column in the OVER() clause, and I can't provide any, because the actual query is only determined at runtime.
Do you guys have any idea, or is it really impossible to do on the DB side?
Thanks in advance!
Gabe
While I have a lot of reservations about this kind of approach, especially wrt SQL Injection attacks, the following should work:
select * from(
select ROW_NUMBER() OVER(ORDER BY (Select NULL)) row_num,
inner__c3p__query.*
from ({0}) inner__c3p__query)
where row_num>#offset and (row_num<=(#offset+#count) or (#count<1))
Unfortunately, since the ordering column never changes, if you are trying to use this for paging, you cannot guarantee that the same row_num is always assigned to the same row in repeated executions.
ROW_NUMBER() only requires a column in the ORDER BY part (otherwise how do you order them?)
If the original was a random order then just pick a random column in the query (like the first one.) In an ORDER BY you can use column numbers... just use the first column or the number one.
ROW_NUMBER() OVER (ORDER BY 1)
This question already has answers here:
sql server 2008 management studio not checking the syntax of my query
(2 answers)
Closed 8 years ago.
I'm confused by an SQL query, and honestly, its one of those things that I'm not even sure how to google for. Thus StackOverflow.
I have what I think is a simple query.
SELECT Id
FROM Customer
WHERE Id IN (SELECT Id from #CustomersWithCancelledOrders)
Here's where I find the weirdness. There is no column called Id in the #CustomersWithCancelledOrders table variable. But there isn't an error.
What this results in is the Ids for all Customers. Every single one. Which obviously defeats the point of doing a sub-query in the first place.
It's like its using the Id column from the outer table (Customers), but I don't understand why it would do that. Is there ever a reason you would want to do that? Am I missing something incredibly obvious?
SQLFiddle of the weirdness. It's not the best SQL Fiddle, as I couldn't find a way to return multiple result sets on that website, but it demonstrates how I ran across the issue.
I suppose what I'm looking for is a name for the "feature" above, some sort of information about why it does what it does and what the incorrect query actually means.
I've updated the above question to use a slightly better example. Its still contrived, but its closer to the script I wrote when I actually encountered the issue.
After doing some reading on correlated subqueries, it looks like my typo (using the wrong Id column in the subquery) changes the behaviour of the subquery.
Instead of evaluating the results of the subquery once and then treating those results as a set (which was what I intended) it evaluates the subquery for every row in the outer query.
This means that the subquery evaluates to a set of different results for every row, and that set of results is guaranteed to have the customer Id of that row in it. The subquery returns a set consisting of the Id of the row repeated X number of times, where X is the number of rows in the table variable that is being selected from.
...
Its really hard to write down a concise description of my understanding of the issue. Sorry. I think I'm good now though.
It's intended behaviour because in a sub query you can access the 'outer queries' column names. Meaning you can use Id from Table within the Subquery and the query therefore thinks you are using Id.
That's why you should qualify with aliases or fully qualified names when working with sub queries.
For example; check out
http://support.microsoft.com/kb/298674
SELECT ID
FROM [Table]
WHERE ID IN (SELECT OtherTable.ID FROM OtherTable)
This will generate an error. As Allan S. Hanses said, in the subquery you can use colums from the main query.
See this example
SELECT ID
FROM [Table]
WHERE ID IN (SELECT ID)
The query is a correlated sub-query and is most often used to limit the results of the outer query based on a column returned by the sub query; hence the 'correlated'.
In this example the ID in the inner query is actually the ID from the table in the outer query. This makes the query valid but probably doesn't give you any useful results as it isn't actually correlating between the outer and inner queries.