The time returned by NOW(), and other date time functions, is derived from the start time of the query. The THD class here is used to contain all the information for the connection. The NOW() function implementation grabs this value and returns it into the now_time structure.
MySQL docs for NOW() also state:
NOW() returns a constant time that indicates the ...
SQL Server uses different calculations in different situations. Your example is different from the linked Q & A because your range is entirely contained within a step; it does not cross a step boundary. It is also an interval with two ends rather than one. Writing BETWEEN is the same as writing two separate predicates with >= and <=.
Interval with ...
A logical read is counted when a single page is retrieved from buffer cache during query execution. This is counted regardless of whether a physical or read-ahead was used to cache the page, or if the page already existed in the buffer cache. Consequentially, logical reads is a measure of how many times pages were actually touched in memory during query ...
One way to determine the logical order of joins is to replace the first inner join in your example with a left outer join:
FROM user_branch T1
LEFT JOIN dimcustomer2 T2
ON T1.BRANCH_CODE = T2.BRANCH_CODE
INNER JOIN customer_guarantee T3
ON T3.CUSTOMER_NUM = T2.CUSTOMER_NUM
Let us assume that some rows in T1 have no matches in T2. More ...
The sort spill itself can probably be addressed by enabling trace flag 7470. See FIX: Sort operator spills to tempdb when estimated number of rows and row size are correct. This trace flag corrects an oversight in the calculation. It is quite safe to use, and in my opinion ought to be on by default. The change is protected by a trace flag simply to ...
SELECT record_id, rec_date
, CASE WHEN rec_date::time < '08:00' THEN 'Night'
WHEN rec_date::time >= '20:00' THEN 'Night'
ELSE 'Day' END AS indicator
It's a matter of date-math rather than date-format. You want to do the math correctly and efficiently. The format of 'Day' and 'Night' are not in ...
After further research, I have discovered from this StackOverflow post that SQL Server breaks FOR JSON queries into "~2kb chunks".
Sql Server splits result of FOR JSON query into ~2KB chunk, so you should either concatenate fragments like on the MSDN page or you can stream results into some output stream.
This means that only ~2000 characters can ...
The subquery from the selected answer isn't needed. To select products with all the given tag ids the query can be simply:
products AS p
tag_ties AS tt
tt.ref_id = p.id
tt.tag_id IN (10, 11, 12)
Extending this idea, we can also query based on the tag ...
You can not select aggregates across a field if you don't include the field in the group by list.
If you want the totals per year you should write
GROUP BY year
If you want the totals per parameterno it should be
GROUP BY parameterno
From the error ...
It is not clear to me what you are asking but I believe that GROUP BY is one of the most misunderstood concepts in SQL, so I'll add this answer anyhow. It may or may not help with the understanding of the concept GROUP BY. Assume we have a table like:
CREATE TABLE T
( YEAR INT NOT NULL
, PARAMETERNO INT NOT NULL
, MARK INT NOT NULL
, PRIMARY KEY (YEAR, ...
The msdb.dbo.sp_send_dbmail procedure (or other procedures) only accept either a parameter or a literal value.
Consider changing the execution of sp_send_dbmail to this:
SET @usage_data += ' end of data'
@subject='Mail from sql server',
I'm afraid the phrase "logical execution" does not make much sense; query execution by definition is physical materialization of a result set. I think what you mean by "logical execution" is the query compilation, the phase where the query syntax and semantic meaning is analyzed and the query plan is prepared to implement said semantic meaning.
One way would be to use NOT EXISTS.
FROM YourTable T1
WHERE Status IN ( 'Closed', 'Rejected', 'Cancelled' )
AND NOT EXISTS (SELECT *
FROM YourTable T2
WHERE T2.Id = T1.Id
AND T2.Status NOT IN ( 'Closed', 'Rejected', 'Cancelled' )) ;
Or an alternative method if ...
IMHO the easiest way is by using a lookup table.
create table lk (id int, name text);
insert into lk values
(1, 'apple'),(2, 'google'),(3, 'msft'),(4, 'amazon');
create table t (id serial, lk int);
insert into t (lk) values (2),(1),(3),(4);
on lk.id = t.lk;
id | name
-: | :-----
2 | apple
1 | ...
With the help of @a_horse_with_no_name, I was able to find the error in my thinking. There is a great example in the PostgresSQL docs that really clarified it for me:
Insert or update new distributors as appropriate. Assumes a unique index has been defined that constrains values appearing in the did column. Note that the special excluded table is used to ...
Permissions to create a table in the current database shouldn't preclude you from creating one you can work with. You can just create a #temp table:
CREATE TABLE #test123(
Does this get you what you want?
DECLARE @x XML = '
<SearchCriteria name="Search query" >
<SimpleAttributeExpression displayName="Date" npmPropertyId="4" searchOperation="GREATER_EQUAL" dataType="string" ...
You can use grouping set also
declare @JobRequiredProducts table (jobId int, productId int);
declare @Product table(productId int, productUnitPrice decimal(8,2))
insert into @JobRequiredProducts(jobId,productId)
insert into @Product(productId, productUnitPrice)
values(4,175.99),(5 ,100.00),( 6 , 125.00)
3 solutions (2 of which are similar to @stickybit's, but easier on the eye) are below.
I often find it beneficial to look at answers/threads which have multiple solutions to the problem - some of which are obviously better than others but it can be a learning experience!
The simplest and by far the most elegant solution is (thanks to the hint from
There will always be logical reads. SQL Server never returns data to you directly from disk. If the page you need is already in the buffer pool, there is a logical read. If it’s not, it will be moved into the buffer pool, and then there is also a logical read. You will always get the read coming from cache.
Also, I don’t understand the point of your test. ...
The SHOW ALL command displays the current setting of run-time parameters in 3 columns.
SHOW ALL ;
The pg_settings view shows the same items as SHOW ALL but with additional details, across 17 columns versus 3 columns.
TABLE pg_settings ;
To read what is stored in the postgresql.conf file itself, use the view ...
This is usually called a GROUPING AND WINDOWS solution.
Basically you set reset points according some rules, then you set up groups by SUMming reset points and finally use aggregated functions to get the desired value.
;WITH reset AS
/* Next CASE returns 1 when:
a) It is the ...
You should use a case statement like this:
(CASE WHEN C1 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C2 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C3 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C4 IS NULL THEN 1 ELSE 0 END +
CASE WHEN C5 IS NULL THEN 1 ELSE 0 END)
The ISNULL approach is returning ...
This should work for you. At least for the entries in the PowerCurve table that I copied over, it works correctly. This should get you close. See comments in the code for more details.
Assistance from https://stackoverflow.com/questions/9873990/round-to-5-or-1-0-in-sql on how to round to nearest .5/0.
/** FIRST - we setup the data and tables we need.
select id from table where field in (gigantic list of ordered longs)
Queries with long IN lists are slow, as they are parsed and compiled each call.
Either pass the list as a JSON array:
select id from table where field in (select value from openjson(@values))
Or use a Table-Valued Parameter or bulk load a temp table with the values. Ensure there is an ...
I am assuming that the ID column is either the PK or otherwise declared as unique in all three tables.
Now, if each of these scenarios is equally possible:
all three tables have a row with the ID of 101,
only two (any two) of the three tables have a row with the ID of 101,
only one (any one) of the three tables has a row with the ID of 101,
and you want ...
In SQL Server 2012 or later, I would implement this using sequence objects.
For SQL Server 2008 R2, my replacement for that missing feature is Sequence Tables. In this case, there would be a key in the master sequence table for each year, for example:
CREATE TABLE dbo.SequenceTable
sequence_name nvarchar(20) NOT NULL,
next_value integer ...
t_min.value AS value_1st,
CASE WHEN min_id <> max_id THEN t_min.value END AS value_last
SELECT name, min(id) AS min_id, max(id) AS max_id
GROUP BY name
) AS t_min_max
JOIN table AS t_min
ON t_min_max.min_id = t_min.id
JOIN table AS t_max
ON t_min_max.max_id = t_max.id;
With complex ...
You could try something like this
SELECT CASE WHEN RIGHT(fieldname,1) = '"' AND LEFT(fieldname,1) = '"' THEN REVERSE(STUFF(REVERSE(STUFF(fieldname, 1, 1, '')), 1, 1, ''))
WHEN RIGHT(fieldname,1) = '"' THEN REVERSE(STUFF(REVERSE(fieldname), 1, 1, ''))
WHEN LEFT(fieldname,1) = '"' THEN STUFF(fieldname, 1, 1, '')
ELSE fieldname END as ...