Showing posts with label case. Show all posts
Showing posts with label case. Show all posts

Wednesday, March 28, 2012

No grouping?

Hi to all!

To count(something), it is usually required to use a group by function, however, that is not applicable in this case: I would like to select records that have similar values for 'col2' and 'col3', given a certain 'col1', and how many records each 'col1' returns. The following example hopefully makes it clear.

The table:
Col1 Col2 Col3

s1 A B
s1 C D
s2 A B
s2 C D

The result should be:
col1 col2 col3 Expr

s2 A B 2
s2 C D 2

I had figured out the not-working-query:

SELECT *, count(col1)
FROM table as t1 INNER JOIN table as t2 ON
t1.col2 = t2.col2 AND t1.col3 = t3.col3
WHERE col1 = 's1'

Could anyone provide me a hand? Many thanks in advance.

Kind regards,

Nele

SELECT Col1,Col2,Col3,Count(Col1)

FROM TEST

GROUP BY Col1,Col2,Col3

|||

No, that returns for 'expr' value 1.

|||

Are you trying to find those records where value of COL2 and COl3 are similar, which means there is more than one record having same value for col2 and col3

Col1 Col2 Col3

s1 A B
s1 C D
s2 A B
s2 C D

s3 C A

IF I get it correctly, here the last record should not be displayed. Is that what you intend?

|||

In SQL 2000, you can do:

select *, (select count(*) from table as t2 where t2.col1 = 's1') as cnt

from table as t1

where t1.col1 = 's1'

In SQL 2005, you can do:

select *, count(*) over(order by t1.col1) as cnt

from table as t1

where t1.col1 = 's1'

Having said this, there is really no need to complicate the query by doing this. You will only get slow performance. The number of rows that qualifies the query can be obtained by looking at @.@.ROWCOUNT after the SELECT statement or determined in the client side easily. So it seems unnecessary to do this .

|||Yes! That is what I intend.|||

I tried your code, but for sql 2005 it returned an error "incorrect syntax near the keyword 'order' "?

@.@.rowcount returns the total number, I rather would like to know how many records of a certain value for 'col1' are returned (to select the top(100) in the end). E.g., for 's2' that is 2.

|||

How's about:

Select Col2, Col3, Count(distinct col1 )

From ...

Group by Col2, Col3

Having Count(distinct col1) > 1

And if you want Col1 to display, you can add use

Select Max(Col1), Col2, Col3, Count(distinct col1)

|||

Sorry that should be partition by not order by. We don't support ORDER BY clause for aggregate function yet in OVER clause. We support only partition clause.

|||

No, the problem is that I want to know how many times a certain value for 'col1' occurs, not taking into account the values of col2 and col3 for that calculation.

Besides, all have an nvarchar as type.

|||

Yes, that is what I wanted to achieve

Thanks for the reactions!

sql

Wednesday, March 21, 2012

no case statement in views + sql 7

I have a query which will be a subquery that I want to use a case statement on. (It is an outer join and I want to substitue nulls for a specific value.) I can make it work in query analyzer and can also make it work in a stored procedure, but I get a message that the case statement is not supported in views. So, I tried to make a function ,but they are not supported in SQL 7, and apparently stored procedures cannot be called inline in views, either. I can retrieve the desired recordset with exec sp_name, but I need the recordset to be used in another query and cannot figure out how to use the results from a Stored procedure as a subquery in a view. Any help is greatly appreciated.

Thanks,

Tman2Case statements are supported in views, but the GUI query designer built into Enterprise Manager cannot parse and graphically display statements using CASE. You can write your query using the Query Analyzer tool instead (no serious TSQL programmer uses the GUI).
Regardless, you do not need a CASE statement to substitute NULL for a specific value. Use the NULLIF() function instead.|||I was able to create a view in query designer that uses case, but it does not function correctly. (It defaults all values to 0). However, if I use the exact same SQL statement in query designer without it being a view, it works. (ie. create view sQL statement to create view, select * from view produces erroneous results, but SQL statement in query designer works.)

I will try the nullif function you mentioned, but htat will only work for this particular cases, and there will be many instances where I will need the case statement. Any ideas what to do? This only seems to be causing a problem in SQL 7, not the newer versions...|||Post your code if you want somebody here to review it. There could be a problem with default connection settings in Query Analyzer. The SET CONCAT NULL option, for instance, has bitten me several times...|||It probably is something like that. WHEN NULL was not identifying nulls as nulls. Any ideas, or this there something wrong with my syntax (ie must use isnull like in VB)? Thanks for all your help. I have it working now, (really can just use isnull without the case statement at all) but for academic purposes, I would be intrested to know if there is an option that changes this. I did a little research on the option you pointed out above, what a #!@.* that must have been to find...

Thanks again.

no case sensitive

Hi,
Does anybody know how to let the database treat my data as no case
sensitive?
For example: I have a customer type field, when I retrieve it from the
mainframe, it has "retail" or "RETAIL" as differently typed by users. But
when I run a report for customer type "Retail", I want to them to be showing
as one type on the report.
How do I do this?
Thanks,
SarahUse the lower function to convert the column to lowercase.
i.e.
Select *
from Orders
where lower(OrderType) = 'retail'
"Sarah G." <sguo@.coopervision.com> wrote in message
news:uMv8Wv#GEHA.1076@.TK2MSFTNGP10.phx.gbl...
> Hi,
> Does anybody know how to let the database treat my data as no case
> sensitive?
> For example: I have a customer type field, when I retrieve it from the
> mainframe, it has "retail" or "RETAIL" as differently typed by users. But
> when I run a report for customer type "Retail", I want to them to be
showing
> as one type on the report.
> How do I do this?
> Thanks,
> Sarah
>|||Are you using SQL Server 2000? If so, look up the COLLATE keyword in BOL.
Here's a very simple example:
create table #a(mychar varchar(20) collate sql_latin1_general_cp1_cs_as)
go
insert #a values('aBc')
insert #a values('abC')
go
select distinct mychar from #a
go
select distinct mychar collate sql_latin1_general_cp1_ci_as from #a
go
"Sarah G." <sguo@.coopervision.com> wrote in message
news:uMv8Wv#GEHA.1076@.TK2MSFTNGP10.phx.gbl...
> Hi,
> Does anybody know how to let the database treat my data as no case
> sensitive?
> For example: I have a customer type field, when I retrieve it from the
> mainframe, it has "retail" or "RETAIL" as differently typed by users. But
> when I run a report for customer type "Retail", I want to them to be
showing
> as one type on the report.
> How do I do this?
> Thanks,
> Sarah
>sql

no case sensitive

Hi,
Does anybody know how to let the database treat my data as no case
sensitive?
For example: I have a customer type field, when I retrieve it from the
mainframe, it has "retail" or "RETAIL" as differently typed by users. But
when I run a report for customer type "Retail", I want to them to be showing
as one type on the report.
How do I do this?
Thanks,
Sarah
Use the lower function to convert the column to lowercase.
i.e.
Select *
from Orders
where lower(OrderType) = 'retail'
"Sarah G." <sguo@.coopervision.com> wrote in message
news:uMv8Wv#GEHA.1076@.TK2MSFTNGP10.phx.gbl...
> Hi,
> Does anybody know how to let the database treat my data as no case
> sensitive?
> For example: I have a customer type field, when I retrieve it from the
> mainframe, it has "retail" or "RETAIL" as differently typed by users. But
> when I run a report for customer type "Retail", I want to them to be
showing
> as one type on the report.
> How do I do this?
> Thanks,
> Sarah
>

Monday, February 20, 2012

Newbie: Modifying table = wrong data in view?

Hi there,

Completely new to the world of databases. I'm a designer who works primarily in Flash. In any case, I'm trying to manage an application that uses MS SQL and learn about the wonderful world of databases.

Ok, I modified a table (e.g. I added a column called "Rate") that had associated views (created by another developer). Noticed that my application went a little wonky as some of my variables within my app took on the value of the data in the "Rate" column. I checked one of the views and noticed that a column within the view (e.g. TutorID) was assuming the values in the "Rate" column. Note: The column TutorID had been blank before the change to the table. I'm completely lost as to why this is happening. Do I need to rebuild the view? Can I just reset the original view?

Thanks.

Oh yeah, I'm using SQL4X Manager J from Mac Guru (if that helps).Scott, where are you? This is a prime example of what you were talking about at the bar, remember?|||Hey RD STEP BACK! nothing to see here....
Novian,
give us some DDL on your view and your table

DDL(object definitions) = a list of all of the cols in the view and in the underlying table

Perform the following steps:

Execute sp_helptext against the view to copy it's code.
Execute sp_depends against your view to obtain the name of the tables that the view depends on
right click your table in the object browser in the query analyzer and select "script object to new window as create"
copy the table script and the view code and reply here and paste them in here and we'll hook you up.

if you dont know how to run any of these commands, type them in the query analyzer and then select them and press SHIFT+F1 on your keyboard. Books Online will open to the helpfile for the appropriate procedure|||Hey guys,

Thanks for your help. Here's the info:

Results of sp_texthelp:

Text
CREATE VIEW dbo.GetUsers
AS
SELECT dbo.getUsersAndTutors.*,dbo.Students.StudentsID AS StudentID
FROM dbo.getUsersAndTutors LEFT OUTER JOIN
dbo.Students ON dbo.getUsersAndTutors.UID = dbo.Students.UID

Results of sp_depends:

dbo.getUsersAndTutors

Unfortunately, I can't seem to make the "script object to new window as create" work. I can't right click since I'm on a Mac. Any suggestions?

Thanks.|||For starters, get rid of SELECT *

On a MAC?|||Hi Brett,

Yeah, I'm using a db management software for the Mac. As such, "right-click" doesn't exist.

What do you mean by "get rid of Select *" ?

I know I can type this into the query analyzer and add the name of a table in order to see the table but I'm not sure how this fits in with what Ruprect has asked me to do.

Thanks.|||Okay, doing a little reading and I "think" I know what's going on...

As far as I understand, you can use shortcuts to insert data into tables. If you make a change to the table, you could run into problems if your values don't match up with the new table.

With that being said, I'm wondering if my 'view' which depends on the table that I changed is accepting data into the wrong field because the original table was created using a shortcut?

I guess the question still remains...how do I see the script that describes how my table was created? More specifically, how do I do this with a Mac-based MS SQL manager?

One last question: Is there a query that will allow me to see this script?|||Hey Ruprect,

Figured out that command. Here's what I got...

CREATE TABLE [dbo].[Users] (
[UID] int IDENTITY(1,1) NOT NULL,
[Firstname] nvarchar(255) NOT NULL,
[Lastname] nvarchar(255) NOT NULL,
[Nickname] nvarchar(50) NOT NULL,
[Password] nvarchar(50) NOT NULL,
[Gender] int NULL DEFAULT (0),
[EmailAddr] nvarchar(255) NOT NULL,
[City] nvarchar(255) NULL,
[ProvID] int NULL DEFAULT (0),
[CurrentSecurityLevel] int NULL DEFAULT (0),
[DesiredSecurityLevel] int NULL DEFAULT (0),
[ATID] int NULL DEFAULT (0),
[AFID] int NULL DEFAULT (0),
[SystemStatus] int NULL DEFAULT (0),
[isOnline] bit NOT NULL DEFAULT (0),
[isAvailISM] bit NOT NULL DEFAULT (0),
[UserInfo] ntext NULL,
[DOB] datetime NULL,
[RegistrationDate] datetime NOT NULL,
[LastLoginDate] datetime NULL,
[stat_TimesLoggedin] int NOT NULL DEFAULT (0),
[stat_ISMRecvBytes] int NULL DEFAULT (0),
[stat_ISMSentBytes] int NULL DEFAULT (0),
[stat_Apptsmade] int NULL DEFAULT (0),
[stat_MsgPosted] int NULL DEFAULT (0),
[TutorRate] nvarchar(18) NULL,
CONSTRAINT [aaaaaUsers_PK] PRIMARY KEY([UID])
)
GO
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [Users_FK05]
FOREIGN KEY([CurrentSecurityLevel])
REFERENCES [dbo].[Type_SecurityLevel]([SecurityLevel])
GO
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [Users_FK03]
FOREIGN KEY([ProvID])
REFERENCES [dbo].[Type_Province]([ProvID])
GO
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [Users_FK01]
FOREIGN KEY([ATID])
REFERENCES [dbo].[Type_Avatar]([ATID])
GO
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [Users_FK04]
FOREIGN KEY([DesiredSecurityLevel])
REFERENCES [dbo].[Type_SecurityLevel]([SecurityLevel])
GO
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [Users_FK00]
FOREIGN KEY([AFID])
REFERENCES [dbo].[Type_Affiliation]([AFID])
GO
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [Users_FK02]
FOREIGN KEY([Gender])
REFERENCES [dbo].[Type_Gender]([GID])
GO

Thanks, again.