I have a table named FriendRequests which has columns for e.g. Id, RequestFrom and RequestTo
which has data for e.g
Id RequestFrom RequestTo
1 3C24B040-3CE2-46B6-950F-D76A8275505D 4C668138-BB4F-4228-9946-44DED1851C51
2 4C668138-BB4F-4228-9946-44DED1851C51 3C24B040-3CE2-46B6-950F-D76A8275505D
So now i want to find out how many such combinations are missing. For e.g. that table has only one row and the second row is missing.
I have tried this but not working
SELECT RequestTo ,
RequestFrom ,
IsPending ,
IsAccept ,
ActedDate
FROM FriendRequests
WHERE RequestFrom NOT IN ( SELECT DISTINCT
requestTo
FROM FriendRequests )
Try this:
SELECT f1.RequestTo ,
f1.RequestFrom ,
f1.IsPending ,
f1.IsAccept ,
f1.ActedDate
FROM FriendRequests f1
EXCEPT
SELECT f1.RequestTo ,
f1.RequestFrom ,
f1.IsPending ,
f1.IsAccept ,
f1.ActedDate
FROM FriendRequests f1
JOIN FriendRequests f2
ON f1.RequestFrom = f2.RequestTo
AND f2.RequestFrom = f1.RequestTo
Edit: Here is another approach
SELECT f1.RequestTo ,
f1.RequestFrom ,
f1.IsPending ,
f1.IsAccept ,
f1.ActedDate
FROM FriendRequests f1
WHERE NOT EXISTS (SELECT *
FROM FriendRequests f2
ON f1.RequestFrom = f2.RequestTo
AND f2.RequestFrom = f1.RequestTo)
It will find all the friend requests that has not been accepted.
Related
Receive error when execute in SQL.
SELECT viewz_INTAKE_STACK.A3 AS STNum, viewz_INTAKE_STACK.A6 AS DateofRef
, viewz_INTAKE_STACK.A6b AS TimeofRef
, viewz_INTAKE_STACK.A7 AS InitRespDate
, viewz_INTAKE_STACK.A7b AS InitTime
, viewz_INTAKE_STACK.A8a
, viewz_INTAKE_STACK viewz_INTAKE_STACK.A8c
, viewz_INTAKE_STACK.A8d
, viewz_INTAKE_STACK.A8e
, viewz_INTAKE_STACK.A8f
, viewz_INTAKE_STACK.A8g
, viewz_INTAKE_STACK.A8h
, viewz_INTAKE_STACK.A8i
, viewz_INTAKE_STACK.A8j
, viewz_INTAKE_STACK.A8k
, viewz_INTAKE_STACK.A8l
, viewz_INTAKE_STACK.L2a AS Gen
SELECT datediff(dd, A6, A7) AS WAITTIME
FROM viewz_INTAKE_STACK
INNER JOIN RAI_RAI ON viewz_INTAKE_STACK.rai_uno = RAI_RAI.rai_uno
WHERE (Where A6 BETWEEN $P{FromDate} and $P{ToDate})ISNULL(A6, '1900-01-01')!='1900-01-01'
Can someone tell me what is wrong with my query?
If you would have formatted it as Dale did for you, you could see that you have a nested select statement. This part:
SELECT datediff(dd, A6, A7) AS WAITTIME
FROM viewz_INTAKE_STACK
INNER JOIN RAI_RAI ON viewz_INTAKE_STACK.rai_uno = RAI_RAI.rai_uno
WHERE (Where A6 BETWEEN $P{FromDate} and $P{ToDate})ISNULL(A6, '1900-01-01')!='1900-01-01'
If this is supposed to be a sub-query, it'd need to be wrapped in parentheses.
Additionally, for your outer query, there is no FROM clause.
So, as a guess, you probably just want to remove the second SELECT word and add a comma for the column, remove the one instance where you only called the base table and no column, and remove the duplicate where. Here's a stab at it.
SELECT
viewz_INTAKE_STACK.A3 AS STNum
, viewz_INTAKE_STACK.A6 AS DateofRef
, viewz_INTAKE_STACK.A6b AS TimeofRef
, viewz_INTAKE_STACK.A7 AS InitRespDate
, viewz_INTAKE_STACK.A7b AS InitTime
, viewz_INTAKE_STACK.A8a
, viewz_INTAKE_STACK.A8c
, viewz_INTAKE_STACK.A8d
, viewz_INTAKE_STACK.A8e
, viewz_INTAKE_STACK.A8f
, viewz_INTAKE_STACK.A8g
, viewz_INTAKE_STACK.A8h
, viewz_INTAKE_STACK.A8i
, viewz_INTAKE_STACK.A8j
, viewz_INTAKE_STACK.A8k
, viewz_INTAKE_STACK.A8l
, viewz_INTAKE_STACK.L2a AS Gen
, datediff(dd, A6, A7) AS WAITTIME
FROM viewz_INTAKE_STACK
INNER JOIN RAI_RAI ON viewz_INTAKE_STACK.rai_uno = RAI_RAI.rai_uno
WHERE A6 BETWEEN $P{FromDate} and $P{ToDate} and ISNULL(A6, '1900-01-01')!='1900-01-01'
I have a query like below, but ShowRoomName and UnitNameare not shown in the result. How can I solve it?
SELECT A.ShowRoomId
, Sum(B.BasicAmount) AS TotalBasic
, Sum(B.HouseAmount) AS TotalHouseAmount
, Sum(B.MedicaleAmount) AS TotalMedicaleAmount
, Sum(B.ConvenceAmount) AS TotalConvenceAmount
, Sum(B.PhoneBillAmount) AS TotalPhoneBillAmount
, Sum(B.DirectorRemuneration) AS TotalDirectorRemuneration
, Sum(B.OthersAmount) AS TotalOthersAmount
FROM Employees A
OUTER APPLY (SELECT TOP 1 *
FROM EmployeeBasics B
WHERE ( A.EmployeeID = B.EmployeeID )
ORDER BY B.BasicUpdateDate DESC) AS B
OUTER APPLY (SELECT ShowRoomId
, ShowRoomName
, UnitId
FROM dbo.ShowRooms C
WHERE A.ShowRoomId = C.ShowRoomId) AS C
OUTER APPLY (SELECT UnitId
, UnitName
FROM dbo.Units D
WHERE C.UnitId = D.UnitId) AS D
GROUP BY A.SHowRoomId
use a more descriptive title when you ask a question. query not
working describes basically 99% of all questions.
your unit name and
showroomname dont appear because you dont select them...
I am guessing that you want something like this. Notice I changed your aliases into something meaningful. Using A, B, C is a bad habit for aliases. It is challenging to maintain code like that.
SELECT e.ShowRoomId
, Sum(eb.BasicAmount) AS TotalBasic
, Sum(eb.HouseAmount) AS TotalHouseAmount
, Sum(eb.MedicaleAmount) AS TotalMedicaleAmount
, Sum(eb.ConvenceAmount) AS TotalConvenceAmount
, Sum(eb.PhoneBillAmount) AS TotalPhoneBillAmount
, Sum(eb.DirectorRemuneration) AS TotalDirectorRemuneration
, Sum(eb.OthersAmount) AS TotalOthersAmount
, sr.ShowRoomName
, u.UnitName
FROM Employees e
OUTER APPLY
(
SELECT TOP 1 *
FROM EmployeeBasics eb1
WHERE e.EmployeeID = eb1.EmployeeID
ORDER BY eb1.BasicUpdateDate DESC
) AS eb
LEFT JOIN ShowRooms sr ON sr.ShowRoomId = e.ShowRoomId
LEFT JOIN Units u ON u.UnitID = sr.UnitID
GROUP BY e.SHowRoomId
, sr.ShowRoomName
, u.UnitName
I'm using MS Visual Studio 2012. I'm having an issue with writing a subquery. This is the entire query as follows:
SELECT ACCOUNTNUM
, NAME
, ADDRESS
, PHONE
, TELEFAX
, INVOICEACCOUNT
, CUSTGROUP
, PAYMTERMID
, CURRENCY
, DIMENSION
, CELLULARPHONE
, STATISTICSGROUP
, PAYMMODE
, NAMEALIAS
, CONTACTPERSONID
, STREET
, PARTYID
, SEGMENTID
, TAXGROUP
, DATAAREAID
, ISNULL((
SELECT PERCENT1 AS DiscCount
FROM PRICEDISCTABLE
WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
AND (ACCOUNTRELATION = CUSTTABLE.ACCOUNTNUM)
), 0) / 100 AS DiscCount
, (
SELECT NAME
FROM CONTACTPERSON
WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
AND (CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM)
AND (CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID)
) AS ContactName
, (
SELECT PHONE
FROM CONTACTPERSON AS CONTACTPERSON_1
WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
AND (CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM)
AND (CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID)
) AS ContactPhone
, ISNULL((
SELECT STATGROUPNAME
FROM CUSTSTATISTICSGROUP
WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
AND (CUSTSTATISTICSGROUP = CUSTTABLE.STATISTICSGROUP)
), 0) AS StatisticsName
FROM CUSTTABLE
WHERE CUSTGROUP = #ty
AND DATAAREAID = N'OTN'
AND STATISTICSGROUP LIKE #ss;
and then I get the error
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
Where the issue comes from?
Issue is that one of your subqueries returns more than one value (HEY, ERROR DESCRIPTION SAYS THAT).
How to find which one?
Try commenting out each subquery in your select statement until your query works fine.
How to fix it?
That's probably the best case to use CROSS APPLY. That'd be your query:
SELECT ACCOUNTNUM
, NAME
, ADDRESS
, PHONE
, TELEFAX
, INVOICEACCOUNT
, CUSTGROUP
, PAYMTERMID
, CURRENCY
, DIMENSION
, CELLULARPHONE
, STATISTICSGROUP
, PAYMMODE
, NAMEALIAS
, CONTACTPERSONID
, STREET
, PARTYID
, SEGMENTID
, TAXGROUP
, DATAAREAID
, ISNULL(T1.DiscCount, 0) / 100 AS DiscCount
, T2.ContactName
, T3.ContactPhone
, ISNULL(T4, 0) AS StatisticsName
FROM CUSTTABLE
CROSS APPLY (
SELECT PERCENT1 AS DiscCount
FROM PRICEDISCTABLE
WHERE DATAAREAID = CUSTTABLE.DATAAREAID
AND ACCOUNTRELATION = CUSTTABLE.ACCOUNTNUM
) AS T1
CROSS APPLY (
SELECT NAME
FROM CONTACTPERSON
WHERE DATAAREAID = CUSTTABLE.DATAAREAID
AND CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM
AND CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID
) AS T2
CROSS APPLY (
SELECT PHONE
FROM CONTACTPERSON AS CONTACTPERSON_1
WHERE DATAAREAID = CUSTTABLE.DATAAREAID
AND CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM
AND CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID
) AS T3
CROSS APPLY (
SELECT STATGROUPNAME
FROM CUSTSTATISTICSGROUP
WHERE DATAAREAID = CUSTTABLE.DATAAREAID
AND CUSTSTATISTICSGROUP = CUSTTABLE.STATISTICSGROUP
) AS T4
WHERE CUSTGROUP = #ty
AND DATAAREAID = N'OTN'
AND STATISTICSGROUP LIKE #ss;
Also, these can be rewritten as simple INNER JOINS.
When encountering this error, try running the subqueries individually and if more than one record is returned then refine your conditions to ensure only one record is returned.
Of course, if more than one record is wanted then you need to change your query to use a join instead of a subquery.
I am working on a dynamic query and I am a little unsure how to accomplish this task. It seems like something that would be common so I will use this as a learning experience.
My Structure has several tables. I have included a few of them for this example as once I can get the base query down, I can add on to it.
In my final query, the WHERE clause will be generated dynamically.
Here is a fiddle of my structure: http://sqlfiddle.com/#!6/2b104
In the inner select c. you will notice I have a column called localeID. I need to be able to query this in my outer WHERE clause.
For example, that localeID will link to the localeCodes table and from there, I have another table called locations. End result would be saying "Show me everything in North America". Well, we know localeID 8 = Utah and Utah is in North America (when joined to the locations table).
Here is the query to keep it with the OP:
SELECT a.[trainingEventID],
a.[teTitle],
a.[teSource],
a.[teType],
a.[teMedium],
a.[teFlag],
a.[teCreator],
a.[teCreated],
a.[tePOC],
a.[teDirector],
a.[teTeammateImpact],
a.[teCustomerImpact],
a.[teComplexity],
a.[intID],
a.[teNeededBy],
a.[approver],
a.[approvalDate],
(SELECT b.[trainingEventID],
b.[segmentDate],
b.[nonProdHrs],
(SELECT c.[segmentID],
c.[localeID],
c.[teammateCount],
c.[leaderCount]
FROM BS_TrainingEvent_SegmentDetails AS c
WHERE c.[segmentID] = b.teSegmentID
FOR XML PATH ('detail'), TYPE, ELEMENTS, ROOT ('details'))
FROM BS_TrainingEvent_Segments AS b
WHERE b.trainingEventID = a.[trainingEventID]
FOR XML PATH ('segment'), TYPE, ELEMENTS, ROOT ('segments'))
FROM BS_TrainingEvents AS a
--WHERE c.[localeID] = '8'
FOR XML PATH ('event'), TYPE, ELEMENTS, ROOT ('events');
How about we also join to those two subordinate tables so we can narrow the result set based on your c.localeID:
SELECT a.[trainingEventID],
a.[teTitle] ,
a.[teSource] ,
a.[teType] ,
a.[teMedium] ,
a.[teFlag] ,
a.[teCreator] ,
a.[teCreated] ,
a.[tePOC] ,
a.[teDirector] ,
a.[teTeammateImpact] ,
a.[teCustomerImpact] ,
a.[teComplexity] ,
a.[intID] ,
a.[teNeededBy] ,
a.[approver] ,
a.[approvalDate] ,
( SELECT b.[teSegmentID],
b.[trainingEventID] ,
b.[segmentDate] ,
b.[nonProdHrs] ,
( SELECT c.[segmentID] ,
c.[localeID] ,
c.[teammateCount] ,
c.[leaderCount]
FROM BS_TrainingEvent_SegmentDetails AS c
WHERE c.[segmentID] = b.teSegmentID
AND c.segmentID = tesd.SegmentID
FOR
XML PATH('detail') ,
TYPE ,
ELEMENTS ,
ROOT('details')
)
FROM BS_TrainingEvent_Segments AS b
WHERE b.trainingEventID = a.[trainingEventID]
AND b.trainingEventID = tes.trainingEventID
FOR
XML PATH('segment') ,
TYPE ,
ELEMENTS ,
ROOT('segments')
)
FROM BS_TrainingEvents a
INNER JOIN BS_TrainingEvent_Segments tes ON a.trainingEventID = tes.trainingeventID
INNER JOIN BS_TrainingEvent_SegmentDetails tesd ON tes.teSegmentID = tesd.SegmentID
INNER JOIN BS_LocaleCodes as locale ON tesd.localeID = locale.localeID
INNER JOIN BS_Locations as loc ON loc.location = locale.location
WHERE loc.[location] = 'Arizona'
FOR XML PATH('event') ,
TYPE ,
ELEMENTS ,
ROOT('events');
work on sql-server-2005
SELECT A.specific_customization_id ,
A.customization_id ,
A.customization_value ,
A.customization_price ,
A.customization_cost ,
A.customization_code ,
A.customization_check ,
A.sort_order ,
A.option_code ,
A.product_id ,
A.image_1 ,
A.image_2 ,
A.MainProductID ,
A.customization_product_id ,
A.inactive ,
A.customization_description ,
A.customization_select ,
A.UOM ,
A.allow_recur ,
auto_reorder = CASE A.MainProductID --it's an int type column
WHEN NULL THEN A.allow_recur
ELSE ( SELECT allow_recur
FROM wc_product
WHERE product_id = A.MainProductID
)
END
FROM dbo.wc_product_specific A
WHERE A.product_id = 1133
Using the above query I want to set auto_reorder column value .want to set two login
When My MainProductID column is null I want to set allow_recur column value
When it’s not null I want to set another table column value
My logic 2) works fine ,fail to understand why does 1) not work? Help me to fix the problem?
If have any query plz ask thanks in advance.
Does this work:?
SELECT A.specific_customization_id ,
A.customization_id ,
A.customization_value ,
A.customization_price ,
A.customization_cost ,
A.customization_code ,
A.customization_check ,
A.sort_order ,
A.option_code ,
A.product_id ,
A.image_1 ,
A.image_2 ,
A.MainProductID ,
A.customization_product_id ,
A.inactive ,
A.customization_description ,
A.customization_select ,
A.UOM ,
A.allow_recur ,
A.auto_reorder = CASE --it's an int type column
WHEN A.MainProductID IS NULL THEN A.allow_recur
ELSE B.allow_recur
END
FROM dbo.wc_product_specific A
left outer join wc_product B on B.product_id = A.MainProductID
WHERE A.product_id = 1133