How to compare data using WHERE clause TSQL - sql

Basically, I want to see the difference in data based on a site code, for instance
SELECT
*
FROM
data.base.table
WHERE
site = 1
site = 2
Even though it's the same table, I want to compare differences based on that site code, but I can't figure out how. My initial chain of thought was to use a left join, but there's only one table.
Thanks

You can get the differences by using EXCEPT
Try this:
SELECT
* --Your column list minus site
FROM
data.base.table
WHERE
site = 1
EXCEPT
SELECT
*--Your column list minus site
FROM
data.base.table
WHERE
site = 2

I am not sure but you may try this:
SELECT
*
FROM
data.base.table
WHERE
site in(1,2)
This will give the data having the site 1 and 2 and then you can compare the two rows

Related

How to use aggregate function to filter a dataset in ssrs 2008

I have a matrix in ssrs2008 like below:
GroupName Zone CompletedVolume
Cancer 1 7
Tunnel 1 10
Surgery 1 64
ComplatedVolume value is coming by a specific expression <<expr>>, which is equal to: [Max(CVolume)]
This matrix is filled by a stored procedure that I am not supposed to change if possible. What I need to do is that not to show the data whose CompletedVolume is <= 50. I tried to go to tablix properties and add a filter like [Max(Q9Volume)] >= 50, but when I try to run the report it says that aggregate functions cannot be used in dataset filters or data region filters. How can I fix this as easy as possible?
Note that adding a where clause in sql query would not solve this issue since there are many other tables use the same SP and they need the data where CompletedVolume <= 50. Any help would be appreciated.
EDIT: I am trying to have the max(Q9Volume) value on SP, but something happening I have never seen before. The query is like:
Select r.* from (select * from results1 union select * from results2) r
left outer join procedures p on r.pid = p.id
The interesting this is there are some columns I see that does not included by neither results1/results2 nor procedures tables when I run the query. For example, there is no column like Q9Volume in the tables (result1, result2 and procedures), however when I run the query I see the columns on the output! How is that possible?
You can set the Row hidden property to True when [Max(CVolume)] is less or equal than 50.
Select the row and go to Row Visibility
Select Show or Hide based on an expression option and use this expression:
=IIF(
Max(Fields!Q9Volume.Value)<=50,
True,False
)
It will show something like this:
Note maximum value for Cancer and Tunnel are 7 and 10 respectively, so
they will be hidden if you apply the above expression.
Let me know if this helps.

Access SQL Query - Top 25 containing /images/

I'm a university student (thus a beginner) with some Access tasks to do, but I need help because so far it does not provide the desired result.
I have a table Table1 containing web log records. My field of interest is "cs-uri-stem", as it contains all the URL GET requests.
I want to select the TOP 25 images (records must contain /images/ in the "cs-uri-stem" field). So far I tried the following, with no success:
SELECT TOP 25
FROM Table 1
WHERE "cs-uri-stem"="cs-uri-stem"
HAVING [/images/];
An alert window keeps appearing saying that the SELECT request is not correct, but I don't know if this is caused by the fact that the Access is in Spanish in my university.
Thanks in advance!
Possible answer to my question, provided with Siyual and MCP_infiltrator's help [Thanks again!]:
SELECT TOP 25
Table1.[cs-uri-stem],
Count(Table1.[cs-method]) AS TotalHits
FROM Table1
WHERE (((Table1.[cs-uri-stem]) Like '*/images*'))
GROUP BY Table1.[cs-uri-stem]
ORDER BY Count(Table1.[cs-method]) DESC`
This will provide a top25 list of only the visited images, with no duplicates, instead of all the URLs.
There are several things wrong with your query, but this should work for what you need:
SELECT TOP 25 Images
FROM Table1
WHERE cs-uri-stem Like '%/images/%'
As for the what/why things are wrong...
For your Select statement, you aren't specifying any fields that you're wanting to get. I'm assuming by your question that you have a field named images that you're wanting to get back. If it's some other field, change that, or just use SELECT TOP 25 * to get everything.
Your From clause has a space in the table name.
Your Where clause makes no sense. This is where you need to be putting your logic for your query. In this case, you want anything that has /images/ in the cs-uri-stem field. Like is the operator you need to use here.
Finally, your Having is just plain wrong. It's not used correctly, nor is it even in the right context.
SELECT TOP 25 *
FROM TABLE1
WHERE CS-URI-STEM LIKE "*/images*"
This will select the top 25 records and all columns from the table where the cs-uri-stem has /images in it. When you use the * that is telling the db that you want all the columns of the table to be pulled for viewing.
Here is a link describing some of it
See Here: Select Top n in MS-Access
Another link
from office.microsoft.com
I would create three queries:
query1:
SELECT Images
FROM Table1
WHERE cs-uri-stem Like '*/images/*'
query2:
select images, count(images) as image_count
from query1 as q
group by images
query3:
select top 25 *
from query2
order by image_count desc
query3 should have the 25 results that you're interested in.
SELECT TOP 25 * FROM Table1 WHERE cs-uri-stem LIKE '%/images/%'
http://www.w3schools.com/sql/sql_like.asp

SQL Multiple IN statements on one column

Okay, I'm using WordPress, but this pertains to the SQL side.
I have a query in which I need to filter out posts using three different categories, but they're all terms in the post.
For example:
In my three categories, I select the following: (Academia,Webdevelopment) (Fulltime,Parttime) (Earlycareer).
Now what I want to do is make sure when I query that the post has AT LEAST ONE of each of those terms.
CORRECT RESULT: A post with tags Academia, Fulltime, Earlycareer
INCORRECT RESULT: A post with tags Academia, Earlycareer (doesn't have fulltime or parttime)
Currently, my query looks something like this:
SELECT * FROM $wpdb->posts WHERE
(
$wpdb->terms.slug IN (list of selected from category 1) AND
$wpdb->terms.slug IN (list of selected from category 2) AND
$wpdb->terms.slug IN (list of selected from category 3)
)
AND $wpdb->term_taxonomy.taxonomy = 'jobtype' AND .......
When using this query, it returns no results when I select across the different categories (that is, I can choose 4 things from category 1 and it has results, but I can't choose anything from category 2 or 3. And vice versa)
I'm not sure if this is something to do with using IN more than once on the same column.
Thanks in advance for any help!
Your query seems to be correct. There is no any limitations in SQL about using IN for the same column miltimple times.
But ensure that you don't have any NULL values in your list of selected from category 1/2/3 queries. Even single NULL value in these lists will give NULL as a result of whole 'WHERE' condition and you will get nothing as a result.
If this won't help then it must be WordPress issue.

Convert row data into columns Access 07 without using PIVOT

I am on a work term from school. I am not very comfortable using SQL, I am trying to get a hold of it....
My supervisor gave me a task for a user in which I need to take row data and make columns. We used the Crosstab Wizard and automagically created the SQL to get what we needed.
Basically, we have a table like this:
ReqNumber Year FilledFlag(is a checkbox) FilledBy
1 2012 (notchecked) ITSchoolBoy
1 2012 (checked) GradStudent
1 2012 (notchecked) HighSchooler
2 etc, etc.
What the user would like is to have a listing of all of the req numbers and what is checked
Our automatic pivot code gives us all of the FilledBy options (there are 9 in total) as column headings, and groups it all by reqnumber.
How can you do this without the pivot? I would like to wrap my head around this. Nearest I can find is something like:
SELECT
SUM(IIF(FilledBy = 'ITSchoolboy',1,0) as ITSchoolboy,
SUM(IIF(FilledBy = 'GradStudent',1,0) as GradStudent, etc.
FROM myTable
Could anyone help explain this to me? Point me in the direction of a guide? I've been searching for the better part of a day now, and even though I am a student, I don't think this will be smiled upon for too long. But I would really like to know!
I think your boss' suggestion could work if you GROUP BY ReqNumber.
SELECT
ReqNumber,
SUM(IIF(FilledBy = 'ITSchoolboy',1,0) as ITSchoolboy,
SUM(IIF(FilledBy = 'GradStudent',1,0) as GradStudent,
etc.
FROM myTable
GROUP BY ReqNumber;
A different approach would be to JOIN multiple subqueries. This example pulls in 2 of your categories. If you need to extend it to 9 categories, you would have a whole lot of joining going on.
SELECT
itsb.ReqNumber,
itsb.ITSchoolboy,
grad.GradStudent
FROM
(
SELECT
ReqNumber,
FilledFlag AS ITSchoolboy
FROM myTable
WHERE FilledBy = "ITSchoolboy"
) AS itsb
INNER JOIN
(
SELECT
ReqNumber,
FilledFlag AS GradStudent
FROM myTable
WHERE FilledBy = "GradStudent"
) AS grad
ON itsb.ReqNumber = grad.ReqNumber
Please notice I'm not suggesting you should use this approach. However, since you asked about alternatives to your pivot approach (which works) ... this is one. Stay tuned in case someone else offers a simpler alternative. :-)

Include a blank row in query results

Is there a way to include a blank row at the top of a sql query, eg if it is meant for a dropdown list? (MS Sql Server 2005 or 2008)
Select *
FROM datStatus
ORDER BY statusName
Where I want something like
-1 (please choose one)
1 Beginning
2 Middle
3 Ending
4 Canceled
From a table that is ordinarily just the above, but without the top row?
I feel it's nicer to do it outside SQL, but if you insist...
SELECT -1, '(please choose one)'
UNION
SELECT * FROM datStatus
ORDER BY statusName
I have found that it is better to do this in the presentation layer of your application, as you might have different requirements based on the context. In general I try to keep my data service layer free of these sorts of implementation specific rules. So in your case I would usually just add a new item by index in the first position of the list, after i had loaded it with data from my service layer.
Enjoy!
How about unioning the first row together with the rest of the query?
Select -1,'(please choose one)'
union all
select * FROM datStatus ORDER BY statusName