Showing posts with label t-sql. Show all posts
Showing posts with label t-sql. Show all posts

Wednesday, March 28, 2012

Please help !!!

Hi, i have a problem about t-sql.

Table is with data

ID Name Part_Count

- --

1 Bush 3

2 Blair 2

3 Erdogan 2

Result table must be

ID NAME PART PART_COUNT

- - --

1 BUSH 3 1

2 BUSH 3 2

3 BUSH 3 3

4 BLAIR 2 1

5 BLAIR 2 2

6 ERDOGAN 2 1

7 ERDOGAN 2 2

HOW CAN I DO ?

The query below uses a couple of tricks. In order to expand the original part_count into a matching number of rows, you need a source of numbers stored in rows. Spt_values is a system table that has such a source of numbers. You could also build your own table or a table function.

Second, to generate the id notice that the original table can be used to get the starting id for the final list by summing the part_count field of all the rows with lower ids.

I have included my work tables so that you will have a complete example.

If this is for an assignment, I'd bet that you are supposed to use a cursor. That solution you ought to be able to figure out on your own.

/*Create table #one(
pid int not null Identity(1,1),
pname varchar(10) not null,
part_count int not null
)

insert #one values( 'Bush', 3 )
insert #one values( 'Blair', 2 )
insert #one values( 'Erdogan', 2 )
*/


Select id = (
Select start = IsNull( Sum( p2.part_count ), 0 )
From #one p2
Where p2.pid < parts.pid
) + v.number + 1,
name = parts.pname,
part = parts.part_count,
part_count = v.number + 1
From #one parts,
master..spt_values v
Where v.type = 'P'
And v.number < parts.part_count

Monday, March 12, 2012

Place or book to test writing T-SQL queries?

Hi,
I am looking for a place where I get to write sql queries and test my sql knowledge.If any one can suggest me site or book where I can test my sql knowledge. I have good exposure of SQL syntax and want to test how I implement it. Also, If Forum moderator can suggest me any book on this to improve myself in T-SQL.

Regards

Download the FREE copy of SQL Server 2005 Express, and use it to test your code on your local computer. Get your copy here.

For books, look at BOTH volumes of Itzak Ben-Gan's Microsoft Press titles.

|||Jump start to test your SQL skills...
http://www.sql-ex.ru/

|||Is there any book that provides sample scenarios for which one can write queries , like questions handbook? I do not have access to web all time, so , was looking for some offline solution. My requirement is getting some real life scenario query making.|||

SQL Server Express is installed locally on your computer, so it can be used whether or not you're connected to the web.

That will give you the environment in which to practice.

As to what to practice, try one of the Celko "puzzle" books or the SQL Server 2005: Applied techniques Step By Step book.

A search on Amazon.com for beginning SQL Server will also give you some possibilities.

Additionally, come to this forum, or any other sql forum where people post questions looking for help, and work on solving their issue(s).

(Even if someone else has provided a solution, disregard that solution while you work on coming up with your own and then check to see what others came up with.)

Wednesday, March 7, 2012

pivoting query on t-sql

gud day.

please help me. im working right now on a case study that will
retrieve/produce a simple report on sql. my problem is I dont know how
to pivot queries like in access. please help me. thanksHere's an example of a simple crosstab in SQL. Monthly Sales by region:

CREATE TABLE DailySales (region CHAR(10), saledate DATETIME, saleamount
DECIMAL(10,2) NOT NULL, PRIMARY KEY (region,saledate))

SELECT region,
SUM(CASE MONTH(saledate) WHEN 1 THEN saleamount END) AS jan,
SUM(CASE MONTH(saledate) WHEN 2 THEN saleamount END) AS feb,
SUM(CASE MONTH(saledate) WHEN 3 THEN saleamount END) AS mar,
SUM(CASE MONTH(saledate) WHEN 4 THEN saleamount END) AS apr,
SUM(CASE MONTH(saledate) WHEN 5 THEN saleamount END) AS may,
SUM(CASE MONTH(saledate) WHEN 6 THEN saleamount END) AS jun,
SUM(CASE MONTH(saledate) WHEN 7 THEN saleamount END) AS jul,
SUM(CASE MONTH(saledate) WHEN 8 THEN saleamount END) AS aug,
SUM(CASE MONTH(saledate) WHEN 9 THEN saleamount END) AS sep,
SUM(CASE MONTH(saledate) WHEN 10 THEN saleamount END) AS oct,
SUM(CASE MONTH(saledate) WHEN 11 THEN saleamount END) AS nov,
SUM(CASE MONTH(saledate) WHEN 12 THEN saleamount END) AS [dec]
FROM DailySales
GROUP BY region

These articles give examples of more complex, dynamic crosstabs:

http://www.sqlteam.com/item.asp?ItemID=2955
http://www.sqlmag.com/Articles/Inde...ArticleID=15608

--
David Portas
----
Please reply only to the newsgroup
--|||Check out the RAC utility.It is similar to
Access crosstab and has many more features/options.
You will find it as easy to use as Access.

www.rac4sql.net

pivot t-sql adding a percentage column

in a cross table result is very common to add a percentage column in respect

of the sum of another column

for example

x z%

a 2 0,46 = 2/(2+5+6)

b 5 0,38 = 5/(2+5+6)

c 6 0,15 = 6/(2+5+6)

is there an easy way to do that in t-sql using the pivot command ?

This was going to be my first answer

"Not easily that I can think of because it requires knowledge of the other rows in the result set and that is where SQL is not very good. That is performing calculations at two levels one aggregate is across th

You need to find the total and then calculate the percentage. This is normally done using a derived table"

However in digging I looked into windowing. This is a new feature that can be used with the new ranking functions as well as the normal aggregates.

In your situation you can do

select x, 1.0*x / sum(x) over (parition by 1) percentage

from mytable

What this does is that it performs a sum of x over the partition of 1 (as this is the same for each row it does a sum across the whole resultset).