August 3, 2018 at 12:59 am
August 3, 2018 at 1:08 am
pcreddy1005 - Friday, August 3, 2018 12:59 AM
Please post the DDL (create table) scripts, sample data as an insert statement, the expected output and what you have tried so far.
😎
These are simple queries but your samples are too vague.
August 3, 2018 at 2:21 am
for beginners may i recommend Sams tech yourself SQL in 10 minutes 🙂 10 minutes later you will be able to write the SQL to answer these questions yourself 🙂
***The first step is always the hardest *******
August 3, 2018 at 2:31 am
SGT_squeequal - Friday, August 3, 2018 2:21 AMfor beginners may i recommend Sams tech yourself SQL in 10 minutes 🙂 10 minutes later you will be able to write the SQL to answer these questions yourself 🙂
It is a good book for a beginner!
😎
Had a look at it few years back when a joker friend of mine gave me a copy, didn't have it for long as a gave it to a production DBA 😉
August 3, 2018 at 4:28 am
We have an excellent primer on T-SQL right here on this web site. I'd suggest you read through that and if anything doesn't make sense, please ask. However, I'm not doing your homework for you.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
August 3, 2018 at 5:07 am
Grant Fritchey - Friday, August 3, 2018 4:28 AMHowever, I'm not doing your homework for you.
Oh Grant you're so tetchy :laugh:
Far away is close at hand in the images of elsewhere.
Anon.
August 3, 2018 at 5:15 am
Not so much as a please.
August 3, 2018 at 7:52 am
Beatrix Kiddo - Friday, August 3, 2018 5:15 AMNot so much as a please.
Won't even transcribe the questions. Asking for good manners is even harder.
August 3, 2018 at 7:53 am
Since I'm feeling a little bit sympathetic, I'll share this article with you that could help you solve the first 2 questions.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - SQLServerCentral
August 3, 2018 at 9:04 am
Hi PC Reddy,
here is the query you asked for
Question 1
(Need to czategorize the column as positive and negative and sum it as below.)
SELECT SUM([Positive Numbers]) AS [Positive Numbers], SUM([Netagive Numbers]) AS [Netagive Numbers]
FROM (
SELECT SUM(CASE WHEN Num>0 THEN Num ELSE 0 END) AS [Positive Numbers],
SUM(CASE WHEN Num<0 THEN Num ELSE 0 END) AS [Netagive Numbers]
FROM #TableA
GROUP BY Num
) ResultSet
Question 2
(Need to Just pivot the column)
SELECT [Chandra] [FirstName], [Sekhar] [MiddleName], [Reddy] [LastName], [Peddireddy] [SurName]
FROM
(SELECT Name
FROM #TableB) AS SourceTable
PIVOT
(
MIN(Name)
FOR Name IN ([Chandra], [Sekhar], [Reddy], [Peddireddy])
) AS PivotTable;
Question 3
(Just use the case statement with Modulus (Mod = "%"))
SELECT id, name, (CASE id%2 WHEN 1 THEN 'M' ELSE 'F' END) AS Gender
FROM #TableC
August 3, 2018 at 9:43 am
prabhu.st - Friday, August 3, 2018 9:04 AMHi PC Reddy,here is the query you asked for
Question 1
(Need to czategorize the column as positive and negative and sum it as below.)
SELECT SUM([Positive Numbers]) AS [Positive Numbers], SUM([Netagive Numbers]) AS [Netagive Numbers]
FROM (
SELECT SUM(CASE WHEN Num>0 THEN Num ELSE 0 END) AS [Positive Numbers],
SUM(CASE WHEN Num<0 THEN Num ELSE 0 END) AS [Netagive Numbers]
FROM #TableA
GROUP BY Num
) ResultSetQuestion 2
(Need to Just pivot the column)
SELECT [Chandra] [FirstName], [Sekhar] [MiddleName], [Reddy] [LastName], [Peddireddy] [SurName]
FROM
(SELECT Name
FROM #TableB) AS SourceTable
PIVOT
(
MIN(Name)
FOR Name IN ([Chandra], [Sekhar], [Reddy], [Peddireddy])
) AS PivotTable;Question 3
(Just use the case statement with Modulus (Mod = "%"))
SELECT id, name, (CASE id%2 WHEN 1 THEN 'M' ELSE 'F' END) AS Gender
FROM #TableC
In your first query, the derived table is only going to return one row, so there is no need to SUM again in the outer query. In fact, you can just do away with the outer query altogether.
Your other two queries are ad hoc. That is, they will only work with the specific data provided and are not a general solution.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
August 3, 2018 at 10:29 am
drew.allen - Friday, August 3, 2018 9:43 AMprabhu.st - Friday, August 3, 2018 9:04 AMHi PC Reddy,here is the query you asked for
Question 1
(Need to czategorize the column as positive and negative and sum it as below.)
SELECT SUM([Positive Numbers]) AS [Positive Numbers], SUM([Netagive Numbers]) AS [Netagive Numbers]
FROM (
SELECT SUM(CASE WHEN Num>0 THEN Num ELSE 0 END) AS [Positive Numbers],
SUM(CASE WHEN Num<0 THEN Num ELSE 0 END) AS [Netagive Numbers]
FROM #TableA
GROUP BY Num
) ResultSetQuestion 2
(Need to Just pivot the column)
SELECT [Chandra] [FirstName], [Sekhar] [MiddleName], [Reddy] [LastName], [Peddireddy] [SurName]
FROM
(SELECT Name
FROM #TableB) AS SourceTable
PIVOT
(
MIN(Name)
FOR Name IN ([Chandra], [Sekhar], [Reddy], [Peddireddy])
) AS PivotTable;Question 3
(Just use the case statement with Modulus (Mod = "%"))
SELECT id, name, (CASE id%2 WHEN 1 THEN 'M' ELSE 'F' END) AS Gender
FROM #TableCIn your first query, the derived table is only going to return one row, so there is no need to SUM again in the outer query. In fact, you can just do away with the outer query altogether.
Your other two queries are ad hoc. That is, they will only work with the specific data provided and are not a general solution.
Drew
Hi Drew,
I accept your feedback for the second query (but, since there is no much information about the DDL, just tried with the given samples)
but for the thir one is generic as per the data and the DDL, if you notice the sample data, all the odd numbers are marked mistakenly as "F" and the even numbers are as "M", based on this I posted my queries..
I believe there may be some other better way, but I just wrote whichever there in the top of my mind that time..
thank you so much for the valueable feedback.. it could correct me on my mistakes...
August 3, 2018 at 11:24 am
prabhu.st - Friday, August 3, 2018 10:29 AMbut for the thir one is generic as per the data and the DDL, if you notice the sample data, all the odd numbers are marked mistakenly as "F" and the even numbers are as "M", based on this I posted my queries..
The point is that the numbers are arbitrarily assigned to the names. Your "solution" is ad hoc because it will not work for every (or even most) arbitrary assignments of numbers to names.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
August 3, 2018 at 6:30 pm
pcreddy1005 - Friday, August 3, 2018 12:59 AM
Dude... do your own homework. We're not the ones that need to pass the final exam... you are. Sit down and science it out as if your future jobs depended on it... because they do. 😉
If this is a pre-exam for an interview, then you might be in trouble for real because it appears that you've applied for a job that you can't actually do.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 3, 2018 at 6:38 pm
prabhu.st - Friday, August 3, 2018 9:04 AMHi PC Reddy,here is the query you asked for
Question 1
(Need to czategorize the column as positive and negative and sum it as below.)
SELECT SUM([Positive Numbers]) AS [Positive Numbers], SUM([Netagive Numbers]) AS [Netagive Numbers]
FROM (
SELECT SUM(CASE WHEN Num>0 THEN Num ELSE 0 END) AS [Positive Numbers],
SUM(CASE WHEN Num<0 THEN Num ELSE 0 END) AS [Netagive Numbers]
FROM #TableA
GROUP BY Num
) ResultSetQuestion 2
(Need to Just pivot the column)
SELECT [Chandra] [FirstName], [Sekhar] [MiddleName], [Reddy] [LastName], [Peddireddy] [SurName]
FROM
(SELECT Name
FROM #TableB) AS SourceTable
PIVOT
(
MIN(Name)
FOR Name IN ([Chandra], [Sekhar], [Reddy], [Peddireddy])
) AS PivotTable;Question 3
(Just use the case statement with Modulus (Mod = "%"))
SELECT id, name, (CASE id%2 WHEN 1 THEN 'M' ELSE 'F' END) AS Gender
FROM #TableC
So if the person that posted the original request with absolutely no effort on their part gets your job because you helped him with his homework or an interview, are you going to feel bad? 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply