|
Hi,
Is there any query through which i can get the number of columns in a table.
Best Regards,
Mushq
Mushtaque Ahmed Nizamani
Software Engineer
Ultimus Pakistan
"English is my second language, so please don't mind if i do some grammatical or spelling mistakes in my messages."
|
|
|
|
|
SELECT Count(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_Name = 'TableName'
|
|
|
|
|
I try this code, and it's work.
select name,date=max(date),
sum(case when type='A' then value else 0 end) as 'A',
sum(case when type='B' then value else 0 end) as 'B',
sum(case when type='C' then value else 0 end) as 'C'
from table1
group by name
But after few days, my database is update with more than one record in the same name, separate by column name 'date',
Regarding I just want to display data depend on last date,
Example my table is below:
name type value date
-----------------------------------------------------
a A 1 07/10/2007
a B 1 07/10/2007
a C 1 07/10/2007
a A 2 07/13/2007
a B 2 07/13/2007
a C 2 07/13/2007
but the result is:
the result is:
name date A B C
-----------------------------------------
a 06/13/2007 3 3 3
I'm expecting like this:
name date A B C
-----------------------------------------
a 06/13/2007 2 2 2
I try to modified the code:
select name,
sum(case when type='A' and date=max(date) then....
but found error
So, any suggestion, thanks a lot.
|
|
|
|
|
Have you tried:
<code> select name,date,
sum(case when type='A' then value else 0 end) as 'A',
sum(case when type='B' then value else 0 end) as 'B',
sum(case when type='C' then value else 0 end) as 'C'
from table1 where date=max(date)
group by name </code>
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
|
|
|
|
|
Are your sure and Have you checked is the query wroks.
Regards
KP
|
|
|
|
|
Krish - KP,
This one should do it. I created the same table you had, ran this one and it gives the right results
<code>select name,max(date),
sum(case when type='A' then value else 0 end) as 'A',
sum(case when type='B' then value else 0 end) as 'B',
sum(case when type='C' then value else 0 end) as 'C'
from table_1 where date=(select max(date) from table_1)
group by name </code>
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
|
|
|
|
|
Now syntax is right.
what about logic. is this what required
Regards
KP
|
|
|
|
|
About logic is OK, I already use convert.
And about your code below:
SELECT a.name, MAX(a.dt) AS [date],
SUM(CASE WHEN type = 'A' THEN valu ELSE 0 END) AS A,
SUM(CASE WHEN type = 'B' THEN valu ELSE 0 END) AS B,
SUM(CASE WHEN type = 'C' THEN valu ELSE 0 END) AS C
FROM TblB a
INNER JOIN (SELECT name, MAX(dt) AS dt FROM tblB GROUP by name) b
ON a.name = b.name and a.dt = b.dt
GROUP BY a.name
this only show each name, with maximum value
not each name, each type with maximum value.
I have been tried to modified inside inner join code but, still don't get.
Pls help mee.....
|
|
|
|
|
example given gives max with name. that's why in inner join table (subquery)
i've not used type.
include type also in subquery's group by clause.
Regards
KP
|
|
|
|
|
OK Krish, I done with your code, it's very2 work thanks.
But how I write sql syntax if I want to select another table
(for example tbl2) instead of a.tbl1 INNER JOIN b.tbl1 ???
Thanks a lot
B.regards
|
|
|
|
|
Hi Jonh, I have error on your code
Msg 147, Level 15, State 1, Line 1
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
I try to modified the code below:
select name,date,
sum(case when type='A' then value else 0 end) as 'A',
sum(case when type='B' then value else 0 end) as 'B',
sum(case when type='C' then value else 0 end) as 'C'
from table1 where date=(select max(date) from table1)
group by name
it's work but, this code will only show maximum date on whole table,
not show each name which have maximum date.
Need your suggestion. thanks
|
|
|
|
|
SELECT a.name, MAX(a.dt) AS [date], <br />
SUM(CASE WHEN type = 'A' THEN valu ELSE 0 END) AS A, <br />
SUM(CASE WHEN type = 'B' THEN valu ELSE 0 END) AS B, <br />
SUM(CASE WHEN type = 'C' THEN valu ELSE 0 END) AS C<br />
FROM TblB a <br />
INNER JOIN (SELECT name, MAX(dt) AS dt FROM tblB GROUP by name) b <br />
ON a.name = b.name and a.dt = b.dt<br />
GROUP BY a.name
Regards
KP
|
|
|
|
|
Does that one do what you need it to?
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
|
|
|
|
|
Yes, you all great !!!
Thanks a lot.
B.regards
|
|
|
|
|
Hi All, sorry to bothering you again, need your help.
my format date is complete like "7/12/2007 10:40:49 AM"
so, concern that time/date is different each row, example (i write date as integer)
name type value date
-----------------------------------------------
a A 1 1
a B 1 2
a C 1 3
a A 2 4
a B 2 5
a C 2 6
when I run the code the result is:
name date A B C
----------------------------------------
a 6 0 0 2
so, the code show value each name with maximum date,
not show value each name, each type with maximum date.
so, i very confuse, realy need your help, thanks
|
|
|
|
|
how do i make a backup plan that can backup the database to another location automaticly
|
|
|
|
|
|
Use maintenance plans in SQL Management!!!
I was born dumb!!
Programming made me laugh !!!
--sid--
|
|
|
|
|
How to Import the SQL DataBase Diagram In visio
merwa
|
|
|
|
|
I don't think you can import it directly... but depending on the version of Visio you are using, you can tell it to create a diagram from a database by going to:
Tools > Options > Add-ins > Database Wizard
or Database > Reverse Engineer...
and following the instructions.
"It was the day before today.... I remember it like it was yesterday."
-Moleman
|
|
|
|
|
How to write query to find relationship between the primary key and foreign key re lation ship between the tables
|
|
|
|
|
Elaborate a bit more about your question. What database are you using?
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Rmesh wrote: How to write query to find relationship between the primary key and foreign key re lation ship between the tables
That depends on the database you are using. In SQL Server there are sys* tables and INFORMATION_SCHEMA views that will allow you to get at this information.
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
"I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless."
Ready to Give up - Your help will be much appreciated.
My website
|
|
|
|
|
Colin Angus Mackay wrote: depends on the database you are using
That is why I asked earlier. easy in SQL, but I am not sure about Access. I've looked at some of the system table and it might be a little more work.
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Having a website that is dealing with large numbers, I am storing values inside a decimal(36,18) SQL Server 2005 column.
Until recently, everything run smooth for about 1 year.
Then it crashed with a System.OverflowException exception ("Conversion overflows.") and the following stack trace (excerpt):
at System.Data.SqlClient.SqlBuffer.get_Decimal()
at System.Data.SqlClient.SqlBuffer.get_Value()
at System.Data.SqlClient.SqlDataReader.GetValueInternal(Int32 i)
at System.Data.SqlClient.SqlDataReader.GetValues(Object[] values)
at System.Data.ProviderBase.DataReaderContainer.CommonLanguageSubsetDataReader.GetValues(Object[] values)
at System.Data.ProviderBase.SchemaMapping.LoadDataRow()
at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
...
The value it seems to be unable to read from the database is 90807364313.118401000000000000 (i.e. a rather huge, approx. 90 billions ).
Now after digging, I found this thread at Google Groups. [^].
This thread, from 2004, tells that this is a bug in the .NET framework and that it will be fixed in next version. Now since it is already the next version, I am rather clueless.
Question: Anyone knows a hint or a workaround/fix for this issue? Maybe a hotfix from Microsoft is available?
Thanks
Uwe
|
|
|
|