Why does T-SQL think a an item is a column name? - tsql

Note: I am just starting with SQL/T-SQL and I am not searching for a more elegant solution, just for a fix.
So I'm trying to use the Stack Exchange Data Explorer to count how many times a badge has been awarded. My query goes something like this:
SELECT COUNT(Id)
FROM Badges
WHERE Name = "Scholar"
The results should be the number of times the Badge has been awarded. It, however, returns this error:
Line 3: Invalid column name 'Scholar'.

Replace double quote to single quote
SELECT COUNT(Id)
FROM Badges
WHERE Name = 'Scholar'

Related

In SSRS, can you group multiple parameter values into one?

I am relatively new to SSRS but have been working with SQL for years. I have been tasked with creating a report that reflects shipped items based on their status. For example, I have x number of items with varying statuses including "IN_TRANSIT", "RECEIVING", "SHIPPED", "WORKING", and "CLOSED". The requestor is asking if I can provide the following options in a report drop down:
"IN_PROCESS" Status filter including all statuses except "CLOSED".
"CLOSED".
Essentially, they want to be able to view all non closed statuses, closed, statuses, or all. Right now, I have it set so you can individually select all statuses, essentially getting them the data they want, just not with the "right" parameters.
My question is, does SSRS provide a way to essentially 'group' the non-closed statuses into one inside the report so that when they select "IN_PROCESS" it sends those non-closed statuses to the SQL query I have built in? The problem with using SQL for this is that the dataset I created to generate the dropdown options provides "CLOSED" and "IN_PROCESS" as it's output options, but when they select "IN_PROCESS" (sending that value to the filter in the report), since it's not an actual status, nothing comes back.
If more information or clarification is required, please let me know.
Thanks ahead of time!
You can create a new column in your SQL query and use a CASE statement to give the value of IN_PROCESS or CLOSED for the applicable status. Then you will just need to the filtering condition to match the SSRS parameter to the new column.
Depending on how often this case is likely to be reused should help determine how to approach it. If it sounds like it might become a regular process.... "Oh can we have another report with the same filter but showing xyz " then take the time to setup correctly and it will save time in the future.
Personally I would add a database table, if possible, that contains the status names and then a status group name (ignoring fully normalising for the sake of simplicity here).
CREATE TABLE StatusGroups(Status varchar(10), StatusGroup varchar(10))
INSERT INTO StatusGroups VALUES
('IN_TRANSIT', 'In Process'),('RECEIVING, 'In Process'),('SHIPPED', 'In Process'),('WORKING', 'In Process'),('CLOSED' 'Closed')
Then a simple view
CREATE MyNewView AS
SELECT t.*, g.StatusGroup
FROM MyTable t
JOIN StatusGroups g on t.STATUS = g.Status
Now change your report dataset query to use this view passing in the report parameter like this...
SELECT *
FROM MyNewView
WHERE StatusGroup = #myReportParameter
Your dataset for your report parameter's available values list could then be something like
SELECT DISTINCT StatusGroup FROM StatusGroups
This way if you every add more status or statusgroup values you can add an entry to this table and everything will work without ever having to edit your report.

SQL query with ranking order

All,
Need a help with one of the sql queries. I have a query which pulls up records on ranking order.
Select * from
(select count(*) cnt, customer_cd, smallint(Rank() Over(Order by count(8) Desc)) as rnk
from table.customer
Now, the result shows like,
Cnt Customer Cd
110 1- Retail
90 2-Human resources
20 3-Information Technology
11 Not Standard
I want to remove the description from it and will have only the Customer Codes such as 1,2,3,NS etc. Any help how to achieve this.
Thanks.
You could use LOCATE to find the position of the hyphen, assuming you always have a hyphen. Then, you could use SUBSTRING to get the portion of the string before the position found by LOCATE.
select substring(customer_cd,0,locate('-',customer_cd))
from table.customer
should show you what you will get.
You do seem to have some data (e.g. "Non Standard") that has no code at all. Such fields will come out as blank. If you want to replace that with some specific code, you can use a CASE...END expression.
select CASE when locate('-',customer_cd)==0 then ""
else substring(customer_cd,0, locate('-',customer_cd) ) END
from table.customer

Trying to find the last character in a non-standard length string

I'm a relatively new coder and I've been struggling with the following problem for a few days. I am trying to separate the characters after the last period in an email address so I can group results by them. To do this for the text after the # symbol, I wrote the following code:
select lower(substring(email, position('#' in email))) as email
This code returns things like #gmail.com or #yahoo.com, which I can then group by in my longer query. However, I would also like to compare the .com results to the .net results. When I type a similar query:
select lower(substring(email, position('.' in email))) as email
it returns the first period in the email address. So my email would be returned as .lastname#gmail.com rather than .com. I've experimented with right( and left(, but these don't work with substring in Postgresql. Does anyone have any other suggestions? Thanks!
Try this. The trick was using reverse to find where that last period was.
SQL Fiddle Example
select
substring(email, char_length(email) - position('.' in (reverse(email))) + 1) as Domain
from yourTable
Try something like:
select regexp_matches(email, '[^.]+$', 'g')
from your_table

Can't select command field which type is memo

I'm working on a crystal report and I want to insert a pie chart that shows percentages. I have a view that contains the field that I'm interested in : Descr(descrtiption) (NVARCHAR), I successfully added a command that gets the number of occurences of each Description. The problem is that when I try to add that field to the chart I can't see it,
here is the query that gets the values
here is the chart expert window I get
and as you can see the field is not checked in the field explorer
You will need to shorten the memo field to be able to use it in this manner. Try:
CAST(Desc AS NVARCHAR(2000))
Use Having clause instead of where condition in query.
Select count(*) as NBoccurrence,descr
From tbl_nm
Group by descr
Having your condition

Getting first 100 records from the table in Progress OpenEdge database (e.g. SELECT TOP 100..)

How can I get a limited number of records from the table in Progress OpenEdge database?
Something like in SQL:
SELECT TOP 100 * FROM MyTable
The only ugly solution I can find is looping through all records and breaking when 100 of them were displayed. But feels like there should be some better way of doing it.
If you are using the 4GL you might also want to look at using OPEN QUERY and MAX-ROWS to achieve the result that you are looking for. The following shows a traditional FOR EACH loop with a counter and then a QUERY with MAX-ROWS:
define variable i as integer no-undo.
define frame a with 10 down.
for each customer no-lock break by name:
i = i + 1.
display i custNum name discount.
if i >= 5 then leave.
end.
define query q for customer scrolling.
open query q for each customer no-lock break by name max-rows 5.
do i = 1 to 5 with frame a:
get next q.
display i custNum name discount.
end.
If you are using the SQL-92 engine then something like:
SELECT TOP 100 FROM pub.customer;
should work just fine.
If you are using the 4GL engine then you should not be attempting to blend SQL with 4GL. It will only lead to pain, misery and agony. The 4GL is not SQL. There are a few SQL-89 statements that were put in the 4GL a long, long time ago for marketing reasons. Trying to use them will result in severe emotional trauma. You have been warned.
Please follow the link to download the file. Hoping that the question will answer on it
OpenEdge Database
DEFINE VARIABLE i AS INTEGER NO-UNDO.
FOR EACH customer NO-LOCK with width 320:
ASSIGN i = i + 1.
if i <= 100 then
DISP CustNum Address Balance City Contact
Country CreditLimit Discount
Name Phone PostalCode SalesRep State Terms .
END.