Tag Info

New answers tagged

0

Looks like you just need to add a WHERE filter to your query: SELECT A, B, C, ... FROM YourTable WHERE D IN ('IA1', 'IA2', 'IA3') GROUP BY A, B, C ; That way those A, B, C groups that do not have 'IA1', 'IA2' or 'IA3' in column D will be excluded from the output.


1

What you expected to have actually exists in Eloquent. In order to attach/detach related models you need to call attach/detach methods on the relations and pass a single ID or array of IDs: $user->roles()->attach($roleId); $user->roles()->attach([$roleId1, $roleId2]); $role->users()->attach($userId); $role->users()->attach([$userId1, ...


1

You essentially want to pivot the meta_value column. Some SQL products have dedicated syntax for this operation. MySQL does not have it, but there is a universal method that works in most products, including MySQL: conditional aggregation. SELECT user_id, MAX(CASE meta_key WHEN 'first_name' THEN meta_value END) AS first_name, MAX(CASE meta_key WHEN ...


1

You are saying: EXEC @SQLString; This is like saying: EXEC N'SELECT 1;'; Which is probably something that looks like it will work on first glance, but go ahead and try it, it fails. You want: DECLARE @SQL NVARCHAR(MAX); SET @SQL = N'SELECT ...;'; EXEC sys.sp_executesql @sql; I wrote a pretty detailed article on dynamic pivot here: ...


2

atThis is pretty much when bluefeet gave you as an example in the comment. DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) select @cols = STUFF((SELECT distinct ',' + QUOTENAME([Status]) from [EVAULTTEST].[dbo].EvidenceItems FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') set ...



Top 50 recent answers are included