SQL Teaser
May 31, 1999
I have a table (POS) with 3 columns and 4 rows:
KEY COLUMNA COLUMNB
1 1 dog
2 1 cat
3 2 dog
4 2 cat
What is the SQL to return
COLUMNA COLUMNB
1 dog
2 dog
?
Thanks
View 2 Replies
Nov 14, 2003
I’ve just remembered a very small yet challenging SQL statement brain teaser a friend of mine asked me long ago.
Though as simple as it might seem, I still couldn’t figure out how to solve it until this day. I just thought that some of you guys might have an answer to it.
Here is the question:
You have a table named “table1� that has only 2 fields: “name� and “phone�.
This table is populated with the following sample data
NamePhone
---------------------
John111111
Tom222222
Jack333333
John444444
Smith555555
----------------------
5 records
To get all the distinct names in this table, you would write the sql statement:
Select distinct name from table1
And the result would be:
Name
-------
John
Tom
Jack
Smith
-------
4 records
The question is: can you write a sql statement that can return all the distinct names along with their phone numbers.
E.g.
NamePhone
---------------------
John111111
Tom222222
Jack333333
Smith555555
----------------------
4 Records
View 3 Replies
View Related
Sep 5, 2005
This one has been stumping me for several days. I can run a query thatreturns several different items from several different manufacturers,each with a ranking score. Each manufacturer can have any number ofitems:Item_Name Manufacturer rankItem 1 Manu_A 82Item 2 Manu_A 65Item 3 Manu_A 41Item 4 Manu_B 32Item 5 Manu_C 21Item 6 Manu_B 19However, I would like the records to be ordered so that the highestranking item is shown first, then the next highest item from adifferent manufacturer is shown second, then the next highest item froma third manufacturer is show, etc.:Item 1 Manu_A 82Item 4 Manu_B 32Item 5 Manu_C 21Item 2 Manu_A 65Item 6 Manu_B 19Item 3 Manu_A 41Does anyone have any thoughts on how to order the results in thisfashion?thanks,Matt Weiner
View 2 Replies
View Related
Mar 8, 2006
I have a requirement (motivated by a SOX thing) that is just giving mefits. I know it should be easy and I'm probably overthinking it, but Ijust can seem to find the best way to get where I need to go.I have some payment projection data derived from a huge procedure thatI'm dumping into a temp table that looks like looks this:Key Pd Start End AnnualAmt MonthAmt DailyAmt6789 1 2005-06-01 2010-05-31 49,500.00 4,125.00 135.6164386789 2 2010-06-01 2015-05-31 54,450.00 4,537.50 149.1780826789 3 2015-06-01 2020-05-31 59,895.00 4,991.25 164.0958906789 4 2020-06-01 2024-05-31 65,884.50 5,490.38 180.505479(there are actually 6 levels of keys, but you get the idea)I need it to get into a reporting table looking like this:Key Rev Year ProjectedAmt6789 2005 29,021.926789 2006 49,500.006789 2007 49,500.006789 2008 49,500.006789 2009 49,500.006789 2010 20,478.086789 2010 31,924.116789 2011 54,450.006789 2012 54,450.006789 2013 54,450.006789 2014 54,450.006789 2015 22,525.886789 2015 35,117.406789 2016 59,895.006789 2017 59,895.006789 2018 59,895.006789 2019 59,895.006789 2020 24.779.10etc...I'm having a problem wrapping my head around how to get the rows in themiddle of each period.The other, probably minor and statistically insignificant, issue isproration on a leap year. If a proration occurs on a leap year and Ihave to calculate the proration based on a DATEDIFF and an Annual orMonthly Amount, I'm going to be a day over.Anybody have any tricks or ideas???Thanks so much for your help!Jody
View 3 Replies
View Related
May 10, 2007
I have a table with 2 columns as follows:
COORD LEVEL===== =====1 11 22 23 21 32 33 34 35 3
I need to produce a result that has 1 column that is the concatenation of each COORD separated by a '.' by increasing LEVEL
EX: The result should look like this
1.1.1
1.2.1
1.2.2
1.2.3
1.2.4
1.2.5
1.3.1
1.3.2
1.3.3
1.3.4
1.3.5
Please note that the answer must be flexible enough to handle any number of levels and coordinates (although levels will most likely be less than 5 and coordinates less than 100)
Can this be done in a simple SQL statement?
ANY help is very much appreciated.
View 7 Replies
View Related