November 19, 2017 at 5:11 am
Hello,
I want the combine the following queries
Select Complains.[Subject] as NewComplains,Complains.ID AS ComplainID from Complains where Complains.ReadStatus = 'false'
Select Suggestions.[Subject] as NewSuggestions,Suggestions.ID AS SuggestionID from Suggestions where Suggestions.ReadStatus = 'false'
Select KidID,Name from KidsData where KidsData.KidID = (Select KidID from Payments where EndDate < GETDATE())
NOTICE
- There is no relationship between the 3 tables
- No problem if there are duplicates I will handle it in my code
- The tables may have different numbers of rows
November 19, 2017 at 5:44 am
using union will make them in the same column which is not correct in my case
November 19, 2017 at 5:47 am
Then what is you're aim? You should supply DDL, sample data and expected outputs for T-SQL questions, could you do so please? You can see how to by the link in my signature.
Thanks.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
November 19, 2017 at 6:21 am
I'm aiming to have a table with 6 columns
SuggestionID - NewSuggestions - ComplainID - NewComplains - KidID - Name
my problem here I'm getting the data from 3 different tables which have no relationship between each other plus the number of rows maybe different
for example you may get data for
___________________________________________________________________
SuggestionID | NewSuggestions | ComplainID | NewComplains | KidID | Name
3 | sugSample | 4 | CompSampl | 20 | Hans
9 | sugSample2 | | | 21 | Sandra
----------------------------------------------------------------------------------------------------------------------
I know it might cause duplicates but this will not be a problem
November 19, 2017 at 6:31 am
Sounds like you need something like
(Kid LEFT JOIN Suggestions) LEFT JOIN Complaints
But then you'd need KidID in Suggestions AND in Complaints. Otherwise, how do you know which "kid" record is related to the Suggestion/Complaint record?
November 19, 2017 at 6:43 am
You say there is "no relationship", however, what defines that KidID 20 appears on the same line as ComplainID 4 and SuggestionID 3? Is it just "random"?
This is another total guess, without DDL, sample data and Expected results, and is completely untested but..:
If there any errors in the syntax, you'll need to resolve those.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
November 19, 2017 at 7:18 am
Thank you Thom A,
thats exactly what I need to do
Thank you all for replys and help
--------------------
the idea was to have a form to view a summary of notifications based on user choices, So the notifications could be from different tables which not related to each other,
However, Thom A's code is exactly what I need to do
Regards
December 25, 2017 at 3:57 am
Hello Thom A,
it seems there is something wrong with the last query, if there are more than 1 record it return the same error again
any suggestion ?
December 26, 2017 at 7:52 am
Mando_eg - Monday, December 25, 2017 3:57 AMHello Thom A,
it seems there is something wrong with the last query, if there are more than 1 record it return the same error againany suggestion ?
You haven't provided any details on what error you are getting, nor whether or not you made any modifications to Thom A's code to fit your situation. Those are essential elements when troubleshooting.
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
December 26, 2017 at 12:35 pm
I am really struggling to understand what the aim is. All three tables produce three completely different answers to three completely different questions. To that end presenting the data in the format you are suggesting is completely misleading and interpretion at best, extremely difficult.
If you are looking for a summary of notifications for each of the three areas represented by your queries, then create a parameterised report using the three tables each presented separately.
At least that is what I would do.....
December 27, 2017 at 1:33 am
Like the others said, if a different scenario is providing a "wrong" result, or error message, we need to know what the "wrong" result is and why, or what the error message is.
I'll quote my original reply again here, just remember to post the data for your new scenario
Thom A - Sunday, November 19, 2017 5:47 AMThen what is you're aim? You should supply DDL, sample data and expected outputs for T-SQL questions, could you do so please? You can see how to by the link in my signature.Thanks.
Please do not post it like you did before (seen below); make it consumable.
Mando_eg - Sunday, November 19, 2017 6:21 AM___________________________________________________________________
SuggestionID | NewSuggestions | ComplainID | NewComplains | KidID | Name
3 | sugSample | 4 | CompSampl | 20 | Hans
9 | sugSample2 | | | 21 | Sandra
----------------------------------------------------------------------------------------------------------------------
Thanks
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 4, 2018 at 7:42 am
Here is what you need, I think. Union All works but in order to get the results you want you need to add some NULL columns. It's a weird work around, but I think this gets you what you need.
Select Complains.[Subject] AS NewComplains,Complains.ID AS ComplainID, NULL AS NewSuggestions, NULL AS SuggestionID, NULL AS Name from Complains where Complains.ReadStatus = 'false'
UNION ALL
Select NULL AS NewComplains, NULL AS ComplainID, Suggestions.[Subject] as NewSuggestions,Suggestions.ID AS SuggestionID, NULL AS Name from Suggestions where Suggestions.ReadStatus = 'false'
UNION ALL
Select NULL AS NewComplains, NULL AS ComplainID,NULL AS NewSuggestions, NULL AS SuggestionID, KidID,Name from KidsData where KidsData.KidID = (Select KidID from Payments where EndDate < GETDATE())
Regards,
Matt
January 7, 2018 at 12:20 am
Well, Let me explain the scenario of this procedure
I have 3 tables
Suggestions, Complains, Payments
I'm getting a notification of New or Unread of Suggestion and Complains and I get a notification about the expired period from Payment
for example if the payment covers from 1-Dec-2017 I should get a notification about it on 1-Jan-2018
so instead of doing 3 queries I want combine them all to do only 1 query
the problem now is when there are many different results of each table I get that error
for example
I have 0 new complains , 1 new suggestion, 2 expired payments
I get the below error
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
January 15, 2018 at 8:21 am
Mando_eg - Sunday, January 7, 2018 12:20 AMWell, Let me explain the scenario of this procedure
I have 3 tables
Suggestions, Complains, Payments
I'm getting a notification of New or Unread of Suggestion and Complains and I get a notification about the expired period from Payment
for example if the payment covers from 1-Dec-2017 I should get a notification about it on 1-Jan-2018
so instead of doing 3 queries I want combine them all to do only 1 query
the problem now is when there are many different results of each table I get that error
for example
I have 0 new complains , 1 new suggestion, 2 expired payments
I get the below error
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Ah, eventually we get an error message. Your error is right in your first post. You can't do this
Select KidID,Name from KidsData where KidsData.KidID = (Select KidID from Payments where EndDate < GETDATE())
if there are potentially more than one rows coming back from the Payments table. Which row would it pick? What you should rather do isSELECT KD.KidID, KD.Name
FROM KidsData KD
JOIN Payments P ON KD.KidID = P.KidID ON KD.KidID = P.KidID
WHERE P.EndDate < GETDATE()
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply