info <- paste("select count (*) as total
from ANNUAL_CROP a
where a.CROP_TYPE='Rye' and a.GEO = 'Canada'
and a.YEAR = '%1968' ")
query <- sqlQuery(conn,info,believeNRows = FALSE)
query
can i ask how can write sqlQuery in R under the condition with sub value, the original dataset has the year variable as a character and i required to query the total of the rows where CROP_TYPE='Rye' and a.GEO = 'Canada'
and a.YEAR = 1968
and this the sample of Annual_crops the original dataset
Related
There is a field in the sql query that I can't do. First of all, a new column must be added to the table below. The value of this column needs to be percent complete, so it's a percentage value. So for example, there are 7 values from Cupboard=1 shelves. Where IsCounted is here, 3 of them are counted. In other words, those with Cupboard = 1 should write the percentage value of 3/7 as the value in the new column to be created. If the IsCounted of the others is 0, it will write zero percent. How can I do this?
My Sql Code:
SELECT a.RegionName,
a.Cupboard,
a.Shelf,
(CASE WHEN ToplamSayım > 0 THEN 1 ELSE 0 END) AS IsCounted
FROM (SELECT p.RegionName,
r.Shelf,
r.Cupboard,
(SELECT COUNT(*)
FROM FAZIKI.dbo.PM_ProductCountingNew
WHERE RegionCupboardShelfTypeId = r.Id) AS ToplamSayım
FROM FAZIKI.dbo.DF_PMRegionType p
JOIN FAZIKI.dbo.DF_PMRegionCupboardShelfType r ON p.Id = r.RegionTypeId
WHERE p.WarehouseId = 45) a
ORDER BY a.RegionName;
The result is as in the picture below:
It looks like a windowed AVG should do the trick, although it's not entirely clear what the partitioning column should be.
The SELECT COUNT can be simplified to an EXISTS
SELECT a.RegionName,
a.Cupboard,
a.Shelf,
a.IsCounted,
AVG(a.IsCounted * 1.0) OVER (PARTITION BY a.RegionName, a.Cupboard) Percentage
FROM (
SELECT p.RegionName,
r.Shelf,
r.Cupboard,
CASE WHEN EXISTS (SELECT 1
FROM FAZIKI.dbo.PM_ProductCountingNew pcn
WHERE pcn.RegionCupboardShelfTypeId = r.Id
) THEN 1 ELSE 0 END AS IsCounted
FROM FAZIKI.dbo.DF_PMRegionType p
JOIN FAZIKI.dbo.DF_PMRegionCupboardShelfType r ON p.Id = r.RegionTypeId
WHERE p.WarehouseId = 45
) a
ORDER BY a.RegionName;
Let's say there are users of a product, and the table has:
a column of the month the user started to use the product
a column for the user id
a column for the status of the user in that month
I would like to get a field that identify the change of status of any user from 1 month to the next month and count how many "active_to_idle" are there, and how many "idle_to_active" are there.
In this case, I would like the SQL query to return active_to_idle = 1 and idle_to_active = 1.
I tried using subquery but failed to give the results I wanted. I tried "CASE WHEN" like this:
select month, userid, segment,
case
when
((month = 1 and segment = 'active') and (month = 2 and segment = 'idle')) OR
((month = 2 and segment = 'active') and (month = 3 and segment = 'idle')) OR
((month = 3 and segment = 'active') and (month = 4 and segment = 'idle'))
then 'active_to_idle'
when
((month = 1 and segment = 'idle') and (month = 2 and segment = 'active')) OR
((month = 2 and segment = 'idle') and (month = 3 and segment = 'active')) OR
((month = 3 and segment = 'idle') and (month = 4 and segment = 'active'))
then 'idle_to_active'
else 'no_change'
end as transition
from table1
I know where the mistake is, it will always produce 'no_change', because there is no row that is both month = 1 and month = 2. But I don't really know how to write the correct code to identify and count the 'transition'. I tried search for Windows Function, but not sure if we can use it here.
My SQL skill is not that advanced yet, could someone please share some help/advice how can I fix the code? Much appreciated, many thanks.
You can use lead() to get the next segment, filter out the rows where there is no change, and then aggregate:
select segment, next_segment, count(*)
from (select t1.*,
lead(segment) over (partition by userid order by month) as next_segment
from table1 t1
) t1
where next_segment <> segment
group by segment, next_segment;
I have some records in a table and i want to show all records from that table after that i want to show the sum of corresponding columns then what will be the sql query for this problem ??
Example
Please find the Input and Expected output in the attachment.
Thanks
To test: http://rextester.com/HUE40725
This would do the job for you:
SELECT Category = CASE WHEN GROUPING(Category) = 0 THEN Category ELSE 'Total' END,
A = SUM(A),
B=SUM(B),
C = SUM(C),
D=SUM(D),
Total = SUM(Total)
FROM dbo.NameTable
GROUP BY Category WITH ROLLUP
HAVING GROUPING(Category) = 0
OR GROUPING(Category) = 1
;
Use ROLLUP :
select coalesce(category, 'Total') as Category,
sum(a) as A,
. . .
from table
group by rollup(category);
My problem is that I have 4 differents SELECT with
SELECT COUNT (*) AS regular
WHERE experience = 1 AND bl = 1
SELECT COUNT (*) AS rptmm
WHERE experience = 1 AND bl = 0
SELECT COUNT (*) AS new
WHERE experience = 0 AND bl = 0
SELECT COUNT (*) AS rptss
WHERE experience = 0 AND bl = 1
I want that the results appear together whith the respective names like:
regular rptmm new rptss
10 5 2 6
Firstly, I'd suggest not to use Count()*. There are many answers on this site explaining why so I am not going to repeat it.
Instead, I'd suggest you to use a query like this:
SELECT (SELECT COUNT (tab.someColumnName)
FROM TableName tab
WHERE tab.experience = 1 AND tab.bl = 1) AS 'Regular',
(SELECT COUNT (tab.someColumnName)
FROM TableName tab
WHERE tab.experience = 1 AND tab.bl = 0) AS 'rptmm',
(SELECT COUNT (tab.someColumnName)
FROM TableName tab
WHERE tab.experience = 0 AND tab.bl = 0) AS 'New',
(SELECT COUNT (tab.someColumnName)
FROM TableName tab
WHERE tab.experience = 0 AND tab.bl = 1) AS 'rptss'
Hope this helps!!!
Just put UNION ALL between your four statements you will get four rows with each count on its own row. However, you will lose the column name. You could also use join to get one row with four columnes. Just put the keyword join between each sql statement.
SELECT COUNT (*) AS regular
WHERE experience = 1 AND bl = 1
JOIN
SELECT COUNT (*) AS rptmm
WHERE experience = 1 AND bl = 0
JOIN
SELECT COUNT (*) AS new
WHERE experience = 0 AND bl = 0
JOIN
SELECT COUNT (*) AS rptss
WHERE experience = 0 AND bl = 1
You could create a temp table to hold all of this data for you: Replace Name1, Name2, Name3,Name4 with whatever you want to call them. These will be the column headers.
CREATE TABLE #Temp(
NAME1 INT
,NAME2 INT
,NAME3 INT
,NAME4 INT
)
INSERT INTO #Temp
(NAME1)
SELECT COUNT(*) AS regular
WHERE experience = 1 AND bl = 1
INSERT INTO #Temp
(NAME2)
SELECT COUNT(*) AS regular
WHERE experience = 1 AND bl = 0
INSERT INTO #Temp
(NAME3)
SELECT COUNT(*) AS regular
WHERE experience = 0 AND bl = 0
INSERT INTO #Temp
(NAME4)
SELECT COUNT(*) AS regular
WHERE experience = 0 AND bl = 1*
SELECT * FROM #Temp
I'm not good at asking question, so i'll give an example of what i want to have.
if i = 1 and xi = 0 then
select a,b,c,d,e,f,g where z = 1
elseif i=0 and xi = 1 then
select a,c,f,h,l,n where w = var
elseif i=1 and xi=1 then
select a,b,c,d,e,f,g, where z = 1
union all
select a,c,f,h,l,n where w = var
end if
How can I join the 2 select statement if their columns are not equal and they both have a unique condition?
Based on the conditions you can create derived tables to fetch desired columns and then to get a union of the two tables add null values in column list of derived tables which have less number of columns:
Pseudo code:
select * from
(select a,b,c,d,e,f,g
where z = 1
and 1 = case when i = 1 and xi = 0 then 1
when i = 1 and xi = 1 then 1
else 0
end) as T1
union all
(select a,c,f,h,l,n ,null -- add null value to equate number of columns
where w = var
and 1 = case when i=0 and xi = 1 then 1
when i=1 and xi = 1 then 1
else 0
end) as T2
Hope this helps!!!
If it is not a requirement not to use dynamic sql I will opt for that one.
Another idea will be to use user defined function returnin tables.
So you encapsulate there the logic...