WITH keyword is required - sql

While running the following SQL query -
INSERT INTO Countries ('sno', 'Name' ) VALUES
(1, 'Afghanistan'),
(2, 'Albania'),
(3, 'Algeria'),
(4, 'American Samoa'),
(5, 'Andorra'),
(6, 'Angola'),
(7, 'Anguilla'),
(8, 'Antarctica'),
(9, 'Antigua and Barbuda'),
(10, 'Argentina'),
(11, 'Armenia'),
(12, 'Armenia'),
(13, 'Aruba'),
(14, 'Australia'),
(15, 'Austria'),
(16, 'Azerbaijan'),
(17, 'Azerbaijan'),
(18, 'Bahamas'),
(19, 'Bahrain'),
(20, 'Bangladesh'),
(21, 'Barbados'),
(22, 'Belarus'),
(23, 'Belgium'),
(24, 'Belize'),
(25, 'Benin'),
(26, 'Bermuda'),
(27, 'Bhutan'),
(28, 'Bolivia'),
(29, 'Bosnia and Herzegovina'),
(30, 'Botswana'),
(31, 'Bouvet Island'),
(32, 'Brazil'),
(33, 'British Indian Ocean Territory'),
(34, 'Brunei Darussalam'),
(35, 'Bulgaria'),
(36, 'Burkina Faso'),
(37, 'Burundi'),
(38, 'Cambodia'),
(39, 'Cameroon'),
(40, 'Canada'),
(41, 'Cape Verde'),
(42, 'Cayman Islands'),
(43, 'Central African Republic'),
(44, 'Chad'),
(45, 'Chile'),
(46, 'China'),
(47, 'Christmas Island'),
(48, 'Cocos (Keeling) Islands'),
(49, 'Colombia'),
(50, 'Comoros'),
(51, 'Congo'),
(52, 'Congo, The Democratic Republic of The'),
(53, 'Cook Islands'),
(54, 'Costa Rica'),
(55, 'Cote Divoire'),
(56, 'Croatia'),
(57, 'Cuba'),
(58, 'Cyprus'),
(59, 'Cyprus'),
(60, 'Czech Republic'),
(61, 'Denmark'),
(62, 'Djibouti'),
(63, 'Dominica'),
(64, 'Dominican Republic'),
(65, 'Easter Island'),
(66, 'Ecuador'),
(67, 'Egypt'),
(68, 'El Salvador'),
(69, 'Equatorial Guinea'),
(70, 'Eritrea'),
(71, 'Estonia'),
(72, 'Ethiopia'),
(73, 'Falkland Islands (Malvinas)'),
(74, 'Faroe Islands'),
(75, 'Fiji'),
(76, 'Finland'),
(77, 'France'),
(78, 'French Guiana'),
(79, 'French Polynesia'),
(80, 'French Southern Territories'),
(81, 'Gabon'),
(82, 'Gambia'),
(83, 'Georgia'),
(84, 'Georgia'),
(85, 'Germany'),
(86, 'Ghana'),
(87, 'Gibraltar'),
(88, 'Greece'),
(89, 'Greenland'),
(90, 'Greenland'),
(91, 'Grenada'),
(92, 'Guadeloupe'),
(93, 'Guam'),
(94, 'Guatemala'),
(95, 'Guinea'),
(96, 'Guinea-bissau'),
(97, 'Guyana'),
(98, 'Haiti'),
(99, 'Heard Island and Mcdonald Islands'),
(100, 'Honduras'),
(101, 'Hong Kong'),
(102, 'Hungary'),
(103, 'Iceland'),
(104, 'India'),
(105, 'Indonesia'),
(106, 'Indonesia'),
(107, 'Iran'),
(108, 'Iraq'),
(109, 'Ireland'),
(110, 'Israel'),
(111, 'Italy'),
(112, 'Jamaica'),
(113, 'Japan'),
(114, 'Jordan'),
(115, 'Kazakhstan'),
(116, 'Kazakhstan'),
(117, 'Kenya'),
(118, 'Kiribati'),
(119, 'Korea, North'),
(120, 'Korea, South'),
(121, 'Kosovo'),
(122, 'Kuwait'),
(123, 'Kyrgyzstan'),
(124, 'Laos'),
(125, 'Latvia'),
(126, 'Lebanon'),
(127, 'Lesotho'),
(128, 'Liberia'),
(129, 'Libyan Arab Jamahiriya'),
(130, 'Liechtenstein'),
(131, 'Lithuania'),
(132, 'Luxembourg'),
(133, 'Macau'),
(134, 'Macedonia'),
(135, 'Madagascar'),
(136, 'Malawi'),
(137, 'Malaysia'),
(138, 'Maldives'),
(139, 'Mali'),
(140, 'Malta'),
(141, 'Marshall Islands'),
(142, 'Martinique'),
(143, 'Mauritania'),
(144, 'Mauritius'),
(145, 'Mayotte'),
(146, 'Mexico'),
(147, 'Micronesia, Federated States of'),
(148, 'Moldova, Republic of'),
(149, 'Monaco'),
(150, 'Mongolia'),
(151, 'Montenegro'),
(152, 'Montserrat'),
(153, 'Morocco'),
(154, 'Mozambique'),
(155, 'Myanmar'),
(156, 'Namibia'),
(157, 'Nauru'),
(158, 'Nepal'),
(159, 'Netherlands'),
(160, 'Netherlands Antilles'),
(161, 'New Caledonia'),
(162, 'New Zealand'),
(163, 'Nicaragua'),
(164, 'Niger'),
(165, 'Nigeria'),
(166, 'Niue'),
(167, 'Norfolk Island'),
(168, 'Northern Mariana Islands'),
(169, 'Norway'),
(170, 'Oman'),
(171, 'Pakistan'),
(172, 'Palau'),
(173, 'Palestinian Territory'),
(174, 'Panama'),
(175, 'Papua New Guinea'),
(176, 'Paraguay'),
(177, 'Peru'),
(178, 'Philippines'),
(179, 'Pitcairn'),
(180, 'Poland'),
(181, 'Portugal'),
(182, 'Puerto Rico'),
(183, 'Qatar'),
(184, 'Reunion'),
(185, 'Romania'),
(186, 'Russia'),
(187, 'Russia'),
(188, 'Rwanda'),
(189, 'Saint Helena'),
(190, 'Saint Kitts and Nevis'),
(191, 'Saint Lucia'),
(192, 'Saint Pierre and Miquelon'),
(193, 'Saint Vincent and The Grenadines'),
(194, 'Samoa'),
(195, 'San Marino'),
(196, 'Sao Tome and Principe'),
(197, 'Saudi Arabia'),
(198, 'Senegal'),
(199, 'Serbia and Montenegro'),
(200, 'Seychelles'),
(201, 'Sierra Leone'),
(202, 'Singapore'),
(203, 'Slovakia'),
(204, 'Slovenia'),
(205, 'Solomon Islands'),
(206, 'Somalia'),
(207, 'South Africa'),
(208, 'South Georgia and The South Sandwich Islands'),
(209, 'Spain'),
(210, 'Sri Lanka'),
(211, 'Sudan'),
(212, 'Suriname'),
(213, 'Svalbard and Jan Mayen'),
(214, 'Swaziland'),
(215, 'Sweden'),
(216, 'Switzerland'),
(217, 'Syria'),
(218, 'Taiwan'),
(219, 'Tajikistan'),
(220, 'Tanzania, United Republic of'),
(221, 'Thailand'),
(222, 'Timor-leste'),
(223, 'Togo'),
(224, 'Tokelau'),
(225, 'Tonga'),
(226, 'Trinidad and Tobago'),
(227, 'Tunisia'),
(228, 'Turkey'),
(229, 'Turkey'),
(230, 'Turkmenistan'),
(231, 'Turks and Caicos Islands'),
(232, 'Tuvalu'),
(233, 'Uganda'),
(234, 'Ukraine'),
(235, 'United Arab Emirates'),
(236, 'United Kingdom'),
(237, 'United States'),
(238, 'United States Minor Outlying Islands'),
(239, 'Uruguay'),
(240, 'Uzbekistan'),
(241, 'Vanuatu'),
(242, 'Vatican City'),
(243, 'Venezuela'),
(244, 'Vietnam'),
(245, 'Virgin Islands, British'),
(246, 'Virgin Islands, U.S.'),
(247, 'Wallis and Futuna'),
(248, 'Western Sahara'),
(249, 'Yemen'),
(250, 'Yemen'),
(251, 'Zambia'),
(252, 'Zimbabwe');
following error code is seen on microsoft sql server-
Msg 215, Level 16, State 1, Line 1
Parameters supplied for object 'Countries' which is not a function. If the parameters are intended as a table hint, a WITH keyword is required.
Please help by sharing the reason ?

Don't use single quotes for column name.
INSERT INTO Countries ([sno], [Name])...
You can use brackets normally.
Or you can use double quote " if SET QUOTED_IDENTIFIER is ON (it should be by default)
Currently you have string literals which is causing SQL Server to interpret the code way different to what you expect

You could leave all the bracketed section out all together: SQL Fiddle
INSERT INTO Countries VALUES
This is therefore none explicit and not best practice (see comment by #AaronBertrand) - but still an alternative.

Related

Why is my sql script returning "[null]" and "0" values even though my table is populated?

I have manually populated a table as follows:
INSERT INTO country VALUES
-- columns are countryid, name, continent, population, gdp, lifeexpectancy, founded
(1, 'Argentina', 36.3, 348.2, 70.75, 9/7/1816),
(2, 'Brazil', 183.3, 1004, 65.6, 9/7/1822),
(3, 'Canada', 30.1, 658, 77.08, 1/7/1867),
(4, 'England', 60.8, 1256, 75.15, NULL),
(5, 'France', 60, 1000, 75.15, 14/7/1789),
(6, 'Mexico', 107.5, 694.3, 69.36, 16/9/1821),
(7, 'USA', 270, 8003, 75.75, 4/7/1776),
(8, 'Cuba', 11.7, 16.9, 75.95, 24/2/1895),
(9, 'Guatemala', 20, 200, 60, 15/9/1821),
(10, 'Tanzania', 55.57, 47.43, 60.76, 9/12/1961),
(11, 'India', 1324, 2264, 68.35, NULL),
(12, 'South Africa', 55.91, 294.8, 57.44, 31/05/1910),
(13, 'Costa Rica', 4.86, 57.44, 79.59, 15/9/1821),
(14, 'Uganda', 41.49, 25.53, 59.18, NULL);
but when I query
SELECT lifeexpectancy FROM country;
The tables returns a combination of "0" and "[null]" values.
I've tried changing the data_type of lifeexpectancy. Neither text nor numeric data types return values that have been entered.
Any suggestions?
You are not inserting the continent. Presumably you want that, but it is not in your sample data.
You should be using standard date formats. In most databases, YYYY-MM-DD works. And you should list the columns. So:
INSERT INTO country (countryid, name, population, gdp, lifeexpectancy, founded)
VALUES (1, 'Argentina', 36.3, 348.2, 70.75, '1816-07-09'),
(2, 'Brazil', 183.3, 1004, 65.6, '1822-07-09'),
(3, 'Canada', 30.1, 658, 77.08, '1867-07-01'),
(4, 'England', 60.8, 1256, 75.15, NULL),
(5, 'France', 60, 1000, 75.15, '1789-07-14'),
(6, 'Mexico', 107.5, 694.3, 69.36, '1821-09-16'),
(7, 'USA', 270, 8003, 75.75, '1776-07-04'),
(8, 'Cuba', 11.7, 16.9, 75.95, '1895-02-24'),
(9, 'Guatemala', 20, 200, 60, '1821-09-15'),
(10, 'Tanzania', 55.57, 47.43, 60.76, '1961-12-09'),
(11, 'India', 1324, 2264, 68.35, NULL),
(12, 'South Africa', 55.91, 294.8, 57.44, '1910-05-31'),
(13, 'Costa Rica', 4.86, 57.44, 79.59, '1821-09-15'),
(14, 'Uganda', 41.49, 25.53, 59.18, NULL);
I believe you're supplying 6 values instead of 7 (may be because of Auto Increment), if that the case you need to specify the columns here.
Also pass date with single quotes surrounded:
INSERT INTO country (countryid, name, continent, population, gdp, lifeexpectancy, founded)
VALUES
(1, 'Argentina', 36.3, 348.2, 70.75, '9/7/1816'),
(2, 'Brazil', 183.3, 1004, 65.6, '9/7/1822'),
(3, 'Canada', 30.1, 658, 77.08, '1/7/1867'),
(4, 'England', 60.8, 1256, 75.15, NULL),
(5, 'France', 60, 1000, 75.15, '14/7/1789'),
(6, 'Mexico', 107.5, 694.3, 69.36, '16/9/1821'),
(7, 'USA', 270, 8003, 75.75, '4/7/1776'),
(8, 'Cuba', 11.7, 16.9, 75.95, '24/2/1895'),
(9, 'Guatemala', 20, 200, 60, '15/9/1821'),
(10, 'Tanzania', 55.57, 47.43, 60.76, '9/12/1961'),
(11, 'India', 1324, 2264, 68.35, NULL),
(12, 'South Africa', 55.91, 294.8, 57.44, '31/05/1910'),
(13, 'Costa Rica', 4.86, 57.44, 79.59, '15/9/1821'),
(14, 'Uganda', 41.49, 25.53, 59.18, NULL);

Preparing bollinger bands with postgresql - STDDEV and PARTITION BY

I'm working on a pet project on cryptocurrency and Bollinger Bands, and I'm stuck on a problem I'm not able to solve.
Given this table:
CREATE TABLE public.dataset
(
"From_symbol" character varying(10) COLLATE pg_catalog."default" NOT NULL,
"To_symbol" character varying(10) COLLATE pg_catalog."default" NOT NULL,
"Timestamp" timestamp without time zone NOT NULL,
"Open" numeric(18,9),
"High" numeric(18,9),
"Low" numeric(18,9),
"Close" numeric(18,9),
"Volume_From" numeric(18,9),
"Volume_To" numeric(18,9),
"Weighted_Price" numeric(18,9),
"Id" integer NOT NULL DEFAULT nextval('dataset_id_seq'::regclass),
CONSTRAINT dataset_pkey PRIMARY KEY ("From_symbol", "To_symbol", "Timestamp")
If I run the following query
SELECT "From_symbol",
"To_symbol",
"Timestamp",
"Open",
"High",
"Low",
"Close",
"Volume_From",
"Volume_To",
"Weighted_Price",
AVG("Close") OVER
(PARTITION BY "Id"
ORDER BY "Id"
ROWS BETWEEN 19 PRECEDING AND CURRENT ROW) AS SMA20,
AVG("Close") OVER
(PARTITION BY "Id"
ORDER BY "Id"
ROWS BETWEEN 19 PRECEDING AND CURRENT ROW) +
STDDEV_SAMP("Close") OVER
(PARTITION BY "Id"
ORDER BY "Id"
ROWS BETWEEN 19 PRECEDING AND CURRENT ROW) * 2 AS "Upper_Bollinger_Band",
AVG("Close") OVER
(PARTITION BY "Id"
ORDER BY "Id"
ROWS BETWEEN 19 PRECEDING AND CURRENT ROW) -
STDDEV_SAMP("Close") OVER
(PARTITION BY "Id"
ORDER BY "Id"
ROWS BETWEEN 19 PRECEDING AND CURRENT ROW) * 2 AS "Lower_Bollinger_Band"
FROM public.dataset;
I get a null result on both the upper and lower bollinger bands.
While I have a very large dataset (2012-2020), I provide you with a sample of 40 lines. This should be enough in case you wish to test it.
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2011-12-31 08:52:00', 4.390000000, 4.390000000, 4.390000000, 4.390000000, 0.455580870, 2.000000019, 4.390000000, 1);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2011-12-31 16:50:00', 4.390000000, 4.390000000, 4.390000000, 4.390000000, 48.000000000, 210.720000000, 4.390000000, 2);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2011-12-31 17:59:00', 4.500000000, 4.570000000, 4.500000000, 4.570000000, 37.862297230, 171.380337530, 4.526411498, 3);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2011-12-31 18:00:00', 4.580000000, 4.580000000, 4.580000000, 4.580000000, 9.000000000, 41.220000000, 4.580000000, 4);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-01 05:16:00', 4.580000000, 4.580000000, 4.580000000, 4.580000000, 1.502000000, 6.879160000, 4.580000000, 5);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-01 16:28:00', 4.840000000, 4.840000000, 4.840000000, 4.840000000, 10.000000000, 48.400000000, 4.840000000, 6);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-01 23:45:00', 5.000000000, 5.000000000, 5.000000000, 5.000000000, 10.100000000, 50.500000000, 5.000000000, 7);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-02 21:04:00', 5.000000000, 5.000000000, 5.000000000, 5.000000000, 19.048000000, 95.240000000, 5.000000000, 8);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-03 12:45:00', 5.320000000, 5.320000000, 5.320000000, 5.320000000, 2.419172930, 12.869999988, 5.320000000, 9);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-03 15:22:00', 5.140000000, 5.140000000, 5.140000000, 5.140000000, 0.680000000, 3.495200000, 5.140000000, 10);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-03 15:54:00', 5.260000000, 5.260000000, 5.260000000, 5.260000000, 29.319391630, 154.219999970, 5.260000000, 11);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-03 16:32:00', 5.290000000, 5.290000000, 5.290000000, 5.290000000, 29.302457470, 155.010000020, 5.290000000, 12);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-03 18:10:00', 5.290000000, 5.290000000, 5.290000000, 5.290000000, 11.285444230, 59.699999977, 5.290000000, 13);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-03 18:14:00', 5.140000000, 5.140000000, 5.140000000, 5.140000000, 0.020000000, 0.102800000, 5.140000000, 14);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-03 18:26:00', 5.290000000, 5.290000000, 5.290000000, 5.290000000, 11.000000000, 58.190000000, 5.290000000, 15);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-03 18:27:00', 5.290000000, 5.290000000, 5.290000000, 5.290000000, 4.010814660, 21.217209551, 5.290000000, 16);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-04 05:17:00', 4.930000000, 4.930000000, 4.930000000, 4.930000000, 2.320000000, 11.437600000, 4.930000000, 17);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-04 06:05:00', 4.930000000, 4.930000000, 4.930000000, 4.930000000, 9.680000000, 47.722400000, 4.930000000, 18);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-04 13:41:00', 5.190000000, 5.190000000, 5.190000000, 5.190000000, 2.641618500, 13.710000015, 5.190000000, 19);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-04 13:57:00', 5.190000000, 5.190000000, 5.190000000, 5.190000000, 8.724470130, 45.279999975, 5.190000000, 20);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-04 16:39:00', 5.190000000, 5.190000000, 5.190000000, 5.190000000, 16.344726030, 84.829128096, 5.190000000, 21);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-04 16:53:00', 5.320000000, 5.320000000, 5.320000000, 5.320000000, 0.186090230, 0.990000024, 5.320000000, 22);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-04 16:54:00', 5.320000000, 5.320000000, 5.320000000, 5.320000000, 10.394736840, 55.299999989, 5.320000000, 23);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-04 17:00:00', 5.360000000, 5.370000000, 5.360000000, 5.370000000, 13.629422720, 73.060000006, 5.360461812, 24);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-04 18:51:00', 5.370000000, 5.570000000, 5.370000000, 5.570000000, 43.312195780, 235.747069370, 5.442972011, 25);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 02:40:00', 5.720000000, 5.720000000, 5.720000000, 5.720000000, 5.000000000, 28.600000000, 5.720000000, 26);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 04:52:00', 5.750000000, 5.750000000, 5.750000000, 5.750000000, 5.200000000, 29.900000000, 5.750000000, 27);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 08:19:00', 5.750000000, 5.790000000, 5.750000000, 5.790000000, 14.800000000, 85.500000000, 5.777027027, 28);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 09:58:00', 6.000000000, 6.000000000, 6.000000000, 6.000000000, 2.236666670, 13.420000020, 6.000000000, 29);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 10:03:00', 6.000000000, 6.000000000, 6.000000000, 6.000000000, 0.168482700, 1.010896200, 6.000000000, 30);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 10:48:00', 6.150000000, 6.150000000, 6.150000000, 6.150000000, 10.000000000, 61.500000000, 6.150000000, 31);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 11:08:00', 6.190000000, 6.190000000, 6.190000000, 6.190000000, 0.571890150, 3.540000029, 6.190000000, 32);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 11:10:00', 6.190000000, 6.230000000, 6.190000000, 6.230000000, 16.000000000, 99.285718902, 6.205357431, 33);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 11:48:00', 6.230000000, 6.250000000, 6.230000000, 6.250000000, 14.000000000, 87.420000000, 6.244285714, 34);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 12:20:00', 6.460000000, 6.460000000, 6.460000000, 6.460000000, 0.773993810, 5.000000013, 6.460000000, 35);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 12:21:00', 6.460000000, 6.460000000, 6.460000000, 6.460000000, 0.178018570, 1.149999962, 6.460000000, 36);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 12:28:00', 6.430000000, 6.430000000, 6.430000000, 6.430000000, 0.311041990, 1.999999996, 6.430000000, 37);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 14:07:00', 6.440000000, 6.440000000, 6.440000000, 6.440000000, 0.310559010, 2.000000024, 6.440000000, 38);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 14:38:00', 6.430000000, 6.430000000, 6.430000000, 6.430000000, 0.466562990, 3.000000026, 6.430000000, 39);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 15:31:00', 6.420000000, 6.420000000, 6.420000000, 6.420000000, 0.311526480, 2.000000002, 6.420000000, 40);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-05 23:50:00', 6.430000000, 6.430000000, 6.430000000, 6.430000000, 0.311526480, 2.003115266, 6.430000000, 41);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 00:35:00', 6.440000000, 6.440000000, 6.440000000, 6.440000000, 0.466562990, 3.004665656, 6.440000000, 42);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 00:39:00', 6.470000000, 6.470000000, 6.470000000, 6.470000000, 0.952012380, 6.159520099, 6.470000000, 43);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 00:41:00', 6.650000000, 6.650000000, 6.650000000, 6.650000000, 20.777443610, 138.170000010, 6.650000000, 44);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 00:43:00', 6.650000000, 6.650000000, 6.650000000, 6.650000000, 1.466275650, 9.750733073, 6.650000000, 45);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 00:46:00', 6.650000000, 6.650000000, 6.650000000, 6.650000000, 0.499265780, 3.320117437, 6.650000000, 46);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 07:02:00', 6.650000000, 6.650000000, 6.650000000, 6.650000000, 1.425497660, 9.479559439, 6.650000000, 47);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 07:04:00', 6.690000000, 6.730000000, 6.690000000, 6.730000000, 6.310000000, 42.363858320, 6.713765186, 48);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 14:20:00', 6.800000000, 6.900000000, 6.800000000, 6.900000000, 9.310559010, 63.611801268, 6.832221481, 49);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 16:21:00', 6.760000000, 6.760000000, 6.760000000, 6.760000000, 0.295857990, 2.000000012, 6.760000000, 50);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 16:36:00', 6.500000000, 6.500000000, 6.500000000, 6.500000000, 0.500000000, 3.250000000, 6.500000000, 51);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 16:37:00', 6.490000000, 6.490000000, 6.490000000, 6.490000000, 1.540832050, 10.000000005, 6.490000000, 52);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 17:37:00', 6.400000000, 6.400000000, 6.400000000, 6.400000000, 0.500000000, 3.200000000, 6.400000000, 53);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 18:59:00', 6.400000000, 6.400000000, 6.400000000, 6.400000000, 1.550387590, 9.922480576, 6.400000000, 54);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 19:00:00', 6.400000000, 6.400000000, 6.400000000, 6.400000000, 0.838759680, 5.368061952, 6.400000000, 55);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 19:42:00', 6.400000000, 6.400000000, 6.400000000, 6.400000000, 9.110852730, 58.309457472, 6.400000000, 56);
INSERT INTO public.dataset VALUES ('BTC', 'USD', '2012-01-06 21:18:00', 6.300000000, 6.300000000, 6.300000000, 6.300000000, 0.500000000, 3.150000000, 6.300000000, 57);
Would you be so kind as to let me understand what am I doing wrong? I traced the problem to the STDDEV usage, but I have no clue on why the PARTITION BY clause works on AVG and fails on the STDDEV.
I'm running PostgreSQL 12.2 on Ubuntu:
PostgreSQL 12.2 (Ubuntu 12.2-4) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-8ubuntu1) 9.3.0, 64-bit
Thanks!
The reason is this logic;
AVG("Close") OVER
(PARTITION BY "Id"
------------------^
ORDER BY "Id"
ROWS BETWEEN 19 PRECEDING AND CURRENT ROW
) AS SMA20,
Your id is a unique value on each row so the partition ha only one row -- and the standard deviation of a single value is not defined.
Presumably, you intend:
AVG(close) OVER
(PARTITION BY from_symbol, to_symbol
ORDER BY timestamp
ROWS BETWEEN 19 PRECEDING AND CURRENT ROW
) AS SMA20,
Notes:
Do not enclose identifiers in double quotes. That just makes it harder to write queries.
Why are you using a sequence when you can simply use generated always as identity?
Use the timestamp for ordering rather than the id.
Have you checked your results ? do the averages look right to you ?
I say this because your Id is unique and if you PARTITON BY on it, you will get 1 row partitions. While you can average 1 row, you cannot compute the standard deviation for one single row.
My suggestion would be to remove the PARTITION BY "Id" from all your aggregate functions. It seems you want to use the whole table as 1 partition anyway, or find the right column to partition by. A good candidate might be the From_symbol,To_symbol pair as you do not want to mix exchange pairs. So my suggestion would be to PARTITION BY "From_symbol","To_symbol" but you know the data best.

Deriving the first instance of a specific node type in a tree structure query using SQL

I am designing an Electrical design software that will model an electrical utility system from the incoming Power Utility right down to the individual circuits such as computers and coffee machines.
I want to give each component of the system a dedicated table. eg. Transformers, Loads, cables, PowerPanels (called buses in this example).
Each component can be connected to one or many other components. I am using a parent/child table to manage the connections and plan to use a CTE to derive the hierarchical tree structure for a given component.
The voltage supplying any component in the system will be derived by finding the first instance of a transformer or a utility in the tree.
I have developed a query that can handle this as demonstrated below.
However, this only works for selecting one component in the CTE. I am looking for a way to select all buses and their connected voltage (nearest trafo or Utility). The only solution I can come up with is to use a table function on the above query. Is there a better way of doing this.
CREATE TABLE #componentConnection
(componentConnectionID int, parentComponentID varchar(4), childComponentID int)
;
INSERT INTO #componentConnection
(componentConnectionID, parentComponentID, childComponentID)
VALUES
(1, '13', 18),
(2, '13', 19),
(3, '13', 20),
(4, '13', 21),
(5, '13', 22),
(6, '13', 23),
(7, '14', 24),
(8, '14', 25),
(9, '14', 26),
(10, '14', 27),
(11, '14', 28),
(12, '14', 29),
(13, '15', 30),
(14, '15', 31),
(15, '15', 32),
(16, '15', 33),
(17, '15', 34),
(18, '15', 35),
(19, '16', 36),
(20, '16', 37),
(21, '16', 38),
(22, '16', 39),
(23, '16', 40),
(24, '16', 41),
(25, '1', 5),
(27, '5', 13),
(28, NULL, 1),
(29, '18', 6),
(30, '6', 11),
(31, '11', 7),
(32, '7', 14)
;
CREATE TABLE #component
(componentID int, componentName varchar(8), componentType varchar(7))
;
INSERT INTO #component
(componentID, componentName, componentType)
VALUES
(1, 'Utility1', 'utility'),
(2, 'Utility2', 'utility'),
(3, 'utility3', 'utility'),
(4, 'utility4', 'utility'),
(5, 'Cable1', 'cable'),
(6, 'Cable2', 'cable'),
(7, 'Cable3', 'cable'),
(8, 'Cable4', 'cable'),
(9, 'Cable5', 'cable'),
(10, 'Cable6', 'cable'),
(11, 'Trafo1', 'trafo'),
(12, 'Trafo2', 'trafo'),
(13, 'Bus1', 'bus'),
(14, 'Bus2', 'bus'),
(15, 'Bus3', 'bus'),
(16, 'Bus4', 'bus'),
(17, 'Bus5', 'bus'),
(18, 'cub1', 'cir'),
(19, 'cub2', 'cir'),
(20, 'cub3', 'cir'),
(21, 'cub4', 'cir'),
(22, 'cub5', 'cir'),
(23, 'cub6', 'cir'),
(24, 'cub1', 'cir'),
(25, 'cub2', 'cir'),
(26, 'cub3', 'cir'),
(27, 'cub4', 'cir'),
(28, 'cub5', 'cir'),
(29, 'cub6', 'cir'),
(30, 'cub1', 'cir'),
(31, 'cub2', 'cir'),
(32, 'cub3', 'cir'),
(33, 'cub4', 'cir'),
(34, 'cub5', 'cir'),
(35, 'cub6', 'cir'),
(36, 'cub1', 'cir'),
(37, 'cub2', 'cir'),
(38, 'cub3', 'cir'),
(39, 'cub4', 'cir'),
(40, 'cub5', 'cir'),
(41, 'cub6', 'cir')
;
CREATE TABLE #utility
([utilityID] int, [componentID] int, [utlityKV] float)
;
INSERT INTO #utility
([utilityID], [componentID], [utlityKV])
VALUES
(1, 1, 0.4),
(2, 2, 0.208),
(4, 3, 0.48),
(5, 4, 0.208)
;
CREATE TABLE #transformer
([transformerID] int, [componentID] int, [facilityID] int, [transformerName] varchar(4), [transformerPrimaryTapKv] float, [transformerSecondaryTapKv] float, [transformerPrimaryKv] float, [transformerSecondaryKv] float)
;
INSERT INTO #transformer
([transformerID], [componentID], [facilityID], [transformerName], [transformerPrimaryTapKv], [transformerSecondaryTapKv], [transformerPrimaryKv], [transformerSecondaryKv])
VALUES
(3, 11, 1, NULL, 0.48, 0.208, 0.48, 0.208),
(4, 12, 2, NULL, 0.48, 0.4, 0.48, 0.4)
;
CREATE TABLE #Bus
([busID] int, [busTypeID] int, [componentID] int, [bayID] int, [busName] varchar(4), [busConductorType] varchar(6), [busRatedCurrent] int)
;
INSERT INTO #Bus
([busID], [busTypeID], [componentID], [bayID], [busName], [busConductorType], [busRatedCurrent])
VALUES
(8, 1, 13, 1, 'bus1', 'Copper', 60),
(9, 1, 14, 1, 'bus2', 'copper', 50),
(10, 2, 15, 1, 'bus3', 'copper', 35),
(11, 2, 16, 1, 'bus4', 'copper', 35),
(13, 1, 17, 1, 'bus5', 'copper', 50)
;
WITH CTE AS (SELECT childComponentID AS SourceID, childComponentID, 0 AS depth
FROM #ComponentConnection
UNION ALL
SELECT C1.SourceID, C.childComponentID, c1.depth + 1 AS depth
FROM #ComponentConnection AS C INNER JOIN
CTE AS C1 ON C.parentComponentID = C1.childComponentID)
SELECT childComponentID,b.busName, min(depth)
--,c.componentType
,isnull(t.transformerSecondaryKv,u.utlityKV) kV
FROM CTE AS CTE1
join #Component c
on CTE1.SourceID = c.componentID
left join #Utility u
on CTE1.SourceID = u.componentID
left join #Transformer t
on CTE1.SourceID = t.componentID
LEFT JOIN #Bus b
on cte1.childComponentID = b.componentID
where busName is not null and c.componentType in ('Utility','trafo')
group by childComponentID,b.busName,isnull(t.transformerSecondaryKv,u.utlityKV)
order by depth
The desired result would be as follows for a Bus. I want to list all Buses and their associated Voltage. I would select all from the Bus table and derive the voltage from the heirarchical structure
Result
BusName | Voltage
Bus 1 | 0.4
Bus 2 | 0.208
Bus 3 | etc

SQL Server Management: The INSERT statement conflicted with the FOREIGN KEY constraint

I'm using SQL Server Management to create a database (fot a work in the university), I'm trying to insert new values into a table but recieving error.
The two relevant tables I created are:
create table ballotBox
(
bno integer,
cid numeric(4,0) references city,
street varchar(20),
hno integer,
totvoters integer,
primary key (bno)
);
create table votes
(
cid numeric(4,0) references city,
bno integer references ballotBox,
pid numeric(3,0) references party,
nofvotes integer
);
I've entered first the values to ballotBox (as well as for the table 'city' and 'party'):
insert into ballotBox values
(1, 1, 'street1', 10, 1500),
(2, 1, 'street2', 15, 490),
(3, 1, 'street2', 15, 610),
(4, 1, 'street2', 15, 650),
(5, 2, 'street3', 10, 900),
(6, 2, 'street3', 55, 800),
(7, 2, 'street4', 67, 250),
(8, 2, 'street4', 67, 990),
(9, 2, 'street5', 5, 600),
(10, 3, 'street1', 72, 1000),
(11, 3, 'street6', 25, 610),
(12, 3, 'street6', 25, 600),
(13, 4, 'street2', 3, 550),
(14, 4, 'street7', 15, 500),
(15, 5, 'street8', 44, 1100),
(16, 5, 'street9', 7, 710),
(17, 5, 'street10', 13, 950);
And then I tried to enter values to votes:
insert into votes values
(1, 1, 200, 100),
(1, 11, 210, 220),
(1, 1, 220, 2),
(1, 1, 230, 400),
(1, 1, 240, 313),
(1, 1, 250, 99),
(2, 1, 200, 55),
(2, 10, 210, 150),
(2, 10, 220, 2),
(2, 1, 230, 16),
(2, 1, 240, 210),
(2, 9, 250, 54),
(3, 9, 200, 234),
(3, 9, 210, 123),
(3, 1, 220, 8),
(3, 1, 230, 87),
(3, 1, 240, 76),
(3, 1, 250, 6),
(4, 1, 200, 135),
(4, 1, 210, 246),
(4, 17, 220, 7),
(4, 1, 230, 18),
(4, 1, 240, 44),
(4, 1, 250, 66),
(1, 2, 200, 373),
(1, 2, 210, 12),
(1, 2, 220, 3),
(1, 2, 230, 74),
(1, 2, 240, 58),
(1, 2, 250, 272),
(2, 6, 200, 139),
(2, 6, 210, 2580),
(2, 2, 220, 6),
(2, 2, 230, 73),
(2, 2, 240, 7),
(2, 2, 250, 99),
(3, 2, 200, 15),
(3, 2, 210, 68),
(3, 2, 220, 12),
(3, 2, 230, 12),
(3, 2, 240, 15),
(3, 2, 250, 25),
(4, 2, 200, 7),
(4, 2, 210, 245),
(4, 2, 220, 8),
(1, 0, 0.0, 361),
(4, 2, 240, 67),
(4, 2, 250, 144),
(5, 2, 200, 123),
(5, 2, 210, 76),
(5, 2, 220, 15),
(5, 2, 230, 158),
(5, 2, 240, 76),
(5, 2, 250, 132),
(1, 3, 200, 152),
(1, 3, 210, 517),
(1, 3, 220, 0),
(1, 3, 230, 267),
(2, 3, 200, 87),
(2, 3, 210, 134),
(2, 3, 220, 4),
(2, 3, 230, 11),
(2, 3, 240, 256),
(2, 3, 250, 76),
(3, 3, 200, 105),
(3, 3, 210, 132),
(3, 3, 3220, 3),
(3, 3, 230, 24),
(3, 3, 240, 254),
(3, 3, 250, 12),
(1, 4, 200, 61),
(1, 4, 210, 54),
(1, 4, 220, 5),
(1, 4, 230, 19),
(1, 4, 240, 1),
(1, 4, 250, 47),
(2, 4, 200, 17),
(2, 4, 210, 23),
(2, 4, 220, 0),
(2, 4, 230, 64),
(2, 4, 240, 11),
(2, 4, 250, 149),
(1, 5, 0200, 187),
(1, 5, 210, 88),
(1, 5, 220, 1),
(1, 5, 230, 255),
(1, 5, 240, 12),
(1, 5, 250, 373),
(2, 2, 500, 245),
(2, 5, 210, 120),
(2, 5, 220, 9),
(2, 5, 230, 19),
(2, 5, 240, 234),
(2, 5, 250, 5),
(3, 5, 200, 107),
(3, 5, 210, 18),
(3, 5, 220, 11),
(3, 5, 230, 54),
(3, 5, 240, 378),
(3, 5, 250, 243);
But I'm getting an error:
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__votes__bno__1920BF5C". The conflict occurred in database "Voting", table "dbo.ballotBox", column 'bno'. The statement has been terminated.
you are trying to insert
(1, 0, 0.0, 361),
in votes table have FK bno
there are no record corresponding to 0 in ballotbox table
please remove this line from votes statement then insert
The relationship between two tables is not correct.
Make it so:
create table votes
(
cid numeric(4,0),
bno integer,
pid numeric(3,0),
nofvotes integer
foreing key cid reference city(cid)
foreing key bno reference ballotbox (bno)
foreing key pid reference party(pid)
);

nested select TOP 1 statement

I am working on a system currently for Asset Management within our company.
The system allows users to sign in/out items on either a permenant or temporary basis.
I am working on some reporting on the system and have hit a stumbling block with returning the relevent data.
So i know that i need a nested Select TOP 1 statement within my main query but i cant get it to work.
The main query is as follows:
SELECT [Asset-User].ID, [Asset-User].Asset_ID, [Assets].ID, [Assets].Signed_Out, [Assets].Asset_Tag_Serial_Num, [Assets].Name_Hostname, [Assets].Type_ID, [Asset_Type].ID, [Asset_Type].Name_Model, [Asset-User].User_ID, [Company_Users].ID, [Asset-User].Sign_Out_Date, [Asset-User].Return_Date, [Asset-User].[Perm_Signout?]
FROM [Asset-User], [Assets], [Asset_Type], [Company_Users]
WHERE ([User_ID] = '1') AND [Asset-User].Asset_ID = [Assets].ID AND [Assets].Type_ID = [Asset_Type].ID AND [Asset-User].User_ID = [Company_Users].ID AND [Assets].Signed_Out = '1'
So this query returns everything that is currently marked as "Signed Out" that has ever been signed out by User 1.
Somewhere in here i need to add a nested Select TOP 1 on Asset-User.ID so that it only returns items that are marked as permanent sign outs, OR if not permanent that they have most recently been signed out by User 1.
This would then only give me a list of items currently assigned to that user and not display the Asset if someone else has signed it out since
Any help would be greatly appreciated
As per request Sample data below:
https://docs.google.com/spreadsheets/d/1o4T6bsxyO-1dGE0-FUtWFboRupcq4o4V9i2Em0_BjyU/edit?usp=sharing
First sheet shows actual results, second sheet shows roughly what should be expected
As you will see here, this user has signed out a few items multiple times (and those items may not most recently have been signed out by this user)
Its hard to get the sample data but in essence there should be no duplicate Asset_ID's in the list as only the most recent ID (sign out ID this relates to) should be displayed for each Asset_ID.
Does that help?
Thanks again
As per request here is the Schema build code that will create a sample DB to work with
CREATE TABLE Asset_Type
([ID] int, [Global_ID] int, [Name_Model] varchar(30), [Description_Spec] varchar(54))
;
INSERT INTO Asset_Type
([ID], [Global_ID], [Name_Model], [Description_Spec])
VALUES
(1, 1, 'Dell Optiplex 3020', 'Windows 7 Professional, Intel Core i3 3.40GHz,4Gb RAM'),
(2, 3, 'Viewsonic VA2231wa', 'Viewsonic Widescreen Monitor'),
(3, 3, 'Samsung S24B150BL', 'Samsung LED 24" Widescreen Monitor')
;
CREATE TABLE Assets
([ID] int, [Asset_Tag_Serial_Num] varchar(29), [Type_ID] int, [Purchase_Date] varchar(10), [Purchase_Price] varchar(7), [Name_Hostname] varchar(36), [Signed_Out] int)
;
INSERT INTO Assets
([ID], [Asset_Tag_Serial_Num], [Type_ID], [Purchase_Date], [Purchase_Price], [Name_Hostname], [Signed_Out])
VALUES
(1, '0206', 1, '2013-11-29', '323.30', 'WS0206', 1),
(3, '0226', 2, NULL, NULL, 'Viewsonic VA2231wa - 0226', 1),
(4, '0204', 1, '2013-11-29', '323.00', 'WS0204', 1),
(5, '0205', 1, '2013-11-29', '323.00', 'WS0205', 1),
(6, '0108', 1003, NULL, NULL, 'Small Office Sat Nav', 1),
(7, '0092', 1004, NULL, NULL, 'Large Office Sat Nav', 1),
(8, 'GWC36-DHDBC-J2MXY-H2BGY-8C79G', 1005, '1900-01-01', '0.00', 'MS Office for WS0020', 1),
(9, '0020', 1006, '1900-01-01', '0.00', 'WS0020', 1),
(10, '0173', 2, '1900-01-01', '0.00', 'Viewsonic VA2231wa - 0173', 1),
(11, '0172', 1007, '1900-01-01', '0.00', 'Dell 19" Monitor 0172', 1),
(12, '00104926EC6B', 1008, '1900-01-01', '0.00', 'Shortel 230 - EC6B', 1),
(13, '0227', 1009, NULL, NULL, 'Blue - Yeti Mic', 0),
(14, '0221', 1, NULL, NULL, 'WS0221', 1),
(15, '0222', 1, '2013-11-29', '323.00', 'WS0222', 1),
(16, '0223', 1, NULL, NULL, 'WS0223', 1),
(17, '0220', 1, '2013-11-29', '323.00', 'WS0220', 1),
(18, '0217', 1, '2013-11-29', '323.00', 'WS0217', 1),
(19, '0218', 1, NULL, NULL, 'WS0218', 1),
(20, '0219', 1, '2013-11-29', '323.00', 'WS0219', 1),
(21, '0228', 2, NULL, NULL, 'Viewsonic VA2231wa - 0228', 1),
(22, '0229', 1010, NULL, NULL, 'Dell 19" Monitor 0229', 1),
(23, '00104931AA16', 1011, NULL, NULL, 'Shortel 115 - AA16', 1),
(24, '0093 - DYTJ18X4DJ8T', 1012, NULL, NULL, 'Office IPad 3', 1),
(25, '0095', 1013, '1900-01-01', '0.00', '320Gb External HDD', 1),
(26, '0071', 1014, NULL, NULL, '0071 - NEC Projector', 0),
(27, '0072', 1015, NULL, NULL, '0072 - Black Dell Projector', 0),
(28, '0073', 1016, '1900-01-01', '0.00', '0073 - Dell Projector', 0),
(29, '0230', 1017, '1900-01-01', '0.00', '0230 - Silver Dell Projector', 0),
(30, '0064', 1018, NULL, NULL, 'WS0064', 0),
(31, '0231', 1019, NULL, NULL, 'Freecom 1GB Pen - 0231', 1),
(47, '0165', 2, NULL, NULL, 'Viewsonic VA2231wa - 0165', 1),
(48, '0232', 1010, '1900-01-01', '0.00', 'Dell 19" Monitor 0232', 1),
(49, '0233', 1010, '1900-01-01', '0.00', 'Dell 19" Monitor 0233', 1),
(50, '0137', 1022, NULL, NULL, 'Viewsonic VA2248-LED - 0137', 1),
(51, '0234', 1010, '1900-01-01', '0.00', 'Dell 19" Monitor 0234', 1),
(52, '0235', 1010, '1900-01-01', '0.00', 'Dell 19" Monitor 0235', 1),
(53, '0134', 1010, NULL, NULL, 'Dell 19" Monitor 0134', 0),
(54, '0135', 1022, NULL, NULL, 'Viewsonic VA2248-LED - 0135', 1),
(55, '0236', 3, '1900-01-01', '0.00', 'Samsung S24B150BL - 0236', 1),
(56, '001049201D9A', 1008, '1900-01-01', '0.00', 'Shortel 230 - 1D9A', 1),
(57, '0010492015AE', 1008, '1900-01-01', '0.00', 'Shortel 230 - 15AE', 1),
(93, '0269', 1029, '1900-01-01', '0.00', 'TP-Link Switch - 0269', 0),
(94, '0058', 1030, NULL, NULL, 'WS0058', 1),
(95, '0270', 1031, NULL, NULL, 'MeetingRoom3', 1),
(96, '0243', 1032, NULL, NULL, 'MeetingRoom2', 1),
(97, '0271', 1027, NULL, NULL, 'Dynamode SW80010-D Switch - 0271', 0),
(123, '0281', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0281', 0),
(124, '0282', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0282', 1),
(125, '0283', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0283', 0),
(126, '0284', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0284', 1),
(127, '0285', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0285', 1),
(128, '0286', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0286', 1),
(129, '0287', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0287', 1),
(143, '0280', 1, '2014-07-03', '403.80', 'WS0280', 1),
(144, '0296', 1, '2014-07-03', '403.80', 'WS0296', 1),
(145, '0297', 1, '2014-07-03', '403.80', 'WS0297', 1),
(146, '0298', 1, '2014-07-03', '403.80', 'WS0298', 1),
(147, '0299', 1, '2014-07-03', '403.80', 'WS0299', 1),
(148, '0052', 1036, '1900-01-01', '0.00', 'WS0052', 1),
(168, '0312', 1047, NULL, NULL, 'Epson White HD - 0312', 1),
(169, '0201', 1049, '1900-01-01', '0.00', 'Ipad 4 - 0201', 0),
(170, 'HP27J-2C496-83KXB-RGMX6-8QJQG', 1020, '1900-01-01', '0.00', 'MS Office for Jonny D', 1),
(171, '7N4QY-DFGWD-P6662-CFCHG-QYFP2', 1021, '1900-01-01', '0.00', 'MS Office for WS0215 (Simeon Laptop)', 1),
(172, '0140', 1022, '1900-01-01', '0.00', 'Viewsonic VA2248-LED - 0140', 1),
(198, '0109', 1037, '1900-01-01', '0.00', 'WS0109', 1),
(199, '0324', 1052, '1900-01-01', '0.00', 'Philips 23.6" Monitor 0324', 1)
;
CREATE TABLE [Asset-User]
([ID] int, [Asset_ID] int, [User_ID] int, [Sign_Out_Date] datetime, [Return_Date] datetime, [Perm_Signout] int)
;
INSERT INTO [Asset-User]
([ID], [Asset_ID], [User_ID], [Sign_Out_Date], [Return_Date], [Perm_Signout])
VALUES
(2, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(3, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(4, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(5, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(6, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(7, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(8, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(9, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(10, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(11, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(1002, 1, 1, '2014-04-01 00:00:00', '2014-04-01 00:00:00', 0),
(1003, 1, 1, '2014-01-01 00:00:00', '2014-01-01 00:00:00', 1)
;
CREATE TABLE Company_Users
([ID] int, [Name] varchar(14), [Domain_Username] varchar(14), [Dept] varchar(16), [Email] varchar(25), [DD_Ext] int, [Job_Title] varchar(30), [Deleted_Left] int)
;
INSERT INTO Company_Users
([ID], [Name], [Domain_Username], [Dept], [Email], [DD_Ext], [Job_Title], [Deleted_Left])
VALUES
(1, 'Neil Smithson', 'Neil.Smithson', '2nd Line Support', 'neil.smithson#dezrez.com', 3041, 'Second Line Support Technician', 0)
;
CREATE TABLE Global_Types
([ID] int, [Name] varchar(13), [Description] varchar(54))
;
INSERT INTO Global_Types
([ID], [Name], [Description])
VALUES
(1, 'PC', 'Desktop PC')
;
This is not really an answer but will show you how you could rework this query using aliases and the (not really) newer join style.
SELECT au.ID
, au.Asset_ID
, a.ID
, a.Signed_Out
, a.Asset_Tag_Serial_Num
, a.Name_Hostname
, a.Type_ID
, at.ID
, at.Name_Model
, au.User_ID
, cu.ID
, au.Sign_Out_Date
, au.Return_Date
, au.[Perm_Signout?]
FROM [Asset-User] au
join Assets a on au.Asset_ID = a.ID
join Asset_Type at on a.Type_ID = at.ID
join Company_Users cu on au.User_ID = cu.ID
WHERE au.User_ID = '1'
AND a.Signed_Out = '1'