I have a query like
SELECT
NON EMPTY
{
[Measures].[ItemPrice]
}
ON COLUMNS ,
NON EMPTY
[DimItemView].[ItemName].[ItemName]
*[DimDateView].[MonthOfYear].[MonthOfYear]
*[DimCustomerView].[CustomerName].[CustomerName]
on rows
FROM [Model]
WHERE (
[DimCompanyView].[CompanyKey].&[smr],
[DimDateView].[Year].&[2017],
[DimCurrencyParam].[CurrencyCode].&[TL],
[DimItemView].[ItemKey].&[03001],--Filtered for single item, just for simplicity. Will be all ItemKey s
[DimDateView].[MonthName].[All])
And the output is
Another query
WITH
SET [AllItems] AS [DimItemView].[ItemName].[ItemName]
SET [AllMonths] AS [DimDateView].[MonthOfYear].[MonthOfYear]
SET [A] AS
Generate
(
{[AllItems]*[AllMonths] } AS s
,
s.current*
TopCount
(
[DimCustomerView].[CustomerName].[CustomerName]
,1
,[Measures].[ItemPrice]
)
)
SELECT
[Measures].[ItemPrice]
ON 0
,
[A] ON 1
FROM [Model]
WHERE (
[DimCompanyView].[CompanyKey].&[smr],
[DimDateView].[Year].&[2017],
[DimCurrencyParam].[CurrencyCode].&[TL],
[DimItemView].[ItemKey].&[03001], --Filtered for single item, just for simplicity. Will be all ItemKey s
[DimDateView].[MonthName].[All])
And the output is
What I would like to achieve is Left join two result in a single query, but containing TopPrice for each row for the left result for corresponding group (ItemName and MonthOfYear). Desired output is like
Any help appreciated.
If you need to calculate it once, I'd use the following code:
WITH
MEMBER [Measures].[GroupPrice] AS
([DimCustomerView].[CustomerName].[All],[Measures].[ItemPrice])
SELECT
NON EMPTY
{
[Measures].[ItemPrice],
[Measures].[GroupPrice]
}
ON COLUMNS ,
NON EMPTY
[DimItemView].[ItemName].[ItemName]
*[DimDateView].[MonthOfYear].[MonthOfYear]
*[DimCustomerView].[CustomerName].[CustomerName]
on rows
FROM [Model]
WHERE (
[DimCompanyView].[CompanyKey].&[smr],
[DimDateView].[Year].&[2017],
[DimCurrencyParam].[CurrencyCode].&[TL],
[DimItemView].[ItemKey].&[03001],--Filtered for single item, just for simplicity. Will be all ItemKey s
[DimDateView].[MonthName].[All])
ItemPrice=ExwAmount/QtyKg
Actually below code is gives me expected results, but too slow. Any improvments?
WITH
MEMBER [Measures].[GroupPrice] AS
max([DimCustomerView].[CustomerName].Members,[Measures].[ItemPrice])
SELECT
NON EMPTY
{
[Measures].[ItemPrice],
[Measures].[GroupPrice]
}
ON COLUMNS ,
NON EMPTY
[DimItemView].[ItemName].[ItemName]
*[DimDateView].[MonthOfYear].[MonthOfYear]
*[DimCustomerView].[CustomerName].[CustomerName]
having [Measures].[Fiili_Fiyat]<>0
on rows
Related
I am trying to map a certain value of a column based on its count on another table. If the count of [Location] i.e a column of IMPORT.DATA_SCRAP table in each row. For now for location static value i.e Utah and Kathmandu is supplied for test purpose only is equal to 1, then only i need to get the result in the select statement i.e only single value expression must be returned but here n rows of table with value is returned.
For. eg. In the below query,total rows of IMPORT.DATA_SCRAP gets returned, i only need the single first row value in my case.
I came to know whether cursor or CTE will acheive my result but i am unable to figure it out.
Here,
select
case
when
((SELECT COUNT(stateName) FROM Location.tblState where stateName = 'Utah')=1)
then (select stateName, CountryName from Location.tblState where stateName= 'Utah')
end as nameof
from IMPORT.DATA_SCRAP
The relation between country, state, city is as below:
select
case
when
((SELECT COUNT(cityName) FROM Location.tblCity where cityName = 'Kathmandu')=1)
then (select ct.countryName from Location.tblCity c
inner join Location.tblState s
on c.stateID = s.StateID
inner join Location.tblCountry ct
on ct.countryId = s.CountryId
where c.cityName = 'Kathmandu'
)
end as nameof
from IMPORT.DATA_SCRAP
How can i return only a single value expresion despite of multiple nmax rows of IMPORT.DATA_SCRAP row in the result.
If i comment out the -- from IMPORT.DATA_SCRAP in the above query i would get the desired single result expression in my case, but unable how can i acheive it in other ways or suggest me the appropriate way to do these types of situation.
I have a details table with adeet column defined as jsonb[]
a sample value stored in adeet column is as below image
Sample data stored in DB :
I want to return the rows which satisfies id=26088 i.e row 1 and 3
I have tried array operations and json operations but it does'nt work as required. Any pointers
Obviously the type of the column adeet is not of type JSON/JSONB, but maybe VARCHAR and we should fix the format so as to convert into a JSONB type. I used replace() and r/ltrim() funcitons for this conversion, and preferred to derive an array in order to use jsonb_array_elements() function :
WITH t(jobid,adeet) AS
(
SELECT jobid, replace(replace(replace(adeet,'\',''),'"{','{'),'}"','}')
FROM tab
), t2 AS
(
SELECT jobid, ('['||rtrim(ltrim(adeet,'{'), '}')||']')::jsonb as adeet
FROM t
)
SELECT t.*
FROM t2 t
CROSS JOIN jsonb_array_elements(adeet) j
WHERE (j.value ->> 'id')::int = 26088
Demo
You want to combine JSONB's <# operator with the generic-array ANY construct.
select * from foobar where '{"id":26088}' <# ANY (adeet);
Working on a new TSQL Stored Procedure, I am wanting to get all rows where values in a specific column don't start with any of a specific set of 2 character substrings.
The general idea is:
SELECT * FROM table WHERE value NOT LIKE 's1%' AND value NOT LIKE 's2%' AND value NOT LIKE 's3%'.
The catch is that I am trying to make it dynamic so that the specific substrings can be pulled from another table in the database, which can have more values added to it.
While I have never used the IN operator before, I think something along these lines should do what I am looking for, however, I don't think it is possible to use wildcards with IN, so I might not be able to compare just the substrings.
SELECT * FROM table WHERE value NOT IN (SELECT substrings FROM subTable)
To get around that limitation, I am trying to do something like this:
SELECT * FROM table WHERE SUBSTRING(value, 1, 2) NOT IN (SELECT Prefix FROM subTable WHERE Prefix IS NOT NULL)
but I'm not sure this is right, or if it is the most efficient way to do this. My preference is to do this in a Stored Procedure, but if that isn't feasible or efficient I'm also open to building the query dynamically in C#.
Here's an option. Load values you want to filter to a table, left outer join and use PATINDEX().
DECLARE #FilterValues TABLE
(
[FilterValue] NVARCHAR(10)
);
--Table with values we want filter on.
INSERT INTO #FilterValues (
[FilterValue]
)
VALUES ( N's1' )
, ( N's2' )
, ( N's3' );
DECLARE #TestData TABLE
(
[TestValues] NVARCHAR(100)
);
--Load some test data
INSERT INTO #TestData (
[TestValues]
)
VALUES ( N's1 Test Data' )
, ( N's2 Test Data' )
, ( N's3 Test Data' )
, ( N'test data not filtered out' )
, ( N'test data not filtered out 1' );
SELECT a.*
FROM #TestData [a]
LEFT OUTER JOIN #FilterValues [b]
ON PATINDEX([b].[FilterValue] + '%', [a].[TestValues]) > 0
WHERE [b].[FilterValue] IS NULL;
I have a table like this
and i want a 1 query that reference_id = 1 and reference fields equal to title and alias and introtext
expected result:
title -> හැඳින්වීම
alias -> overview-si
introtext ->ධීවර
is it possible? and how can I get it ?
if you have refence id and that three reference_field values, use
SELECT reference_field,value FROM tbl_name WHERE reference_id=1 AND ( reference_field='title' OR reference_field='alias' OR reference_field='introtext)
I want to display the contents of the aggregated columns that is part of group by sql statement.
Example:
SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
WHERE Orders.ShipperID=Shippers.ShipperID
GROUP BY ShipperName
In the above example the output gives me count as one of the result, whereas i need the aggregated orders.orderID actual values even. So say if one result count shows me 2. I need to know what are those two values which have been grouped. This result should be as another column in the same table.
try this with GROUP_CONCAT
SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders , array_agg(Orders.OrderID) as which_are_those FROM Orders
WHERE Orders.ShipperID=Shippers.ShipperID
GROUP BY ShipperName
array_agg returns an array, but you can CAST that to text and edit as needed (see clarifications, below).
Prior to version 8.4, you have to define it yourself prior to use:
CREATE AGGREGATE array_agg (anyelement)
(
sfunc = array_append,
stype = anyarray,
initcond = '{}'
);
or simply this:
SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders , string_agg(Orders.OrderID, ',') as which_are_those FROM Orders
WHERE Orders.ShipperID=Shippers.ShipperID
GROUP BY ShipperName