SQL Order the rows - sql

This is the following db structure i have, here the issue we are facing to order the rows based on the filed wise.
Field - table hold the field information ed, 2002,2015, Region 1 etc
IndicatorData- hold the row data
datafield - relationship with IndicatorData table... row can have multiple fields
/****** object: table [dbo].[indicatordata] ******/
create table [dbo].[indicatordata](
[id] [bigint] null,
[value] [decimal](18, 2) null,
[hopevalue] [decimal](18, 2) null,
[indicatorid] [int] null,
[datakind] [int] null
) on [primary]
go
insert [dbo].[indicatordata] ([id], [value], [hopevalue], [indicatorid], [datakind]) values (195045, cast(70.00 as decimal(18, 2)), cast(0.00 as decimal(18, 2)), 2032, 0)
insert [dbo].[indicatordata] ([id], [value], [hopevalue], [indicatorid], [datakind]) values (195046, cast(40.00 as decimal(18, 2)), cast(0.00 as decimal(18, 2)), 2032, 0)
insert [dbo].[indicatordata] ([id], [value], [hopevalue], [indicatorid], [datakind]) values (195047, cast(5.00 as decimal(18, 2)), cast(0.00 as decimal(18, 2)), 2032, 0)
insert [dbo].[indicatordata] ([id], [value], [hopevalue], [indicatorid], [datakind]) values (195048, cast(100.00 as decimal(18, 2)), cast(0.00 as decimal(18, 2)), 2032, 0)
insert [dbo].[indicatordata] ([id], [value], [hopevalue], [indicatorid], [datakind]) values (195049, cast(87.00 as decimal(18, 2)), cast(0.00 as decimal(18, 2)), 2032, 0)
insert [dbo].[indicatordata] ([id], [value], [hopevalue], [indicatorid], [datakind]) values (195050, cast(9.00 as decimal(18, 2)), cast(0.00 as decimal(18, 2)), 2032, 0)
/****** object: table [dbo].[indicator] ******/
go
create table [dbo].[indicator](
[id] [int] null,
[name] [varchar](50) null
) on [primary]
go
insert [dbo].[indicator] ([id], [name]) values (2032, n'test tile')
/****** object: table [dbo].[field] ******/
go
create table [dbo].[field](
[id] [int] null,
[name] [varchar](255) null,
[rank] [int] null,
[parentid] [int] null
) on [primary]
go
insert [dbo].[field] ([id], [name], [rank], [parentid]) values (120, n'2006', 18, 57)
insert [dbo].[field] ([id], [name], [rank], [parentid]) values (63, n'2015', 17, 57)
insert [dbo].[field] ([id], [name], [rank], [parentid]) values (303, n'2007', 9, 57)
insert [dbo].[field] ([id], [name], [rank], [parentid]) values (168, n'2018', 20, 57)
insert [dbo].[field] ([id], [name], [rank], [parentid]) values (1463, n'region 1', 1, 1459)
insert [dbo].[field] ([id], [name], [rank], [parentid]) values (1461, n'region 2', 3, 1459)
insert [dbo].[field] ([id], [name], [rank], [parentid]) values (57, n'year', 0, 0)
insert [dbo].[field] ([id], [name], [rank], [parentid]) values (1459, n'region', 0, 0)
insert [dbo].[field] ([id], [name], [rank], [parentid]) values (315, n'2002', 1, 57)
insert [dbo].[field] ([id], [name], [rank], [parentid]) values (123, n'2017', 19, 57)
/****** object: table [dbo].[datafields] ******/
set ansi_nulls on
go
create table [dbo].[datafields](
[dataid] [int] null,
[fieldid] [int] null
) on [primary]
go
insert [dbo].[datafields] ([dataid], [fieldid]) values (195045, 120)
insert [dbo].[datafields] ([dataid], [fieldid]) values (195045, 1463)
insert [dbo].[datafields] ([dataid], [fieldid]) values (195046, 63)
insert [dbo].[datafields] ([dataid], [fieldid]) values (195046, 1461)
insert [dbo].[datafields] ([dataid], [fieldid]) values (195047, 303)
insert [dbo].[datafields] ([dataid], [fieldid]) values (195047, 1463)
insert [dbo].[datafields] ([dataid], [fieldid]) values (195048, 168)
insert [dbo].[datafields] ([dataid], [fieldid]) values (195048, 1463)
insert [dbo].[datafields] ([dataid], [fieldid]) values (195049, 315)
insert [dbo].[datafields] ([dataid], [fieldid]) values (195049, 1463)
insert [dbo].[datafields] ([dataid], [fieldid]) values (195050, 123)
insert [dbo].[datafields] ([dataid], [fieldid]) values (195050, 1463)
go
Here is the query that i have tried to archive it. but its fail
select fieldid, groupedData.value as value, groupedData.hopeValue as hopeValue,
groupedData.datakind, groupedData.id as id, FieldSelector.name,
groupedData.rank
from DataFields FieldsToInsert
join (
select d.id id,min(d.datakind) as datakind, sum(rank) rank, min(value) value, min(hopeValue) hopeValue
from indicatorData d join datafields df on d.id = df.dataid
join field f on df.fieldId=f.id where indicatorId=2032
group by d.id) groupedData on FieldsToInsert.dataid = groupedData.id
join Field FieldSelector on FieldSelector.id=FieldsToInsert.fieldId
order by groupedData.rank asc, groupedData.id
Output we got it is
View Image
Expecting the output is
View image

So the first thought that strikes me is "Is the data correct?" I ask because looking at the years and rank values it suggests that 2006 should maybe be 2016 or the rank for 2006 should be 8 instead of 18.
That aside, and assuming the data is correct, you need to associate the region with the appropriate year. You can do this by getting the parent name for the field record and including the fieldid and then self joining back to get the associated year for that fieldid region record as follows...
WITH GroupedData AS
( SELECT d.id id,
min(d.datakind) AS datakind,
sum(f.rank) rank,
min(d.value) value,
min(d.hopevalue) hopeValue
FROM indicatordata d
JOIN datafields df ON d.id = df.dataid
JOIN field f ON df.fieldid = f.id
WHERE d.indicatorid = 2032
GROUP BY d.id
)
, df_parentType AS
( SELECT df.dataid,
df.fieldid,
f.name,
f.rank,
f.parentid,
fy.name AS parentname
FROM dbo.datafields AS df
JOIN field AS f ON df.fieldid = f.Id
JOIN field AS fy ON f.parentId = fy.Id
)
, df_yearregionmatched AS
( SELECT df.dataid,
df.fieldid,
df.name,
df.rank,
dfp.name AS yearname,
CASE df.parentname WHEN 'year' THEN 0 ELSE 1 END AS datafieldtype
FROM df_parentType AS df
JOIN df_parentType AS dfp ON dfp.dataid = df.dataid AND dfp.parentname = 'year'
)
SELECT GroupedData.id AS fieldid,
GroupedData.value AS value,
GroupedData.hopeValue AS hopeValue,
GroupedData.datakind,
GroupedData.id AS id,
FieldSelector.name,
GroupedData.rank,
FieldSelector.yearname,
FieldSelector.datafieldtype
FROM GroupedData
JOIN df_yearregionmatched FieldSelector ON GroupedData.id = FieldSelector.dataid
ORDER BY FieldSelector.yearname,
FieldSelector.datafieldtype;
I have used CTEs to keep the code uncomplicated. The ordering then is simply by yearname and a generated value to put year before region.

#nickFry
Check another with complex example with one more field...
create table indicator(id int not null,name varchar(255) not null)
insert indicator (id, name) values (1, 'basic employee details')
create table fields(
id int not null,
rank int,
name varchar(255) not null,
parentid int not null)
insert fields (id, rank, name, parentid) values (1, 0, 'year', 0)
insert fields (id, rank, name, parentid) values (2, 1, '2010', 1)
insert fields (id, rank, name, parentid) values (5, 2, '2011', 1)
insert fields (id, rank, name, parentid) values (6, 3, '2012', 1)
insert fields (id, rank, name, parentid) values (7, 4, '2013', 1)
insert fields (id, rank, name, parentid) values (8, 5, '2014', 1)
insert fields (id, rank, name, parentid) values (9, 0, 'nationality', 0)
insert fields (id, rank, name, parentid) values (10, 1, 'libya', 9)
insert fields (id, rank, name, parentid) values (11, 2, 'ukrine', 9)
insert fields (id, rank, name, parentid) values (12, 0, 'gender', 0)
insert fields (id, rank, name, parentid) values (13, 1, 'male', 12)
insert fields (id, rank, name, parentid) values (14, 2, 'fe male', 12)
insert fields (id, rank, name, parentid) values (15, 0, 'maritalstatus', 0)
insert fields (id, rank, name, parentid) values (16, 1, 'married', 15)
insert fields (id, rank, name, parentid) values (17, 2, 'unmarried', 15)
insert fields (id, rank, name, parentid) values (18, 3, 'divorced', 15)
create table indicatorfields(
indicatorid int not null,
fieldid int not null,rank int)
insert indicatorfields (indicatorid, fieldid,rank) values (1, 2,1)
insert indicatorfields (indicatorid, fieldid,rank) values (1, 5,1)
insert indicatorfields (indicatorid, fieldid,rank) values (1, 6,1)
insert indicatorfields (indicatorid, fieldid,rank) values (1, 7,1)
insert indicatorfields (indicatorid, fieldid,rank) values (1, 8,1)
insert indicatorfields (indicatorid, fieldid,rank) values (1, 10,3)
insert indicatorfields (indicatorid, fieldid,rank) values (1, 11,3)
insert indicatorfields (indicatorid, fieldid,rank) values (1, 16,2)
insert indicatorfields (indicatorid, fieldid,rank) values (1, 17,2)
insert indicatorfields (indicatorid, fieldid,rank) values (1, 18,2)
create table indicatordata(
dataid int not null,
value varchar(255) not null,
indicatorid int not null)
insert indicatordata (dataid, value, indicatorid) values (1, '1015', 1)
insert indicatordata (dataid, value, indicatorid) values (2, '15', 1)
insert indicatordata (dataid, value, indicatorid) values (3, '12', 1)
insert indicatordata (dataid, value, indicatorid) values (4, '187', 1)
insert indicatordata (dataid, value, indicatorid) values (5, '50', 1)
insert indicatordata (dataid, value, indicatorid) values (6, '65', 1)
create table datafields(
dataid int not null,
fieldid int not null)
insert datafields (dataid, fieldid) values (1, 8)
insert datafields (dataid, fieldid) values (1, 10)
insert datafields (dataid, fieldid) values (1, 16)
insert datafields (dataid, fieldid) values (2, 6)
insert datafields (dataid, fieldid) values (2, 11)
insert datafields (dataid, fieldid) values (2, 17)
insert datafields (dataid, fieldid) values (3, 7)
insert datafields (dataid, fieldid) values (3, 11)
insert datafields (dataid, fieldid) values (3, 17)
insert datafields (dataid, fieldid) values (4, 2)
insert datafields (dataid, fieldid) values (4, 11)
insert datafields (dataid, fieldid) values (4, 16)
insert datafields (dataid, fieldid) values (5, 8)
insert datafields (dataid, fieldid) values (5, 10)
insert datafields (dataid, fieldid) values (5, 18)
insert datafields (dataid, fieldid) values (6, 2)
insert datafields (dataid, fieldid) values (6, 10)
insert datafields (dataid, fieldid) values (6, 16)
We need to get rows based on the indicatorfield & fields table order by rank
See the attached image for
Actual Output
This is wat i am looking for
Expected OutPut

#Prasad, solution without reference to any hardcoded data value...
WITH GroupedData AS
( SELECT d.dataid id,
--min(d.datakind) AS datakind,
sum(f.rank) rank,
min(d.value) value --,
--min(d.hopevalue) hopeValue
FROM indicatordata d
JOIN datafields df ON d.dataid = df.dataid
JOIN fields f ON df.fieldid = f.id
WHERE d.indicatorid = 1
GROUP BY d.dataid
)
, df_parentType AS
( SELECT df.dataid,
df.fieldid,
f.name,
f.rank,
f.parentid,
fy.name AS parentname
FROM dbo.datafields AS df
JOIN fields AS f ON df.fieldid = f.Id
JOIN fields AS fy ON f.parentId = fy.Id
)
, df_parentmatched AS
( SELECT df.dataid,
df.fieldid,
df.name,
df.rank,
dfp.name AS parentname
FROM df_parentType AS df
JOIN df_parentType AS dfp ON dfp.dataid = df.dataid AND dfp.parentname = (SELECT DISTINCT parentname FROM df_parentType WHERE parentid = (SELECT min(dataid) FROM df_parentType))
)
SELECT GroupedData.id AS datadid,
GroupedData.value AS value,
--GroupedData.hopeValue AS hopeValue,
--GroupedData.datakind,
FieldSelector.fieldid,
FieldSelector.name,
GroupedData.rank,
FieldSelector.parentname
FROM GroupedData
JOIN df_parentmatched FieldSelector ON GroupedData.id = FieldSelector.dataid
ORDER BY FieldSelector.parentname,
GroupedData.rank,
FieldSelector.fieldid;

#nickFy
The one you provide is fine... With this complex example... expected order is not coming in the correct way
Here is another complex example without the year
insert indicator (id, name) values (2, 'testing employee details 2')
insert indicatorfields (indicatorid, fieldid,rank) values (2, 10,3)
insert indicatorfields (indicatorid, fieldid,rank) values (2, 11,3)
insert indicatorfields (indicatorid, fieldid,rank) values (2, 13,1)
insert indicatorfields (indicatorid, fieldid,rank) values (2, 14,1)
insert indicatorfields (indicatorid, fieldid,rank) values (2, 16,2)
insert indicatorfields (indicatorid, fieldid,rank) values (2, 17,2)
insert indicatorfields (indicatorid, fieldid,rank) values (2, 18,2)
insert indicatordata (dataid, value, indicatorid) values (7, '1015', 2)
insert indicatordata (dataid, value, indicatorid) values (8, '15', 2)
insert indicatordata (dataid, value, indicatorid) values (9, '12', 2)
insert indicatordata (dataid, value, indicatorid) values (10, '187', 2)
insert indicatordata (dataid, value, indicatorid) values (11, '50', 2)
insert datafields (dataid, fieldid) values (7, 11)
insert datafields (dataid, fieldid) values (7, 13)
insert datafields (dataid, fieldid) values (7, 16)
insert datafields (dataid, fieldid) values (8, 10)
insert datafields (dataid, fieldid) values (8, 13)
insert datafields (dataid, fieldid) values (8, 17)
insert datafields (dataid, fieldid) values (9, 10)
insert datafields (dataid, fieldid) values (9, 14)
insert datafields (dataid, fieldid) values (9, 18)
insert datafields (dataid, fieldid) values (10, 11)
insert datafields (dataid, fieldid) values (10, 13)
insert datafields (dataid, fieldid) values (10, 16)
insert datafields (dataid, fieldid) values (11, 10)
insert datafields (dataid, fieldid) values (11, 14)
insert datafields (dataid, fieldid) values (11, 16)
**Acutal Output we got is **
select a.dataid,value,df.fieldid,name from indicatordata a INNER JOIN datafields df on a.dataid=df.dataid
INNER JOIN fields f ON df.fieldid=f.id INNER JOIN indicatorfields indFields ON indFields.fieldid=df.fieldid
where a.indicatorid=2 and indFields.indicatorid=2 order by a.dataid
We need to order the fields based on the indicatorfield & field table
Expected Output should be like this

Related

SQL exclude negative quantity

Please help, there is a request in which sometimes there is a negative value in the KOLVO_RZ column. Is it possible to check this in the request and if the value is negative, then return 0?
SELECT KOLVO_T - KOLVO_KKM - KOLVO_RZ AS KOLVO_T
FROM PRICE
This should do the trick:
SELECT KOLVO_T - KOLVO_KKM - (CASE WHEN KOLVO_RZ < 0 THEN 0 ELSE KOLVO_RZ END) AS KOLVO_T
FROM PRICE
Here's a sample that works for me with InterBase 2020. Which InterBase version are you using?
create table hikers (
hiker_id integer not null,
name varchar (256) not null,
constraint pk_hid primary key (hiker_id));
create table hiker_data (
hiker_id integer not null,
steps_gained integer,
constraint fk_hid foreign key (hiker_id) references hikers (hiker_id));
commit;
insert into hikers values (1, 'Aarti');
insert into hikers values (2, 'Bala');
insert into hikers values (3, 'Chaaya');
insert into hikers values (4, 'Deepak');
insert into hiker_data values (1, 20);
insert into hiker_data values (1, 10);
insert into hiker_data values (1, -5);
insert into hiker_data values (1, 15);
insert into hiker_data values (1, -20);
insert into hiker_data values (2, 12);
insert into hiker_data values (2, 6);
insert into hiker_data values (2, 20);
insert into hiker_data values (2, -5);
insert into hiker_data values (2, 10);
insert into hiker_data values (2, -18);
insert into hiker_data values (3, 20);
insert into hiker_data values (3, 10);
insert into hiker_data values (3, -5);
insert into hiker_data values (3, 15);
insert into hiker_data values (3, -20);
insert into hiker_data values (3, 12);
insert into hiker_data values (4, 6);
insert into hiker_data values (4, 20);
insert into hiker_data values (4, -5);
insert into hiker_data values (4, 10);
insert into hiker_data values (4, -18);
commit;
/* test foreign key enforcement */
insert into hiker_data values (-100, -18);
and then, I execute the following queries successfully.
select hiker_id,
SUM(0 + (CASE WHEN steps_gained < 0 THEN 0
ELSE steps_gained
END)) AS total_steps_climbed,
SUM(0 + (CASE WHEN steps_gained > 0 THEN 0
ELSE steps_gained
END)) AS total_steps_descended,
SUM(steps_gained) AS final_tally
from hiker_data
group by hiker_id;
select hiker_id,
(CASE WHEN steps_gained < 0 THEN 0
ELSE steps_gained
END) AS steps_climbed
from hiker_data
where hiker_id=2;

How to convert rows to columns with commasepart using huge data in SQL Server

I have a question about SQL Server: how to convert rows to columns with comma-separated data using bulk data?
CREATE TABLE [dbo].[Emp]
(
[eid] [int] NULL,
[name] [varchar](50) NULL,
[sal] [money] NULL,
[doj] [date] NULL,
[deptno] [int] NULL
)
INSERT [dbo].[Emp] ([eid], [name], [sal], [doj], [deptno])
VALUES (1, N'a', 100.0000, CAST(N'2010-10-01' AS Date), 10)
INSERT [dbo].[Emp] ([eid], [name], [sal], [doj], [deptno])
VALUES (2, N'bb', 200.0000, CAST(N'2010-02-03' AS Date), 20)
INSERT [dbo].[Emp] ([eid], [name], [sal], [doj], [deptno])
VALUES (3, N'c', 300.0000, CAST(N'2017-02-03' AS Date), 30)
INSERT [dbo].[Emp] ([eid], [name], [sal], [doj], [deptno])
VALUES (4, N'd', 301.0000, CAST(N'2010-03-04' AS Date), 10)
INSERT [dbo].[Emp] ([eid], [name], [sal], [doj], [deptno])
VALUES (5, N'teee', 250.0000, CAST(N'2010-06-04' AS Date), 20)
INSERT [dbo].[Emp] ([eid], [name], [sal], [doj], [deptno])
VALUES (7, N'tte', 800.0000, CAST(N'2010-08-09' AS Date), 70)
I want output like below :
userlist
1,2,3,4,5,7
I tried this code:
select distinct
stuff((select ',' + cast(u.eid as varchar)
from emp u
where 1 = 1
for xml path('')), 1, 1, '') as userlist
from emp
Suppose if data has 100 thousand entries, then this query is not returning 100 thousand comma-separated
Could you please tell me how to write query to achieve this task in SQL Server?
I think the query is working as expected. But the result is too large to be displayed in management studio.

Select from 3 tables (one to many, many to one relationship)

I have 3 tables:
Recipes (1 --- * ) Ingridients ( *---1) Products. I need to obtain Recipes that contains products in a given list or products that are not in list but have a specific flag set. I have a flag in product table (bool). So where clause looks like:
WHERE Product.Caption IN ('A', 'B', 'C') OR (Product.Caption NOT IN ('A', 'B', 'C') AND Product.Flag=TRUE)
Important is: I do not need recipes that contain products in list and also contain other products (not in list and flag is false).
Bellow is an example database dump for MSSQL:
USE [master]
IF EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE name = N'movedb') ALTER DATABASE [movedb] SET SINGLE_USER With ROLLBACK IMMEDIATE
IF EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE name = N'movedb') DROP DATABASE [movedb]
IF NOT EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE name = N'movedb') CREATE DATABASE [movedb]
USE [movedb]
--
-- Table structure for table 'Ingridients'
--
IF object_id(N'Ingridients', 'U') IS NOT NULL DROP TABLE [Ingridients]
CREATE TABLE [Ingridients] (
[Id] INT NOT NULL IDENTITY,
[Quantity] INT DEFAULT 0,
[IdProduct] INT DEFAULT 0,
[IdRecipe] INT DEFAULT 0,
PRIMARY KEY ([Id])
)
SET IDENTITY_INSERT [Ingridients] ON
GO
--
-- Dumping data for table 'Ingridients'
--
INSERT INTO [Ingridients] ([Id], [Quantity], [IdProduct], [IdRecipe]) VALUES (1, 0, 1, 2)
INSERT INTO [Ingridients] ([Id], [Quantity], [IdProduct], [IdRecipe]) VALUES (2, 0, 3, 2)
INSERT INTO [Ingridients] ([Id], [Quantity], [IdProduct], [IdRecipe]) VALUES (3, 0, 4, 2)
INSERT INTO [Ingridients] ([Id], [Quantity], [IdProduct], [IdRecipe]) VALUES (4, 0, 6, 2)
INSERT INTO [Ingridients] ([Id], [Quantity], [IdProduct], [IdRecipe]) VALUES (5, 0, 2, 3)
INSERT INTO [Ingridients] ([Id], [Quantity], [IdProduct], [IdRecipe]) VALUES (6, 0, 4, 3)
INSERT INTO [Ingridients] ([Id], [Quantity], [IdProduct], [IdRecipe]) VALUES (7, 0, 8, 3)
INSERT INTO [Ingridients] ([Id], [Quantity], [IdProduct], [IdRecipe]) VALUES (8, 0, 1, 4)
INSERT INTO [Ingridients] ([Id], [Quantity], [IdProduct], [IdRecipe]) VALUES (9, 0, 6, 4)
INSERT INTO [Ingridients] ([Id], [Quantity], [IdProduct], [IdRecipe]) VALUES (10, 0, 5, 4)
-- 10 records
SET IDENTITY_INSERT [Ingridients] OFF
GO
--
-- Table structure for table 'Products'
--
IF object_id(N'Products', 'U') IS NOT NULL DROP TABLE [Products]
CREATE TABLE [Products] (
[Id] INT NOT NULL IDENTITY,
[Caption] NVARCHAR(255),
[EasyToFind] BIT DEFAULT 0,
PRIMARY KEY ([Id])
)
SET IDENTITY_INSERT [Products] ON
GO
--
-- Dumping data for table 'Products'
--
INSERT INTO [Products] ([Id], [Caption], [EasyToFind]) VALUES (1, N'ProductA', 0)
INSERT INTO [Products] ([Id], [Caption], [EasyToFind]) VALUES (2, N'ProductB', 0)
INSERT INTO [Products] ([Id], [Caption], [EasyToFind]) VALUES (3, N'ProductC', 0)
INSERT INTO [Products] ([Id], [Caption], [EasyToFind]) VALUES (4, N'ProductD', -1)
INSERT INTO [Products] ([Id], [Caption], [EasyToFind]) VALUES (5, N'ProductE', 0)
INSERT INTO [Products] ([Id], [Caption], [EasyToFind]) VALUES (6, N'ProductF', -1)
INSERT INTO [Products] ([Id], [Caption], [EasyToFind]) VALUES (7, N'ProductG', 0)
INSERT INTO [Products] ([Id], [Caption], [EasyToFind]) VALUES (8, N'ProductH', 0)
INSERT INTO [Products] ([Id], [Caption], [EasyToFind]) VALUES (9, N'ProductI', 0)
INSERT INTO [Products] ([Id], [Caption], [EasyToFind]) VALUES (10, N'ProductJ', 0)
-- 10 records
SET IDENTITY_INSERT [Products] OFF
GO
--
-- Table structure for table 'Recipes'
--
IF object_id(N'Recipes', 'U') IS NOT NULL DROP TABLE [Recipes]
CREATE TABLE [Recipes] (
[Id] INT NOT NULL IDENTITY,
[Caption] NVARCHAR(255),
PRIMARY KEY ([Id])
)
SET IDENTITY_INSERT [Recipes] ON
GO
--
-- Dumping data for table 'Recipes'
--
INSERT INTO [Recipes] ([Id], [Caption]) VALUES (2, N'RecipeA')
INSERT INTO [Recipes] ([Id], [Caption]) VALUES (3, N'RecipeB')
INSERT INTO [Recipes] ([Id], [Caption]) VALUES (4, N'RecipeC')
-- 3 records
SET IDENTITY_INSERT [Recipes] OFF
GO
Example:
If I search for ProductA and ProductE it should give me only RecipeC
Right now I have something like this for MySQL ( it is not final. I can only operate with Ids, I neet somehow to change it to work only with product captions and adapt for MSSQL)
SELECT
*
FROM
recipes AS r
INNER JOIN
ingridients i ON i.IdRecipe = r.Id
WHERE
i.IdProduct IN (1 , 5, 6)
GROUP BY r.Id
HAVING COUNT(*) = (SELECT
COUNT(*)
FROM
ingridients AS ing
WHERE
ing.IdRecipe = r.Id);
The following sql fetches the recipes that contains no products other than those in the list or having P.EasyToFind=-1.
select *
From Recipes
Where Id not in
(
select IdRecipe
from Ingridients I
inner join Products P ON I.IdProduct = P.Id
where P.Caption NOT IN ('ProductA','ProductE')
and P.EasyToFind=0
)
It works by having an inner query that identifies the unwanted ingredients and fetching the recipes that does not match any of them.

Hierarchical data with CTE

I am sorry if the answer has been posted already, but I was not able to find the answer even after searching.
I have the following table
CREATE TABLE [dbo].[emp](
[id] [int] NOT NULL,
[name] [varchar](20) NULL,
[mgrid] [int] NULL
) ON [PRIMARY]
With data
INSERT [dbo].[emp] ([id], [name], [mgrid]) VALUES (1, N'a', 0)
INSERT [dbo].[emp] ([id], [name], [mgrid]) VALUES (2, N'a1', 1)
INSERT [dbo].[emp] ([id], [name], [mgrid]) VALUES (3, N'a11', 2)
INSERT [dbo].[emp] ([id], [name], [mgrid]) VALUES (4, N'a12', 2)
INSERT [dbo].[emp] ([id], [name], [mgrid]) VALUES (5, N'a13', 2)
INSERT [dbo].[emp] ([id], [name], [mgrid]) VALUES (6, N'a2', 1)
INSERT [dbo].[emp] ([id], [name], [mgrid]) VALUES (7, N'a3', 1)
INSERT [dbo].[emp] ([id], [name], [mgrid]) VALUES (8, N'a31', 7)
INSERT [dbo].[emp] ([id], [name], [mgrid]) VALUES (9, N'a32', 7)
INSERT [dbo].[emp] ([id], [name], [mgrid]) VALUES (10, N'b', 0)
INSERT [dbo].[emp] ([id], [name], [mgrid]) VALUES (11, N'b1', 10)
INSERT [dbo].[emp] ([id], [name], [mgrid]) VALUES (12, N'b2', 10)
And I would like the following output
a
a1
a11
a12
a13
a2
a3
a31
a32
b
b1
b2
Is this possible in SQL Server?
SQLFiddle demo
You can use ' ' instead of '+' in the last line to pad a string.
with t as
(
select id,name,mgrid,1 as level,cast(name as varchar(max)) as path
from emp where mgrid=0
union all
select emp.id,emp.name,emp.mgrid, t.level+1 as level,
t.path+cast(emp.name as varchar(max)) as path
from emp
join t on emp.mgrid=t.id
)
select replicate('+', level)+name from t order by path
with CTE as
(
select *
, level = 1
from emp
where mgrid = 0
union all
select emp.id,
name = cast(space((level) * 3) + emp.name as varchar(20)),
emp.mgrid,
level = level + 1
from emp
inner join CTE on CTE.id = emp.mgrid
)
select name
from CTE
order by ltrim(name)
Query:
DECLARE #temp TABLE
(
[id] [int] NOT NULL,
[name] [varchar](20) NULL,
[mgrid] [int] NULL
)
INSERT INTO #temp ([id], [name], [mgrid])
VALUES
(1, N'a', 0), (2, N'a1', 1),
(3, N'a11', 2), (4, N'a12', 2),
(5, N'a13', 2), (6, N'a2', 1),
(7, N'a3', 1), (8, N'a31', 7),
(9, N'a32', 7), (10, N'b', 0),
(11, N'b1', 10), (12, N'b2', 10)
DECLARE #out VARCHAR(MAX) = ''
;WITH cte AS
(
SELECT *, Lvl = 0, nn = CAST(name AS VARCHAR(MAX))
FROM #temp
WHERE mgrid = 0
UNION ALL
SELECT t.*, c.Lvl + 1, nn = CAST(REPLICATE(' ', c.Lvl + 1) + t.name AS VARCHAR(MAX))
FROM #temp t
JOIN cte c ON c.id = t.mgrid
)
SELECT #out = (
SELECT nn + CHAR(13)
FROM cte
ORDER BY LTRIM(nn)
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)')
PRINT #out
Output:
a
a1
a11
a12
a13
a2
a3
a31
a32
b
b1
b2
You can use CTE (Comnon table expression to achieve this)
check this sample code:
declare #emp table(
[id] [int] NOT NULL,
[name] [varchar](20) NULL,
[mgrid] [int] NULL
)
INSERT #emp ([id], [name], [mgrid]) VALUES (1, N'a', 0)
INSERT #emp ([id], [name], [mgrid]) VALUES (2, N'a1', 1)
INSERT #emp ([id], [name], [mgrid]) VALUES (3, N'a11', 2)
INSERT #emp ([id], [name], [mgrid]) VALUES (4, N'a12', 2)
INSERT #emp ([id], [name], [mgrid]) VALUES (5, N'a13', 2)
INSERT #emp ([id], [name], [mgrid]) VALUES (6, N'a2', 1)
INSERT #emp ([id], [name], [mgrid]) VALUES (7, N'a3', 1)
INSERT #emp ([id], [name], [mgrid]) VALUES (8, N'a31', 7)
INSERT #emp ([id], [name], [mgrid]) VALUES (9, N'a32', 7)
INSERT #emp ([id], [name], [mgrid]) VALUES (10, N'b', 0)
INSERT #emp ([id], [name], [mgrid]) VALUES (11, N'b1', 10)
INSERT #emp ([id], [name], [mgrid]) VALUES (12, N'b2', 10);
with cte (id,name, MGRID) as
(
select id, name,MGRID
from #emp
union all
select c.id, c.name, c.MGRID
from #emp c
inner join cte p
on c.mgrid = P.id
)
SELECT Distinct * FROM CTE
WITH RCTE AS
(
SELECT id, name, mgrid, CAST('' AS NVARCHAR(MAX)) AS blanks , CAST(name AS NVARCHAR(MAX)) AS ordr
FROM dbo.emp WHERE mgrid = 0
UNION ALL
SELECT e.id, e.name, e.mgrid, blanks + ' ' AS blanks, ordr + e.name AS ordr
FROM dbo.emp e
INNER JOIN RCTE r ON e.mgrid = r.id
)
SELECT blanks + name FROM RCTE
ORDER BY ordr

SQL Query count

HI there I have this table,
Recipe = (idR, recipeTitle, prepText, cuisineType, mealType)
Ingredient = (idI, ingrDesc)
RecipIngr = (idR*, idI*)
and I'm trying to query a list for ingrDesc with a count of how many recipies that ingrDesc is in. I want to list only those ingrDesc that occur more than 10 times.
Here's what I have:
SELECT a.idI, a.recipeTitle
FROM Recipe a
INNER JOIN recpingr b
ON a.idr = b.idr
WHERE a.preptext = '>10'
Any help as I don't know how to carry on with this query
Use GROUP BY with HAVING:
SELECT i.idI, i.ingrDesc, COUNT(*)
FROM Ingredient i
INNER JOIN RecipIngr ri ON i.idI = ri.idI
GROUP BY i.idI, i.ingrDesc
HAVING COUNT(*) > 10
You need to use a group by clause and having. I have created a quick sample here but my sample data does not go up to 10 so I used any ingredient that was used more than once (> 1).
Here is the sample data:
create table dbo.recipe (
idR int not null,
recipeTitle varchar(100) not null,
prepText varchar(4000) null,
cuisineType varchar(100) null,
mealType varchar(100) null
)
go
insert into dbo.recipe values (1, 'Eggs and Bacon', 'Prep Text 1', 'American', 'Breakfast')
insert into dbo.recipe values (2, 'Turkey Sandwich', 'Prep Text 2', 'American', 'Lunch')
insert into dbo.recipe values (3, 'Roast Beef Sandwich', 'Prep Text 3', 'American', 'Lunch')
go
create table dbo.ingredient (
idI int not null,
ingrDesc varchar(200) not null
)
go
insert into dbo.ingredient values (1, 'Large Egg')
insert into dbo.ingredient values (2, 'Bacon');
insert into dbo.ingredient values (3, 'Butter');
insert into dbo.ingredient values (4, 'Sliced Turkey');
insert into dbo.ingredient values (5, 'Lettuce');
insert into dbo.ingredient values (6, 'Tomato');
insert into dbo.ingredient values (7, 'Onion');
insert into dbo.ingredient values (8, 'Bread');
insert into dbo.ingredient values (9, 'Mustard');
insert into dbo.ingredient values (10, 'Horseradish');
insert into dbo.ingredient values (11, 'Sliced Roast Beef');
go
create table dbo.recipingr(
idR int not null,
idI int not null
)
go
insert into dbo.recipingr values (1, 1);
insert into dbo.recipingr values (1, 2);
insert into dbo.recipingr values (2, 4);
insert into dbo.recipingr values (2, 5);
insert into dbo.recipingr values (2, 6);
insert into dbo.recipingr values (2, 7);
insert into dbo.recipingr values (2, 8);
insert into dbo.recipingr values (2, 9);
insert into dbo.recipingr values (3, 11);
insert into dbo.recipingr values (3, 10);
insert into dbo.recipingr values (3, 8);
insert into dbo.recipingr values (3, 6);
insert into dbo.recipingr values (3, 5);
go
Here is the query:
select
i.ingrDesc,
count(*) ingrCount
from
dbo.recipe r
inner join dbo.recipingr ri on ri.idR = r.idR
inner join dbo.ingredient i on i.idI = ri.idI
group by
i.ingrDesc
having
count(*) > 1