July 24, 2016 at 8:30 pm
I have a table like this :
How can I get all possible 35 combinations of each date by SQL query.
Thanks very much :-):-):-)
July 24, 2016 at 8:49 pm
Can't see the image, but generally speaking, when you want "all possible combinations", you need a CROSS JOIN between two tables... If you had a table of Courses and a table of Students and you wanted all possible combinations of Course.CourseID and Student.StudentID, you would write something like this:
SELECT Student.StudentID
,Course.CourseID
FROM Student CROSS JOIN Course;
July 24, 2016 at 9:09 pm
Thanks very much
the image is updated ?
any detail SQL statement would you suggest ?
July 24, 2016 at 9:41 pm
calvin_wk_wong (7/24/2016)
Thanks very muchthe image is updated ?
any detail SQL statement would you suggest ?
What is the domain of numbers involved?
--Jeff Moden
Change is inevitable... Change for the better is not.
July 24, 2016 at 9:57 pm
What are N1...N7? Are they the number of days to add to a date?
Maybe this article will help... should be required reading.
Forum Etiquette: How to post data/code on a forum to get the best help[/url]
Could you post CREATE TABLE and INSERT scripts so we can see what you're working with? Pictures are pretty, but they don't paste into SSMS really well...
July 24, 2016 at 11:52 pm
pietlinden (7/24/2016)
What are N1...N7? Are they the number of days to add to a date?Maybe this article will help... should be required reading.
Forum Etiquette: How to post data/code on a forum to get the best help[/url]
Could you post CREATE TABLE and INSERT scripts so we can see what you're working with? Pictures are pretty, but they don't paste into SSMS really well...
This is a table
N1, N2, N3.....is the column name (header)
July 25, 2016 at 12:04 am
calvin_wk_wong (7/24/2016)
pietlinden (7/24/2016)
What are N1...N7? Are they the number of days to add to a date?Maybe this article will help... should be required reading.
Forum Etiquette: How to post data/code on a forum to get the best help[/url]
Could you post CREATE TABLE and INSERT scripts so we can see what you're working with? Pictures are pretty, but they don't paste into SSMS really well...
This is a table
N1, N2, N3.....is the column name (header)
That's how it's been asked on the test.
It's a theoretical question to test the skills, no actual business meaning behind it.
_____________
Code for TallyGenerator
July 25, 2016 at 12:10 am
I am not sure what you mean by 35 combinations and your description is not detailed enough either.
Your request doesn't seem too complex and I am sure you will get a quick response if you provide us the below mentioned things.
1. DDL statements of the table(s) involved
2. Some sample data to populate the table(s), at least for 2 - 3 dates
3. Expected results in an easy to understand form
This will help us understand your question better and also test any solutions before posting it to you.
You can have a look at the link in my signature if you are not sure on how to do this.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
July 25, 2016 at 12:56 am
Complete stab in the dark, that what you are looking at is some kind of table with 5 rows and based on what can be seen, and assuming it is indeed only one table: Lots of if buts and maybes here, which is why it is important to provide the detail requested; how about:
CREATE TABLE dbo.MyHomework
(
MyDateCol date
, N1 INT
, N2 INT
, N3 INT
, N4 INT
, N5 INT
, N6 INT
, N7 INT
, CONSTRAINT PK_MyHW PRIMARY KEY CLUSTERED (MyDateCol) ON [PRIMARY]
);
INSERT INTO dbo.MyHomework (MyDateCol, N1, N2, N3, N4, N5, N6, N7)
VALUES
('04-07-2002', 5, 32, 34, 39, 49, 38, 8),
('06-07-2002', 21, 10, 33, 40, 20, 44, 34),
('08-07-2002', 10, 36, 48, 35, 30, 38, 12);
SELECT MyDateCol, Numb, MyCol
FROM dbo.MyHomework
UNPIVOT (
MyCol FOR Numb IN (
N1, N2, N3, N4, N5, N6, N7
)
) unpvt
...
August 3, 2016 at 3:17 pm
That's how it's been asked on the test.
Then wouldn't it be more honest to just say you don't know?
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
August 3, 2016 at 9:03 pm
I think Andrew (Happy Geek) hit the nail on the head. Assuming that there are 5 dates/rows total there should be a total of 35 number/date combinations.
Taking Andrew's test data and adding two more rows we have this:
CREATE TABLE dbo.MyHomework
(
MyDateCol date
, N1 INT
, N2 INT
, N3 INT
, N4 INT
, N5 INT
, N6 INT
, N7 INT
, CONSTRAINT PK_MyHW PRIMARY KEY CLUSTERED (MyDateCol) ON [PRIMARY]
);
INSERT INTO dbo.MyHomework (MyDateCol, N1, N2, N3, N4, N5, N6, N7)
VALUES
('04-07-2002', 5, 32, 34, 39, 49, 38, 8),
('06-07-2002', 21, 10, 33, 40, 20, 44, 34),
('08-07-2002', 10, 36, 48, 35, 30, 38, 12),
('10-07-2002', 27, 26, 28, 31, 30, 36, 2),
('12-07-2002', 50, 46, 41, 75, 32, 3, 1);
Here's an alternative method for resolving this which will produces the same results and same execution plan minus the compute scalar and filter operators:
-- Unpivot alternative
SELECT MyDateCol, Numb, MyCol
FROM dbo.MyHomework
CROSS APPLY
(VALUES
('N1',N1),('N2',N2),('N3',N3),('N4',N4),('N5',N5),('N6',N6),('N7',N7)
) AS UnpivotTable (Numb, MyCol);
-- Itzik Ben-Gan 2001
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply