How to do a Select Distinct when returning XML - sql

I have a a stored procedure that returns an XML, the problem now is that i want it to return distinct ClientID's but when I do this.. it says
The xml data type cannot be selected as DISTINCT because it is not comparable.
How can i do this? here is my Query
SELECT DISTINCT ClientTable.ClientID,
ClientTable.ClientAddress,
ClientTable.RetailStore,
ClientTable.PhoneNumber,
ClientTable.City,
ClientTable.Amount,
(SELECT Rating = IsNull(AVG(Rate), 0),
NumberRates = IsNUll(COUNT(ClientID), 0)
FROM ReviewsTable
WHERE ReviewsTable.ClientID = ClientTable.ClientID
FOR XML PATH(''), TYPE)
FROM ClientTable
INNER JOIN ClientTypes
ON ClientTable.ClientID = ClientTypes.ClientID
WHERE ClientTable.ClientID IN (SELECT myFreeTextTableID
FROM myFreeTextTable
WHERE FREETEXT(*, #Keyword))
OR
ClientTypes.ClientID IN (SELECT myFreeTextTableID
FROM myFreeTextTable
WHERE FREETEXT(*, #Keyword))
ORDER BY ClientTable.Order ASC
FOR XML AUTO, TYPE, ELEMENTS

I'm not even going to try to reproduce your schema or data, but what about this query?
;WITH ft AS
(
SELECT ClientID = myFreeTextTableID
FROM myFreeTextTable WHERE FREETEXT(*, #Keyword)
),
c AS
(
SELECT DISTINCT
c.ClientID,
c.ClientAddress,
c.RetailStore,
c.PhoneNumber,
c.City,
c.Amount,
c.[Order]
FROM dbo.ClientTable AS c
INNER JOIN dbo.ClientTypes AS t
ON c.ClientID = t.ClientID
WHERE EXISTS
(
SELECT 1 FROM ft
WHERE ClientID IN (c.ClientID, t.ClientID)
)
)
SELECT
c.ClientID,
c.ClientAddress,
c.RetailStore,
c.PhoneNumber,
c.City,
c.Amount,
Rating = (SELECT COALESCE(AVG(Rate), 0),
NumberRates = COALESCE(COUNT(ClientID), 0)
FROM ReviewsTable
WHERE ReviewsTable.ClientID = c.ClientID
FOR XML PATH(''), TYPE)
FROM c
ORDER BY c.[Order]
FOR XML AUTO, TYPE, ELEMENTS;

Related

How to add a temp table in SQL server

I am trying to add a temp table to my query so that I can query that temp table, I have searched the internet but I couldn't get a solution.
this is my query
;WITH cte AS (
SELECT ID, g.Name
FROM game.Game g WITH(NOLOCK
WHERE ID IN (SELECT Data FROM system.Split(1, ','))
UNION ALL
SELECT g.ID, g.Name
FROM game.Game g WITH(NOLOCK)
JOIN cte ON g.ParentID = cte.ID
)
SELECT c.ID,
c.Name
FROM cte c
INNER JOIN list.Type gt WITH(NOLOCK) ON c.TypeId = gt.TypeID
WHERE c.ID NOT IN (SELECT Data FROM system.Split(1, ','))
AND c.ID IN (SELECT ID FROM game.code WITH(NOLOCK)
WHERE ID = c.ID
AND StatusCode IN ('OP', 'CL', 'SU')
AND isDisplay = 'True'
AND GETDATE() BETWEEN DisplayStart AND DisplayEnd
AND GETDATE() < ISNULL(ResultDateTime, ResultExpected)
)
which gives me the following when I run it
ID | Name
1111 | BaseBall
2222 |BasketBall
45896 |Relay
now I tried to create a temp table as follows
Create Table #temp(
ID int,
Name varchar
)
;WITH cte AS (
SELECT ID, g.Name
FROM game.Game g WITH(NOLOCK)
WHERE ID IN (SELECT Data FROM system.Split(1, ','))
UNION ALL
SELECT g.ID, g.Name
FROM game.Game g WITH(NOLOCK)
JOIN cte ON g.ParentID = cte.ID
)
insert into #temp // i wanted to set these values in the temp table
SELECT c.ID,
c.Name
FROM cte c
INNER JOIN list.Type gt WITH(NOLOCK) ON c.TypeId = gt.TypeID
WHERE c.ID NOT IN (SELECT Data FROM system.Split(1, ','))
AND c.ID IN (SELECT ID FROM game.code WITH(NOLOCK)
WHERE ID = c.ID
AND StatusCode IN ('OP', 'CL', 'SU')
AND isDisplay = 'True'
AND GETDATE() BETWEEN DisplayStart AND DisplayEnd
AND GETDATE() < ISNULL(ResultDateTime, ResultExpected)
)
every time I try to store this information in the temp table it gives me an error 'Column name or number of supplied values does not match table definition.' But I only have two values in. What am I doing wrong that I cant see?
First, why not just use select into?
IF OBJECT_ID('TempDB..#temp') IS NOT NULL
BEGIN
DROP TABLE #temp
END
select c.ID, c.Name
into #temp
from . . .
Then you don't need to define #temp as a table.
Next, your definition is bad, because Name has only one character. This would be fixed with select into.
However, I don't know why you are getting the particular error you are getting. The numbers of columns appears to match.

Transposing Rows to Columns and having values flow down in TSQL

I am using TSQL.
If I have the following results:
And I want to transpose this to be like this:
How can I achieve this?
I have the following query:
WITH q AS (
SELECT *
FROM tableOne
WHERE ID = 1
UNION ALL
SELECT m.*
FROM tableOne m
JOIN ON m.ParentID = q.ID
)
SELECT *
FROM q
This gives me all of the items underneath the specified node including the specified node.
It could be easy for us to help you if you add scripting data and not an image. Note that tbl is the name of your table, it is called 3 times. Try this:
select
a.fieldValue company,
b.fieldValue department,
c.fieldValue Job
from tbl a
inner join tbl b on a.parentId is null and a.id=b.parentID
inner join tbl c on b.id= c.parentID
If it does not bring desired results please add data as text and let me know, I could modify the query
Another option (assuming this is NOT a jagged hierarchy).
This is a standard Recursive CTE, with a little twist in the final SELECT
Example
;with cteP as (
Select ID
,ParentID
,PathID = cast(FieldValue as varchar(max))
From YourTable
Where ParentID is Null
Union All
Select ID = r.ID
,ParentID = r.ParentID
,PathID = cast(p.PathID+'|||'+r.FieldValue as varchar(max))
From YourTable r
Join cteP p on r.ParentID = p.ID)
Select ID
,B.*
From cteP A
Cross Apply (
Select Company = xDim.value('/x[1]','varchar(max)')
,Department = xDim.value('/x[2]','varchar(max)')
,Job = xDim.value('/x[3]','varchar(max)')
From (Select Cast('<x>' + replace(PathID,'|||','</x><x>')+'</x>' as xml) as xDim) as X
) B
Where ID not in (Select Distinct ParentID from YourTable where ParentID is not null)
Order By PathID
Returns

SQL add STUFF function into this query

I have the following query
SELECT TOP 1000
o.BuyerEMail,
COUNT(*) HowMany,
o.Name, o2.OrderID
FROM
Orders o
JOIN
(SELECT
BuyerEmail, MAX(OrderDate) Latest
FROM Orders
GROUP BY BuyerEmail) l ON o.BuyerEmail = l.BuyerEmail
JOIN
Orders o2 ON l.BuyerEmail = o2.BuyerEmail
AND l.OrderDate = o2.OrderDate
WHERE
Pay != 'PayPal'
GROUP BY
o.BuyerEmail, o.Name, l.Latest
ORDER BY
It's just producing a report about customers and how many orders they have with us and the latest order.
What I want to add is a list of the products of their last order which are in lines table.
Lines table is linked to order table by OrderID.
I was looking for something like:
JOIN
(SELECT
OrderID,
STUFF((SELECT ', ' + li.Code
FROM tblLines li
WHERE li.OrderID = o2.OrderID
FOR XML PATH ('')), 1, 1, '') AS [Codes]
GROUP BY
OrderID, Code
So the final table displays as
BuyerEmail | HowMany | Name | Latest | Codes
---------------------------------------------
Bob#bob | 4 | bob | 10000 | 123,10,201231
As you posted the query from another question (SQL most recent order? MS SQL), I will use my answer as it is cleaner than the above query:
SELECT o.*
, OrderID as LastOrderID
FROM (
SELECT BuyerEMail
, Name
, COUNT(*) as TotalOrders
FROM Orders
WHERE Pay != 'PayPal'
GROUP BY BuyerEmail, Name
) o
CROSS APPLY (
SELECT TOP 1 OrderID, OrderDate
FROM Orders s
WHERE s.BuyerEmail = o.BuyerEmail
ORDER BY OrderDate DESC
) ca
You posted a good example, it wasn't complete thou. You would need the following xmlpath query:
SELECT OrderID
, Codes
FROM tblLines r1
CROSS APPLY (
SELECT
STUFF((SELECT ',' + CAST(Code AS NVARCHAR)
FROM tblLines r2
WHERE r2.OrderID = r1.OrderID
GROUP BY OrderID, Code
ORDER BY Code
FOR XML PATH (''), TYPE)
.value('.', 'varchar(max)')
, 1, 1, '')) OrderLines(Codes)
GROUP BY OrderID, OrderList
Add it to the previous statement with a simple join:
SELECT o.BuyerEMail
,o.Name
,o.TotalOrders
, OrderID as LastOrderID
, c.Codes
FROM (
SELECT BuyerEMail
, Name
, COUNT(*) as TotalOrders
FROM Orders
WHERE Pay != 'PayPal'
GROUP BY BuyerEmail, Name
) o
CROSS APPLY (
SELECT TOP 1 OrderID, OrderDate
FROM Orders s
WHERE s.BuyerEmail = o.BuyerEmail
ORDER BY OrderDate DESC
) ca
INNER JOIN (
SELECT OrderID
, Codes
FROM tblLines r1
CROSS APPLY (
SELECT
STUFF((SELECT ',' + CAST(Code AS NVARCHAR)
FROM tblLines r2
WHERE r2.OrderID = r1.OrderID
GROUP BY OrderID, Code
ORDER BY Code
FOR XML PATH (''), TYPE)
.value('.', 'varchar(max)')
, 1, 1, '')) OrderLines(Codes)
GROUP BY OrderID, OrderList
) c
ON ca.OrderID = c.OrderID
SELECT TOP 1000
o.BuyerEMail,
COUNT(*) HowMany,
o.Name, l.Latest,
STUFF((SELECT ', ' + li.Code
FROM tblLines li
WHERE li.OrderID = o2.OrderID
FOR XML PATH ('')), 1, 1, '') AS [Codes]
FROM
Orders o
JOIN
(SELECT
BuyerEmail, MAX(OrderDate) Latest
FROM Orders
GROUP BY BuyerEmail) l ON o.BuyerEmail = l.BuyerEmail
JOIN
Orders o2 ON l.BuyerEmail = o2.BuyerEmail
AND l.OrderDate = o2.OrderDate
WHERE
Pay != 'PayPal'
GROUP BY
o.BuyerEmail, o.Name, l.Latest
ORDER BY

Use Inner Select value in Where Clause

I have an SQL query that I am trying to use a subs elects value in the outer where clause.
SELECT count(A.[processID]) AS total,
B.[process] AS processName,
(SELECT TOP 1 LocationDesc
FROM dbo.EmployeeTable_Historical AS D
WHERE C.leaver = D.QID
ORDER BY meta_logDate DESC)
FROM dbo.attritionDataPrevention AS A
INNER JOIN
attritionProcesses AS B
ON A.[processID] = B.[processID]
INNER JOIN
dbo.attritionData AS C
ON A.[recordID] = C.recordID
WHERE D.[locationDesc] IN (SELECT location
FROM #table)
AND YEAR(C.[leaveDate]) = #year
GROUP BY B.[processID]
ORDER BY total DESC
FOR XML PATH ('type'), TYPE, ELEMENTS, ROOT ('root');
In my main WHERE clause, it is saying that it cant BIND WHERE D.[locationDesc]. The S alias is in a sub query so I'm not too sure how to use its value.
UPDATE
Per suggestion, I changed the code to the following:
SELECT count(A.[processID]) AS total,
B.[process] AS processName
FROM dbo.attritionDataPrevention AS A
INNER JOIN
attritionProcesses AS B
ON A.[processID] = B.[processID]
INNER JOIN
dbo.attritionData AS C
ON A.recordID = C.recordID,
(
SELECT TOP 1 D.LocationDesc
FROM dbo.EmployeeTable_Historical AS D
WHERE C.leaver = D.QID
ORDER BY D.[meta_logDate] DESC
) AS D
WHERE D.locationDesc IN (SELECT location FROM #table)
AND YEAR(C.[leaveDate]) = #year
GROUP BY B.[process]
ORDER BY total DESC
FOR XML PATH ('type'), TYPE, ELEMENTS, ROOT ('root');
The only issue now is WHERE C.leaver wont bind to the inner join.
Try this :
SELECT count(A.[processID]) Over (Partition by B.[processID]) AS total,
B.[process] AS processName,
(SELECT TOP 1 LocationDesc
FROM dbo.EmployeeTable_Historical AS D
WHERE C.leaver = D.QID
AND D.[locationDesc] IN (SELECT location FROM #table)
ORDER BY meta_logDate DESC)
FROM dbo.attritionDataPrevention AS A
INNER JOIN
attritionProcesses AS B
ON A.[processID] = B.[processID]
INNER JOIN
dbo.attritionData AS C
ON A.[recordID] = C.recordID
WHERE YEAR(C.[leaveDate]) = #year
ORDER BY total DESC
FOR XML PATH ('type'), TYPE, ELEMENTS, ROOT ('root');

How can I use Sql to Order By This Statement?

How can I order the list 'widgets_spec by number of widgets?
select distinct
m.p_c_id
,(select distinct '<li>' +convert(varchar,widgets) + '<br> '
from dbo.spec_master m2
where m.p_c_id = m2.p_c_id and widgets is not null
for xml path(''), type).value('.[1]', 'nvarchar(max)'
) as widgets_spec
from dbo.spec_master m
inner join dbo.ProductVaration pv on pv.p_c_id = m.p_c_id
inner join dbo.Varation v on v.varation_id = pv.varation_type_id
where v.varation_id = 4
group by m.p_c_id
Right now output looks like:
<li>10<br> <li>12<br> <li>15<br> <li>8<br>
When I want it to look like:
<li>8<br> <li>10<br> <li>12<br> <li>15<br>
Thanks for your help.
EDIT: I'm trying to order the internal select statement that concatenates the values.
You do not need both Distinct and Group By. You should use one or the other. In this case, I believe you have to use Group By for it to work.
Select m.p_c_id
, (
Select '<li>' + Cast( m2.num_of_lights As varchar(10)) + '<br /> '
From dbo.spec_master As m2
Where m.p_c_id = m2.p_c_id
And m2.num_of_lights Is Not Null
Group By m2.num_of_lights
Order By m2.num_of_lights
For Xml Path(''), type).value('.[1]', 'nvarchar(max)'
) As numLights_spec
From dbo.spec_master As m
Inner Join dbo.ProductVaration As pv
On pv.p_c_id = m.p_c_id
Inner Join dbo.Varation As v
On v.varation_id = pv.varation_type_id
Where v.varation_id = 4
Group by m.p_c_id
select distinct
m.p_c_id
,(select distinct '<li>' +convert(varchar,num_of_lights) + '<br> '
from dbo.spec_master m2
where m.p_c_id = m2.p_c_id and num_of_lights is not null
ORDER BY convert(varchar,num_of_lights)
) as numLights_spec
from dbo.spec_master m
inner join dbo.ProductVaration pv on pv.p_c_id = m.p_c_id
inner join dbo.Varation v on v.varation_id = pv.varation_type_id
where v.varation_id = 4
group by m.p_c_id
) As SubA
Some of the other answers here won't work, since ordering by the now-varchar num_of_lights will put '8' after '15' as is happening now. You want to order the numLights numerically, which isn't going to happen with those html tags around them. You can add a subselect to your subselect so that you order them, then select them with the tags around them. Example (not tested):
SELECT * FROM (
select distinct
m.p_c_id
,(select distinct '<li>' +convert(varchar,num_of_lights) + '<br> '
from (select distinct p_c_id, num_of_lights from dbo.spec_master order by num_of_lights) m2
where m.p_c_id = m2.p_c_id and num_of_lights is not null
for xml path(''), type).value('.[1]', 'nvarchar(max)'
) as numLights_spec
from dbo.spec_master m
inner join dbo.ProductVaration pv on pv.p_c_id = m.p_c_id
inner join dbo.Varation v on v.varation_id = pv.varation_type_id
where v.varation_id = 4
group by m.p_c_id
Personally, I'd just add the html tags in whatever back-end code is getting the result of the query.